| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package pub
- import (
- "app/apis/middleware"
- "app/commons/constant"
- "app/commons/core"
- "app/commons/core/exchange"
- "app/commons/model/entity"
- "fmt"
- "github.com/gin-gonic/gin"
- "time"
- )
- // 授权登录
- func (s *Server) AuthInfo(ctx *gin.Context) {
- c := s.FromContext(ctx)
- redirectUri := c.Query("redirectUri") // 当前端传入调整地址 授权成功后需跳转地址
- type AuthInfoResp struct {
- DefaultDomainAuthUrl string `json:"defaultDomainAuthUrl"` // 含默认主域名授权地址
- NoDomainAuthUrl string `json:"noDomainAuthUrl"` // 无主域名授权地址 -- 授权路径 用于正式环境下前端获取主域名后 拼接用
- DefaultDomain string `json:"defaultDomain"` // 默认主域名
- IsLogin bool `json:"isLogin"` // 用户当前是否登陆
- }
- resp := new(AuthInfoResp)
- resp.IsLogin = c.OpenId() != ""
- resp.DefaultDomainAuthUrl = exchange.AuthUrl(redirectUri)
- resp.DefaultDomain = exchange.AuthRoot()
- resp.NoDomainAuthUrl = exchange.NoDomainAuthUrl(redirectUri)
- c.Resp(resp)
- }
- func (s *Server) Auth(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type request struct {
- Code string `json:"code"`
- }
- req := new(request)
- if err := c.BindJSON(&req); err != nil {
- c.Fail(err.Error())
- return
- }
- // code 获取用户信息 openId
- loginInfo, err := exchange.AccessToken(req.Code)
- if err != nil {
- core.JobLog.Infof("兑换交易所TOKEN失败:%s", err.Error())
- c.Fail(constant.ErrorAuthorizationFail)
- return
- }
- core.JobLog.Infof("兑换交易所TOKEN:%+v req:%+v", loginInfo, req)
- if loginInfo.Code != 200 {
- core.JobLog.Infof("兑换交易所TOKEN失败:%+v req:%+v", loginInfo, req)
- c.Fail(constant.ErrorAuthorizationFail)
- return
- }
- core.Log.Infof("用户授权登录:%+v", loginInfo.Data)
- strUserId := fmt.Sprintf("%d", loginInfo.Data.UserId)
- user, err := s.CheckUserWithOpenIdAndUid(loginInfo.Data.OpenId, strUserId, true)
- if err != nil {
- core.Log.Infof("用户授权登录:%+v", err.Error())
- c.Resp(err.Error())
- return
- }
- err = s.DB().Model(&entity.User{}).
- Where("id", user.Id).Updates(map[string]interface{}{
- "last_login_time": time.Now().Unix(),
- "last_login_ip": c.ClientIP(),
- "last_login_remote_ip": c.RemoteIP(),
- }).Error
- if err != nil {
- core.Log.Infof("ERROR:记录用户登陆信息失败:%+v", err.Error())
- }
- type LoginResp struct {
- Uid string `json:"uid"`
- OpenId string `json:"openId"`
- Code string `json:"code"`
- Authorization string `json:"authorization"`
- }
- resp := new(LoginResp)
- resp.Uid = strUserId
- resp.OpenId = user.OpenId
- resp.Code = user.Code
- resp.Authorization, err = s.getTokenWithUser(user)
- if err != nil {
- c.Resp(err.Error())
- return
- }
- c.Resp(resp)
- }
- // 通过用户信息获取TOKEN
- func (s *Server) getTokenWithUser(user *entity.User) (string, error) {
- member := middleware.Member{
- ID: user.Id,
- OpenId: user.OpenId,
- Uid: user.Uid,
- }
- token, err := middleware.GenerateJWT(member)
- if err != nil {
- return "", err
- }
- // 将新token设置为用户当前有效token -- redis 单点登录限制
- if err := middleware.SetUserCurrentToken(user.Id, token, 24*time.Hour); err != nil {
- core.Log.Errorf("设置用户当前token失败:%+v", err.Error())
- }
- return token, nil
- }
|