...
|
...
|
@@ -9,7 +9,6 @@ import ( |
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
...
|
...
|
@@ -41,68 +40,112 @@ func validatePassword(from, to string) bool { |
|
|
//LoginAuth 登录认证
|
|
|
func LoginAuthByPassword(account, password string) error {
|
|
|
var (
|
|
|
user *models.User
|
|
|
uAuth *models.UserAuth
|
|
|
err error
|
|
|
user *models.User
|
|
|
uAuth *models.UserAuth
|
|
|
companys []models.Company
|
|
|
mcompany models.Company
|
|
|
loginToken protocol.LoginAuthToken
|
|
|
err error
|
|
|
)
|
|
|
user, err = models.GetUserByPhone(account)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return protocol.NewErrWithMessage("10021", err)
|
|
|
}
|
|
|
if ok := validatePassword(password, user.Passwd); !ok {
|
|
|
return protocol.NewErrWithMessage("1", err)
|
|
|
return protocol.NewErrWithMessage("10021", err)
|
|
|
}
|
|
|
|
|
|
uAuth, err = models.ReadUserAuthByDevice(user.Id, models.DEVICE_TYPE_WEB)
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
e := fmt.Errorf("ReadUserAuthByDevice(%d,%d) err:%s", user.Id, models.DEVICE_TYPE_WEB, err)
|
|
|
if ok := user.IsEnable(); !ok {
|
|
|
return protocol.NewErrWithMessage("10022")
|
|
|
}
|
|
|
companys, err = models.GetCompanyByUser(user.Id)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GetCompanyByUser(%d) err:%s", user.Id, err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if len(companys) <= 0 {
|
|
|
log.Error("can not found company")
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
mcompany = companys[0]
|
|
|
loginToken, err = GenerateAuthToken(user.Id, mcompany.Id)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("GenerateAuthToken err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
// uAuth, err = models.ReadUserAuthByDevice(user.Id, models.DEVICE_TYPE_WEB)
|
|
|
// if err != nil && err != orm.ErrNoRows {
|
|
|
// e := fmt.Errorf("ReadUserAuthByDevice(%d,%d) err:%s", user.Id, models.DEVICE_TYPE_WEB, err)
|
|
|
// log.Error(e.Error())
|
|
|
// return protocol.NewErrWithMessage("1", e)
|
|
|
// }
|
|
|
// var (
|
|
|
// authcode string
|
|
|
// authcodeExp time.Time
|
|
|
// )
|
|
|
// authcode = utils.GenerateIDByUUID()
|
|
|
// authcodeExp = time.Now().Add(time.Duration(models.AUTHCODE_TIME) * time.Second)
|
|
|
// if err == orm.ErrNoRows {
|
|
|
// uAuth := &models.UserAuth{
|
|
|
// UserId: user.Id,
|
|
|
// AuthCode: authcode,
|
|
|
// AuthCodeExp: authcodeExp,
|
|
|
// CreateAt: time.Now(),
|
|
|
// }
|
|
|
// _, err = models.AddUserAuth(uAuth)
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("AddUserAuth err:%s", err)
|
|
|
// log.Error(e.Error())
|
|
|
// return protocol.NewErrWithMessage("1", e)
|
|
|
// }
|
|
|
// }
|
|
|
// if err == nil {
|
|
|
// uAuth.AuthCode = authcode
|
|
|
// uAuth.AuthCodeExp = authcodeExp
|
|
|
// uAuth.UpdateAt = time.Now()
|
|
|
// err = models.UpdateUserAuthById(uAuth)
|
|
|
// if err != nil {
|
|
|
// e := fmt.Errorf("UpdateUserAuthById err:%s", err)
|
|
|
// log.Error(e.Error())
|
|
|
// return protocol.NewErrWithMessage("1", e)
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//ResetLoginToken token存数据库
|
|
|
func ResetLoginToken(loginToken protocol.LoginAuthToken, userid int64, companyid int64) error {
|
|
|
var (
|
|
|
authcode string
|
|
|
authcodeExp time.Time
|
|
|
uAuth *models.UserAuth
|
|
|
err error
|
|
|
nowTime = time.Now()
|
|
|
)
|
|
|
authcode = utils.GenerateIDByUUID()
|
|
|
authcodeExp = time.Now().Add(time.Duration(models.AUTHCODE_TIME) * time.Second)
|
|
|
uAuth, err = models.GetUserAuthByUser(userid)
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
return err
|
|
|
}
|
|
|
if err == orm.ErrNoRows {
|
|
|
uAuth := &models.UserAuth{
|
|
|
UserId: user.Id,
|
|
|
AuthCode: authcode,
|
|
|
AuthCodeExp: authcodeExp,
|
|
|
CreateAt: time.Now(),
|
|
|
//添加用户授权
|
|
|
uAuth = &models.UserAuth{
|
|
|
UserId: userid,
|
|
|
CurrentCompanyId: companyid,
|
|
|
AccessToken: loginToken.AccessToken,
|
|
|
AccessTokenExp: time.Unix(loginToken.ExpiresIn, 0), //,
|
|
|
RefreshToken: loginToken.RefreshToken,
|
|
|
RefreshTokenExp: time.Unix(loginToken.RefreshExpires, 0),
|
|
|
CreateAt: nowTime,
|
|
|
UpdateAt: nowTime,
|
|
|
DeviceType: models.DEVICE_TYPE_WEB,
|
|
|
}
|
|
|
_, err = models.AddUserAuth(uAuth)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("AddUserAuth err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
return err
|
|
|
}
|
|
|
if err == nil {
|
|
|
uAuth.AuthCode = authcode
|
|
|
uAuth.AuthCodeExp = authcodeExp
|
|
|
uAuth.UpdateAt = time.Now()
|
|
|
err = models.UpdateUserAuthById(uAuth)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("UpdateUserAuthById err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return protocol.NewErrWithMessage("1", e)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
//更新用户授权
|
|
|
|
|
|
//RefreshAccessToken 刷新token
|
|
|
func RefreshAccessToken(account string, token string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// func buildNewUserAuth(uid int64,) *models.UserAuth {
|
|
|
// m:=&models.UserAuth{
|
|
|
// User
|
|
|
// }
|
|
|
// return nil
|
|
|
// } |
|
|
//TODO token 存redis |
...
|
...
|
|