| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package acquirer_mkt_qr
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.i2edu.net/i2/i2-bill-api/model"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- "git.i2edu.net/i2/i2-bill-api/internal/svc"
- "git.i2edu.net/i2/i2-bill-api/internal/types"
- "git.i2edu.net/i2/go-zero/core/logx"
- )
- type AcquirerMktQrUpdateLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewAcquirerMktQrUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) AcquirerMktQrUpdateLogic {
- return AcquirerMktQrUpdateLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- func (l *AcquirerMktQrUpdateLogic) AcquirerMktQrUpdate(r *http.Request) (*types.Response, error) {
- // todo: add your logic here and delete this line
- userId := l.svcCtx.GetUserIdByJwt(l.ctx)
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- bean := new(model.I2billAcquirerMktQrXorm)
- err = json.Unmarshal(body, bean)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- userInfo, err := model.GetI2bilUserInfo(userId, l.svcCtx.DB)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- //var ty = "part"
- //mk
- if userInfo.ErpId != "" {
- erpId, err := model.GetErpUser("", userInfo.ErpId, l.svcCtx.Transformer)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- if erpId == "" {
- return &types.Response{500, "未找到mk用户", nil}, nil
- }
- //ty = "mk"
- } else {
- //兼职
- partUser, err := model.GetPartTimeXormByUserId(userId, l.svcCtx.DB)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- if partUser.Id == 0 {
- return &types.Response{500, "请先申请成为兼职", nil}, nil
- }
- erpId, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- if erpId == "" {
- return &types.Response{500, "未找到mk用户", nil}, nil
- }
- bean.QudaoId = 1058
- bean.ActivityId = 0
- }
- acquirersQr := new(model.I2billAcquirerMktQrXorm)
- _, err = l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(acquirersQr)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- if acquirersQr.Id != 0 {
- bean.LastUpdateTime = time.Now()
- bean.LastUpdateBy = userId
- _, err = l.svcCtx.DB.ID(acquirersQr.Id).Cols("school_id", "activity_id", "qudao_id", "last_update_time", "last_update_by").Update(bean)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- return &types.Response{200, "", bean}, nil
- }
- bean.LastUpdateBy = userId
- bean.LastUpdateTime = time.Now()
- bean.CreateBy = userId
- bean.CreateTime = bean.LastUpdateTime
- bean.DelFlag = 0
- bean.UserId = userId
- //生成二维码
- //scene, _ := utils.AesEncrypt([]byte(fmt.Sprintf("%d", userId)), []byte(l.svcCtx.Config.AesSecret))
- sceneStr := fmt.Sprintf("%d", userId)
- atr, err := l.svcCtx.Wechat.GenQrCode(sceneStr, "pages/code/code")
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- if atr == nil || atr.Url == "" {
- return &types.Response{500, "二维码生成失败", nil}, nil
- }
- _, err = l.svcCtx.DB.Insert(atr)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- bean.Qr = atr.Url
- _, err = l.svcCtx.DB.Insert(bean)
- bean.Qr = strings.TrimLeft(bean.Qr, "/")
- domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
- bean.Qr = domain + "/" + bean.Qr
- if err != nil {
- return &types.Response{500, err.Error(), nil}, nil
- }
- return &types.Response{200, "", bean}, nil
- }
|