作者 yangfu

增加:登录修改

... ... @@ -69,7 +69,13 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
err = protocol.NewCustomMessage(1, "登录方式不支持!")
break
}
rsp.AuthCode, _ = utils.GenerateToken(partnerInfo.Id, request.Phone, protocol.AuthCodeExpire*time.Second)
userClaim := utils.UserTokenClaims{
UserId: partnerInfo.Id,
Phone: partnerInfo.Account,
AdminType: int(protocolx.AdminTypePartner),
CompanyId: 1, //默认公司
}
rsp.AuthCode, _ = utils.GenerateTokenWithClaim(userClaim, protocol.AuthCodeExpire*time.Second)
if _, err = InitOrUpdateUserIMInfo(partnerInfo.Id, partnerInfo.PartnerName, transactionContext); err != nil {
log.Error(err)
... ... @@ -133,11 +139,9 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
claim *utils.UserTokenClaims
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
//PartnerSubAccountRepository, _ = factory.CreatePartnerSubAccountRepository(transactionContext)
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
partnerInfo *domain.PartnerInfo
//partnerSubAccount *domain.PartnerSubAccount
user *domain.Users
userId int64
)
... ... @@ -159,27 +163,22 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
return
}
//验证用户有效
//var e error
//if partnerSubAccount, e = PartnerSubAccountRepository.FindOne(map[string]interface{}{"account": claim.Phone}); e == nil {
// partnerInfo, e = PartnerInfoService.FindOne(map[string]interface{}{"id": partnerSubAccount.PartnerId})
//} else {
// partnerInfo, e = PartnerInfoService.FindOne(map[string]interface{}{"account": claim.Phone})
//}
switch claim.AdminType {
case 1:
if user, err = UsersRepository.FindOne(map[string]interface{}{"phone": claim.Phone, "companyId": claim.CompanyId, "status": 1}); err != nil || user != nil {
case int(protocolx.AdminTypePartner):
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"account": claim.Phone, "companyId": claim.CompanyId, "status": 1}); err != nil || partnerInfo == nil {
log.Error(err)
err = protocol.NewErrWithMessage(4140, err)
return
}
userId = user.Id
userId = partnerInfo.Id
break
case 2:
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"account": claim.Id, "companyId": claim.CompanyId, "status": 1}); err != nil || partnerInfo == nil {
case int(protocolx.AdminTypeManager):
if user, err = UsersRepository.FindOne(map[string]interface{}{"phone": claim.Phone, "companyId": claim.CompanyId, "status": 1}); err != nil || user == nil {
log.Error(err)
err = protocol.NewErrWithMessage(4140, err)
return
}
userId = partnerInfo.Id
userId = user.Id
break
default:
err = protocol.NewErrWithMessage(4140, err)
... ... @@ -192,8 +191,14 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT
// err = protocol.NewErrWithMessage(4140, err)
// return
//}
rsp.AccessToken, _ = utils.GenerateTokenWithAdminType(userId, claim.Phone, claim.AdminType, protocol.TokenExpire*time.Second)
rsp.RefreshToken, _ = utils.GenerateTokenWithAdminType(userId, claim.Phone, claim.AdminType, protocol.RefreshTokenExipre*time.Second)
userClaim := utils.UserTokenClaims{
UserId: userId,
Phone: claim.Phone,
AdminType: claim.AdminType,
CompanyId: claim.CompanyId,
}
rsp.AccessToken, _ = utils.GenerateTokenWithClaim(userClaim, protocol.TokenExpire*time.Second)
rsp.RefreshToken, _ = utils.GenerateTokenWithClaim(userClaim, protocol.RefreshTokenExipre*time.Second)
rsp.ExpiresIn = protocol.TokenExpire
//newAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(claim.UserId),
... ... @@ -390,8 +395,8 @@ func LoginV2(header *protocol.RequestHeader, request *protocol.LoginRequestV2) (
//根据simnum + cid
userClaims := utils.UserTokenClaims{
UserId: userId,
CompanyId: claim.CompanyId,
AdminType: claim.AdminType,
CompanyId: int64(request.Cid),
AdminType: request.IdType,
Phone: claim.Phone,
}
rsp.AuthCode, _ = utils.GenerateTokenWithClaim(userClaims, protocol.AuthCodeExpire*time.Second)
... ...
... ... @@ -9,6 +9,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/auth"
"strconv"
"strings"
)
... ... @@ -20,8 +21,10 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest)
transactionContext, _ = factory.CreateTransactionContext(nil)
CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
ImInfoRepository, _ = factory.CreateImInfoRepository(transactionContext)
company *domain.Company
user *domain.Users
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
... ... @@ -30,11 +33,13 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest)
transactionContext.RollbackTransaction()
}()
rsp = &protocol.UserInfoResponse{}
funcPartnerInfo := func() {
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": 1}); err != nil {
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": header.CompanyId}); err != nil {
return
}
rsp.User = protocol.User{
... ... @@ -56,7 +61,43 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest)
} else {
rsp.User.Salesman = map[string]interface{}{}
}
if imInfo, e := ImInfoRepository.FindOne(map[string]interface{}{"user_id": partnerInfo.Id}); e == nil && imInfo != nil {
}
funcManagerInfo := func() {
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": header.CompanyId}); err != nil {
return
}
rsp.User = protocol.User{
Id: user.Id,
PartnerName: user.Name,
Phone: user.Phone,
CooperateCompany: protocol.Company{
Id: company.Id,
Name: company.Name,
Phone: company.Phone,
},
//JoinWay: partnerInfo.PartnerCategoryInfo(),
//District: map[string]interface{}{"id": partnerInfo.RegionInfo.RegionId, "name": partnerInfo.RegionInfo.RegionName},
//SerialNo: partnerInfo.Id,
//CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
}
}
switch header.AdminType {
case int(protocolx.AdminTypePartner):
funcPartnerInfo()
break
case int(protocolx.AdminTypeManager):
funcManagerInfo()
break
default:
funcPartnerInfo()
break
}
if imInfo, e := ImInfoRepository.FindOne(map[string]interface{}{"user_id": header.SimNum}); e == nil && imInfo != nil {
rsp.User.ImToken = imInfo.ImToken
rsp.User.AccountID, _ = strconv.ParseInt(imInfo.ImId, 10, 64)
rsp.User.CsAccountID = fmt.Sprintf("%v", imInfo.CustomerImId)
... ... @@ -221,3 +262,109 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
err = transactionContext.CommitTransaction()
return
}
func UserInfoV2(header *protocol.RequestHeader, request *protocol.UserInfoRequest) (rsp interface{}, err error) {
var (
partnerInfo *domain.PartnerInfo
transactionContext, _ = factory.CreateTransactionContext(nil)
CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
company *domain.Company
user *domain.Users
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
rsp = &protocol.UserInfoResponse{}
type xcompany struct {
Id int64 `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
//合作区域
District interface{} `json:"district"`
//合作编码
SerialNo int64 `json:"serialNo"`
//合作时间
CooperateTime int64 `json:"cooperationTime"`
Salesman interface{} `json:"salesman"`
}
type xuser struct {
Id int64 `json:"uid"`
//用户名称
PartnerName string `json:"uname"`
//手机号
Phone string `json:"phone"`
//合作公司
CooperateCompany xcompany `json:"company"`
}
rspMap := make(map[string]interface{})
funcPartnerInfo := func() {
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": header.CompanyId}); err != nil {
return
}
u := xuser{
Id: partnerInfo.Id,
PartnerName: partnerInfo.PartnerName,
Phone: partnerInfo.Account,
CooperateCompany: xcompany{
Id: company.Id,
Name: company.Name,
Phone: company.Phone,
SerialNo: partnerInfo.Id,
CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
//JoinWay: partnerInfo.PartnerCategoryInfo(),
District: map[string]interface{}{"id": partnerInfo.RegionInfo.RegionId, "name": partnerInfo.RegionInfo.RegionName},
},
}
if len(partnerInfo.Salesman) > 0 {
u.CooperateCompany.Salesman = map[string]interface{}{"uname": partnerInfo.Salesman[0].Name, "phone": partnerInfo.Salesman[0].Telephone}
} else {
u.CooperateCompany.Salesman = map[string]interface{}{}
}
rspMap["User"] = u
rsp = rspMap
}
funcManagerInfo := func() {
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": header.CompanyId}); err != nil {
return
}
rspMap["User"] = xuser{
Id: user.Id,
PartnerName: user.Name,
Phone: user.Phone,
CooperateCompany: xcompany{
Id: company.Id,
Name: company.Name,
Phone: company.Phone,
},
}
rsp = rspMap
}
switch header.AdminType {
case int(protocolx.AdminTypePartner):
funcPartnerInfo()
break
case int(protocolx.AdminTypeManager):
funcManagerInfo()
break
default:
funcPartnerInfo()
break
}
err = transactionContext.CommitTransaction()
return
}
... ...
... ... @@ -25,9 +25,11 @@ func ParseJWTToken(token string) (*UserTokenClaims, error) {
}
if tokenClaims != nil {
if claim, ok := tokenClaims.Claims.(*UserTokenClaims); ok && tokenClaims.Valid {
// TODO:多类型用户登录
claim.CompanyId = 1
claim.AdminType = 1
// TODO:版本兼容
//if claim.CompanyId==0{
// claim.CompanyId = 1
// claim.AdminType = 1
//}
return claim, nil
}
}
... ...
... ... @@ -82,6 +82,7 @@ func (this *BaseController) GetRequestHeader(ctx *context.Context) *protocol.Req
h.UserId = claim.UserId
h.CompanyId = claim.CompanyId
h.AdminType = claim.AdminType
h.SimNum, _ = strconv.Atoi(claim.Phone)
}
}
return h
... ...
... ... @@ -151,3 +151,23 @@ func (this *UserController) ChangePassword() {
}
msg = protocol.NewReturnResponse(data, err)
}
//UserInfo
//@router /userInfo [post]
func (this *UserController) UserInfoV2() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.UserInfoRequest
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(2)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := this.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(user.UserInfoV2(header, request))
}
... ...
... ... @@ -18,7 +18,8 @@ func CheckJWTToken(ctx *context.Context) {
strings.HasSuffix(ctx.Request.RequestURI, "refreshToken") ||
strings.HasSuffix(ctx.Request.RequestURI, "smsCode") ||
strings.HasSuffix(ctx.Request.RequestURI, "centerCompanys") ||
strings.HasSuffix(ctx.Request.RequestURI, "centerCompanys") {
strings.HasSuffix(ctx.Request.RequestURI, "companys") ||
strings.HasSuffix(ctx.Request.RequestURI, "loginV2") {
return
}
defer func() {
... ...
... ... @@ -37,7 +37,8 @@ func init() {
}
func InitV2() {
nsV2 := beego.NewNamespace("v2") // , beego.NSBefore(middleware.CheckJWTToken)
nsV2 := beego.NewNamespace("v2", beego.NSBefore(middleware.CheckJWTToken)) // ,
nsV2.Router("/auth/login", &controllers.AuthController{}, "Post:LoginV2")
nsV2.Router("/user/userInfo", &controllers.UserController{}, "Post:UserInfoV2")
beego.AddNamespace(nsV2)
}
... ...
... ... @@ -13,6 +13,7 @@ type RequestHeader struct {
CompanyId int64 //公司编号
UserId int64 //UserId 唯一标识,(类型是合伙人对应表partner_info.id,类型是高层对应表users.id)
AdminType int //合伙人:1 高管:2
SimNum int
requestId string //请求编号 md5
reqIndex int64 //请求链序号
... ...