作者 yangfu

im csaccount

@@ -4,18 +4,19 @@ import ( @@ -4,18 +4,19 @@ import (
4 "bytes" 4 "bytes"
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
7 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"  
8 "html/template" 7 "html/template"
9 "math/rand" 8 "math/rand"
10 "strconv" 9 "strconv"
11 "strings" 10 "strings"
12 "time" 11 "time"
13 12
  13 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
  14 +
14 "ability/internal/repository" 15 "ability/internal/repository"
15 "ability/models" 16 "ability/models"
16 "ability/protocol" 17 "ability/protocol"
17 - s_sms "ability/services/sms"  
18 s_im "ability/services/im" 18 s_im "ability/services/im"
  19 + s_sms "ability/services/sms"
19 20
20 "github.com/astaxie/beego" 21 "github.com/astaxie/beego"
21 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 22 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
@@ -42,7 +43,7 @@ func assertImplement() { @@ -42,7 +43,7 @@ func assertImplement() {
42 43
43 var ( 44 var (
44 //服务 45 //服务
45 - sms s_sms.ISmsService = &s_sms.YunPianSmsService{} 46 + sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
46 ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{} 47 ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
47 //仓储 48 //仓储
48 UserRepository repository.IUserRepository = &repository.UserRepository{} 49 UserRepository repository.IUserRepository = &repository.UserRepository{}
@@ -51,9 +52,9 @@ var ( @@ -51,9 +52,9 @@ var (
51 //登录 52 //登录
52 func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { 53 func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
53 var ( 54 var (
54 - user *models.Users  
55 - userInfo *models.UserInfo  
56 - result bool 55 + user *models.Users
  56 + userInfo *models.UserInfo
  57 + result bool
57 checkImResponse *protocol.CheckImResponse 58 checkImResponse *protocol.CheckImResponse
58 ) 59 )
59 user, err = UserRepository.GetUsersByMobile(request.Phone) 60 user, err = UserRepository.GetUsersByMobile(request.Phone)
@@ -105,7 +106,7 @@ Success: @@ -105,7 +106,7 @@ Success:
105 if userInfo.CsAccount==0{ 106 if userInfo.CsAccount==0{
106 userInfo.CsAccount = imGetRandomCSAccount() 107 userInfo.CsAccount = imGetRandomCSAccount()
107 } 108 }
108 - userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire) 109 + userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
109 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil { 110 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
110 return 111 return
111 } 112 }
@@ -153,7 +154,7 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr @@ -153,7 +154,7 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
153 //刷新token 154 //刷新token
154 func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) { 155 func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
155 var ( 156 var (
156 - userInfo *models.UserInfo 157 + userInfo *models.UserInfo
157 ) 158 )
158 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret) 159 _, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
159 if err != nil { 160 if err != nil {
@@ -330,94 +331,96 @@ Fail: @@ -330,94 +331,96 @@ Fail:
330 return 331 return
331 } 332 }
332 333
333 -func CheckIm(request *protocol.CheckImRequest)(rsp *protocol.CheckImResponse,err error){  
334 - var (  
335 -  
336 - ) 334 +func CheckIm(request *protocol.CheckImRequest) (rsp *protocol.CheckImResponse, err error) {
  335 + var ()
337 if beego.BConfig.RunMode != "prod" { 336 if beego.BConfig.RunMode != "prod" {
338 return 337 return
339 } 338 }
340 - rsp =&protocol.CheckImResponse{}  
341 - if !request.IsCreated{  
342 - if err = imUserCreate(request,rsp);err!=nil{ 339 + rsp = &protocol.CheckImResponse{}
  340 + if !request.IsCreated {
  341 + if err = imUserCreate(request, rsp); err != nil {
343 return 342 return
344 } 343 }
345 - }else{  
346 - if err = imUserInfoUpdate(request,rsp);err!=nil{ 344 + } else {
  345 + if err = imUserInfoUpdate(request, rsp); err != nil {
347 return 346 return
348 } 347 }
349 } 348 }
350 - if err = imUserRefreshToken(request,rsp);err!=nil{ 349 + if err = imUserRefreshToken(request, rsp); err != nil {
351 return 350 return
352 } 351 }
353 return 352 return
354 } 353 }
  354 +
355 //create 355 //create
356 -func imUserCreate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){  
357 - var(  
358 - param s_im.UserCreate=s_im.UserCreate{  
359 - Accid:request.Uid,  
360 - Name:request.Uname,  
361 - Icon:request.Icon, 356 +func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
  357 + var (
  358 + param s_im.UserCreate = s_im.UserCreate{
  359 + Accid: request.Uid,
  360 + Name: request.Uname,
  361 + Icon: request.Icon,
362 } 362 }
363 resp []byte 363 resp []byte
364 - out s_im.UserCreateResult 364 + out s_im.UserCreateResult
365 ) 365 )
366 - if resp,err=s_im.DefaultImClient.Call(param);err!=nil{ 366 + if resp, err = s_im.DefaultImClient.Call(param); err != nil {
367 return 367 return
368 } 368 }
369 - if err = json.Unmarshal(resp,&out);err!=nil{ 369 + if err = json.Unmarshal(resp, &out); err != nil {
370 return 370 return
371 } 371 }
372 - if out.Code!=200 || (out.Info.Accid!=request.Uid){ 372 + if out.Code != 200 || (out.Info.Accid != request.Uid) {
373 return s_im.ErrorFailCall 373 return s_im.ErrorFailCall
374 } 374 }
375 rsp.ImToken = out.Info.Token 375 rsp.ImToken = out.Info.Token
376 return 376 return
377 } 377 }
  378 +
378 //update user info 379 //update user info
379 -func imUserInfoUpdate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){  
380 - var(  
381 - param s_im.UserUpdateUinfo=s_im.UserUpdateUinfo{  
382 - Accid:request.Uid,  
383 - Name:request.Uname,  
384 - Icon:request.Icon, 380 +func imUserInfoUpdate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
  381 + var (
  382 + param s_im.UserUpdateUinfo = s_im.UserUpdateUinfo{
  383 + Accid: request.Uid,
  384 + Name: request.Uname,
  385 + Icon: request.Icon,
385 } 386 }
386 resp []byte 387 resp []byte
387 - out s_im.BaseResp 388 + out s_im.BaseResp
388 ) 389 )
389 - if resp,err=s_im.DefaultImClient.Call(param);err!=nil{ 390 + if resp, err = s_im.DefaultImClient.Call(param); err != nil {
390 return 391 return
391 } 392 }
392 - if err = json.Unmarshal(resp,&out);err!=nil{ 393 + if err = json.Unmarshal(resp, &out); err != nil {
393 return 394 return
394 } 395 }
395 - if out.Code!=200{ 396 + if out.Code != 200 {
396 return s_im.ErrorFailCall 397 return s_im.ErrorFailCall
397 } 398 }
398 return 399 return
399 } 400 }
  401 +
400 //refresh token 402 //refresh token
401 -func imUserRefreshToken(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){  
402 - var(  
403 - param s_im.UserRefreshToken=s_im.UserRefreshToken{  
404 - Accid:request.Uid, 403 +func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
  404 + var (
  405 + param s_im.UserRefreshToken = s_im.UserRefreshToken{
  406 + Accid: request.Uid,
405 } 407 }
406 resp []byte 408 resp []byte
407 - out s_im.UserCreateResult 409 + out s_im.UserCreateResult
408 ) 410 )
409 - if resp,err=s_im.DefaultImClient.Call(param);err!=nil{ 411 + if resp, err = s_im.DefaultImClient.Call(param); err != nil {
410 return 412 return
411 } 413 }
412 - if err = json.Unmarshal(resp,&out);err!=nil{ 414 + if err = json.Unmarshal(resp, &out); err != nil {
413 return 415 return
414 } 416 }
415 - if out.Code!=200 || (out.Info.Accid!=request.Uid){ 417 + if out.Code != 200 || (out.Info.Accid != request.Uid) {
416 return s_im.ErrorFailCall 418 return s_im.ErrorFailCall
417 } 419 }
418 rsp.ImToken = out.Info.Token 420 rsp.ImToken = out.Info.Token
419 return 421 return
420 } 422 }
  423 +
421 // 获取客服id 424 // 获取客服id
422 func imGetRandomCSAccount()( acid int64){ 425 func imGetRandomCSAccount()( acid int64){
423 kefus,err :=models.GetUsersKefu() 426 kefus,err :=models.GetUsersKefu()
@@ -436,4 +439,3 @@ func imGetRandomCSAccount()( acid int64){ @@ -436,4 +439,3 @@ func imGetRandomCSAccount()( acid int64){
436 return acid 439 return acid
437 } 440 }
438 441
439 -  
1 package auth 1 package auth
2 2
  3 +import (
  4 + "ability/protocol"
  5 + "math/rand"
  6 + "strings"
  7 + "time"
  8 +
  9 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
  10 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  11 +)
  12 +
3 type ILoginAuth interface { 13 type ILoginAuth interface {
4 - LoginAuth() error  
5 - TODO(v interface{}) error 14 + LoginAuth() (*UserIdentity, error) //登录认证操作,返回身份ID和错误
  15 + TODO(v interface{}) error //预留,不知道干嘛用,也许可以用来获取取认证结果,或进行认证后附带的操作
  16 +}
  17 +
  18 +type UserIdentity struct {
  19 + Auth string `json:"auth`
  20 + Uid int64 `json:"uid"`
  21 + Imtoken string `json:"imtoken"`
  22 + Accid int64 `json:"accid"`
  23 + Icon string `json:"icon"`
  24 + Uname string `json:"uname"`
6 } 25 }
7 26
8 //TODO 27 //TODO
@@ -10,12 +29,28 @@ type ILoginAuth interface { @@ -10,12 +29,28 @@ type ILoginAuth interface {
10 type LoginByPassword struct { 29 type LoginByPassword struct {
11 Username string 30 Username string
12 Password string 31 Password string
  32 + err error
  33 +}
  34 +
  35 +func NewLoginByPassword(username string, password string) *LoginByPassword {
  36 + return &LoginByPassword{
  37 + Username: username,
  38 + Password: password,
  39 + }
13 } 40 }
14 41
15 //手机短信登录 42 //手机短信登录
16 type LoginBySms struct { 43 type LoginBySms struct {
17 Phone string 44 Phone string
18 Code string 45 Code string
  46 + err error
  47 +}
  48 +
  49 +func NewLoginBySms(phone string, code string) *LoginBySms {
  50 + return &LoginBySms{
  51 + Phone: phone,
  52 + Code: code,
  53 + }
19 } 54 }
20 55
21 //其他第三方登录 56 //其他第三方登录
@@ -30,19 +65,66 @@ var ( @@ -30,19 +65,66 @@ var (
30 _ ILoginAuth = LoginByXxxx{} 65 _ ILoginAuth = LoginByXxxx{}
31 ) 66 )
32 67
33 -func (o LoginByPassword) LoginAuth() error {  
34 - return nil 68 +func (o LoginByPassword) LoginAuth() (*UserIdentity, error) {
  69 + user, err := UserRepository.GetUserInfoByMobile(o.Username)
  70 + if err != nil {
  71 + log.Error(err)
  72 + o.err = common.NewError(2020, err) //账号不存在
  73 + return nil, o.err
  74 + }
  75 + if strings.Compare(user.Passwd, o.Password) != 0 {
  76 + o.err = common.NewError(2021, err) //登录密码错误
  77 + return nil, o.err
  78 + }
  79 + identity := &UserIdentity{
  80 + Uid: user.Id,
  81 + Imtoken: user.ImToken,
  82 + Accid: user.CsAccount,
  83 + Icon: user.Icon,
  84 + Uname: user.Uname,
  85 + }
  86 + return identity, nil
35 } 87 }
36 88
37 -func (o LoginBySms) LoginAuth() error {  
38 - return nil 89 +func (o LoginBySms) LoginAuth() (*UserIdentity, error) {
  90 + user, err := UserRepository.GetUserInfoByMobile(o.Phone)
  91 + if err != nil {
  92 + log.Error(err)
  93 + o.err = common.NewError(2020, err) //账号不存在
  94 + return nil, o.err
  95 + }
  96 + result, err := CheckSmsCode(o.Phone, o.Code, protocol.SmsLoginCode)
  97 + if err != nil && !result {
  98 + return nil, err
  99 + }
  100 + identity := &UserIdentity{
  101 + Uid: user.Id,
  102 + Imtoken: user.ImToken,
  103 + Accid: user.CsAccount,
  104 + Icon: user.Icon,
  105 + Uname: user.Uname,
  106 + }
  107 + return identity, nil
39 } 108 }
40 109
41 -func (o LoginByXxxx) LoginAuth() error {  
42 - return nil 110 +func (o LoginByXxxx) LoginAuth() (*UserIdentity, error) {
  111 + //TODO
  112 + return nil, nil
43 } 113 }
44 114
45 func (o LoginByPassword) TODO(v interface{}) error { 115 func (o LoginByPassword) TODO(v interface{}) error {
  116 + // rv := reflect.ValueOf(v)
  117 + // if rv.Kind() != reflect.Ptr {
  118 + // return errors.New("v must be point")
  119 + // }
  120 + // rv = rv.Elem()
  121 + // if ok := rv.CanSet(); !ok {
  122 + // panic("v can not set")
  123 + // }
  124 + // a := "random string"
  125 + // ra := reflect.ValueOf(a)
  126 + // rv.Set(ra)
  127 + //生成并更新authcode
46 return nil 128 return nil
47 } 129 }
48 130
@@ -53,3 +135,15 @@ func (o LoginBySms) TODO(v interface{}) error { @@ -53,3 +135,15 @@ func (o LoginBySms) TODO(v interface{}) error {
53 func (o LoginByXxxx) TODO(v interface{}) error { 135 func (o LoginByXxxx) TODO(v interface{}) error {
54 return nil 136 return nil
55 } 137 }
  138 +
  139 +func randomString(l int) string {
  140 + str := "0123456789abcdefghijklmnopqrstuvwxyz"
  141 + bytes := []byte(str)
  142 + result := []byte{}
  143 + r := rand.New(rand.NewSource(time.Now().UnixNano()))
  144 + for i := 0; i < l; i++ {
  145 + result = append(result, bytes[r.Intn(len(bytes))])
  146 + }
  147 + return string(result)
  148 +
  149 +}