|
|
@@ -0,0 +1,958 @@
|
|
|
+package controller
|
|
|
+
|
|
|
+import (
|
|
|
+ "crypto/md5"
|
|
|
+ "designs/app/common/request"
|
|
|
+ localConfig "designs/config"
|
|
|
+ "designs/global"
|
|
|
+ "designs/model"
|
|
|
+ "designs/response"
|
|
|
+ "designs/service"
|
|
|
+ "designs/utils"
|
|
|
+ "encoding/hex"
|
|
|
+ "fmt"
|
|
|
+ "github.com/antihax/optional"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/tencentad/marketing-api-go-sdk/pkg/ads/v3"
|
|
|
+ "github.com/tencentad/marketing-api-go-sdk/pkg/api/v3"
|
|
|
+ "github.com/tencentad/marketing-api-go-sdk/pkg/config/v3"
|
|
|
+ "io"
|
|
|
+ "mime/multipart"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+func GetAccessToken(gid string, pid string) string {
|
|
|
+ var org struct {
|
|
|
+ AccessToken string `json:"access_token" gorm:"column:access_token"`
|
|
|
+ }
|
|
|
+
|
|
|
+ var AccessToken string
|
|
|
+ global.App.DB.Table("organizations").Where("oid", 46379136).Select("access_token").First(&org)
|
|
|
+ if org.AccessToken != "" {
|
|
|
+ AccessToken = org.AccessToken
|
|
|
+ } else {
|
|
|
+ AccessToken = "da5a0590d1653fd1e6c9b72cabcb14f1"
|
|
|
+ }
|
|
|
+
|
|
|
+ return AccessToken
|
|
|
+}
|
|
|
+
|
|
|
+func generateFileMD5(fileHeader *multipart.FileHeader) (string, error) {
|
|
|
+ file, err := fileHeader.Open()
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ hash := md5.New()
|
|
|
+ if _, err := io.Copy(hash, file); err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return hex.EncodeToString(hash.Sum(nil)), nil
|
|
|
+}
|
|
|
+
|
|
|
+// 上传图片到服务器
|
|
|
+func Upload(c *gin.Context) {
|
|
|
+ // 获取文件
|
|
|
+ file, err := c.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 400, "未选择文件")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 类型检查
|
|
|
+ ext := strings.ToLower(filepath.Ext(file.Filename))
|
|
|
+ allowed := map[string]bool{".jpg": true, ".jpeg": true, ".png": true, ".avi": true, ".mp4": true}
|
|
|
+ if !allowed[ext] {
|
|
|
+
|
|
|
+ response.Fail(c, 1001, "图片仅支持JPG/PNG,视频支持avi/mp4")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ md5Value, err := generateFileMD5(file)
|
|
|
+
|
|
|
+ // 存储路径生成
|
|
|
+ saveDir := fmt.Sprintf("uploads/%s", time.Now().Format("2006-01-02"))
|
|
|
+
|
|
|
+ os.MkdirAll(saveDir, 0755)
|
|
|
+
|
|
|
+ savePath := filepath.Join(saveDir, md5Value+ext)
|
|
|
+
|
|
|
+ // 保存文件
|
|
|
+ if err := c.SaveUploadedFile(file, savePath); err != nil {
|
|
|
+ response.Fail(c, 500, "文件保存失败")
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "path": savePath,
|
|
|
+ "md5": md5Value,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func ImageSet(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ ImgPath string `json:"imgPath" binding:"required"`
|
|
|
+ Md5 string `json:"md5" binding:"required"`
|
|
|
+ Name string `json:"name" binding:"required"`
|
|
|
+ Gid string `json:"gid" binding:"required"`
|
|
|
+ Pid string `json:"pid" binding:""`
|
|
|
+ Tags []int `json:"tags" binding:""`
|
|
|
+ IsImg9 int `json:"isImg9" binding:""`
|
|
|
+ Resolution struct {
|
|
|
+ Width int `json:"width" binding:""`
|
|
|
+ Height int `json:"height" binding:""`
|
|
|
+ } `json:"resolution" binding:""`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ if !CheckTags(form.Tags) {
|
|
|
+ response.Fail(c, 500, "tag中有不存在或错误的ID")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //检查重复
|
|
|
+ var image model.PropertyImage
|
|
|
+ global.App.DB.Table(model.TablePropertyImage).Where("name", form.Name).Or("md5", form.Md5).First(&image)
|
|
|
+ if image.ID != 0 {
|
|
|
+ response.Fail(c, 1001, "图片名称或素材重复")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //处理9图
|
|
|
+ if form.IsImg9 != 0 {
|
|
|
+ err := service.SliceImg(form.ImgPath, form.Name)
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //上传微信
|
|
|
+ files, err := os.Open(form.ImgPath)
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, "文件读取失败"+err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer files.Close()
|
|
|
+
|
|
|
+ //请求微信接口
|
|
|
+ va := api.ImagesAddOpts{File: optional.NewInterface(files), AccountId: optional.NewInt64(57213719)}
|
|
|
+ tads := ads.Init(&config.SDKConfig{
|
|
|
+ AccessToken: GetAccessToken("", ""),
|
|
|
+ })
|
|
|
+ ctx := *tads.Ctx
|
|
|
+ //res, _, err := tads.Advertiser().Get(ctx, nil)
|
|
|
+ res, _, err := tads.Images().Add(ctx, "UPLOAD_TYPE_FILE", form.Md5, &va)
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 1001, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //归档到纯皓系统中
|
|
|
+ now := model.XTime{Time: time.Now()}
|
|
|
+ newImage := model.PropertyImage{
|
|
|
+ Name: form.Name,
|
|
|
+ LocalPath: form.ImgPath,
|
|
|
+ Gid: form.Gid,
|
|
|
+ Pid: form.Pid,
|
|
|
+ WxId: *res.ImageId,
|
|
|
+ ImageWidth: int(*res.ImageWidth),
|
|
|
+ ImageHeight: int(*res.ImageHeight),
|
|
|
+ ImageFileSize: int(*res.ImageFileSize),
|
|
|
+ ImageSignature: *res.ImageSignature,
|
|
|
+ IsImg9: form.IsImg9,
|
|
|
+ CreatedAt: now,
|
|
|
+ UpdatedAt: now,
|
|
|
+ Resolution: strconv.Itoa(form.Resolution.Width) + "x" + strconv.Itoa(form.Resolution.Height),
|
|
|
+ }
|
|
|
+
|
|
|
+ err = global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImage).Create(&newImage).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //增加新tag
|
|
|
+ var tags []model.PropertyImageTag
|
|
|
+ for _, v := range form.Tags {
|
|
|
+ tags = append(tags, model.PropertyImageTag{
|
|
|
+ TagId: v,
|
|
|
+ ImageId: newImage.ID,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImageTag).CreateInBatches(&tags, 100).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("新增", userId, "图片", newImage)
|
|
|
+ if err != nil {
|
|
|
+ global.App.Log.Error("")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 1001, "存入数据库失败"+err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": res,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func ImageList(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Limit int `form:"limit" json:"limit" binding:"required"`
|
|
|
+ Offset int `form:"offset" json:"offset" binding:""`
|
|
|
+ Search string `form:"search" json:"search" binding:""`
|
|
|
+ Gid string `form:"gid" json:"gid" binding:""`
|
|
|
+ Pid *string `form:"pid" json:"pid" binding:""`
|
|
|
+ Order string `form:"order" json:"order" binding:""`
|
|
|
+ Prop string `form:"prop" json:"prop" binding:""`
|
|
|
+ IsImg9 string `form:"isImg9" json:"isImg9" binding:""`
|
|
|
+ Resolution string `form:"resolution" json:"resolution" binding:""`
|
|
|
+ Tag []int `form:"tag" json:"tag" binding:""`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ query := global.App.DB.Table(model.TablePropertyImage)
|
|
|
+
|
|
|
+ if form.Search != "" {
|
|
|
+ query = query.Where("name", "like", "%"+form.Search+"%")
|
|
|
+ }
|
|
|
+
|
|
|
+ if form.Gid != "" {
|
|
|
+ query = query.Where("gid", "=", form.Gid)
|
|
|
+ }
|
|
|
+ if form.Pid != nil && form.Gid == "" {
|
|
|
+ var gid []string
|
|
|
+ global.App.DB.Table(model.TableGame).Where("pid", form.Pid).Pluck("gid", &gid)
|
|
|
+
|
|
|
+ query = query.WhereIn("gid", gid)
|
|
|
+ }
|
|
|
+ if form.IsImg9 != "" {
|
|
|
+ query = query.Where("is_img9", "=", form.IsImg9)
|
|
|
+ }
|
|
|
+ if form.Resolution != "" {
|
|
|
+ query = query.Where("resolution", "=", form.Resolution)
|
|
|
+ }
|
|
|
+
|
|
|
+ if form.Order != "" && form.Prop != "" {
|
|
|
+ query = query.Order(form.Prop + " " + form.Order)
|
|
|
+ }
|
|
|
+
|
|
|
+ if form.Tag != nil {
|
|
|
+ var imageIds []int
|
|
|
+ query1 := global.App.DB.Table(model.TablePropertyImageTag)
|
|
|
+ for _, tag := range form.Tag {
|
|
|
+ query1 = query1.Where("tag_id", tag)
|
|
|
+ }
|
|
|
+ query1.Pluck("image_id", &imageIds)
|
|
|
+
|
|
|
+ query = query.WhereIn("id", imageIds)
|
|
|
+ }
|
|
|
+
|
|
|
+ var count int64
|
|
|
+ err := query.Count(&count).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ type TagData struct {
|
|
|
+ model.PropertyImageTag
|
|
|
+ Name string `json:"name" gorm:"not null;"`
|
|
|
+ }
|
|
|
+ var imgData []model.PropertyImage
|
|
|
+
|
|
|
+ err = query.Offset(form.Offset).Limit(form.Limit).Scan(&imgData).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //查询出tag
|
|
|
+ var ids []int
|
|
|
+ for _, v := range imgData {
|
|
|
+ ids = append(ids, v.ID)
|
|
|
+ }
|
|
|
+ var tagData []TagData
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImageTag).
|
|
|
+ LeftJoin(model.TablePropertyTag, "property_image_tag.tag_id = property_tag.id").
|
|
|
+ WhereIn("property_image_tag.image_id", ids).
|
|
|
+ Scan(&tagData).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ list := make([]struct {
|
|
|
+ model.PropertyImage
|
|
|
+ Tag []TagData `json:"tag"`
|
|
|
+ Download string `json:"download"`
|
|
|
+ }, len(imgData))
|
|
|
+
|
|
|
+ for k, v := range imgData {
|
|
|
+ list[k].ID = v.ID
|
|
|
+ list[k].Gid = v.Gid
|
|
|
+ list[k].Pid = v.Pid
|
|
|
+ list[k].WxId = v.WxId
|
|
|
+ list[k].Name = v.Name
|
|
|
+ list[k].CreatedAt = v.CreatedAt
|
|
|
+ list[k].UpdatedAt = v.UpdatedAt
|
|
|
+ list[k].LocalPath = v.LocalPath
|
|
|
+ list[k].ImageWidth = v.ImageWidth
|
|
|
+ list[k].ImageHeight = v.ImageHeight
|
|
|
+ list[k].ImageFileSize = v.ImageFileSize
|
|
|
+ list[k].ImageSignature = v.ImageSignature
|
|
|
+ list[k].ImageType = v.ImageType
|
|
|
+ list[k].Resolution = v.Resolution
|
|
|
+ list[k].Download = GetDownload(v.LocalPath)
|
|
|
+
|
|
|
+ for _, tag := range tagData {
|
|
|
+ if v.ID == tag.ImageId {
|
|
|
+ list[k].Tag = append(list[k].Tag, tag)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": map[string]interface{}{
|
|
|
+ "list": list,
|
|
|
+ "count": count,
|
|
|
+ },
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func GetImageListHead(c *gin.Context) {
|
|
|
+ //获取分辨率的数据
|
|
|
+ var resolution []string
|
|
|
+ err := global.App.DB.Table(model.TablePropertyImage).Group("resolution").Pluck("resolution", &resolution).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": map[string]interface{}{
|
|
|
+ "resolution": resolution,
|
|
|
+ },
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func GetVideoListHead(c *gin.Context) {
|
|
|
+ //获取分辨率的数据
|
|
|
+ var resolution []string
|
|
|
+ err := global.App.DB.Table(model.TablePropertyVideo).Group("resolution").Pluck("resolution", &resolution).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": map[string]interface{}{
|
|
|
+ "resolution": resolution,
|
|
|
+ },
|
|
|
+ })
|
|
|
+}
|
|
|
+func ImageDelete(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ IdList []int `form:"idList" json:"idList" binding:"required"`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ var image []model.PropertyImage
|
|
|
+ global.App.DB.Table(model.TablePropertyImage).WhereIn("id", form.IdList).Scan(&image)
|
|
|
+ if len(image) != len(form.IdList) {
|
|
|
+ response.Fail(c, 500, "图片id错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ ////删除云端数据
|
|
|
+ //for _, v := range image {
|
|
|
+ // var AccountId = int64(57213719)
|
|
|
+ // va := modelV3.ImagesDeleteRequest{
|
|
|
+ // AccountId: &AccountId,
|
|
|
+ // ImageId: &v.WxId,
|
|
|
+ // }
|
|
|
+ // tads := ads.Init(&config.SDKConfig{
|
|
|
+ // AccessToken: GetAccessToken("", ""),
|
|
|
+ // })
|
|
|
+ // ctx := *tads.Ctx
|
|
|
+ // //res, _, err := tads.Advertiser().Get(ctx, nil)
|
|
|
+ // _, _, err := tads.Images().Delete(ctx, va)
|
|
|
+ // if err != nil {
|
|
|
+ // response.Fail(c, 1001, err.Error())
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+ var d interface{}
|
|
|
+ err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ //删除image
|
|
|
+ err := global.App.DB.Table(model.TablePropertyImage).WhereIn("id", form.IdList).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ //删除tag
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImageTag).WhereIn("image_id", form.IdList).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("删除", userId, "图片", image)
|
|
|
+ if err != nil {
|
|
|
+ global.App.Log.Error("")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err.Error())
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //记录日志
|
|
|
+
|
|
|
+ response.Success(c, gin.H{})
|
|
|
+}
|
|
|
+
|
|
|
+func UpdateImageTags(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Id int `form:"id" json:"id" binding:"required"`
|
|
|
+ Tags []int `form:"tags" json:"tags" binding:""`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ if !CheckTags(form.Tags) {
|
|
|
+ response.Fail(c, 500, "tag中有不存在或错误的ID")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var image model.PropertyImage
|
|
|
+ global.App.DB.Table(model.TablePropertyImage).Where("id", form.Id).Select("id", "wx_id").First(&image)
|
|
|
+ if image.ID == 0 {
|
|
|
+ response.Fail(c, 500, "图片id错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var d interface{}
|
|
|
+ err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ //删除tag
|
|
|
+ err := global.App.DB.Table(model.TablePropertyImageTag).Where("image_id", image.ID).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ //增加新tag
|
|
|
+ var tags []model.PropertyImageTag
|
|
|
+ for _, v := range form.Tags {
|
|
|
+ tags = append(tags, model.PropertyImageTag{
|
|
|
+ TagId: v,
|
|
|
+ ImageId: image.ID,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImageTag).CreateInBatches(&tags, 100).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新image表更新时间
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImage).Where("id", image.ID).Updates(map[string]interface{}{
|
|
|
+ "updatedAt": time.Now(),
|
|
|
+ }).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("修改tag", userId, "图片", form.Tags)
|
|
|
+ if err != nil {
|
|
|
+ global.App.Log.Error("")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //记录日志
|
|
|
+
|
|
|
+ response.Success(c, gin.H{})
|
|
|
+}
|
|
|
+
|
|
|
+func CheckTags(Tags []int) bool {
|
|
|
+ var count int64
|
|
|
+ global.App.DB.Table(model.TablePropertyTag).WhereIn("id", Tags).Count(&count)
|
|
|
+
|
|
|
+ return int(count) == len(Tags)
|
|
|
+}
|
|
|
+
|
|
|
+func SetTag(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Name string `form:"name" json:"name" binding:"required"`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ var tag model.PropertyTag
|
|
|
+ global.App.DB.Table(model.TablePropertyTag).Where("name", form.Name).First(&tag)
|
|
|
+ if tag.ID != 0 {
|
|
|
+ response.Fail(c, 1001, "该tag已建立")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ now := model.XTime{
|
|
|
+ Time: time.Now(),
|
|
|
+ }
|
|
|
+ tag = model.PropertyTag{
|
|
|
+ Name: form.Name,
|
|
|
+ CreatedAt: now,
|
|
|
+ }
|
|
|
+ err := global.App.DB.Table(model.TablePropertyTag).Create(&tag).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //记录日志
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("新增", userId, "tag", tag)
|
|
|
+ if err != nil {
|
|
|
+ global.App.Log.Error("")
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": map[string]interface{}{
|
|
|
+ "id": tag.ID,
|
|
|
+ },
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func DeleteTag(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Id int `form:"id" json:"id" binding:"required"`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ var tag model.PropertyTag
|
|
|
+ global.App.DB.Table(model.TablePropertyTag).Where("id", form.Id).First(&tag)
|
|
|
+ if tag.ID == 0 {
|
|
|
+ response.Fail(c, 1001, "该tag不存在")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var d interface{}
|
|
|
+ err := global.App.DB.Table(model.TablePropertyTag).Where("id", form.Id).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //记录日志
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("删除", userId, "tag", tag)
|
|
|
+ if err != nil {
|
|
|
+ global.App.Log.Error("")
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{})
|
|
|
+}
|
|
|
+
|
|
|
+func VideoSet(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ VideoPath string `json:"videoPath" binding:"required"`
|
|
|
+ HeadImgPath string `json:"headImgPath" binding:"required"`
|
|
|
+ Md5 string `json:"md5" binding:"required"`
|
|
|
+ Name string `json:"name" binding:"required"`
|
|
|
+ Gid string `json:"gid" binding:"required"`
|
|
|
+ Pid string `json:"pid" binding:""`
|
|
|
+ Tags []int `json:"tags" binding:""`
|
|
|
+ Resolution struct {
|
|
|
+ Width int `json:"width" binding:""`
|
|
|
+ Height int `json:"height" binding:""`
|
|
|
+ } `json:"resolution" binding:""`
|
|
|
+ VideoType int `json:"videoType" binding:"required"`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ if !CheckTags(form.Tags) {
|
|
|
+ response.Fail(c, 500, "tag中有不存在或错误的ID")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //检查重复
|
|
|
+ var video model.PropertyVideo
|
|
|
+ global.App.DB.Table(model.TablePropertyVideo).Where("name", form.Name).Or("md5", form.Md5).First(&video)
|
|
|
+ if video.ID != 0 {
|
|
|
+ response.Fail(c, 1001, "视频名称或素材重复")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //上传微信
|
|
|
+ files, err := os.Open(form.VideoPath)
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, "文件读取失败"+err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer files.Close()
|
|
|
+
|
|
|
+ //请求微信接口
|
|
|
+ va := api.VideosAddOpts{AccountId: optional.NewInt64(57213719)}
|
|
|
+ tads := ads.Init(&config.SDKConfig{
|
|
|
+ AccessToken: GetAccessToken("", ""),
|
|
|
+ })
|
|
|
+ ctx := *tads.Ctx
|
|
|
+ //res, _, err := tads.Advertiser().Get(ctx, nil)
|
|
|
+ res, _, err := tads.Videos().Add(ctx, files, form.Md5, &va)
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 1001, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //入库
|
|
|
+ //归档到纯皓系统中
|
|
|
+ now := model.XTime{Time: time.Now()}
|
|
|
+
|
|
|
+ newVideo := model.PropertyVideo{
|
|
|
+ Name: form.Name,
|
|
|
+ LocalPath: form.VideoPath,
|
|
|
+ Gid: form.Gid,
|
|
|
+ Pid: form.Pid,
|
|
|
+ WxId: int(*res.VideoId),
|
|
|
+ VideoSignature: form.Md5,
|
|
|
+ CreatedAt: now,
|
|
|
+ UpdatedAt: now,
|
|
|
+ VideoType: form.VideoType,
|
|
|
+ Resolution: strconv.Itoa(form.Resolution.Width) + "x" + strconv.Itoa(form.Resolution.Height),
|
|
|
+ HeadImgPath: form.HeadImgPath,
|
|
|
+ }
|
|
|
+
|
|
|
+ err = global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ err = global.App.DB.Table(model.TablePropertyVideo).Create(&newVideo).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //增加新tag
|
|
|
+ var tags []model.PropertyVideoTag
|
|
|
+ for _, v := range form.Tags {
|
|
|
+ tags = append(tags, model.PropertyVideoTag{
|
|
|
+ TagId: v,
|
|
|
+ VideoId: newVideo.ID,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ err = global.App.DB.Table(model.TablePropertyVideoTag).CreateInBatches(&tags, 100).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //记录日志
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("新增", userId, "视频", newVideo)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 1001, "存入数据库失败"+err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "files": res,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func VideoDelete(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ IdList []int `form:"idList" json:"idList" binding:"required"`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ var video []model.PropertyVideo
|
|
|
+ global.App.DB.Table(model.TablePropertyVideo).WhereIn("id", form.IdList).Scan(&video)
|
|
|
+ if len(video) != len(form.IdList) {
|
|
|
+ response.Fail(c, 500, "图片id错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ ////删除云端数据
|
|
|
+ //videoId := int64(video.WxId)
|
|
|
+ //var AccountId = int64(57213719)
|
|
|
+ //va := modelV3.VideosDeleteRequest{
|
|
|
+ // AccountId: &AccountId,
|
|
|
+ // VideoId: &videoId,
|
|
|
+ //}
|
|
|
+ //tads := ads.Init(&config.SDKConfig{
|
|
|
+ // AccessToken: GetAccessToken("", ""),
|
|
|
+ //})
|
|
|
+ //ctx := *tads.Ctx
|
|
|
+ ////res, _, err := tads.Advertiser().Get(ctx, nil)
|
|
|
+ //_, _, err := tads.Videos().Delete(ctx, va)
|
|
|
+ //if err != nil {
|
|
|
+ // response.Fail(c, 1001, err.Error())
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+
|
|
|
+ var d interface{}
|
|
|
+ err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ //删除video
|
|
|
+ err := global.App.DB.Table(model.TablePropertyVideo).WhereIn("id", form.IdList).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ //删除tag
|
|
|
+ err = global.App.DB.Table(model.TablePropertyVideoTag).Where("video_id", form.IdList).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //记录日志
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("删除", userId, "视频", video)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 501, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{})
|
|
|
+}
|
|
|
+
|
|
|
+func UpdateVideoTags(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Id int `form:"id" json:"id" binding:"required"`
|
|
|
+ Tags []int `form:"tags" json:"tags" binding:""`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ if !CheckTags(form.Tags) {
|
|
|
+ response.Fail(c, 500, "tag中有不存在或错误的ID")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var video model.PropertyVideo
|
|
|
+ global.App.DB.Table(model.TablePropertyVideo).Where("id", form.Id).Select("id", "wx_id").First(&video)
|
|
|
+ if video.ID == 0 {
|
|
|
+ response.Fail(c, 500, "图片id错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var d interface{}
|
|
|
+ err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ //删除tag
|
|
|
+ err := global.App.DB.Table(model.TablePropertyVideoTag).Where("video_id", video.ID).Delete(d).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ //增加新tag
|
|
|
+ var tags []model.PropertyVideoTag
|
|
|
+ for _, v := range form.Tags {
|
|
|
+ tags = append(tags, model.PropertyVideoTag{
|
|
|
+ TagId: v,
|
|
|
+ VideoId: video.ID,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ err = global.App.DB.Table(model.TablePropertyVideoTag).CreateInBatches(&tags, 100).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新image表更新时间
|
|
|
+ err = global.App.DB.Table(model.TablePropertyVideo).Where("id", video.ID).Updates(map[string]interface{}{
|
|
|
+ "updatedAt": time.Now(),
|
|
|
+ }).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ //记录日志
|
|
|
+ userId := c.GetInt("userId")
|
|
|
+ err = service.SetActionLog("修改", userId, "视频", form.Tags)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, "数据库错误"+err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{})
|
|
|
+}
|
|
|
+
|
|
|
+func GetTags(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Offset int `form:"offset" json:"offset" binding:""`
|
|
|
+ Limit int `form:"limit" json:"limit" binding:""`
|
|
|
+ Search string `form:"search" json:"search" binding:""`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ query := global.App.DB.Table(model.TablePropertyTag)
|
|
|
+
|
|
|
+ if form.Search != "" {
|
|
|
+ query = query.Where("name", "like", "%"+form.Search+"%")
|
|
|
+ }
|
|
|
+ var count int64
|
|
|
+ err := query.Count(&count).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if form.Offset != 0 {
|
|
|
+ query = query.Offset(form.Offset)
|
|
|
+ }
|
|
|
+ if form.Limit != 0 {
|
|
|
+ query = query.Limit(form.Limit)
|
|
|
+ }
|
|
|
+
|
|
|
+ var tags []model.PropertyTag
|
|
|
+ err = query.Scan(&tags).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": tags,
|
|
|
+ "count": count,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func VideoList(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Offset int `form:"offset" json:"offset" binding:""`
|
|
|
+ Limit int `form:"limit" json:"limit" binding:"required"`
|
|
|
+ Search string `form:"search" json:"search" binding:""`
|
|
|
+ Order string `form:"order" json:"order" binding:""`
|
|
|
+ Prop string `form:"prop" json:"prop" binding:""`
|
|
|
+ Gid string `form:"gid" json:"gid" binding:""`
|
|
|
+ Pid string `form:"pid" json:"pid" binding:""`
|
|
|
+ Resolution string `form:"resolution" json:"resolution" binding:""`
|
|
|
+ VideoType string `form:"video_type" json:"video_type" binding:""`
|
|
|
+ Tag []int `form:"tag" json:"tag" binding:""`
|
|
|
+ }{})
|
|
|
+
|
|
|
+ query := global.App.DB.Table(model.TablePropertyVideo)
|
|
|
+ if form.Search != "" {
|
|
|
+ query = query.Where("name", "like", "%"+form.Search+"%")
|
|
|
+ }
|
|
|
+ if form.Gid != "" {
|
|
|
+ query = query.Where("gid", "=", form.Gid)
|
|
|
+ }
|
|
|
+ if form.Pid != "" {
|
|
|
+ query = query.Where("pid", "=", form.Pid)
|
|
|
+ }
|
|
|
+
|
|
|
+ if form.Order != "" && form.Prop != "" {
|
|
|
+ query = query.Order(form.Prop + " " + form.Order)
|
|
|
+ }
|
|
|
+ if form.Resolution != "" {
|
|
|
+ query = query.Where("resolution", "=", form.Resolution)
|
|
|
+ }
|
|
|
+ if form.VideoType != "" {
|
|
|
+ query = query.Where("video_type", "=", form.VideoType)
|
|
|
+ }
|
|
|
+ if form.Tag != nil {
|
|
|
+ var videoIds []int
|
|
|
+ query1 := global.App.DB.Table(model.TablePropertyVideoTag)
|
|
|
+ for _, tag := range form.Tag {
|
|
|
+ query1 = query1.Where("tag_id", tag)
|
|
|
+ }
|
|
|
+ query1.Pluck("video_id", &videoIds)
|
|
|
+
|
|
|
+ query = query.WhereIn("id", videoIds)
|
|
|
+ }
|
|
|
+
|
|
|
+ var count int64
|
|
|
+ err := query.Count(&count).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ type TagData struct {
|
|
|
+ model.PropertyVideoTag
|
|
|
+ Name string `json:"name" gorm:"not null;"`
|
|
|
+ }
|
|
|
+ var videos []model.PropertyVideo
|
|
|
+ err = query.Offset(form.Offset).Limit(form.Limit).Scan(&videos).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询出tag
|
|
|
+ var ids []int
|
|
|
+ for _, v := range videos {
|
|
|
+ ids = append(ids, v.ID)
|
|
|
+ }
|
|
|
+ var tagData []TagData
|
|
|
+ err = global.App.DB.Table(model.TablePropertyImageTag).
|
|
|
+ LeftJoin(model.TablePropertyTag, "property_image_tag.tag_id = property_tag.id").
|
|
|
+ WhereIn("property_image_tag.image_id", ids).
|
|
|
+ Scan(&tagData).Error
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c, 500, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ list := make([]struct {
|
|
|
+ model.PropertyVideo
|
|
|
+ Tag []TagData `json:"tag"`
|
|
|
+ Download string `json:"download"`
|
|
|
+ HeadImgDownload string `json:"headImgDownload"`
|
|
|
+ }, len(videos))
|
|
|
+
|
|
|
+ for k, v := range videos {
|
|
|
+ list[k].ID = v.ID
|
|
|
+ list[k].Gid = v.Gid
|
|
|
+ list[k].Pid = v.Pid
|
|
|
+ list[k].Name = v.Name
|
|
|
+ list[k].WxId = v.WxId
|
|
|
+ list[k].CreatedAt = v.CreatedAt
|
|
|
+ list[k].UpdatedAt = v.UpdatedAt
|
|
|
+ list[k].LocalPath = v.LocalPath
|
|
|
+ list[k].Resolution = v.Resolution
|
|
|
+ list[k].VideoType = v.VideoType
|
|
|
+ list[k].Download = GetDownload(v.LocalPath)
|
|
|
+
|
|
|
+ list[k].HeadImgPath = v.HeadImgPath
|
|
|
+
|
|
|
+ list[k].HeadImgDownload = GetDownload(v.HeadImgPath)
|
|
|
+
|
|
|
+ for _, tag := range tagData {
|
|
|
+ if v.ID == tag.VideoId {
|
|
|
+ list[k].Tag = append(list[k].Tag, tag)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Success(c, gin.H{
|
|
|
+ "data": map[string]interface{}{
|
|
|
+ "list": list,
|
|
|
+ "count": count,
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func GetDownload(filPath string) string {
|
|
|
+
|
|
|
+ return localConfig.Get("app.appUrl") + "/download?filename=" + filPath
|
|
|
+}
|