代码生成支持图片、附件上传及富文本编辑器

This commit is contained in:
yxh
2021-08-12 15:32:47 +08:00
parent 1539a4c036
commit 3685967728
12 changed files with 559 additions and 75 deletions
+89
View File
@@ -11,6 +11,9 @@ import (
"gfast/app/common/adapter"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
"net/url"
)
type upload struct {
@@ -31,3 +34,89 @@ func (c *upload) UpImg(r *ghttp.Request) {
}
c.SusJsonExit(r, res)
}
// @Summary CkEditor编辑器上传附件
// @Description CkEditor编辑器上传附件
// @Tags 公共
// @Param upFile body string true "upFile"
// @Success 0 {object} c.Response "{"code": 200, "data": [...]}"
// @Router /system/upload/ckEditorUp [post]
// @Security
func (c *upload) CkEditorUp(r *ghttp.Request) {
upFile := r.GetUploadFile("upload")
fType := gstr.ToLower(r.GetString("type"))
var info *adapter.FileInfo
var err error
if fType == "images" {
info, err = adapter.Upload.UpImg(upFile)
} else if fType == "files" {
info, err = adapter.Upload.UpFile(upFile)
}
if err != nil {
r.Response.WriteJson(g.Map{"error": g.Map{"message": "上传失败," + err.Error(), "number": 105}})
} else {
parseInfo, _ := url.Parse(r.GetUrl())
var fileUrl = info.FileUrl
if !gregex.IsMatchString("^http", info.FileUrl) {
fileUrl = parseInfo.Scheme + "://" + parseInfo.Host + "/" + info.FileUrl
}
r.Response.WriteJson(g.Map{"fileName": info.FileName, "uploaded": 1, "url": fileUrl})
}
}
// @Summary 批量上传图片
// @Description 批量上传图片
// @Tags 公共
// @Param file body string true "file"
// @Success 0 {object} c.Response "{"code": 200, "data": [...]}"
// @Router /system/upload/upImgs [post]
// @Security
func (c *upload) UpImgs(r *ghttp.Request) {
upFiles := r.GetUploadFiles("file")
infos, err := adapter.Upload.UpImgs(upFiles)
if err != nil {
c.FailJson(true, r, "上传失败,"+err.Error())
}
res := g.Map{
"fileInfos": infos,
}
c.SusJson(true, r, "上传成功", res)
}
// @Summary 单文件上传
// @Description 单文件上传
// @Tags 公共
// @Param file body string true "file"
// @Success 0 {object} c.Response "{"code": 200, "data": [...]}"
// @Router /system/upload/upFile [post]
// @Security
func (c *upload) UpFile(r *ghttp.Request) {
upFile := r.GetUploadFile("file")
info, err := adapter.Upload.UpFile(upFile)
if err != nil {
c.FailJson(true, r, "上传失败,"+err.Error())
}
res := g.Map{
"fileInfo": info,
}
c.SusJson(true, r, "上传成功", res)
}
// @Summary 批量上传文件
// @Description 批量上传文件
// @Tags 公共
// @Param file body string true "file"
// @Success 0 {object} c.Response "{"code": 200, "data": [...]}"
// @Router /system/upload/upFiles [post]
// @Security
func (c *upload) UpFiles(r *ghttp.Request) {
upFiles := r.GetUploadFiles("file")
infos, err := adapter.Upload.UpFiles(upFiles)
if err != nil {
c.FailJson(true, r, "上传失败,"+err.Error())
}
res := g.Map{
"fileInfos": infos,
}
c.SusJson(true, r, "上传成功", res)
}
+4
View File
@@ -28,6 +28,10 @@ func init() {
group.Group("/upload", func(group *ghttp.RouterGroup) {
//单图上传
group.POST("/upImg", api.Upload.UpImg)
group.POST("/ckEditorUp", api.Upload.CkEditorUp)
group.POST("/upImgs", api.Upload.UpImgs)
group.POST("/upFile", api.Upload.UpFile)
group.POST("/upFiles", api.Upload.UpFiles)
})
//用户相关
group.Group("/user", func(group *ghttp.RouterGroup) {
+3 -4
View File
@@ -512,16 +512,15 @@ func (s *sysUser) AddUserRole(roleIds interface{}, userId int64) (err error) {
// AddUserPost 添加用户岗位信息
func (s *sysUser) AddUserPost(postIds []int64, userId int64, tx *gdb.TX) (err error) {
if err != nil {
g.Log().Error(err)
return gerror.New("用户岗位设置失败")
}
//删除旧岗位信息
_, err = dao.SysUserPost.TX(tx).Where(dao.SysUserPost.Columns.UserId, userId).Delete()
if err != nil {
g.Log().Error(err)
return
}
if len(postIds) == 0 {
return
}
//添加用户岗位信息
data := g.List{}
for _, v := range postIds {
+10 -3
View File
@@ -451,7 +451,7 @@ func (s *toolsGenTable) GenCode(ids []int, ctx context.Context) (err error) {
err = s.createFile(path, code, false)
if !hasSql {
//第一次生成则向数据库写入菜单数据
err = s.writeDb(path)
err = s.writeDb(path, ctx)
if err != nil {
return
}
@@ -626,14 +626,14 @@ func (s *toolsGenTable) GenData(tableId int64, ctx context.Context) (data g.MapS
//剔除多余的换行
func (s *toolsGenTable) trimBreak(str string) (rStr string, err error) {
var b []byte
if b, err = gregex.Replace("(([\\s\t]*)\r?\n){2,}", []byte("$2\n"), []byte(str)); err == nil {
if b, err = gregex.Replace("(([\\s\t]*)\r?\n){2,}", []byte("$2\n\n"), []byte(str)); err == nil {
rStr = gconv.String(b)
}
return
}
// 写入菜单数据
func (s *toolsGenTable) writeDb(path string) (err error) {
func (s *toolsGenTable) writeDb(path string, ctx context.Context) (err error) {
isAnnotation := false
var fi *os.File
fi, err = os.Open(path)
@@ -646,11 +646,16 @@ func (s *toolsGenTable) writeDb(path string) (err error) {
now := gtime.Now()
var res sql.Result
var id int64
var tx *gdb.TX
tx, err = g.DB().Ctx(ctx).Begin()
for {
bytes, e := br.ReadBytes('\n')
if e == io.EOF {
break
}
if gstr.Trim(string(bytes)) == "" {
continue
}
bytes = bytes[:len(bytes)-2]
str := string(bytes)
@@ -683,6 +688,7 @@ func (s *toolsGenTable) writeDb(path string) (err error) {
//插入业务
res, err = g.DB().Exec(sql)
if err != nil {
tx.Rollback()
return
}
sqlStr = nil
@@ -690,5 +696,6 @@ func (s *toolsGenTable) writeDb(path string) (err error) {
sqlStr = []string{str}
}
}
tx.Commit()
return
}