...
|
...
|
@@ -2,13 +2,18 @@ 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"
|
|
|
"strings"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
|
...
|
...
|
@@ -35,10 +40,11 @@ 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
|
|
|
user *models.User
|
|
|
userAuth *models.UserAuth
|
|
|
result bool
|
|
|
//checkImResponse *protocol.CheckImResponse
|
|
|
userCenterLogin *protocol.UserCenterLoginResponse
|
|
|
)
|
|
|
user, err = repository.User.GetUsersByMobile(request.Phone)
|
|
|
if err != nil {
|
...
|
...
|
@@ -48,16 +54,24 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp |
|
|
}
|
|
|
switch request.GrantType {
|
|
|
case protocol.LoginTypePassPord:
|
|
|
if beego.BConfig.RunMode != "dev" {
|
|
|
|
|
|
}
|
|
|
if strings.Compare(user.Passwd, request.PassWord) == 0 {
|
|
|
goto Success
|
|
|
} else {
|
|
|
err = protocol.NewErrWithMessage(2021, err) //登录密码错误
|
|
|
//if beego.BConfig.RunMode == "prod" {
|
|
|
//
|
|
|
//}
|
|
|
if userCenterLogin, err = userCenterAuthLogin(&protocol.UserCenterLoginRequest{
|
|
|
Phone: request.Phone,
|
|
|
PassWord: request.PassWord,
|
|
|
}); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
break
|
|
|
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
|
...
|
...
|
@@ -93,20 +107,32 @@ 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 {
|
|
|
//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
|
|
|
if err = repository.User.UpdateUserInfo(user); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if checkImResponse != nil && checkImResponse.ImToken != "" {
|
|
|
user.ImToken = checkImResponse.ImToken
|
|
|
}
|
|
|
if user.CsAccount == 0 {
|
|
|
user.CsAccount = imGetRandomCSAccount()
|
|
|
}
|
|
|
userAuth.AuthCodeExp = time.Now().Add(time.Second * protocol.TokenExpire)
|
|
|
if err = repository.UserAuth.UpdateUserAuthById(userAuth); err != nil {
|
|
|
return
|
...
|
...
|
@@ -489,3 +515,56 @@ func imGetRandomCSAccount() (acid int64) { |
|
|
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, "", appKey, 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)
|
|
|
} |
...
|
...
|
|