...
|
...
|
@@ -2,17 +2,13 @@ package auth |
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"crypto/sha1"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
"html/template"
|
|
|
"io/ioutil"
|
|
|
"math/rand"
|
|
|
"net/http"
|
|
|
"opp/internal/utils"
|
|
|
"opp/services/agg"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
...
|
...
|
@@ -21,7 +17,6 @@ import ( |
|
|
"opp/internal/repository"
|
|
|
"opp/models"
|
|
|
"opp/protocol"
|
|
|
s_im "opp/services/im"
|
|
|
s_sms "opp/services/sms"
|
|
|
|
|
|
"github.com/astaxie/beego"
|
...
|
...
|
@@ -40,65 +35,54 @@ var ( |
|
|
//登录
|
|
|
func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
|
|
|
var (
|
|
|
user *models.User
|
|
|
userAuth *models.UserAuth
|
|
|
result bool
|
|
|
//checkImResponse *protocol.CheckImResponse
|
|
|
userCenterLogin *protocol.UserCenterLoginResponse
|
|
|
user *models.User
|
|
|
userAuth *models.UserAuth
|
|
|
getUserRequest *protocol.UCenterGetUserRequest = &protocol.UCenterGetUserRequest{}
|
|
|
getUserResponse *protocol.UCenterGetUserResponse
|
|
|
message *protocol.Message
|
|
|
)
|
|
|
user, err = repository.User.GetUsersByMobile(request.Phone)
|
|
|
user, err = models.GetUserByUcenterId(request.Uid)
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
}
|
|
|
switch request.GrantType {
|
|
|
case protocol.LoginTypePassPord:
|
|
|
//if beego.BConfig.RunMode == "prod" {
|
|
|
//
|
|
|
//}
|
|
|
if userCenterLogin, err = userCenterAuthLogin(&protocol.UserCenterLoginRequest{
|
|
|
Phone: request.Phone,
|
|
|
PassWord: request.PassWord,
|
|
|
}); err != nil {
|
|
|
//TODO:验证模块权限
|
|
|
|
|
|
//从用户中心获取用户信息
|
|
|
if _, err = agg.RequestUserCenter(fmt.Sprintf("%v%v", protocol.MethodGetUser, request.Uid), http.MethodGet, getUserRequest, &message); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("ucenter_id:%v getuser response:", request.Uid), 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
|
|
|
//if strings.Compare(user.Passwd, request.PassWord) == 0 {
|
|
|
// goto Success
|
|
|
//} else {
|
|
|
// err = protocol.NewErrWithMessage(2021, err) //登录密码错误
|
|
|
// return
|
|
|
//}
|
|
|
//break
|
|
|
case protocol.LoginTypeSmdcode:
|
|
|
//if beego.BConfig.RunMode =="dev"{
|
|
|
// goto Success
|
|
|
//}
|
|
|
if result, err = CheckSmsCode(request.Phone, request.Code, protocol.SmsCode); result && err == nil {
|
|
|
goto Success
|
|
|
} else {
|
|
|
//err = protocol.NewErrWithMessage(1012, err)
|
|
|
return
|
|
|
}
|
|
|
default:
|
|
|
err = fmt.Errorf("grantType error")
|
|
|
case 2002:
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
default:
|
|
|
err = fmt.Errorf("error_no:%v msg:%v", message.Errno, message.Errmsg)
|
|
|
break
|
|
|
}
|
|
|
|
|
|
Success:
|
|
|
{
|
|
|
user, err = repository.User.GetUsersByMobile(request.Phone)
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
userAuth, err = repository.UserAuth.GetUserAuthByUserId(user.Id, header.DeviceType)
|
|
|
userAuth, err = repository.UserAuth.GetUserAuthByUserId(user.Id, 1)
|
|
|
if err != nil {
|
|
|
if err == orm.ErrNoRows {
|
|
|
userAuth = &models.UserAuth{
|
|
|
UserId: user.Id,
|
|
|
DeviceType: int8(header.DeviceType),
|
|
|
DeviceType: 1, //int8(header.DeviceType),
|
|
|
}
|
|
|
repository.UserAuth.AddUserAuth(userAuth)
|
|
|
} else {
|
...
|
...
|
@@ -107,28 +91,14 @@ Success: |
|
|
}
|
|
|
}
|
|
|
userAuth.AuthCode = uid.NewV1().StringNoDash()
|
|
|
//if checkImResponse, err = CheckIm(&protocol.CheckImRequest{
|
|
|
// Uid: fmt.Sprintf("%v", user.Id),
|
|
|
// Uname: user.NickName,
|
|
|
// Icon: user.Icon,
|
|
|
// IsCreated: user.ImToken != "",
|
|
|
//}); err != nil {
|
|
|
// return
|
|
|
//}
|
|
|
//if checkImResponse != nil && checkImResponse.ImToken != "" {
|
|
|
// user.ImToken = checkImResponse.ImToken
|
|
|
//}
|
|
|
//if user.CsAccount == 0 {
|
|
|
// user.CsAccount = imGetRandomCSAccount()
|
|
|
//}
|
|
|
|
|
|
/*更新用户信息*/
|
|
|
user.CsAccount, _ = strconv.ParseInt(userCenterLogin.CustomerAccount, 10, 64)
|
|
|
user.ImToken = userCenterLogin.Token
|
|
|
user.Icon = userCenterLogin.Avatar
|
|
|
user.NickName = userCenterLogin.NickName
|
|
|
user.Accid, _ = strconv.ParseInt(userCenterLogin.Accid, 10, 64)
|
|
|
user.UserCenterId = userCenterLogin.Id
|
|
|
user.CsAccount, _ = strconv.ParseInt(getUserResponse.CustomerAccount, 10, 64)
|
|
|
user.ImToken = getUserResponse.Token
|
|
|
user.Icon = getUserResponse.Avatar
|
|
|
user.NickName = getUserResponse.NickName
|
|
|
user.Accid, _ = strconv.ParseInt(getUserResponse.Accid, 10, 64)
|
|
|
user.UserCenterId = getUserResponse.Id
|
|
|
if err = repository.User.UpdateUserInfo(user); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
...
|
...
|
@@ -143,13 +113,60 @@ Success: |
|
|
return
|
|
|
}
|
|
|
|
|
|
//统一用户中心登录
|
|
|
func UCenterLogin(header *protocol.RequestHeader, request *protocol.UCenterLoginRequest) (rsp *protocol.UCenterLoginResponse, err error) {
|
|
|
var (
|
|
|
loginMethod = protocol.MethodLogin
|
|
|
//data []byte
|
|
|
loginResponse *protocol.UserCenterLoginResponse
|
|
|
)
|
|
|
var message protocol.Message
|
|
|
if _, err = agg.RequestUserCenter(loginMethod, http.MethodPost, request, &message); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if message.Errno == 0 && message.Errmsg == "ok" {
|
|
|
if err = message.Unmarshal(&loginResponse); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.UCenterLoginResponse{
|
|
|
Uid: loginResponse.Id,
|
|
|
Module: []*protocol.ModulePermission{
|
|
|
//TODO:取模块权限
|
|
|
&protocol.ModulePermission{
|
|
|
Name: protocol.ModuleOportunity,
|
|
|
},
|
|
|
&protocol.ModulePermission{
|
|
|
Name: protocol.ModuleQuestion,
|
|
|
},
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("simnum:%v login user-center response:", request.Phone), message.Errno, message.Errno)
|
|
|
switch message.Errno {
|
|
|
case -1:
|
|
|
err = protocol.NewErrWithMessage(2021, err) //密码错误
|
|
|
return
|
|
|
case 0:
|
|
|
break
|
|
|
case 2002:
|
|
|
err = protocol.NewErrWithMessage(2002, err) //账号不存在
|
|
|
return
|
|
|
default:
|
|
|
err = fmt.Errorf("error_no:%v msg:%v", message.Errno, message.Errmsg)
|
|
|
break
|
|
|
}
|
|
|
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//更新设备信息
|
|
|
func UpdateDevice(header *protocol.RequestHeader, request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) {
|
|
|
var (
|
|
|
userAuth *models.UserAuth
|
|
|
updateMap = make(map[string]interface{})
|
|
|
)
|
|
|
//rsp =&protocol.UpdateDeviceResponse{}
|
|
|
if userAuth, err = repository.UserAuth.GetUserAuthByUserId(header.Uid, header.DeviceType); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
...
|
...
|
@@ -225,14 +242,6 @@ func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshT |
|
|
return
|
|
|
}
|
|
|
|
|
|
//刷新token loginType mobile im web
|
|
|
//func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) {
|
|
|
// if request.Uid == 0 {
|
|
|
// return
|
|
|
// }
|
|
|
// return nil, nil
|
|
|
//}
|
|
|
|
|
|
//检查token有效性
|
|
|
func CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) {
|
|
|
var (
|
...
|
...
|
@@ -410,161 +419,3 @@ func Revoke(header *protocol.RequestHeader, request *protocol.RevokeRequest) (rs |
|
|
}
|
|
|
return
|
|
|
} |
|
|
|
|
|
func CheckIm(request *protocol.CheckImRequest) (rsp *protocol.CheckImResponse, err error) {
|
|
|
var ()
|
|
|
if beego.BConfig.RunMode != "prod" {
|
|
|
return
|
|
|
}
|
|
|
rsp = &protocol.CheckImResponse{}
|
|
|
if !request.IsCreated {
|
|
|
if err = imUserCreate(request, rsp); err != nil {
|
|
|
return
|
|
|
}
|
|
|
} else {
|
|
|
if err = imUserInfoUpdate(request, rsp); err != nil {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
if err = imUserRefreshToken(request, rsp); err != nil {
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//create
|
|
|
func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
|
|
|
var (
|
|
|
param s_im.UserCreate = s_im.UserCreate{
|
|
|
Accid: request.Uid,
|
|
|
Name: request.Uname,
|
|
|
Icon: request.Icon,
|
|
|
}
|
|
|
resp []byte
|
|
|
out s_im.UserTokenResult
|
|
|
)
|
|
|
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal(resp, &out); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if out.Code != 200 || (out.Info.Accid != request.Uid) {
|
|
|
return s_im.ErrorFailCall
|
|
|
}
|
|
|
rsp.ImToken = out.Info.Token
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//update user info
|
|
|
func imUserInfoUpdate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
|
|
|
var (
|
|
|
param s_im.UserUpdateUinfo = s_im.UserUpdateUinfo{
|
|
|
Accid: request.Uid,
|
|
|
Name: request.Uname,
|
|
|
Icon: request.Icon,
|
|
|
}
|
|
|
resp []byte
|
|
|
out s_im.BaseResp
|
|
|
)
|
|
|
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal(resp, &out); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if out.Code != 200 {
|
|
|
return s_im.ErrorFailCall
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//refresh token
|
|
|
func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
|
|
|
var (
|
|
|
param s_im.UserRefreshToken = s_im.UserRefreshToken{
|
|
|
Accid: request.Uid,
|
|
|
}
|
|
|
resp []byte
|
|
|
out s_im.UserTokenResult
|
|
|
)
|
|
|
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal(resp, &out); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if out.Code != 200 || (out.Info.Accid != request.Uid) {
|
|
|
return s_im.ErrorFailCall
|
|
|
}
|
|
|
rsp.ImToken = out.Info.Token
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 获取客服id
|
|
|
func imGetRandomCSAccount() (acid int64) {
|
|
|
kefus, err := models.GetUserKefu()
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if len(kefus) <= 0 {
|
|
|
return
|
|
|
}
|
|
|
index := rand.Intn(len(kefus))
|
|
|
acid = kefus[index].Id //Accid
|
|
|
return acid
|
|
|
}
|
|
|
|
|
|
//用户中心密码登录
|
|
|
func userCenterAuthLogin(request *protocol.UserCenterLoginRequest) (rsp *protocol.UserCenterLoginResponse, err error) {
|
|
|
var (
|
|
|
loginMethod = "/auth/login"
|
|
|
httpRsp *http.Response
|
|
|
data []byte
|
|
|
curTime = fmt.Sprintf("%v", time.Now().Unix())
|
|
|
appKey = beego.AppConfig.String("user_center_app_key")
|
|
|
salt = beego.AppConfig.String("user_center_salt")
|
|
|
)
|
|
|
rsp = &protocol.UserCenterLoginResponse{}
|
|
|
httpReq := httplib.Post(beego.AppConfig.String("user_center_url") + loginMethod)
|
|
|
httpReq.JSONBody(request)
|
|
|
httpReq.Header("appKey", appKey)
|
|
|
httpReq.Header("curTime", curTime)
|
|
|
httpReq.Header("checkSum", getUserCenterCheckSum(curTime, "", beego.AppConfig.String("user_center_app_secret"), salt))
|
|
|
if httpRsp, err = httpReq.DoRequest(); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
data, err = ioutil.ReadAll(httpRsp.Body)
|
|
|
defer httpRsp.Body.Close()
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
log.Info(fmt.Sprintf("simnum:%v login user-center response:%v", request.Phone, string(data)))
|
|
|
type msg struct {
|
|
|
protocol.ErrorCode
|
|
|
Data *protocol.UserCenterLoginResponse `json:"data"`
|
|
|
}
|
|
|
var message protocol.Message
|
|
|
if err = json.Unmarshal(data, &message); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if message.Errno == 0 && message.Errmsg == "ok" {
|
|
|
if err = message.Unmarshal(&rsp); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
} else {
|
|
|
err = fmt.Errorf("error_no:%v msg:%v", message.Errno, message.Errmsg)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func getUserCenterCheckSum(curTime, nonce, appKey, salt string) string {
|
|
|
sha1 := sha1.New()
|
|
|
sum := sha1.Sum([]byte(fmt.Sprintf("%s%s%s%s", curTime, nonce, appKey, salt)))
|
|
|
return hex.EncodeToString(sum)
|
|
|
} |
...
|
...
|
|