You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
6.0 KiB
166 lines
6.0 KiB
package login
|
|
|
|
import (
|
|
"epur-pay/cache"
|
|
"epur-pay/model"
|
|
"epur-pay/pkg/dapi"
|
|
"epur-pay/pkg/utils"
|
|
"fmt"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"regexp"
|
|
)
|
|
|
|
type SsoLoginParams struct {
|
|
Mobile string `json:"mobile"` // 登陆手机号
|
|
Password string `json:"passWord"` // 密码
|
|
}
|
|
|
|
type SsoLoginResponse struct {
|
|
*dapi.ResponseCommon
|
|
Data struct {
|
|
Token string `json:"token"` //返回token
|
|
} `json:"data"` //数据列表
|
|
}
|
|
|
|
// SsoLogin 用户登录接口
|
|
// @Summary 用户登录
|
|
// @Description 该接口用于用户通过手机号和密码进行登录。如果登录失败,系统会记录错误信息并限制登录次数。
|
|
// @Tags login
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body SsoLoginParams true "用户登录信息"
|
|
// @Success 200 {object} SsoLoginResponse "登录成功返回Token"
|
|
// @Router /api/v1/login/login [post]
|
|
func SsoLogin(a *dapi.ApiBase, data *SsoLoginParams) error {
|
|
Response := SsoLoginResponse{}
|
|
|
|
mobileRegex := `^1[3-9]\d{9}$`
|
|
matched, err := regexp.MatchString(mobileRegex, data.Mobile)
|
|
if err != nil || !matched {
|
|
return a.ReturnPublicErrorResponse(a.Translate("invalid_mobile"))
|
|
}
|
|
|
|
user := model.User{}
|
|
utils.Error(a.Ts.Table(user.TableName()).Where("mobile", data.Mobile).Scan(&user).Error)
|
|
|
|
//if len(user.Role) <= 0 {
|
|
// return a.ReturnPublicErrorResponse(a.Translate("user_role_invalid"))
|
|
//}
|
|
|
|
if a.Log != nil {
|
|
a.Log.Uid = user.Uid
|
|
a.Log.Name = user.Mobile
|
|
a.Log.Event = "登陆"
|
|
}
|
|
|
|
lockNums := cache.Global.Caches.Config.GetInt64("lockNums")
|
|
lockMins := cache.Global.Caches.Config.GetInt64("lockMins")
|
|
|
|
if lockNums > 0 && lockMins > 0 {
|
|
currTime := utils.Time2StampSecond()
|
|
if user.Detail.LoginErrorCount >= lockNums &&
|
|
currTime-user.Detail.LoginErrorTime < lockMins*60 { //输入5次密码错误 锁5分钟
|
|
|
|
return a.ReturnPublicErrorResponse(fmt.Sprintf(a.Translate("pwd_error_lock"),
|
|
lockNums, user.Detail.LoginErrorTime+lockMins*60-currTime)) //密码输入错误超过%d次,%d秒后可以继续操作
|
|
}
|
|
}
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.Detail.PassWord), []byte(data.Password)); err != nil {
|
|
user.Detail.LoginErrorTime = utils.Time2StampSecond()
|
|
user.Detail.LoginErrorCount += 1
|
|
utils.DbErrSkipRecordNotFound(a.Ts.Table(user.TableName()).
|
|
Where("uid", user.Uid).
|
|
Updates(map[string]interface{}{"detail": user.Detail}).Error)
|
|
return a.ReturnPublicErrorResponse(a.Translate("pwd_error"))
|
|
}
|
|
|
|
if user.Status != "0" {
|
|
return a.ReturnPublicErrorResponse(a.Translate("user_blacklist"))
|
|
}
|
|
|
|
user.Detail.Token = dapi.EncryptToken(user.Uid)
|
|
user.Detail.LoginIp = a.ClientIp()
|
|
user.Detail.LoginIpAddress = cache.GetIpAddress(user.Detail.LoginIp)
|
|
user.Detail.LoginTime = utils.Time2StampSecond()
|
|
user.Detail.LoginErrorCount = 0
|
|
|
|
utils.DbErrSkipRecordNotFound(a.Ts.Table(user.TableName()).
|
|
Where("uid", user.Uid).
|
|
Updates(map[string]interface{}{"detail": user.Detail}).Error)
|
|
|
|
a.AfterCallback = func() {
|
|
// 这里需要刷新权限标识
|
|
cache.Global.Caches.User.RefreshSelectRow(user.Uid)
|
|
}
|
|
|
|
Response.Data.Token = user.Detail.Token
|
|
Response.ResponseCommon = a.NewSuccessResponseCommon()
|
|
return a.ReturnSuccessCustomResponse(Response)
|
|
}
|
|
|
|
type UserInfoResponse struct {
|
|
*dapi.ResponseCommon
|
|
Data struct {
|
|
Uid int64 `json:"uid"` // 会员ID
|
|
Type string `json:"type"` // 用户类型 0-普通用户 1-体验用户
|
|
Account string `json:"account"` // 登录账号
|
|
Email string `json:"email"` // 绑定邮箱
|
|
Mobile string `json:"mobile"` // 手机号 - 格式:+86 1234
|
|
Invite string `json:"invite"` // 邀请码
|
|
Auth string `json:"auth"` // 实名认证 0-未认证 1-审核中 2-已认证
|
|
AdvancedStatus string `json:"advancedStatus"` // 高级认证状态 0-未认证 1-审核中 2-已认证 3-已拒绝
|
|
Nickname string `json:"nickName"` // 用户名称
|
|
Avatar string `json:"avatar"` // 用户头像
|
|
ResourceId int64 `json:"resourceId"` // 头像资源文件
|
|
Gender string `json:"gender"` // 0-男 1-女 2-未知
|
|
Token string `json:"token"` // 登陆token
|
|
Integral string `json:"integral"` // 信用积分
|
|
IsGoogle string `json:"isGoogle"` // 是否绑定google验证 0-是 1-否
|
|
GoogleCodeUrl string `json:"googleCodeUrl"` // google地址
|
|
IsPayPwd string `json:"isPayPwd"` // 是否设置支付密码 0-是 1-否
|
|
Role model.ArryString `json:"role"` // 角色
|
|
LoginIp string `json:"loginIp"` // 登陆ip
|
|
LoginIpAddress string `json:"loginIpAddress,omitempty"` // 登陆ip
|
|
Date string `json:"date"`
|
|
} `json:"data"` //数据列表
|
|
}
|
|
|
|
/*
|
|
@Summary 用户详情
|
|
@Router /user/info [post]
|
|
*/
|
|
|
|
func UserInfo(a *dapi.ApiBase) error {
|
|
|
|
Response := UserInfoResponse{}
|
|
|
|
Response.Data.Uid = a.User.Uid
|
|
Response.Data.Account = a.User.Account
|
|
Response.Data.Email = a.User.Email
|
|
Response.Data.Mobile = a.User.Mobile
|
|
Response.Data.Invite = a.User.Invite
|
|
Response.Data.Nickname = a.User.Detail.Nickname
|
|
|
|
Response.Data.Gender = a.User.Detail.Gender
|
|
Response.Data.Token = a.User.Detail.Token
|
|
Response.Data.LoginIp = a.User.Detail.LoginIp
|
|
Response.Data.LoginIpAddress = a.User.Detail.LoginIpAddress
|
|
|
|
if len(Response.Data.LoginIpAddress) <= 0 {
|
|
Response.Data.LoginIpAddress = cache.GetIpAddress(Response.Data.LoginIp)
|
|
}
|
|
|
|
if len(a.User.Detail.PayPassWord) > 0 {
|
|
Response.Data.IsPayPwd = "0"
|
|
} else {
|
|
Response.Data.IsPayPwd = "1"
|
|
}
|
|
|
|
// 角色
|
|
Response.Data.Role = a.User.Role
|
|
|
|
Response.ResponseCommon = a.NewSuccessResponseCommon()
|
|
return a.ReturnSuccessCustomResponse(Response)
|
|
}
|