...
|
...
|
@@ -281,6 +281,177 @@ Success: |
|
|
return
|
|
|
}
|
|
|
|
|
|
//模块认证 (登录V2版本)
|
|
|
func Authorize(header *protocol.RequestHeader, request *protocol.AuthorizeRequest) (rsp *protocol.AuthorizeResponse, err error) {
|
|
|
var ()
|
|
|
rsp = &protocol.AuthorizeResponse{}
|
|
|
var (
|
|
|
user *models.User
|
|
|
userAuth *models.UserAuth
|
|
|
id = request.Uid
|
|
|
getUserRequest *protocol.UCenterServerLoginRequest = &protocol.UCenterServerLoginRequest{
|
|
|
Uid: id,
|
|
|
Token: request.Token,
|
|
|
Type: 2,
|
|
|
CompanyId: request.Cid, //当前登录的公司编号
|
|
|
}
|
|
|
getUserResponse *protocol.UCenterGetUserResponse
|
|
|
message *protocol.Message
|
|
|
//currentCompany *models.Company
|
|
|
//companys []*models.Company
|
|
|
userCompany *models.UserCompany
|
|
|
company *models.Company
|
|
|
)
|
|
|
//验证 当前登录的公司是否有模块权限
|
|
|
if err = CheckBusinessAuth(header, request.Muid); err != nil {
|
|
|
log.Error(request.Muid, err)
|
|
|
return
|
|
|
}
|
|
|
//判断用户是否添加
|
|
|
user, err = models.GetUserByUcenterId(id)
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
}
|
|
|
//判断用户所属公司是否有权限
|
|
|
//if companys, err = models.GetCompanyByPermission(user.Id); err != nil {
|
|
|
// log.Error(err)
|
|
|
// err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
// return
|
|
|
//}
|
|
|
//if len(companys) == 0 {
|
|
|
// err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
// return
|
|
|
//}
|
|
|
|
|
|
//权限验证 判断用户公司是否存在
|
|
|
if userCompany, err = models.GetUserCompanyById(request.Muid); err != nil {
|
|
|
log.Error(err, request.Muid)
|
|
|
err = protocol.NewErrWithMessage(10008, err) //用户无模块权限
|
|
|
return
|
|
|
}
|
|
|
if userCompany.Enable != 1 {
|
|
|
err = protocol.NewErrWithMessage(10006, err) //用户已禁用
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if company, err = models.GetCompanyById(request.Cid); err != nil {
|
|
|
log.Error(err, request.Cid)
|
|
|
err = protocol.NewErrWithMessage(10007, err) //企业已禁用
|
|
|
return
|
|
|
}
|
|
|
if company.Enable != 1 {
|
|
|
err = protocol.NewErrWithMessage(10007, err) ///企业已禁用
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if !utils.ValidVersion(header.Version, protocol.RequireVersion) {
|
|
|
log.Warn(fmt.Sprintf("版本不足 当前手机版本:%v 需要版本大于:%v", header.Version, protocol.RequireVersion))
|
|
|
err = protocol.NewCustomMessage(2002, "版本不足,请升级app") //账号不存在
|
|
|
return
|
|
|
}
|
|
|
//获取最后一次公司编号给统一用户中心
|
|
|
//if u, e := models.GetUserAuthByUserId(user.Id, protocol.DeviceType); e == nil && user.UserCenterId == id {
|
|
|
// if currentCompany, e = models.GetCompanyById(u.CurrentCompanyId); e == nil {
|
|
|
// getUserRequest.CompanyId = currentCompany.UserCenterId
|
|
|
// }
|
|
|
//}
|
|
|
|
|
|
//从用户中心获取用户信息
|
|
|
if _, err = agg.RequestUserCenter(protocol.MethodServerLogin, http.MethodPost, getUserRequest, &message); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("ucenter_id:%v user_id:%v getuser response:", request.Uid, request.Muid), message.Errno, message.Errmsg)
|
|
|
if message.Errno == 0 && message.Errmsg == "ok" {
|
|
|
if err = message.Unmarshal(&getUserResponse); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
switch message.Errno {
|
|
|
case -1:
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
case 0:
|
|
|
goto Success
|
|
|
case 2002:
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
case 10001:
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
case 10003:
|
|
|
err = protocol.NewErrWithMessage(4140, err) //账号不存在
|
|
|
return
|
|
|
default:
|
|
|
log.Error(fmt.Sprintf("error_no:%v msg:%v", message.Errno, message.Errmsg))
|
|
|
err = protocol.NewErrWithMessage(4140, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
Success:
|
|
|
{
|
|
|
userAuth, err = models.GetUserAuthByUserId(user.Id, 1)
|
|
|
if err != nil {
|
|
|
if err == orm.ErrNoRows {
|
|
|
err = nil
|
|
|
userAuth = &models.UserAuth{
|
|
|
UserId: user.Id,
|
|
|
DeviceType: 1, //int8(header.DeviceType),
|
|
|
CurrentCompanyId: request.Muid,
|
|
|
CurrentUserCompanyId: request.Cid,
|
|
|
}
|
|
|
models.AddUserAuth(userAuth)
|
|
|
} else {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
userAuth.AuthCode = uid.NewV1().StringNoDash()
|
|
|
userAuth.CurrentCompanyId = request.Cid
|
|
|
userAuth.CurrentUserCompanyId = request.Muid
|
|
|
|
|
|
/*更新用户信息*/
|
|
|
user.CsAccount = getUserResponse.CustomerAccount
|
|
|
user.ImToken = getUserResponse.ImToken
|
|
|
user.Icon = getUserResponse.Avatar
|
|
|
user.NickName = getUserResponse.NickName
|
|
|
user.Accid = getUserResponse.Accid
|
|
|
user.UserCenterId = getUserResponse.Id
|
|
|
if err = models.UpdateUsersById(user); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
userAuth.AuthCodeExp = time.Now().Add(time.Second * protocol.TokenExpire)
|
|
|
if err = models.UpdateUserAuthById(userAuth); err != nil {
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.AuthorizeResponse{AuthCode: userAuth.AuthCode}
|
|
|
}
|
|
|
err = protocol.NewSuccessWithMessage("登录成功")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//检查企业平台权限
|
|
|
func CheckBusinessAuth(header *protocol.RequestHeader, muid int64) (err error) {
|
|
|
var (
|
|
|
hasAuth bool
|
|
|
authMessage *protocol.Message
|
|
|
)
|
|
|
if authMessage, hasAuth = agg.GetUserModuleAuth(muid); !hasAuth {
|
|
|
err = protocol.NewCustomMessage(authMessage.Errno, authMessage.Errmsg) //账号不存在
|
|
|
return
|
|
|
}
|
|
|
if authMessage.Errno != 0 {
|
|
|
err = protocol.NewCustomMessage(authMessage.Errno, authMessage.Errmsg) //账号不存在
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//统一用户中心登录
|
|
|
func UCenterLogin(header *protocol.RequestHeader, request *protocol.UCenterLoginRequest) (rsp *protocol.UCenterLoginResponse, err error) {
|
|
|
var (
|
...
|
...
|
|