作者 yangfu

net im

@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "ability/controllers" 4 "ability/controllers"
5 "ability/protocol" 5 "ability/protocol"
6 _ "ability/routers" 6 _ "ability/routers"
  7 + "ability/services/im"
7 "github.com/astaxie/beego" 8 "github.com/astaxie/beego"
8 _ "github.com/go-sql-driver/mysql" 9 _ "github.com/go-sql-driver/mysql"
9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
@@ -44,6 +45,8 @@ func init() { @@ -44,6 +45,8 @@ func init() {
44 //Password:"ibfduqhfmgypbffe", //授权码 45 //Password:"ibfduqhfmgypbffe", //授权码
45 //IsUseSsl:true, 46 //IsUseSsl:true,
46 }) 47 })
  48 + //TODO:服务配置
  49 + im.InitImClient("","","")
47 } 50 }
48 51
49 func main() { 52 func main() {
@@ -101,3 +101,16 @@ type CheckUuidRequest struct { @@ -101,3 +101,16 @@ type CheckUuidRequest struct {
101 } 101 }
102 type CheckUuidResponse struct { 102 type CheckUuidResponse struct {
103 } 103 }
  104 +
  105 +
  106 +/*CheckIm */
  107 +type CheckImRequest struct {
  108 + Uid string
  109 + Uname string
  110 + Icon string
  111 + IsCreated bool
  112 +}
  113 +type CheckImResponse struct {
  114 + ImToken string //net im token
  115 + CsAccount int64 //客服id
  116 +}
@@ -13,6 +13,7 @@ import ( @@ -13,6 +13,7 @@ import (
13 "ability/models" 13 "ability/models"
14 "ability/protocol" 14 "ability/protocol"
15 s_sms "ability/services/sms" 15 s_sms "ability/services/sms"
  16 + s_im "ability/services/im"
16 17
17 "github.com/astaxie/beego" 18 "github.com/astaxie/beego"
18 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 19 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
@@ -51,6 +52,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login @@ -51,6 +52,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
51 user *models.Users 52 user *models.Users
52 userInfo *models.UserInfo 53 userInfo *models.UserInfo
53 result bool 54 result bool
  55 + checkImResponse *protocol.CheckImResponse
54 ) 56 )
55 user, err = UserRepository.GetUsersByMobile(request.Phone) 57 user, err = UserRepository.GetUsersByMobile(request.Phone)
56 if err != nil { 58 if err != nil {
@@ -87,7 +89,18 @@ Success: @@ -87,7 +89,18 @@ Success:
87 if userInfo.Auth == "" { 89 if userInfo.Auth == "" {
88 userInfo.Auth = uid.NewV1().StringNoDash() 90 userInfo.Auth = uid.NewV1().StringNoDash()
89 } 91 }
90 - //TODO:更新 92 + if checkImResponse,err=CheckIm(&protocol.CheckImRequest{
  93 + Uid:fmt.Sprintf("%v",userInfo.Id),
  94 + Uname:userInfo.Uname,
  95 + Icon:userInfo.Icon,
  96 + IsCreated:userInfo.ImToken=="",
  97 + });err!=nil{
  98 + return
  99 + }
  100 + userInfo.ImToken = checkImResponse.ImToken
  101 + if userInfo.CsAccount==0{
  102 + userInfo.CsAccount = checkImResponse.CsAccount
  103 + }
91 userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire) 104 userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire)
92 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil { 105 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
93 return 106 return
@@ -312,3 +325,96 @@ Fail: @@ -312,3 +325,96 @@ Fail:
312 } 325 }
313 return 326 return
314 } 327 }
  328 +
  329 +func CheckIm(request *protocol.CheckImRequest)(rsp *protocol.CheckImResponse,err error){
  330 + var (
  331 +
  332 + )
  333 + if beego.BConfig.RunMode != "prod" {
  334 + return
  335 + }
  336 + rsp =&protocol.CheckImResponse{}
  337 + if !request.IsCreated{
  338 + if err = imUserCreate(request,rsp);err!=nil{
  339 + return
  340 + }
  341 + }else{
  342 + if err = imUserInfoUpdate(request,rsp);err!=nil{
  343 + return
  344 + }
  345 + }
  346 + if err = imUserRefreshToken(request,rsp);err!=nil{
  347 + return
  348 + }
  349 + return
  350 +}
  351 +//create
  352 +func imUserCreate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
  353 + var(
  354 + param s_im.UserCreate=s_im.UserCreate{
  355 + Accid:request.Uid,
  356 + Name:request.Uname,
  357 + Icon:request.Icon,
  358 + }
  359 + resp []byte
  360 + out s_im.UserCreateResult
  361 + )
  362 + if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
  363 + return
  364 + }
  365 + if err = json.Unmarshal(resp,&out);err!=nil{
  366 + return
  367 + }
  368 + if out.Code!=200 || (out.Info.Accid!=request.Uid){
  369 + return s_im.ErrorFailCall
  370 + }
  371 + rsp.ImToken = out.Info.Token
  372 + return
  373 +}
  374 +//update user info
  375 +func imUserInfoUpdate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
  376 + var(
  377 + param s_im.UserUpdateUinfo=s_im.UserUpdateUinfo{
  378 + s_im.UserCreate{
  379 + Accid:request.Uid,
  380 + Name:request.Uname,
  381 + Icon:request.Icon,
  382 + },
  383 + }
  384 + resp []byte
  385 + out s_im.BaseResp
  386 + )
  387 + if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
  388 + return
  389 + }
  390 + if err = json.Unmarshal(resp,&out);err!=nil{
  391 + return
  392 + }
  393 + if out.Code!=200{
  394 + return s_im.ErrorFailCall
  395 + }
  396 + return
  397 +}
  398 +//refresh token
  399 +func imUserRefreshToken(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
  400 + var(
  401 + param s_im.UserRefreshToken=s_im.UserRefreshToken{
  402 + Accid:request.Uid,
  403 + }
  404 + resp []byte
  405 + out s_im.UserCreateResult
  406 + )
  407 + if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
  408 + return
  409 + }
  410 + if err = json.Unmarshal(resp,&out);err!=nil{
  411 + return
  412 + }
  413 + if out.Code!=200 || (out.Info.Accid!=request.Uid){
  414 + return s_im.ErrorFailCall
  415 + }
  416 + rsp.ImToken = out.Info.Token
  417 + return
  418 +}
  419 +
  420 +
