Lee-WineList/oauth2/handle/user.go

69 lines
1.7 KiB
Go
Raw Normal View History

2023-04-27 15:56:12 +08:00
package handle
import (
"Lee-WineList/common/constant"
"Lee-WineList/model/entity"
"Lee-WineList/repository"
"Lee-WineList/utils"
"errors"
"git.echol.cn/loser/logger/log"
"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 entity.User
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 = repository.User().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(user.Id)
return
}