...
|
...
|
@@ -12,6 +12,7 @@ import ( |
|
|
"oppmg/protocol"
|
|
|
"oppmg/services/ucenter"
|
|
|
"oppmg/storage/redisdata"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
...
|
...
|
@@ -41,6 +42,7 @@ func validatePassword(from, to string) bool { |
|
|
}
|
|
|
|
|
|
//LoginAuth 登录认证
|
|
|
//TODO 登录校验逻辑修改
|
|
|
func LoginAuthByPassword(account, password string) (protocol.LoginAuthToken, error) {
|
|
|
var (
|
|
|
user *models.User
|
...
|
...
|
@@ -139,7 +141,7 @@ func ResetLoginToken(loginToken protocol.LoginAuthToken) error { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// token 存redis
|
|
|
//ResetLoginTokenRedis token存redis
|
|
|
func ResetLoginTokenRedis(loginToken protocol.LoginAuthToken) error {
|
|
|
var (
|
|
|
mtoken *MyToken
|
...
|
...
|
@@ -150,7 +152,7 @@ func ResetLoginTokenRedis(loginToken protocol.LoginAuthToken) error { |
|
|
log.Error("jwt err:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
err = redisdata.SetLoginToken(loginToken, mtoken.UID)
|
|
|
err = redisdata.SetLoginToken(loginToken, mtoken.UID, mtoken.CompanyID)
|
|
|
if err != nil {
|
|
|
log.Error(" redisdata.SetLoginToken err:%s", err)
|
|
|
}
|
...
|
...
|
@@ -168,14 +170,14 @@ func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error) |
|
|
|
|
|
usercompany, err = models.GetUserCompanyBy(userid, companyid)
|
|
|
if err != nil {
|
|
|
log.Error("GetUserCompanyBy(userid, companyid) err:%s", err)
|
|
|
log.Error("GetUserCompanyBy(userid, companyid)[%d,%d] err:%s", userid, companyid, err)
|
|
|
return logintoken, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if ok := usercompany.IsEnable(); !ok {
|
|
|
log.Debug("公司禁用此用户")
|
|
|
return logintoken, protocol.NewErrWithMessage("10027")
|
|
|
}
|
|
|
if ok := usercompany.IsDelete(); !ok {
|
|
|
if ok := usercompany.IsDelete(); ok {
|
|
|
log.Debug("公司删除此用户")
|
|
|
return logintoken, protocol.NewErrWithMessage("10027")
|
|
|
}
|
...
|
...
|
@@ -201,7 +203,7 @@ func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) { |
|
|
logintoken protocol.LoginAuthToken
|
|
|
mtoken *MyToken
|
|
|
err error
|
|
|
storetoken protocol.LoginAuthToken
|
|
|
storetoken redisdata.RedisLoginToken
|
|
|
)
|
|
|
mtoken, err = ValidJWTToken(refreshtoken)
|
|
|
if err != nil {
|
...
|
...
|
@@ -217,7 +219,6 @@ func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) { |
|
|
return logintoken, protocol.NewErrWithMessage("10024")
|
|
|
}
|
|
|
logintoken, _ = GenerateAuthToken(mtoken.UID, mtoken.CompanyID)
|
|
|
ResetLoginTokenRedis(logintoken)
|
|
|
return logintoken, nil
|
|
|
}
|
|
|
|
...
|
...
|
@@ -226,14 +227,33 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro |
|
|
err error
|
|
|
logintoken protocol.LoginAuthToken
|
|
|
uclientReturn ucenter.ResponseLogin
|
|
|
companyids []int64
|
|
|
companyid int64
|
|
|
userdata *models.User
|
|
|
)
|
|
|
<<<<<<< HEAD
|
|
|
|
|
|
_, err = models.GetUserByPhone(account)
|
|
|
=======
|
|
|
userdata, err = models.GetUserByPhone(account)
|
|
|
>>>>>>> 626a6b6d8e73bd83579be13a797ab6c1fce8d3d8
|
|
|
if err != nil {
|
|
|
log.Debug("GetUserByPhone(%s) err:%s", account, err)
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
|
|
|
if ok := userdata.IsEnable(); !ok {
|
|
|
log.Debug("userdata.IsEnable()==false")
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
if companyids, err = getUserCompanyReal(userdata.Id); err != nil {
|
|
|
log.Error("getUserCompanyReal err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
if len(companyids) == 0 {
|
|
|
log.Debug("no company")
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
companyid = companyids[0]
|
|
|
param := ucenter.RequesLogin{
|
|
|
Phone: account,
|
|
|
Password: password,
|
...
|
...
|
@@ -253,6 +273,34 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro |
|
|
uclientReturn.Msg == ucenter.ResponseMsgOk) {
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
|
|
|
logintoken, _ = GenerateAuthToken(userdata.Id, companyid)
|
|
|
return logintoken, err
|
|
|
}
|
|
|
|
|
|
//getUserCompanyReal 获取用户有效的公司id
|
|
|
func getUserCompanyReal(userid int64) (companyids []int64, err error) {
|
|
|
const (
|
|
|
datasql0 string = `SELECT b.company_id FROM user_company AS b
|
|
|
WHERE b.delete_at=0 AND b.enable = 1 AND b.user_id=?`
|
|
|
datasql2 string = `SELECT b.id FROM company AS b
|
|
|
WHERE b.delete_at=0 AND b.enable = 1 AND b.id in (%s) `
|
|
|
)
|
|
|
var (
|
|
|
ids []string
|
|
|
)
|
|
|
err = utils.ExecuteQueryAll(&ids, datasql0, userid)
|
|
|
if err != nil {
|
|
|
log.Error("EXECUTE SQL err:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
if len(ids) == 0 {
|
|
|
return nil, nil
|
|
|
}
|
|
|
datasql3 := fmt.Sprintf(datasql2, strings.Join(ids, ","))
|
|
|
err = utils.ExecuteQueryAll(&companyids, datasql3)
|
|
|
if err != nil {
|
|
|
log.Error("EXECUTE SQL err:%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
return companyids, nil
|
|
|
} |
...
|
...
|
|