bind_mobile_logic.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package user
  2. import (
  3. "context"
  4. "git.i2edu.net/i2/i2-bill-api/model"
  5. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  6. "git.i2edu.net/i2/i2-bill-api/internal/types"
  7. "git.i2edu.net/i2/go-zero/core/logx"
  8. )
  9. type BindMobileLogic struct {
  10. logx.Logger
  11. ctx context.Context
  12. svcCtx *svc.ServiceContext
  13. UserId int64
  14. SessionKey string
  15. }
  16. func NewBindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) BindMobileLogic {
  17. return BindMobileLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. }
  22. }
  23. func (l *BindMobileLogic) BindMobile(req types.BindMobileRequest) (*types.Response, error) {
  24. mobile, err := l.svcCtx.Wechat.DecryptMobile(l.SessionKey, req.EncryptedData, req.Iv)
  25. if err != nil {
  26. logx.Error(err)
  27. return nil, err
  28. }
  29. _, err = l.svcCtx.SqlConn.Exec(`update i2bill_user set mobile = ? where id = ?`, mobile, l.UserId)
  30. tx := l.svcCtx.DB.NewSession()
  31. tx.Begin()
  32. _, err = tx.Exec("update i2bill_user set mobile = ? where id = ?", mobile, l.UserId)
  33. if err != nil {
  34. tx.Rollback()
  35. logx.Error(err)
  36. return nil, err
  37. }
  38. //_, err = tx.Exec("update i2bill_mkt_part_time_user set ipone_number = ? where user_id = ?", mobile, l.UserId)
  39. //if err != nil {
  40. // tx.Rollback()
  41. // logx.Error(err)
  42. // return nil, err
  43. //}
  44. //关联兼职
  45. partUser, err := model.GetPartTimeXormByUserId(l.UserId, l.svcCtx.DB)
  46. if err != nil {
  47. tx.Rollback()
  48. logx.Error(err)
  49. return nil, err
  50. }
  51. if mobile != "" && (partUser == nil || partUser.UserId == 0) {
  52. partTime := new(model.I2billMktPartTimeXorm)
  53. _, err = l.svcCtx.DB.Where("ipone_number = ? and del_flag = 0 and user_id = 0", mobile).Get(partTime)
  54. if err != nil {
  55. tx.Rollback()
  56. logx.Error(err)
  57. return nil, err
  58. }
  59. partTime.UserId = l.UserId
  60. if partTime.Id != 0 {
  61. _, err := tx.ID(partTime.Id).Update(partTime)
  62. if err != nil {
  63. tx.Rollback()
  64. logx.Error(err)
  65. return nil, err
  66. }
  67. }
  68. }
  69. tx.Commit()
  70. resp := make(map[string]interface{})
  71. resp["id"] = l.UserId
  72. resp["mobile"] = mobile
  73. return &types.Response{
  74. 200, "", resp,
  75. }, nil
  76. }