mkt_qr_add_logic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package mk_qr
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.i2edu.net/i2/i2-bill-api/model"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  12. "git.i2edu.net/i2/i2-bill-api/internal/types"
  13. "git.i2edu.net/i2/go-zero/core/logx"
  14. )
  15. type MktQrAddLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewMktQrAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) MktQrAddLogic {
  21. return MktQrAddLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *MktQrAddLogic) MktQrAdd(r *http.Request) (*types.Response, error) {
  28. // todo: add your logic here and delete this line
  29. type Mk struct {
  30. MkId string `json:"mk_id"`
  31. }
  32. body, err := ioutil.ReadAll(r.Body)
  33. defer r.Body.Close()
  34. if err != nil {
  35. return &types.Response{500, err.Error(), nil}, nil
  36. }
  37. var mk = new(Mk)
  38. err = json.Unmarshal(body, mk)
  39. if err != nil {
  40. return &types.Response{500, err.Error(), nil}, nil
  41. }
  42. mk_id := mk.MkId
  43. if mk_id == "" {
  44. return &types.Response{500, "mk_id 不能为空", nil}, nil
  45. }
  46. erpRes, err := model.GetErpUser("", mk_id, l.svcCtx.Transformer, l.ctx)
  47. if err != nil {
  48. logx.Error(err.Error())
  49. return &types.Response{Code: 500, Msg: err.Error(), Data: nil}, nil
  50. }
  51. if erpRes == nil {
  52. return &types.Response{500, "权限不足", nil}, nil
  53. }
  54. bean := new(model.I2billMktQr)
  55. _, err = l.svcCtx.DB.Where("mk_id = ? and del_flag = 0", mk_id).Get(bean)
  56. if err != nil {
  57. return &types.Response{500, err.Error(), nil}, nil
  58. }
  59. bean.MkId = mk_id
  60. mk_id = strings.ReplaceAll(mk_id, "-", "")
  61. if len(mk_id) > 32 {
  62. return &types.Response{500, "mk_id 格式错误", nil}, nil
  63. }
  64. sceneStr := fmt.Sprintf("%s", mk_id)
  65. atr, err := l.svcCtx.Wechat.GenQrCode(sceneStr, l.svcCtx.Config.Weixin.SharePtPage)
  66. if err != nil {
  67. logx.Error(err.Error())
  68. return &types.Response{500, err.Error(), nil}, nil
  69. }
  70. if atr == nil || atr.Url == "" {
  71. return &types.Response{500, "二维码生成失败", nil}, nil
  72. }
  73. _, err = l.svcCtx.DB.Insert(atr)
  74. if err != nil {
  75. logx.Error(err.Error())
  76. return &types.Response{500, err.Error(), nil}, nil
  77. }
  78. if bean.Id != 0 {
  79. bean.Qr = atr.Url
  80. _, err = l.svcCtx.DB.ID(bean.Id).Cols("qr").Update(bean)
  81. if err != nil {
  82. logx.Error(err.Error())
  83. return &types.Response{500, err.Error(), nil}, nil
  84. }
  85. } else {
  86. bean.Qr = atr.Url
  87. bean.CreateTime = time.Now()
  88. bean.DelFlag = 0
  89. _, err = l.svcCtx.DB.Insert(bean)
  90. if err != nil {
  91. logx.Error(err.Error())
  92. return &types.Response{500, err.Error(), nil}, nil
  93. }
  94. }
  95. bean.Qr = strings.TrimLeft(bean.Qr, "/")
  96. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  97. bean.Qr = domain + "/" + bean.Qr
  98. return &types.Response{200, "", bean}, nil
  99. }