作者 唐旭辉

登录过程处理

@@ -112,6 +112,73 @@ Success: @@ -112,6 +112,73 @@ Success:
112 return 112 return
113 } 113 }
114 114
  115 +func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
  116 + var logintype ILoginAuth
  117 + switch request.GrantType {
  118 + case protocol.LoginTypePassPord:
  119 + logintype = NewLoginByPassword(request.Phone, request.PassWord)
  120 + case protocol.LoginTypeSmdcode:
  121 + logintype = NewLoginBySms(request.Phone, request.Code)
  122 + default:
  123 + err = fmt.Errorf("grantType error:%s", request.GrantType)
  124 + return
  125 + }
  126 + var useridentity *UserIdentity
  127 + useridentity, err = logintype.LoginAuth()
  128 + if err != nil {
  129 + return nil, err
  130 + }
  131 + if len(useridentity.Auth) == 0 {
  132 + useridentity.Auth = uid.NewV1().StringNoDash()
  133 + }
  134 + if len(useridentity.Imtoken) == 0 {
  135 + //usercreate 最多重试俩次
  136 + for i := 0; i < 2; i++ {
  137 + v := s_im.UserCreate{
  138 + Accid: fmt.Sprintf("%s", useridentity.Uid),
  139 + Name: useridentity.Uname,
  140 + Icon: useridentity.Icon,
  141 + }
  142 + tokenInfo, err := s_im.ParseUserCreate(v)
  143 + if err != nil {
  144 + log.Error("s_im.ParseUserCreate err:", err)
  145 + }
  146 + if err == nil {
  147 + if tokenInfo.Code == 200 {
  148 + useridentity.Imtoken = tokenInfo.Info.Token
  149 + // 跳出
  150 + break
  151 + } else {
  152 + log.Error("s_im.ParseUserCreate response code:", tokenInfo.Code)
  153 + }
  154 + }
  155 + }
  156 +
  157 + } else {
  158 + //userupdateunifo
  159 + for i := 0; i < 2; i++ {
  160 + v := s_im.UserUpdateUinfo{
  161 + Accid: fmt.Sprintf("%s", useridentity.Accid),
  162 + Name: useridentity.Uname,
  163 + Icon: useridentity.Icon,
  164 + }
  165 + rsp, err := s_im.ParseUserUpdateUinfo(v)
  166 + if err != nil {
  167 + log.Error("s_im.ParseUserUpdateUinfo err:", err)
  168 + }
  169 + if err == nil {
  170 + if rsp.Code == 200 {
  171 + break
  172 + } else {
  173 + log.Error("s_im.ParseUserUpdateUinfo response code:", rsp.Code)
  174 + }
  175 + }
  176 + }
  177 + }
  178 +
  179 + return nil, err
  180 +}
  181 +
