作者 唐旭辉

登录修改

... ... @@ -37,7 +37,10 @@ func (c *AuthController) RefreshToken() {
return
}
err = serveauth.ResetLoginTokenRedis(logintoken)
msg = protocol.NewReturnResponse(logintoken, err)
data := protocol.ResponseLogin{
Access: logintoken,
}
msg = protocol.NewReturnResponse(data, err)
return
}
... ... @@ -72,13 +75,51 @@ func (c *AuthController) Login() {
if err != nil {
log.Error("token 信息记录redis失败")
}
data := map[string]interface{}{
"access": logintoken,
data := protocol.ResponseLogin{
Access: logintoken,
}
msg = protocol.NewReturnResponse(data, nil)
return
}
// LoginSms 登录
// @router /login [post]
func (c *AuthController) LoginSms() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestLogin
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
// if len(param.Account) == 0 || len(param.Password) == 0 {
// msg = protocol.BadRequestParam("10021")
// return
// }
// logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
// if err != nil {
// msg = protocol.NewReturnResponse(nil, err)
// return
// }
// err = serveauth.ResetLoginToken(logintoken)
// if err != nil {
// log.Error("token 信息记录数据库失败")
// }
// err = serveauth.ResetLoginTokenRedis(logintoken)
// if err != nil {
// log.Error("token 信息记录redis失败")
// }
// data := map[string]interface{}{
// "access": logintoken,
// }
// msg = protocol.NewReturnResponse(data, nil)
return
}
//ChangeCompany 切换公司
//@Router /change_company [post]
func (c *AuthController) ChangeCompany() {
... ... @@ -103,11 +144,18 @@ func (c *AuthController) ChangeCompany() {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = serveauth.ResetLoginToken(logintoken)
if err != nil {
log.Error("token 信息记录数据库失败")
}
err = redisdata.SetLoginToken(logintoken, userid, param.CompanyId)
if err != nil {
log.Error("redisdata.SetLoginToken err:%s", err)
}
msg = protocol.NewReturnResponse(logintoken, err)
data := protocol.ResponseLogin{
Access: logintoken,
}
msg = protocol.NewReturnResponse(data, err)
return
}
... ...
... ... @@ -51,7 +51,7 @@ var AuthToken = func(ctx *context.Context) {
}
mtoken, err = serveauth.ValidJWTToken(accesstoken)
if err == nil {
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
storetoken, err = redisdata.GetLoginToken(mtoken.UID, mtoken.CompanyID)
if err != nil {
log.Error("redisdata.GetLoginToken err:%s", err)
msg = protocol.NewMessage("10024")
... ...
... ... @@ -28,7 +28,7 @@ type RequestLogin struct {
//ResponseLogin 登录响应
type ResponseLogin struct {
LoginAuthToken
Access LoginAuthToken `json:"access"`
}
type LoginAuthToken struct {
... ...
... ... @@ -158,7 +158,7 @@ func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) {
log.Debug("token失效 err:%s", err)
return logintoken, protocol.NewErrWithMessage("10024")
}
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
storetoken, err = redisdata.GetLoginToken(mtoken.UID, mtoken.CompanyID)
if err != nil {
log.Error("redis err:%s", err)
return logintoken, protocol.NewErrWithMessage("10024")
... ...
... ... @@ -155,9 +155,10 @@ func initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*mode
return nil, err
}
} else if err == orm.ErrNoRows {
ucenterUser, err := ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "")
ucenterUser, err := ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "http://这是默认图.jpeg")
if err != nil {
log.Error("获取统一用户中心数据失败 :%s", err)
return nil, err
}
//添加用户
newUser = &models.User{
... ...
... ... @@ -493,7 +493,7 @@ func UserDelete(userCompanyids []int64, companyid int64) error {
return protocol.NewErrWithMessage("1")
}
for _, v := range updateIds {
e := redisdata.DeleteLoginToken(v.UserId)
e := redisdata.DeleteLoginToken(v.UserId, companyid)
if e != nil {
log.Error(e.Error())
}
... ... @@ -676,7 +676,7 @@ func UserForbid(userCompanyids []int64, companyid int64) error {
}
log.Info("更新记录数:%d", num)
for _, v := range updateIds {
e := redisdata.DeleteLoginToken(v.UserId)
e := redisdata.DeleteLoginToken(v.UserId, companyid)
if e != nil {
log.Error(e.Error())
}
... ...
... ... @@ -43,6 +43,7 @@ func (client UCenterClient) buildHeader() http.Header {
h.Set("nonce", "")
h.Set("curTime", nowTime)
h.Set("checkSum", checksum)
h.Set("Accept", "application/json")
return h
}
... ... @@ -64,7 +65,7 @@ func (client UCenterClient) httpDo(path string, mathod string, posts []byte) ([]
}
req.Header = client.buildHeader()
resp, err := httpclient.Do(req)
log.Info("====>Send To URL:%s", reqURL)
log.Info("====>Send To UCenter:%s", string(posts))
// log.Info("====>Send Header:%v", req.Header)
if err != nil {
... ...
package redisdata
import "fmt"
const (
KEY_PREFIX string = "oppmg_" //统一前缀
KEY_USER_TOKEN string = "token" //登录token存储
... ... @@ -10,3 +12,8 @@ type RedisLoginToken struct {
RefreshToken string `json:"refresh_token"`
CurrentCompany int64 `json:"current_company"`
}
func GetKeyLoginToken(userid int64, companyid int64) string {
key := fmt.Sprintf("%s%s:%d_%d", KEY_PREFIX, KEY_USER_TOKEN, userid, companyid)
return key
}
... ...
... ... @@ -2,7 +2,6 @@ package redisdata
import (
"encoding/json"
"fmt"
"oppmg/common/log"
"oppmg/common/redis"
"oppmg/protocol"
... ... @@ -26,12 +25,12 @@ func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64)
}
value, _ = json.Marshal(data)
exp = param.RefreshExpires - nowTime
key = fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
key = GetKeyLoginToken(userid, companyid)
err = client.Set(key, string(value), time.Duration(exp)*time.Second).Err()
return err
}
func GetLoginToken(userid int64) (RedisLoginToken, error) {
func GetLoginToken(userid int64, companyid int64) (RedisLoginToken, error) {
client := redis.GetRedis()
var (
key string
... ... @@ -39,7 +38,7 @@ func GetLoginToken(userid int64) (RedisLoginToken, error) {
err error
result RedisLoginToken
)
key = fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
key = GetKeyLoginToken(userid, companyid)
value, err = client.Get(key).Result()
if err != nil {
return result, err
... ... @@ -53,9 +52,9 @@ func GetLoginToken(userid int64) (RedisLoginToken, error) {
return result, nil
}
func ExistLoginToken(userid int64) bool {
func ExistLoginToken(userid int64, companyid int64) bool {
client := redis.GetRedis()
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
key := GetKeyLoginToken(userid, companyid)
value := client.Exists(key).Val()
if value > 0 {
return true
... ... @@ -63,17 +62,17 @@ func ExistLoginToken(userid int64) bool {
return false
}
func DeleteLoginToken(userid int64) error {
func DeleteLoginToken(userid int64, companyid int64) error {
client := redis.GetRedis()
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
key := GetKeyLoginToken(userid, companyid)
err := client.Del(key).Err()
return err
}
// 刷新token 的有效期
func RefreshLoginTokenExpires(userid int64) error {
func RefreshLoginTokenExpires(userid int64, companyid int64) error {
client := redis.GetRedis()
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
key := GetKeyLoginToken(userid, companyid)
result, err := client.Exists(key).Result()
if err != nil {
return err
... ...