acquirer_mkt_qr_update_logic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package acquirer_mkt_qr
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "git.i2edu.net/i2/i2-bill-api/internal/utils"
  8. "git.i2edu.net/i2/i2-bill-api/model"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. "time"
  13. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  14. "git.i2edu.net/i2/i2-bill-api/internal/types"
  15. "git.i2edu.net/i2/go-zero/core/logx"
  16. )
  17. type AcquirerMktQrUpdateLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewAcquirerMktQrUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) AcquirerMktQrUpdateLogic {
  23. return AcquirerMktQrUpdateLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. }
  28. }
  29. func (l *AcquirerMktQrUpdateLogic) AcquirerMktQrUpdate(r *http.Request) (*types.Response, error) {
  30. // todo: add your logic here and delete this line
  31. userId := l.svcCtx.GetUserIdByJwt(l.ctx)
  32. body, err := ioutil.ReadAll(r.Body)
  33. if err != nil {
  34. logx.Error(err.Error())
  35. return &types.Response{500, err.Error(), nil}, nil
  36. }
  37. bean := new(model.I2billAcquirerMktQrXorm)
  38. err = json.Unmarshal(body, bean)
  39. if err != nil {
  40. logx.Error(err.Error())
  41. return &types.Response{500, err.Error(), nil}, nil
  42. }
  43. userInfo, err := model.GetI2bilUserInfo(userId, l.svcCtx.DB)
  44. if err != nil {
  45. logx.Error(err.Error())
  46. return &types.Response{500, err.Error(), nil}, nil
  47. }
  48. var ty = "part"
  49. //mk
  50. if userInfo.ErpId != "" {
  51. erpId, err := model.GetErpUser("", userInfo.ErpId, l.svcCtx.Transformer)
  52. if err != nil {
  53. logx.Error(err.Error())
  54. return &types.Response{500, err.Error(), nil}, nil
  55. }
  56. if erpId == "" {
  57. return &types.Response{500, "未找到mk用户", nil}, nil
  58. }
  59. ty = "mk"
  60. } else {
  61. //兼职
  62. partUser, err := model.GetPartTimeXormByUserId(userId, l.svcCtx.DB)
  63. if err != nil {
  64. logx.Error(err.Error())
  65. return &types.Response{500, err.Error(), nil}, nil
  66. }
  67. if partUser.Id == 0 {
  68. return &types.Response{500, "请先申请成为兼职", nil}, nil
  69. }
  70. erpId, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer)
  71. if err != nil {
  72. logx.Error(err.Error())
  73. return &types.Response{500, err.Error(), nil}, nil
  74. }
  75. if erpId == "" {
  76. return &types.Response{500, "未找到mk用户", nil}, nil
  77. }
  78. bean.QudaoId = 1058
  79. bean.ActivityId = 0
  80. }
  81. acquirersQr := new(model.I2billAcquirerMktQrXorm)
  82. _, err = l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(acquirersQr)
  83. if err != nil {
  84. logx.Error(err.Error())
  85. return &types.Response{500, err.Error(), nil}, nil
  86. }
  87. if acquirersQr.Id != 0 {
  88. bean.LastUpdateTime = time.Now()
  89. bean.LastUpdateBy = userId
  90. _, err = l.svcCtx.DB.ID(acquirersQr.Id).AllCols().Update(bean)
  91. if err != nil {
  92. logx.Error(err.Error())
  93. return &types.Response{500, err.Error(), nil}, nil
  94. }
  95. return &types.Response{200, "", nil}, nil
  96. }
  97. bean.LastUpdateBy = userId
  98. bean.LastUpdateTime = time.Now()
  99. bean.CreateBy = userId
  100. bean.CreateTime = bean.LastUpdateTime
  101. bean.DelFlag = 0
  102. bean.UserId = userId
  103. //生成二维码
  104. scene, _ := utils.AesEncrypt([]byte(fmt.Sprintf("%d", userId)), []byte(l.svcCtx.Config.AesSecret))
  105. sceneStr := fmt.Sprintf("sign=%s&type=%s", base64.StdEncoding.EncodeToString(scene), ty)
  106. uri, err := l.svcCtx.Wechat.GenQrCode(sceneStr, "/pages/code/code")
  107. if err != nil {
  108. logx.Error(err.Error())
  109. return &types.Response{500, err.Error(), nil}, nil
  110. }
  111. bean.Qr = uri
  112. _, err = l.svcCtx.DB.Insert(bean)
  113. bean.Qr = strings.TrimLeft(bean.Qr, "/")
  114. domain := strings.TrimRight(l.svcCtx.Config.Domain, "/")
  115. bean.Qr = domain + "/" + bean.Qr
  116. if err != nil {
  117. return &types.Response{500, err.Error(), nil}, nil
  118. }
  119. return &types.Response{200, "", bean}, nil
  120. }