作者 yangfu

注入token

... ... @@ -4,7 +4,6 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/astaxie/beego"
"strconv"
"strings"
... ... @@ -108,12 +107,11 @@ func FilterComm(ctx *context.Context) {
// return
//}
//TODO:注入账号,后期移除掉
if beego.BConfig.RunMode != "prod" || ctx.Input.Header("x-mmm-accesstoken") == "" {
if ctx.Input.Header("x-mmm-accesstoken") == "" {
ctx.Request.Header.Set("x-mmm-accesstoken", "6839602f1d8211eabd85000c29ad8d6d")
if ctx.Input.Header("x-mmm-accesstoken") == "" {
ctx.Request.Header.Add("x-mmm-accesstoken", "6839602f1d8211eabd85000c29ad8d6d")
}
//return
} else {
//1.检查签名
if !CheckSign(ctx) {
... ...
... ... @@ -18,6 +18,7 @@ type Company struct {
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
UserCenterId int64 `orm:"column(user_center_id)" description:"统一用户中心企业id"`
}
func (t *Company) TableName() string {
... ...
... ... @@ -32,6 +32,7 @@ type RequestHeader struct {
/*Login */
type LoginRequest struct {
Uid int64 `json:"uid" valid:"Required;"`
Token string `json:"token" valid:"Required;"`
}
type LoginResponse struct {
AuthCode string `json:"authCode"`
... ...
... ... @@ -10,6 +10,7 @@ const (
MethodGetUser = "/users/"
MethodPutUser = "/users/"
MethodUserExists = "/users/isExist"
MethodServerLogin = "/auth/serverLogin"
)
/*UCenterLogin */
... ... @@ -19,6 +20,11 @@ type UCenterLoginRequest struct {
Code string `json:"code"`
GrantType string `json:"grantType" valid:"Required"`
//ClientId string `json:"clientId" valid:"Required"`
Type int `json:"typoe"` //1为密码登录,管理后台登录,2为token登录,app登录
Uid int64 `json:"uid"` // type2
//CompanyId int64 `json:"companyId"` // type2
}
type UCenterLoginResponse struct {
Uid int64 `json:"uid"` //统一用户中心用户编号,作为登录凭证
... ... @@ -39,9 +45,9 @@ type UCenterGetUserResponse struct {
Phone string `json:"phone"`
NickName string `json:"nickname"`
Avatar string `json:"avatar"`
Token string `json:"token"`
Accid string `json:"accid"`
CustomerAccount string `json:"customerAccount"`
ImToken string `json:"imtoken"`
Accid int64 `json:"accid"`
CustomerAccount int64 `json:"customerAccount"`
}
/*修改用户信息 UCenterPutUser */
... ... @@ -53,3 +59,14 @@ type UCenterPutUserRequest struct {
}
type UCenterPutUserResponse struct {
}
/*服务端登录*/
type UCenterServerLoginRequest struct {
Token string `json:"token"`
Uid int64 `json:"uid"`
CompanyId int64 `json:"company_id"`
Type int `json:"type"` //1为密码登录,管理后台登录,2为token登录,app登录
}
//type UCenterServerLoginResponse struct {
//}
... ...
... ... @@ -9,7 +9,6 @@ import (
"net/http"
"opp/internal/utils"
"opp/services/agg"
"strconv"
"time"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
... ... @@ -37,9 +36,14 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
var (
user *models.User
userAuth *models.UserAuth
getUserRequest *protocol.UCenterGetUserRequest = &protocol.UCenterGetUserRequest{}
getUserRequest *protocol.UCenterServerLoginRequest = &protocol.UCenterServerLoginRequest{
Uid: request.Uid,
Token: request.Token,
Type: 2,
}
getUserResponse *protocol.UCenterGetUserResponse
message *protocol.Message
company *models.Company
)
user, err = models.GetUserByUcenterId(request.Uid)
if err != nil {
... ... @@ -47,10 +51,15 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
err = protocol.NewErrWithMessage(2002, err) //账号不存在
return
}
if userAuth, err = models.GetUserAuthByUserId(user.Id, protocol.DeviceType); err == nil {
if company, err = models.GetCompanyById(userAuth.CurrentCompanyId); err == nil {
getUserRequest.CompanyId = company.UserCenterId
}
}
//TODO:验证模块权限
//从用户中心获取用户信息
if _, err = agg.RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodGetUser, request.Uid), http.MethodGet, getUserRequest, &message); err != nil {
if _, err = agg.RequestUserCenter(protocol.MethodServerLogin, http.MethodPost, getUserRequest, &message); err != nil {
log.Error(err)
return
}
... ... @@ -70,9 +79,12 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
case 2002:
err = protocol.NewErrWithMessage(2002, err) //账号不存在
return
case 10001:
err = protocol.NewErrWithMessage(2002, err) //账号不存在
return
default:
err = fmt.Errorf("error_no:%v msg:%v", message.Errno, message.Errmsg)
break
return
}
Success:
... ... @@ -93,11 +105,11 @@ Success:
userAuth.AuthCode = uid.NewV1().StringNoDash()
/*更新用户信息*/
user.CsAccount, _ = strconv.ParseInt(getUserResponse.CustomerAccount, 10, 64)
user.ImToken = getUserResponse.Token
user.CsAccount = getUserResponse.CustomerAccount
user.ImToken = getUserResponse.ImToken
user.Icon = getUserResponse.Avatar
user.NickName = getUserResponse.NickName
user.Accid, _ = strconv.ParseInt(getUserResponse.Accid, 10, 64)
user.Accid = getUserResponse.Accid
user.UserCenterId = getUserResponse.Id
if err = repository.User.UpdateUserInfo(user); err != nil {
log.Error(err)
... ...