|
@@ -11,11 +11,14 @@ import (
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
+ "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
"go.uber.org/zap"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
"path"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type Template struct {
|
|
@@ -34,9 +37,14 @@ func SaveTemplate(c *gin.Context) {
|
|
|
file := Template{}
|
|
|
err := c.BindJSON(&file)
|
|
|
uuid := c.Query("uuid")
|
|
|
- if uuid == "" {
|
|
|
+ if uuid == "undefined" || uuid == "" || uuid == "null" {
|
|
|
uuid = "test"
|
|
|
}
|
|
|
+ if file.Name == "" {
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn("文件名不能为空")
|
|
|
+ e2.ResponseWithMsg(c, e2.ERROR, "文件名不能为空")
|
|
|
+ return
|
|
|
+ }
|
|
|
if err != nil {
|
|
|
// 日志记录参数解析失败
|
|
|
simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
|
|
@@ -54,19 +62,42 @@ func SaveTemplate(c *gin.Context) {
|
|
|
e2.ResponseWithMsg(c, e2.ERROR, e2.ERROR.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
- if count > 0 {
|
|
|
- // 返回文件已存在的响应
|
|
|
- e2.ResponseWithMsg(c, e2.AlreadyExists, e2.AlreadyExists.GetMsg())
|
|
|
+ if count > 0 && file.Type == 1 {
|
|
|
+ err = utils.ParseBase64ImageString(file.ImageUrl, file.Name)
|
|
|
+ if err != nil {
|
|
|
+ simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "base64解码失败")
|
|
|
+ e2.ResponseWithMsg(c, e2.ERROR, "图片转换失败")
|
|
|
+ panic(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var url = global.DownloadSetting.Imageurl + file.Name + ".png"
|
|
|
+ doc := bson.M{
|
|
|
+ "name": file.Name,
|
|
|
+ "type": file.Type,
|
|
|
+ "data": file.Data,
|
|
|
+ "uuid": uuid,
|
|
|
+ "url": url,
|
|
|
+ }
|
|
|
+ // 当文件名字相同并且文件类型为1是替换文件
|
|
|
+ _, err := global.MongoCon.ReplaceOne(context.TODO(), filter, doc, options.Replace().SetUpsert(true))
|
|
|
+ if err != nil {
|
|
|
+ simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "替换文件失败")
|
|
|
+ e2.ResponseWithMsg(c, e2.ERROR, "替换文件失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e2.ResponseSuccess(c, e2.SUCCESS)
|
|
|
return
|
|
|
}
|
|
|
- err = utils.ParseBase64ImageString(file.ImageUrl, file.Name)
|
|
|
+ unix := time.Now().Unix()
|
|
|
+ formatInt := strconv.FormatInt(unix, 10)
|
|
|
+ err = utils.ParseBase64ImageString(file.ImageUrl, file.Name+formatInt)
|
|
|
if err != nil {
|
|
|
simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "base64解码失败")
|
|
|
e2.ResponseWithMsg(c, e2.ERROR, "图片转换失败")
|
|
|
panic(err)
|
|
|
return
|
|
|
}
|
|
|
- var url = global.DownloadSetting.Imageurl + file.Name + ".png"
|
|
|
+ var url = global.DownloadSetting.Imageurl + file.Name + formatInt + ".png"
|
|
|
doc := bson.M{
|
|
|
"name": file.Name,
|
|
|
"type": file.Type,
|
|
@@ -93,7 +124,7 @@ func TemplateItem(c *gin.Context) {
|
|
|
Type int `json:"type"`
|
|
|
}
|
|
|
uuid := c.Query("uuid")
|
|
|
- if uuid == "undefined" {
|
|
|
+ if uuid == "undefined" || uuid == "" || uuid == "null" {
|
|
|
uuid = "test"
|
|
|
}
|
|
|
t := &Types{}
|
|
@@ -105,7 +136,6 @@ func TemplateItem(c *gin.Context) {
|
|
|
var result []bson.M
|
|
|
opts := options.Find()
|
|
|
opts.SetProjection(bson.M{
|
|
|
- "_id": 0,
|
|
|
"data": 0,
|
|
|
})
|
|
|
cur, err := global.MongoCon.Find(context.Background(), filter, opts)
|
|
@@ -131,12 +161,39 @@ func TemplateItem(c *gin.Context) {
|
|
|
|
|
|
// GetTemplate 获取模板
|
|
|
func GetTemplate(c *gin.Context) {
|
|
|
- name := c.Query("name")
|
|
|
- if name == "" {
|
|
|
- e2.ResponseWithMsg(c, e2.ERROR, "参数错误")
|
|
|
+ _id := c.Query("name")
|
|
|
+ uuid := c.Query("uuid")
|
|
|
+ if uuid == "undefined" || uuid == "" || uuid == "null" {
|
|
|
+ uuid = "test"
|
|
|
+ }
|
|
|
+ hex, err2 := primitive.ObjectIDFromHex(_id)
|
|
|
+ if err2 != nil {
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err2, "_id错误")
|
|
|
+ e2.ResponseWithMsg(c, e2.ERROR, "_id错误")
|
|
|
return
|
|
|
}
|
|
|
var result map[string]interface{}
|
|
|
+ err := global.MongoCon.FindOne(context.Background(), bson.M{"_id": hex}).Decode(&result)
|
|
|
+ if err != nil {
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
+ e2.ResponseWithMsg(c, e2.ERROR, "获取文件失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e2.ResponseSuccess(c, result)
|
|
|
+}
|
|
|
+func GetTemplates(c *gin.Context) {
|
|
|
+ name := c.Query("name")
|
|
|
+ uuid := c.Query("uuid")
|
|
|
+ if uuid == "undefined" {
|
|
|
+ uuid = "test"
|
|
|
+ }
|
|
|
+ //hex, err2 := primitive.ObjectIDFromHex(_id)
|
|
|
+ //if err2 != nil {
|
|
|
+ // simple_zap.WithCtx(context.Background()).Sugar().Warn(err2, "_id错误")
|
|
|
+ // e2.ResponseWithMsg(c, e2.ERROR, "_id错误")
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ var result map[string]interface{}
|
|
|
err := global.MongoCon.FindOne(context.Background(), bson.M{"name": name}).Decode(&result)
|
|
|
if err != nil {
|
|
|
simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
@@ -148,17 +205,22 @@ func GetTemplate(c *gin.Context) {
|
|
|
|
|
|
// DeleteTemplate 删除模板
|
|
|
func DeleteTemplate(c *gin.Context) {
|
|
|
- name := c.Query("name")
|
|
|
- //uuid := c.Query("uuid")
|
|
|
- //if uuid == "" {
|
|
|
- // e2.ResponseWithMsg(c, e2.ERROR, "uuid错误")
|
|
|
- // return
|
|
|
- //}
|
|
|
- if name == "" {
|
|
|
+ _id := c.Query("name")
|
|
|
+ uuid := c.Query("uuid")
|
|
|
+ if uuid == "undefined" || uuid == "" || uuid == "null" {
|
|
|
+ uuid = "test"
|
|
|
+ }
|
|
|
+ if _id == "" {
|
|
|
e2.ResponseWithMsg(c, e2.ERROR, "参数错误")
|
|
|
return
|
|
|
}
|
|
|
- one, err := global.MongoCon.DeleteOne(context.Background(), bson.M{"name": name})
|
|
|
+ hex, err2 := primitive.ObjectIDFromHex(_id)
|
|
|
+ if err2 != nil {
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err2, "_id错误")
|
|
|
+ e2.ResponseWithMsg(c, e2.ERROR, "_id错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ one, err := global.MongoCon.DeleteOne(context.Background(), bson.M{"_id": hex, "uuid": uuid})
|
|
|
if err != nil {
|
|
|
simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "删除文件失败")
|
|
|
e2.ResponseWithMsg(c, e2.ERROR, "删除文件失败")
|
|
@@ -175,7 +237,7 @@ func DeleteTemplate(c *gin.Context) {
|
|
|
func SearchTemplate(c *gin.Context) {
|
|
|
query := c.Query("name")
|
|
|
uuid := c.Query("uuid")
|
|
|
- if uuid == "" {
|
|
|
+ if uuid == "undefined" || uuid == "" || uuid == "null" {
|
|
|
uuid = "test"
|
|
|
}
|
|
|
validate := validator.New()
|
|
@@ -189,7 +251,6 @@ func SearchTemplate(c *gin.Context) {
|
|
|
var result []bson.M
|
|
|
find := options.Find()
|
|
|
find.SetProjection(bson.M{
|
|
|
- "_id": 0,
|
|
|
"data": 0,
|
|
|
})
|
|
|
cursor, err := global.MongoCon.Find(context.Background(), fileter, find)
|