do_api_user_login_logic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package User
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "time"
  7. "wechat-api/ent"
  8. "wechat-api/ent/wxcarduser"
  9. "wechat-api/hook/wechat"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "wechat-api/internal/utils/dberrorhandler"
  13. "wechat-api/internal/utils/jwt"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type DoApiUserLoginLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewDoApiUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoApiUserLoginLogic {
  22. return &DoApiUserLoginLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *DoApiUserLoginLogic) DoApiUserLogin(req *types.UserLoginReq) (*types.UserLoginResp, error) {
  28. var err error
  29. AppId := l.svcCtx.Config.Miniprogram.Appid
  30. Secret := l.svcCtx.Config.Miniprogram.Secret
  31. // 1. 初始化小程序应用实例
  32. app, err := wechat.NewClient(AppId, Secret)
  33. if err != nil {
  34. return nil, errorx.NewApiInternalError("init wechat miniprogram failed")
  35. }
  36. // 2. 调用小程序的授权登陆接口
  37. code := *req.JsCode // 前端小程序登录时,从微信获取的code
  38. resp, err := app.Auth.Session(code)
  39. if err != nil {
  40. return nil, errorx.NewApiInternalError("wechat login error")
  41. }
  42. if resp.OpenID == "" {
  43. return nil, errorx.NewApiInternalError(resp.ErrMSG)
  44. }
  45. userInfo, err := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.OpenID(resp.OpenID)).First(l.ctx)
  46. if err != nil {
  47. if ent.IsNotFound(err) {
  48. userInfo, err = l.svcCtx.DB.WxCardUser.Create().
  49. SetOpenID(resp.OpenID).
  50. SetUnionID(resp.UnionID).
  51. SetSessionKey(resp.SessionKey).
  52. Save(l.ctx)
  53. if err != nil {
  54. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  55. }
  56. } else {
  57. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  58. }
  59. } else {
  60. _, err = l.svcCtx.DB.WxCardUser.Update().Where(wxcarduser.OpenID(resp.OpenID)).SetSessionKey(resp.SessionKey).Save(l.ctx)
  61. if err != nil {
  62. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  63. }
  64. }
  65. // 这里需要改成实际用户的数据
  66. dataArray := make([]jwt.Option, 0)
  67. dataArray = append(dataArray, jwt.Option{
  68. Key: "userId",
  69. Val: userInfo.ID,
  70. })
  71. token, err := jwt.NewJwtToken(
  72. l.svcCtx.Config.Auth.AccessSecret,
  73. time.Now().Unix(),
  74. 86400*30,
  75. dataArray...,
  76. )
  77. if err != nil {
  78. return nil, errorx.NewInvalidArgumentError(err.Error())
  79. }
  80. return &types.UserLoginResp{
  81. BaseDataInfo: types.BaseDataInfo{
  82. Code: 0,
  83. Msg: errormsg.Success,
  84. },
  85. Data: types.UserToken{
  86. ID: &userInfo.ID,
  87. Token: &token,
  88. Avatar: &userInfo.Avatar,
  89. Nickname: &userInfo.Nickname,
  90. },
  91. }, nil
  92. }