signature.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "os"
  10. "path"
  11. "strings"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. "go.mongodb.org/mongo-driver/bson"
  15. "go.mongodb.org/mongo-driver/bson/primitive"
  16. )
  17. // 签名管理
  18. func Signature(r *GinRouter) {
  19. // 创建签名
  20. r.POSTJWT("/signature/create", SignatureAdd)
  21. // 获取签名详情
  22. r.GETJWT("/signature/detail/:id", SignatureDetail)
  23. // 获取签名列表
  24. r.GETJWT("/signature/list", SignatureList)
  25. // 更新签名
  26. r.POSTJWT("/signature/update", SignatureUpdate)
  27. // 删除签名
  28. r.POSTJWT("/signature/delete/:id", SignatureDel)
  29. r.POSTJWT("/signature/upload/image", SignatureUpload)
  30. }
  31. // 创建签名
  32. func SignatureAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  33. var signature model.Signature
  34. err := c.ShouldBindJSON(&signature)
  35. if err != nil {
  36. fmt.Println(err)
  37. return nil, errors.New("参数错误!")
  38. }
  39. // if signature.UserId.IsZero() {
  40. // return nil, errors.New("签名用户为空")
  41. // }
  42. signature.Format = `{"autofit":true,"print_obj":true,"lock_aspect_ratio":false,"locked":false,"positioning":"oncell"}`
  43. signature.CreateTime = time.Now()
  44. signature.UpdateTime = time.Now()
  45. return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, &signature)
  46. }
  47. // 获取签名详情
  48. func SignatureDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  49. signatureId := c.Param("id")
  50. id, err := primitive.ObjectIDFromHex(signatureId)
  51. if err != nil {
  52. return nil, errors.New("非法id")
  53. }
  54. var signature model.Signature
  55. option := &repo.DocSearchOptions{
  56. CollectName: repo.CollectionSignature,
  57. Query: repo.Map{"_id": id},
  58. }
  59. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &signature)
  60. if !found || err != nil {
  61. log.Info(err)
  62. return nil, errors.New("数据未找到")
  63. }
  64. return signature, nil
  65. }
  66. // 获取签名列表
  67. func SignatureList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  68. page, size, query := UtilQueryPageSize(c)
  69. option := &repo.PageSearchOptions{
  70. CollectName: repo.CollectionSignature,
  71. Query: query,
  72. Page: page,
  73. Size: size,
  74. Sort: bson.M{"sort": 1},
  75. }
  76. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  77. }
  78. // 更新签名
  79. func SignatureUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  80. var signature model.Signature
  81. err := c.ShouldBindJSON(&signature)
  82. if err != nil {
  83. return nil, errors.New("参数错误")
  84. }
  85. if signature.Id.IsZero() {
  86. return nil, errors.New("id的为空")
  87. }
  88. signature.UpdateTime = time.Now()
  89. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signature.Id.Hex(), &signature)
  90. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionSignature, signature.Id.Hex(), &signature, &repo.RecordLogReq{
  91. Path: c.Request.URL.Path,
  92. TargetId: signature.Id.Hex(),
  93. })
  94. }
  95. // 删除签名
  96. func SignatureDel(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  97. signatureId := c.Param("id")
  98. if signatureId == "" {
  99. return nil, errors.New("id为空")
  100. }
  101. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signatureId)
  102. }
  103. // 本地上传图片
  104. func SignatureUpload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  105. handle, err := c.FormFile("image")
  106. // 上传图像接口
  107. if err != nil {
  108. return nil, err
  109. }
  110. uploadFile, err := handle.Open()
  111. if err != nil {
  112. return nil, err
  113. }
  114. ext := strings.ToLower(path.Ext(handle.Filename))
  115. if ext != ".jpg" && ext != ".png" {
  116. return nil, errors.New("只支持jpg/png图片上传")
  117. }
  118. // 保存图片
  119. os.Mkdir("signature/", 0777)
  120. pathFileName := fmt.Sprintf("%s%s", randName(6), handle.Filename)
  121. saveFile, err := os.OpenFile(fmt.Sprintf("signature/%s", pathFileName), os.O_WRONLY|os.O_CREATE, 0666)
  122. if err != nil {
  123. return nil, err
  124. }
  125. io.Copy(saveFile, uploadFile)
  126. defer uploadFile.Close()
  127. defer saveFile.Close()
  128. // http://127.0.0.1:18088/boxcost/public/
  129. out := map[string]string{
  130. "url": fmt.Sprintf("%s%s", SignatureDir, pathFileName),
  131. "path": fmt.Sprintf("signature/%s", pathFileName),
  132. }
  133. return out, nil
  134. }