do_api_user_login_logic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package User
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "time"
  8. "wechat-api/ent"
  9. "wechat-api/ent/wxcarduser"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "wechat-api/internal/utils/dberrorhandler"
  13. "wechat-api/internal/utils/jwt"
  14. mini "github.com/ArtisanCloud/PowerWeChat/v2/src/miniProgram"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type DoApiUserLoginLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewDoApiUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoApiUserLoginLogic {
  23. return &DoApiUserLoginLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx}
  27. }
  28. func (l *DoApiUserLoginLogic) DoApiUserLogin(req *types.UserLoginReq) (*types.UserLoginResp, error) {
  29. var err error
  30. Appid := l.svcCtx.Config.Miniprogram.Appid
  31. Secret := l.svcCtx.Config.Miniprogram.Secret
  32. //RedisAddr := l.svcCtx.Config.Miniprogram.Redisaddr
  33. // 1. 初始化小程序应用实例
  34. app, err := mini.NewMiniProgram(&mini.UserConfig{
  35. AppID: Appid, // 小程序、公众号或者企业微信的appid
  36. Secret: Secret, // 商户号 appID
  37. HttpDebug: true,
  38. Debug: false,
  39. // 可选,不传默认走程序内存
  40. //Cache: kernel.NewRedisClient(&kernel.RedisOptions{
  41. // Addr: RedisAddr,
  42. // Password: "",
  43. // DB: 0,
  44. //}),
  45. })
  46. if err != nil {
  47. return nil, errorx.NewApiInternalError("init wechat miniprogram failed")
  48. }
  49. // 2. 调用小程序的授权登陆接口
  50. code := *req.JsCode // 前端小程序登录时,从微信获取的code
  51. resp, err := app.Auth.Session(code)
  52. if err != nil {
  53. return nil, errorx.NewApiInternalError("wechat login error")
  54. }
  55. fmt.Printf("resp=%v \n", resp)
  56. if resp.ErrCode != 0 {
  57. return nil, errorx.NewApiInternalError(resp.ErrMSG)
  58. }
  59. userInfo, err := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.OpenID(resp.OpenID)).First(l.ctx)
  60. if err != nil {
  61. if ent.IsNotFound(err) {
  62. userInfo, err = l.svcCtx.DB.WxCardUser.Create().
  63. SetOpenID(resp.OpenID).
  64. SetUnionID(resp.UnionID).
  65. SetSessionKey(resp.SessionKey).
  66. Save(l.ctx)
  67. if err != nil {
  68. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  69. }
  70. } else {
  71. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  72. }
  73. } else {
  74. _, err = l.svcCtx.DB.WxCardUser.Update().Where(wxcarduser.OpenID(resp.OpenID)).SetSessionKey(resp.SessionKey).Save(l.ctx)
  75. if err != nil {
  76. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  77. }
  78. }
  79. //TODO 这里需要改成实际用户的数据
  80. dataArray := make([]jwt.Option, 0)
  81. dataArray = append(dataArray, jwt.Option{
  82. Key: "userId",
  83. Val: userInfo.ID,
  84. })
  85. token, err := jwt.NewJwtToken(
  86. l.svcCtx.Config.Auth.AccessSecret,
  87. time.Now().Unix(),
  88. 86400*30,
  89. dataArray...,
  90. )
  91. if err != nil {
  92. return nil, errorx.NewInvalidArgumentError(err.Error())
  93. }
  94. return &types.UserLoginResp{
  95. BaseDataInfo: types.BaseDataInfo{
  96. Code: 0,
  97. Msg: errormsg.Success,
  98. },
  99. Data: types.UserToken{
  100. ID: &userInfo.ID,
  101. Token: &token,
  102. Avatar: &userInfo.Avatar,
  103. Nickname: &userInfo.Nickname,
  104. },
  105. }, nil
  106. }