作者 唐旭辉
@@ -14,4 +14,9 @@ yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5" @@ -14,4 +14,9 @@ yunpian_app_key ="0bf6fb10a11a68a95dee80901eb545b5"
14 #存储 http://ability.fjmaimaimai.com:8080/ 14 #存储 http://ability.fjmaimaimai.com:8080/
15 source_host ="http://192.168.139.131:8080/" 15 source_host ="http://192.168.139.131:8080/"
16 source_virtual_path=file/ab 16 source_virtual_path=file/ab
17 -source_path =/home/tiptok/www/ab  
  17 +source_path =/home/tiptok/www/ab
  18 +
  19 +#网易云信 IM
  20 +net_im_base_url ="https://api.netease.im/nimserver"
  21 +net_im_app_secret ="a8d231f5c13a"
  22 +net_im_app_key ="9c5410602597a7fe367aeeebd8210262"
@@ -45,8 +45,7 @@ func init() { @@ -45,8 +45,7 @@ func init() {
45 //Password:"ibfduqhfmgypbffe", //授权码 45 //Password:"ibfduqhfmgypbffe", //授权码
46 //IsUseSsl:true, 46 //IsUseSsl:true,
47 }) 47 })
48 - //TODO:服务配置  
49 - im.InitImClient("","","") 48 + im.InitImClient(beego.AppConfig.String("net_im_base_url"),beego.AppConfig.String("net_im_app_key"),beego.AppConfig.String("net_im_app_secret"))
50 } 49 }
51 50
52 func main() { 51 func main() {
@@ -171,3 +171,12 @@ func GetUsersByMobile(mobile string)(v *Users, err error) { @@ -171,3 +171,12 @@ func GetUsersByMobile(mobile string)(v *Users, err error) {
171 } 171 }
172 return nil, err 172 return nil, err
173 } 173 }
  174 +
  175 +func GetUsersKefu()(v []*Users,err error){
  176 + o := orm.NewOrm()
  177 + sql :=`select * from users where is_kefu = 1`
  178 + if _,err = o.Raw(sql).QueryRows(&v); err == nil {
  179 + return v, nil
  180 + }
  181 + return nil, err
  182 +}
@@ -5,6 +5,8 @@ import ( @@ -5,6 +5,8 @@ import (
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
7 "html/template" 7 "html/template"
  8 + "math/rand"
  9 + "strconv"
8 "strings" 10 "strings"
9 "time" 11 "time"
10 12
@@ -90,17 +92,19 @@ Success: @@ -90,17 +92,19 @@ Success:
90 if userInfo.Auth == "" { 92 if userInfo.Auth == "" {
91 userInfo.Auth = uid.NewV1().StringNoDash() 93 userInfo.Auth = uid.NewV1().StringNoDash()
92 } 94 }
93 - if checkImResponse, err = CheckIm(&protocol.CheckImRequest{  
94 - Uid: fmt.Sprintf("%v", userInfo.Id),  
95 - Uname: userInfo.Uname,  
96 - Icon: userInfo.Icon,  
97 - IsCreated: userInfo.ImToken == "",  
98 - }); err != nil { 95 + if checkImResponse,err=CheckIm(&protocol.CheckImRequest{
  96 + Uid:fmt.Sprintf("%v",userInfo.Id),
  97 + Uname:userInfo.Uname,
  98 + Icon:userInfo.Icon,
  99 + IsCreated:userInfo.ImToken!="",
  100 + });err!=nil{
99 return 101 return
100 } 102 }
101 - userInfo.ImToken = checkImResponse.ImToken  
102 - if userInfo.CsAccount == 0 {  
103 - userInfo.CsAccount = checkImResponse.CsAccount 103 + if checkImResponse!=nil && checkImResponse.ImToken!=""{
  104 + userInfo.ImToken = checkImResponse.ImToken
  105 + }
  106 + if userInfo.CsAccount==0{
  107 + userInfo.CsAccount = imGetRandomCSAccount()
104 } 108 }
105 userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire) 109 userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
106 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil { 110 if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
@@ -483,3 +487,22 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR @@ -483,3 +487,22 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR
483 rsp.ImToken = out.Info.Token 487 rsp.ImToken = out.Info.Token
484 return 488 return
485 } 489 }
  490 +
  491 +// 获取客服id
  492 +func imGetRandomCSAccount()( acid int64){
  493 + kefus,err :=models.GetUsersKefu()
  494 + if err!=nil{
  495 + log.Error(err)
  496 + return
  497 + }
  498 + if len(kefus)<=0{
  499 + return
  500 + }
  501 + index :=rand.Intn(len(kefus))
  502 + acid,err = strconv.ParseInt(kefus[index].Accid,10,64)
  503 + if err!=nil{
  504 + log.Error(err)
  505 + }
  506 + return acid
  507 +}
  508 +
@@ -71,4 +71,4 @@ func Test_RefreshToken(t *testing.T) { @@ -71,4 +71,4 @@ func Test_RefreshToken(t *testing.T) {
71 if rsp,err :=auth.RefreshToken(req);err!=nil{ 71 if rsp,err :=auth.RefreshToken(req);err!=nil{
72 t.Fatal(err,rsp) 72 t.Fatal(err,rsp)
73 } 73 }
74 -} 74 +}