@@ -44,6 +44,7 @@ type UserCreate struct { @@ -44,6 +44,7 @@ type UserCreate struct {
44 44
45 var ( 45 var (
46 _ ImParam = UserCreate{} 46 _ ImParam = UserCreate{}
  47 + _ ImParam = UserUpdateUinfo{}
47 ) 48 )
48 49
49 func (p UserCreate) Format() map[string]string { 50 func (p UserCreate) Format() map[string]string {
@@ -65,3 +66,29 @@ func (p UserCreate) Format() map[string]string { @@ -65,3 +66,29 @@ func (p UserCreate) Format() map[string]string {
65 func (p UserCreate) GetPath() string { 66 func (p UserCreate) GetPath() string {
66 return "/user/create.action" 67 return "/user/create.action"
67 } 68 }
  69 +
  70 +type UserRefreshToken struct {
  71 + Accid string //网易云通信ID,最大长度32字符
  72 +}
  73 +
  74 +func (p UserRefreshToken) Format() map[string]string {
  75 + return map[string]string{
  76 + "accid": p.Accid,
  77 + }
  78 +}
  79 +
  80 +func (p UserRefreshToken) GetPath() string {
  81 + return "/user/refreshToken.action"
  82 +}
  83 +
  84 +
  85 +type UserUpdateUinfo struct {
  86 + UserCreate
  87 +}
  88 +
  89 +func (p UserUpdateUinfo) GetPath() string {
  90 + return "/user/updateUinfo.action"
  91 +}
  92 +
  93 +
  94 +
@@ -3,6 +3,7 @@ package im @@ -3,6 +3,7 @@ package im
3 import ( 3 import (
4 "crypto/sha1" 4 "crypto/sha1"
5 "encoding/hex" 5 "encoding/hex"
  6 + "fmt"
6 "io/ioutil" 7 "io/ioutil"
7 "math/rand" 8 "math/rand"
8 "net/http" 9 "net/http"
@@ -12,6 +13,18 @@ import ( @@ -12,6 +13,18 @@ import (
12 "time" 13 "time"
13 ) 14 )
14 15
  16 +var DefaultImClient ImClient
  17 +
  18 +var ErrorFailCall = fmt.Errorf(" imclient call failed")
  19 +
  20 +func InitImClient(baseUrl ,appKey,appSecret string){
  21 + DefaultImClient = ImClient{
  22 + baseUrl:baseUrl,
  23 + appKey:appKey,
  24 + appSecret:appSecret,
  25 + }
  26 +}
  27 +
15 func buildCheckSum(appSecret string, nonce string, curTime string) string { 28 func buildCheckSum(appSecret string, nonce string, curTime string) string {
16 str := []byte(appSecret + nonce + curTime) 29 str := []byte(appSecret + nonce + curTime)
17 sh := sha1.New() 30 sh := sha1.New()