作者 唐旭辉

bug 修复

... ... @@ -69,6 +69,8 @@ var AuthToken = func(ctx *context.Context) {
redisdata.RefreshLoginTokenExpires(mtoken.UID, mtoken.CompanyID)
ctx.Input.SetData(protocol.HeaderCompanyid, mtoken.CompanyID)
ctx.Input.SetData(protocol.HeaderUserid, mtoken.UID)
ctx.Input.SetData(protocol.HeaderUCompanyid, mtoken.UserCompanyId)
log.Info("c=%d,u=%d,cu=%d", mtoken.CompanyID, mtoken.UID, mtoken.UserCompanyId)
return
}
if ok := serveauth.IsJwtErrorExpired(err); ok {
... ...
... ... @@ -91,6 +91,7 @@ func UpdateCompanyById(m *Company, col []string, om ...orm.Ormer) (err error) {
}
var num int64
m.UpdateAt = time.Now()
col = append(col, "UpdateAt")
if num, err = o.Update(m, col...); err == nil {
fmt.Println("Number of records updated in database:", num)
}
... ...
package models
import (
"errors"
"fmt"
"oppmg/common/log"
"time"
... ... @@ -94,19 +93,16 @@ func UpdateUserCompanyById(m *UserCompany, col []string, om ...orm.Ormer) (err e
func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) {
o := orm.NewOrm()
var data []*UserCompany
_, err := o.QueryTable(&UserCompany{}).
var data UserCompany
err := o.QueryTable(&UserCompany{}).
Filter("user_id", userid).
Filter("company_id", companyId).
Filter("delete_at", 0).
All(&data)
One(&data)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, errors.New("UserCompany not found")
}
return data[0], nil
return &data, nil
}
func ExistUserCompany(userid int64, companyId int64) bool {
... ...
... ... @@ -9,8 +9,9 @@ const (
//用来存储从token中解析出来的内容对应的键名
const (
HeaderCompanyid string = "header_companyid"
HeaderUserid string = "header_userid"
HeaderCompanyid string = "header_companyid"
HeaderUserid string = "header_userid"
HeaderUCompanyid string = "header_ucompanyid"
)
//BaseHeader 请求的header数据
... ...
... ... @@ -436,6 +436,7 @@ func TemplateOperateCategory(uid, companyId int64, request *protocol.TemplateOpe
if chanceType.CompanyId != int(companyId) {
err = protocol.NewErrWithMessage("10027")
log.Error("template_id:%v companyId:%v want:%v not equal.", request.Id, companyId, chanceType.CompanyId)
return
}
if err = utils.UpdateTableByMap(chanceType, map[string]interface{}{"Name": request.Name, "Icon": request.Icon, "SortNum": chanceType.SortNum, "UpdateAt": time.Now()}); err != nil {
log.Error(err.Error())
... ...
... ... @@ -134,7 +134,7 @@ func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error)
log.Debug("无效公司")
return logintoken, protocol.NewErrWithMessage("10027")
}
logintoken, err = GenerateAuthToken(userid, companydata.Id)
logintoken, err = GenerateAuthToken(userid, companydata.Id, usercompany.Id)
if err != nil {
log.Error("GenerateAuthToken err:%s", err)
return logintoken, protocol.NewErrWithMessage("1")
... ... @@ -142,38 +142,38 @@ func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error)
return logintoken, nil
}
func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) {
var (
logintoken protocol.LoginAuthToken
mtoken *MyToken
err error
storetoken redisdata.RedisLoginToken
)
mtoken, err = ValidJWTToken(refreshtoken)
if err != nil {
log.Debug("token失效 err:%s", err)
return logintoken, protocol.NewErrWithMessage("10024")
}
storetoken, err = redisdata.GetLoginToken(mtoken.UID, mtoken.CompanyID)
if err != nil {
log.Error("redis err:%s", err)
return logintoken, protocol.NewErrWithMessage("10024")
}
if storetoken.RefreshToken != refreshtoken {
return logintoken, protocol.NewErrWithMessage("10024")
}
logintoken, _ = GenerateAuthToken(mtoken.UID, mtoken.CompanyID)
return logintoken, nil
}
// func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) {
// var (
// logintoken protocol.LoginAuthToken
// mtoken *MyToken
// err error
// storetoken redisdata.RedisLoginToken
// )
// mtoken, err = ValidJWTToken(refreshtoken)
// if err != nil {
// log.Debug("token失效 err:%s", err)
// return logintoken, protocol.NewErrWithMessage("10024")
// }
// storetoken, err = redisdata.GetLoginToken(mtoken.UID, mtoken.CompanyID)
// if err != nil {
// log.Error("redis err:%s", err)
// return logintoken, protocol.NewErrWithMessage("10024")
// }
// if storetoken.RefreshToken != refreshtoken {
// return logintoken, protocol.NewErrWithMessage("10024")
// }
// logintoken, _ = GenerateAuthToken(mtoken.UID, mtoken.CompanyID)
// return logintoken, nil
// }
func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, error) {
var (
err error
logintoken protocol.LoginAuthToken
companys []companybase
companyid int64
userdata *models.User
err error
logintoken protocol.LoginAuthToken
usercompanyid int64
companys []companybase
companyid int64
userdata *models.User
)
var uclientReturn *ucenter.ResponseLogin
uclientReturn, err = ucenter.RequestUCenterLogin(account, password)
... ... @@ -204,11 +204,28 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
//获取上一次登录的公司
uAuth, err := models.GetUserAuthByUser(userdata.Id)
if err == nil {
companyid = uAuth.CurrentCompanyId
has := false
for _, v := range companys {
if v.Id == uAuth.CurrentCompanyId {
companyid = uAuth.CurrentCompanyId
has = true
break
}
}
if !has {
companyid = companys[0].Id
}
} else {
companyid = companys[0].Id
}
ucompany, err := models.GetUserCompanyBy(userdata.Id, companyid)
if err != nil {
log.Error("获取user_company失败;%s", err)
return logintoken, protocol.NewErrWithMessage("1")
}
usercompanyid = ucompany.Id
logintoken, _ = GenerateAuthToken(userdata.Id, companyid, usercompanyid)
//更新用户数据
userdata.Accid = uclientReturn.Data.Accid
userdata.Icon = uclientReturn.Data.Avatar
... ... @@ -220,7 +237,6 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
if err != nil {
log.Error("更新用户数据失败:%s", err)
}
logintoken, _ = GenerateAuthToken(userdata.Id, companyid)
return logintoken, err
}
... ... @@ -329,11 +345,12 @@ func GetUserHasMenu(userid, companyid int64) ([]protocol.PermissionItem, error)
func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, error) {
var (
err error
logintoken protocol.LoginAuthToken
companys []companybase
companyid int64
userdata *models.User
err error
logintoken protocol.LoginAuthToken
companys []companybase
companyid int64
usercompanyid int64
userdata *models.User
)
var uclientReturn *ucenter.ResponseLoginSms
uclientReturn, err = ucenter.RequestUCenterLoginSms(phone, code)
... ... @@ -364,11 +381,27 @@ func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, err
//获取上一次登录的公司
uAuth, err := models.GetUserAuthByUser(userdata.Id)
if err == nil {
companyid = uAuth.CurrentCompanyId
has := false
for _, v := range companys {
if v.Id == uAuth.CurrentCompanyId {
companyid = uAuth.CurrentCompanyId
has = true
break
}
}
if !has {
companyid = companys[0].Id
}
} else {
companyid = companys[0].Id
}
ucompany, err := models.GetUserCompanyBy(userdata.Id, companyid)
if err != nil {
log.Error("获取user_company失败;%s", err)
return logintoken, protocol.NewErrWithMessage("1")
}
usercompanyid = ucompany.Id
logintoken, _ = GenerateAuthToken(userdata.Id, companyid, usercompanyid)
//更新用户数据
userdata.Accid = uclientReturn.Data.CsAccountID
userdata.Icon = uclientReturn.Data.Image.Path
... ... @@ -379,7 +412,6 @@ func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, err
if err != nil {
log.Error("更新用户数据失败:%s", err)
}
logintoken, _ = GenerateAuthToken(userdata.Id, companyid)
return logintoken, err
}
... ...
... ... @@ -15,12 +15,13 @@ var (
//MyToken ...
type MyToken struct {
jwt.StandardClaims
UID int64 `json:"uid"`
CompanyID int64 `json:"company_id"`
UID int64 `json:"uid"`
CompanyID int64 `json:"company_id"`
UserCompanyId int64 `json:"user_company_id"`
}
//CreateJWTToken ...
func CreateJWTToken(uid int64, companyid int64, expires int64) (string, error) {
func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, expires int64) (string, error) {
nowTime := time.Now().Unix()
claims := MyToken{
StandardClaims: jwt.StandardClaims{
... ... @@ -29,8 +30,9 @@ func CreateJWTToken(uid int64, companyid int64, expires int64) (string, error) {
ExpiresAt: expires, //过期时间
Issuer: "mmm_oppmg",
},
UID: uid,
CompanyID: companyid,
UID: uid,
CompanyID: companyid,
UserCompanyId: userCompanyId,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
... ... @@ -64,27 +66,19 @@ func IsJwtErrorExpired(err error) bool {
return false
}
func GenerateAuthToken(uid int64, companyid int64) (protocol.LoginAuthToken, error) {
func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64) (protocol.LoginAuthToken, error) {
var (
authToken protocol.LoginAuthToken
accesstoken string //主token,请求用
expiresIn int64 = 60 * 60 * 6 //主token过期时间,6小时
// refreshtoken string //副token,刷新主token用
// refreshExpires int64 = 60 * 60 * 2 //副token 过期时间 ,60分钟
err error
nowtime = time.Now()
err error
nowtime = time.Now()
)
accesstoken, err = CreateJWTToken(uid, companyid, nowtime.Unix()+expiresIn+2)
accesstoken, err = CreateJWTToken(uid, companyid, usercompanyid, nowtime.Unix()+expiresIn+1)
if err != nil {
return authToken, err
}
// refreshtoken, err = CreateJWTToken(uid, companyid, nowtime.Unix()+refreshExpires+2)
// if err != nil {
// return authToken, err
// }
authToken.AccessToken = accesstoken
authToken.ExpiresIn = nowtime.Unix() + expiresIn
// authToken.RefreshToken = refreshtoken
// authToken.RefreshExpires = nowtime.Unix() + refreshExpires
return authToken, err
}
... ...
... ... @@ -197,7 +197,22 @@ func addNewUser(name string, phone string, ucenterId int64, avatar string, accid
return usrData.Id, nil
}
if err == nil {
// 用户存在,
usr := &models.User{
Id: usrData.Id,
Phone: phone,
NickName: name,
Icon: avatar,
Accid: accid,
CsAccount: customerAccout,
UserCenterId: ucenterId,
}
// 用户存在,更新用户
err = models.UpdateUserById(usr,
[]string{"Phone", "NickName", "Icon", "Accid", "CsAccount", "UserCenterId"},
o)
if err != nil {
log.Error("更新用户失败;%s", err)
}
return usrData.Id, nil
}
return 0, err
... ...