作者 唐旭辉

登录过程处理

... ... @@ -112,6 +112,73 @@ Success:
return
}
func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
var logintype ILoginAuth
switch request.GrantType {
case protocol.LoginTypePassPord:
logintype = NewLoginByPassword(request.Phone, request.PassWord)
case protocol.LoginTypeSmdcode:
logintype = NewLoginBySms(request.Phone, request.Code)
default:
err = fmt.Errorf("grantType error:%s", request.GrantType)
return
}
var useridentity *UserIdentity
useridentity, err = logintype.LoginAuth()
if err != nil {
return nil, err
}
if len(useridentity.Auth) == 0 {
useridentity.Auth = uid.NewV1().StringNoDash()
}
if len(useridentity.Imtoken) == 0 {
//usercreate 最多重试俩次
for i := 0; i < 2; i++ {
v := s_im.UserCreate{
Accid: fmt.Sprintf("%s", useridentity.Uid),
Name: useridentity.Uname,
Icon: useridentity.Icon,
}
tokenInfo, err := s_im.ParseUserCreate(v)
if err != nil {
log.Error("s_im.ParseUserCreate err:", err)
}
if err == nil {
if tokenInfo.Code == 200 {
useridentity.Imtoken = tokenInfo.Info.Token
// 跳出
break
} else {
log.Error("s_im.ParseUserCreate response code:", tokenInfo.Code)
}
}
}
} else {
//userupdateunifo
for i := 0; i < 2; i++ {
v := s_im.UserUpdateUinfo{
Accid: fmt.Sprintf("%s", useridentity.Accid),
Name: useridentity.Uname,
Icon: useridentity.Icon,
}
rsp, err := s_im.ParseUserUpdateUinfo(v)
if err != nil {
log.Error("s_im.ParseUserUpdateUinfo err:", err)
}
if err == nil {
if rsp.Code == 200 {
break
} else {
log.Error("s_im.ParseUserUpdateUinfo response code:", rsp.Code)
}
}
}
}
return nil, err
}
//更新设备信息
func (s *AuthService) UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) {
return nil, nil
... ... @@ -357,7 +424,7 @@ func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImRespons
Icon: request.Icon,
}
resp []byte
out s_im.UserCreateResult
out s_im.UserTokenResult
)
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
return
... ... @@ -402,7 +469,7 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR
Accid: request.Uid,
}
resp []byte
out s_im.UserCreateResult
out s_im.UserTokenResult
)
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
return
... ...
... ... @@ -78,10 +78,11 @@ func (o LoginByPassword) LoginAuth() (*UserIdentity, error) {
}
identity := &UserIdentity{
Uid: user.Id,
Imtoken: user.ImToken,
Imtoken: strings.TrimSpace(user.ImToken),
Accid: user.CsAccount,
Icon: user.Icon,
Uname: user.Uname,
Auth: strings.TrimSpace(user.Auth),
}
return identity, nil
}
... ... @@ -99,10 +100,11 @@ func (o LoginBySms) LoginAuth() (*UserIdentity, error) {
}
identity := &UserIdentity{
Uid: user.Id,
Imtoken: user.ImToken,
Imtoken: strings.TrimSpace(user.ImToken),
Accid: user.CsAccount,
Icon: user.Icon,
Uname: user.Uname,
Auth: strings.TrimSpace(user.Auth),
}
return identity, nil
}
... ...
... ... @@ -7,6 +7,7 @@ import (
type ImParam interface {
Format() map[string]string
GetPath() string
Valid() error
}
type BaseResp struct {
... ... @@ -19,7 +20,7 @@ type TokenInfo struct {
Accid string `json:"accid"`
Name string `json:"name"`
}
type UserCreateResult struct {
type UserTokenResult struct {
BaseResp
Info TokenInfo `json:"info"`
}
... ... @@ -67,6 +68,10 @@ func (p UserCreate) GetPath() string {
return "/user/create.action"
}
func (p UserCreate) Valid() error {
return nil
}
type UserRefreshToken struct {
Accid string
}
... ... @@ -85,6 +90,10 @@ func (p UserRefreshToken) GetPath() string {
return "/user/refreshToken.action"
}
func (p UserRefreshToken) Valid() error {
return nil
}
type UserUpdateUinfo struct {
Accid string
Name string //这边网易云要有昵称以手机号码为昵称
... ... @@ -118,3 +127,7 @@ func (u UserUpdateUinfo) Format() map[string]string {
func (u UserUpdateUinfo) GetPath() string {
return "/user/refreshToken.action"
}
func (u UserUpdateUinfo) Valid() error {
return nil
}
... ...
... ... @@ -14,16 +14,15 @@ import (
"time"
)
var DefaultImClient ImClient
var ErrorFailCall = fmt.Errorf(" imclient call failed")
func InitImClient(baseUrl ,appKey,appSecret string){
func InitImClient(baseUrl, appKey, appSecret string) {
DefaultImClient = ImClient{
baseUrl:baseUrl,
appKey:appKey,
appSecret:appSecret,
baseUrl: baseUrl,
appKey: appKey,
appSecret: appSecret,
}
}
... ... @@ -55,7 +54,9 @@ func (i ImClient) buildHeader() http.Header {
}
func (i ImClient) httpDo(path string, posts map[string]string) ([]byte, error) {
client := http.Client{}
client := http.Client{
Timeout: 5 * time.Second, //请求超时时间5秒
}
reqURL := i.baseUrl + path
params := url.Values{}
for k, v := range posts {
... ... @@ -84,11 +85,42 @@ func (i ImClient) Call(param ImParam) ([]byte, error) {
return i.httpDo(param.GetPath(), param.Format())
}
//ParseUserCreateResult 解析返回值
func ParseUserCreateResult(v []byte) (*UserCreateResult, error) {
var result *UserCreateResult
if err := json.Unmarshal(v, result); err != nil {
//RequestUserCreate 解析返回值
func ParseUserCreate(v UserCreate) (*UserTokenResult, error) {
var result UserTokenResult
btData, err := DefaultImClient.Call(v)
if err != nil {
return nil, err
}
err = json.Unmarshal(btData, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func ParseUserRefreshToken(v UserRefreshToken) (*UserTokenResult, error) {
var result UserTokenResult
btData, err := DefaultImClient.Call(v)
if err != nil {
return nil, err
}
err = json.Unmarshal(btData, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func ParseUserUpdateUinfo(v UserUpdateUinfo) (*BaseResp, error) {
var result BaseResp
btData, err := DefaultImClient.Call(v)
if err != nil {
return nil, err
}
err = json.Unmarshal(btData, &result)
if err != nil {
return nil, err
}
return result, nil
return &result, nil
}
... ...