115 //更新设备信息 182 //更新设备信息
116 func (s *AuthService) UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) { 183 func (s *AuthService) UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) {
117 return nil, nil 184 return nil, nil
@@ -357,7 +424,7 @@ func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImRespons @@ -357,7 +424,7 @@ func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImRespons
357 Icon: request.Icon, 424 Icon: request.Icon,
358 } 425 }
359 resp []byte 426 resp []byte
360 - out s_im.UserCreateResult 427 + out s_im.UserTokenResult
361 ) 428 )
362 if resp, err = s_im.DefaultImClient.Call(param); err != nil { 429 if resp, err = s_im.DefaultImClient.Call(param); err != nil {
363 return 430 return
@@ -402,7 +469,7 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR @@ -402,7 +469,7 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR
402 Accid: request.Uid, 469 Accid: request.Uid,
403 } 470 }
404 resp []byte 471 resp []byte
405 - out s_im.UserCreateResult 472 + out s_im.UserTokenResult
406 ) 473 )
407 if resp, err = s_im.DefaultImClient.Call(param); err != nil { 474 if resp, err = s_im.DefaultImClient.Call(param); err != nil {
408 return 475 return
@@ -78,10 +78,11 @@ func (o LoginByPassword) LoginAuth() (*UserIdentity, error) { @@ -78,10 +78,11 @@ func (o LoginByPassword) LoginAuth() (*UserIdentity, error) {
78 } 78 }
79 identity := &UserIdentity{ 79 identity := &UserIdentity{
80 Uid: user.Id, 80 Uid: user.Id,
81 - Imtoken: user.ImToken, 81 + Imtoken: strings.TrimSpace(user.ImToken),
82 Accid: user.CsAccount, 82 Accid: user.CsAccount,
83 Icon: user.Icon, 83 Icon: user.Icon,
84 Uname: user.Uname, 84 Uname: user.Uname,
  85 + Auth: strings.TrimSpace(user.Auth),
85 } 86 }
86 return identity, nil 87 return identity, nil
87 } 88 }
@@ -99,10 +100,11 @@ func (o LoginBySms) LoginAuth() (*UserIdentity, error) { @@ -99,10 +100,11 @@ func (o LoginBySms) LoginAuth() (*UserIdentity, error) {
99 } 100 }
100 identity := &UserIdentity{ 101 identity := &UserIdentity{
101 Uid: user.Id, 102 Uid: user.Id,
102 - Imtoken: user.ImToken, 103 + Imtoken: strings.TrimSpace(user.ImToken),
103 Accid: user.CsAccount, 104 Accid: user.CsAccount,
104 Icon: user.Icon, 105 Icon: user.Icon,
105 Uname: user.Uname, 106 Uname: user.Uname,
  107 + Auth: strings.TrimSpace(user.Auth),
106 } 108 }
107 return identity, nil 109 return identity, nil
108 } 110 }
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 type ImParam interface { 7 type ImParam interface {
8 Format() map[string]string 8 Format() map[string]string
9 GetPath() string 9 GetPath() string
  10 + Valid() error
10 } 11 }
11 12
12 type BaseResp struct { 13 type BaseResp struct {
@@ -19,7 +20,7 @@ type TokenInfo struct { @@ -19,7 +20,7 @@ type TokenInfo struct {
19 Accid string `json:"accid"` 20 Accid string `json:"accid"`
20 Name string `json:"name"` 21 Name string `json:"name"`
21 } 22 }
22 -type UserCreateResult struct { 23 +type UserTokenResult struct {
23 BaseResp 24 BaseResp
24 Info TokenInfo `json:"info"` 25 Info TokenInfo `json:"info"`
25 } 26 }
@@ -67,6 +68,10 @@ func (p UserCreate) GetPath() string { @@ -67,6 +68,10 @@ func (p UserCreate) GetPath() string {
67 return "/user/create.action" 68 return "/user/create.action"
68 } 69 }
69 70
  71 +func (p UserCreate) Valid() error {
  72 + return nil
  73 +}
  74 +
70 type UserRefreshToken struct { 75 type UserRefreshToken struct {
71 Accid string 76 Accid string
72 } 77 }
@@ -85,6 +90,10 @@ func (p UserRefreshToken) GetPath() string { @@ -85,6 +90,10 @@ func (p UserRefreshToken) GetPath() string {
85 return "/user/refreshToken.action" 90 return "/user/refreshToken.action"
86 } 91 }
87 92
  93 +func (p UserRefreshToken) Valid() error {
  94 + return nil
  95 +}
  96 +
88 type UserUpdateUinfo struct { 97 type UserUpdateUinfo struct {
89 Accid string 98 Accid string
90 Name string //这边网易云要有昵称以手机号码为昵称 99 Name string //这边网易云要有昵称以手机号码为昵称
@@ -118,3 +127,7 @@ func (u UserUpdateUinfo) Format() map[string]string { @@ -118,3 +127,7 @@ func (u UserUpdateUinfo) Format() map[string]string {
118 func (u UserUpdateUinfo) GetPath() string { 127 func (u UserUpdateUinfo) GetPath() string {
119 return "/user/refreshToken.action" 128 return "/user/refreshToken.action"
120 } 129 }
  130 +
  131 +func (u UserUpdateUinfo) Valid() error {
  132 + return nil
  133 +}
@@ -14,16 +14,15 @@ import ( @@ -14,16 +14,15 @@ import (
14 "time" 14 "time"
15 ) 15 )
16 16
17 -  
18 var DefaultImClient ImClient 17 var DefaultImClient ImClient
19 18
20 var ErrorFailCall = fmt.Errorf(" imclient call failed") 19 var ErrorFailCall = fmt.Errorf(" imclient call failed")
21 20
22 -func InitImClient(baseUrl ,appKey,appSecret string){ 21 +func InitImClient(baseUrl, appKey, appSecret string) {
23 DefaultImClient = ImClient{ 22 DefaultImClient = ImClient{
24 - baseUrl:baseUrl,  
25 - appKey:appKey,  
26 - appSecret:appSecret, 23 + baseUrl: baseUrl,
  24 + appKey: appKey,
  25 + appSecret: appSecret,
27 } 26 }
28 } 27 }
29 28
@@ -55,7 +54,9 @@ func (i ImClient) buildHeader() http.Header { @@ -55,7 +54,9 @@ func (i ImClient) buildHeader() http.Header {
55 } 54 }
56 55
57 func (i ImClient) httpDo(path string, posts map[string]string) ([]byte, error) { 56 func (i ImClient) httpDo(path string, posts map[string]string) ([]byte, error) {
58 - client := http.Client{} 57 + client := http.Client{
  58 + Timeout: 5 * time.Second, //请求超时时间5秒
  59 + }
59 reqURL := i.baseUrl + path 60 reqURL := i.baseUrl + path
60 params := url.Values{} 61 params := url.Values{}
61 for k, v := range posts { 62 for k, v := range posts {
@@ -84,11 +85,42 @@ func (i ImClient) Call(param ImParam) ([]byte, error) { @@ -84,11 +85,42 @@ func (i ImClient) Call(param ImParam) ([]byte, error) {
84 return i.httpDo(param.GetPath(), param.Format()) 85 return i.httpDo(param.GetPath(), param.Format())
85 } 86 }
86 87
87 -//ParseUserCreateResult 解析返回值  
88 -func ParseUserCreateResult(v []byte) (*UserCreateResult, error) {  
89 - var result *UserCreateResult  
90 - if err := json.Unmarshal(v, result); err != nil { 88 +//RequestUserCreate 解析返回值
  89 +func ParseUserCreate(v UserCreate) (*UserTokenResult, error) {
  90 + var result UserTokenResult
  91 + btData, err := DefaultImClient.Call(v)
  92 + if err != nil {
  93 + return nil, err
  94 + }
  95 + err = json.Unmarshal(btData, &result)
  96 + if err != nil {
  97 + return nil, err
  98 + }
  99 + return &result, nil
  100 +}
  101 +
  102 +func ParseUserRefreshToken(v UserRefreshToken) (*UserTokenResult, error) {
  103 + var result UserTokenResult
  104 + btData, err := DefaultImClient.Call(v)
  105 + if err != nil {
  106 + return nil, err
  107 + }
  108 + err = json.Unmarshal(btData, &result)
  109 + if err != nil {
  110 + return nil, err
  111 + }
  112 + return &result, nil
  113 +}
  114 +
  115 +func ParseUserUpdateUinfo(v UserUpdateUinfo) (*BaseResp, error) {
  116 + var result BaseResp
  117 + btData, err := DefaultImClient.Call(v)
  118 + if err != nil {
  119 + return nil, err
  120 + }
  121 + err = json.Unmarshal(btData, &result)
  122 + if err != nil {
91 return nil, err 123 return nil, err
92 } 124 }
93 - return result, nil 125 + return &result, nil
94 } 126 }