作者 唐旭辉

Merge branch 'master' of http://gitlab.fjmaimaimai.com/mmm-go/ability

# Conflicts:
#	services/im/models.go
#	services/im/netease.go
... ... @@ -4,6 +4,7 @@ import (
"ability/controllers"
"ability/protocol"
_ "ability/routers"
"ability/services/im"
"github.com/astaxie/beego"
_ "github.com/go-sql-driver/mysql"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
... ... @@ -44,6 +45,8 @@ func init() {
//Password:"ibfduqhfmgypbffe", //授权码
//IsUseSsl:true,
})
//TODO:服务配置
im.InitImClient("","","")
}
func main() {
... ...
... ... @@ -101,3 +101,16 @@ type CheckUuidRequest struct {
}
type CheckUuidResponse struct {
}
/*CheckIm */
type CheckImRequest struct {
Uid string
Uname string
Icon string
IsCreated bool
}
type CheckImResponse struct {
ImToken string //net im token
CsAccount int64 //客服id
}
... ...
... ... @@ -13,6 +13,7 @@ import (
"ability/models"
"ability/protocol"
s_sms "ability/services/sms"
s_im "ability/services/im"
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
... ... @@ -51,6 +52,7 @@ func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.Login
user *models.Users
userInfo *models.UserInfo
result bool
checkImResponse *protocol.CheckImResponse
)
user, err = UserRepository.GetUsersByMobile(request.Phone)
if err != nil {
... ... @@ -87,7 +89,18 @@ Success:
if userInfo.Auth == "" {
userInfo.Auth = uid.NewV1().StringNoDash()
}
//TODO:更新
if checkImResponse,err=CheckIm(&protocol.CheckImRequest{
Uid:fmt.Sprintf("%v",userInfo.Id),
Uname:userInfo.Uname,
Icon:userInfo.Icon,
IsCreated:userInfo.ImToken=="",
});err!=nil{
return
}
userInfo.ImToken = checkImResponse.ImToken
if userInfo.CsAccount==0{
userInfo.CsAccount = checkImResponse.CsAccount
}
userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire)
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
return
... ... @@ -312,3 +325,96 @@ Fail:
}
return
}
func CheckIm(request *protocol.CheckImRequest)(rsp *protocol.CheckImResponse,err error){
var (
)
if beego.BConfig.RunMode != "prod" {
return
}
rsp =&protocol.CheckImResponse{}
if !request.IsCreated{
if err = imUserCreate(request,rsp);err!=nil{
return
}
}else{
if err = imUserInfoUpdate(request,rsp);err!=nil{
return
}
}
if err = imUserRefreshToken(request,rsp);err!=nil{
return
}
return
}
//create
func imUserCreate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
var(
param s_im.UserCreate=s_im.UserCreate{
Accid:request.Uid,
Name:request.Uname,
Icon:request.Icon,
}
resp []byte
out s_im.UserCreateResult
)
if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
return
}
if err = json.Unmarshal(resp,&out);err!=nil{
return
}
if out.Code!=200 || (out.Info.Accid!=request.Uid){
return s_im.ErrorFailCall
}
rsp.ImToken = out.Info.Token
return
}
//update user info
func imUserInfoUpdate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
var(
param s_im.UserUpdateUinfo=s_im.UserUpdateUinfo{
s_im.UserCreate{
Accid:request.Uid,
Name:request.Uname,
Icon:request.Icon,
},
}
resp []byte
out s_im.BaseResp
)
if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
return
}
if err = json.Unmarshal(resp,&out);err!=nil{
return
}
if out.Code!=200{
return s_im.ErrorFailCall
}
return
}
//refresh token
func imUserRefreshToken(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
var(
param s_im.UserRefreshToken=s_im.UserRefreshToken{
Accid:request.Uid,
}
resp []byte
out s_im.UserCreateResult
)
if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
return
}
if err = json.Unmarshal(resp,&out);err!=nil{
return
}
if out.Code!=200 || (out.Info.Accid!=request.Uid){
return s_im.ErrorFailCall
}
rsp.ImToken = out.Info.Token
return
}
... ...
... ... @@ -44,6 +44,7 @@ type UserCreate struct {
var (
_ ImParam = UserCreate{}
_ ImParam = UserUpdateUinfo{}
)
func (p UserCreate) Format() map[string]string {
... ... @@ -117,3 +118,29 @@ func (u UserUpdateUinfo) Format() map[string]string {
func (u UserUpdateUinfo) GetPath() string {
return "/user/refreshToken.action"
}
type UserRefreshToken struct {
Accid string //网易云通信ID,最大长度32字符
}
func (p UserRefreshToken) Format() map[string]string {
return map[string]string{
"accid": p.Accid,
}
}
func (p UserRefreshToken) GetPath() string {
return "/user/refreshToken.action"
}
type UserUpdateUinfo struct {
UserCreate
}
func (p UserUpdateUinfo) GetPath() string {
return "/user/updateUinfo.action"
}
... ...