JM-WechatMini/oauth2/handle/user.go

70 lines
1.7 KiB
Go
Raw Normal View History

2023-11-02 04:34:46 +08:00
package handle
import (
"errors"
"git.echol.cn/loser/logger/log"
"miniapp/model/app"
"miniapp/model/common/constant"
"miniapp/service"
"miniapp/utils"
"strconv"
)
// 获取普通用户信息
func getUser(account string, loginType constant.LoginType, nikeName string, avatarUrl string) (userId string, err error) {
// 根据登录类型获取用户信息
// 定义微信小程序信息
//var unionId, openId, sessionKey string
var mobile string
// 定义用户信息
var user app.User
//user.InviteCode = &inviteCode
switch loginType {
case constant.LoginTypeWeChatMiniApp:
mobile, err = utils.WeChatUtils().GetPhoneNumber(account)
if err != nil {
return
}
if mobile == "" {
err = errors.New("获取手机号失败")
return
}
user.Phone = mobile
user.Nickname = nikeName
user.Avatar = avatarUrl
default:
user.Phone = account
user.Nickname = nikeName
user.Avatar = avatarUrl
}
// 查询用户信息
if err = service.ServiceGroupApp.AppServiceGroup.UserService.GetOrCreate(&user); err != nil {
log.Errorf("获取用户信息或创建用户失败,错误信息:%s", err.Error())
err = errors.New("登录失败,请联系管理员")
return
}
// 校验用户状态
if user.Status == constant.UserStatusDisabled {
err = errors.New("账户已被禁用")
return
}
// 异步缓存小程序SessionKey
//go func() {
// if loginType == constant.LoginTypeWeChatMiniApp {
// // 缓存SessionKey
// if client.Redis.Set(context.Background(), constant.WeChatSessionKey+user.Id, sessionKey, 3*24*time.Hour).Err() != nil {
// log.Errorf("缓存SessionKey失败用户Id%s", user.Id)
// }
// }
//}()
// 返回用户Id
userId = strconv.Itoa(int(user.ID))
return
}