You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

65 lines
1.5 KiB

package logics
import (
"fmt"
"hudongzhuanjia/hdws/connect"
"hudongzhuanjia/libs/jwt"
)
func init() {
connect.N.RegisterLogic(connect.LogicLogin, loginFunc)
}
func loginFunc(c *connect.Client, msg *connect.Message) {
if _, ok := msg.Data["token"]; !ok {
c.Write([]byte("token不能为空"))
return
}
claims, err := jwt.ParseAccessToken(msg.Data["token"].(string))
if err != nil {
c.Write([]byte(err.Error()))
return
}
c.Online = true
c.Id = fmt.Sprintf("%s:%d", claims.AccountType, claims.AccountId)
c.AccountId = claims.AccountId
c.AccountType = claims.AccountType
c.Pid = claims.CustomerPid
if err := c.Register(); err != nil {
c.Write([]byte(err.Error()))
return
}
if claims.AccountType == connect.IDCustomer {
if c.Pid == 0 { // 主账号发给子账号
msg := &connect.Message{
Type: connect.TypeNotice,
Dest: 0,
Tag: connect.IDCustomer,
RoomId: c.RoomId,
From: c.Id,
Data: map[string]interface{}{
"content": "主账号上线",
"nickname": "",
"username": claims.Username,
},
}
c.Send(msg)
} else { // 子账号发给主账号
msg := &connect.Message{
Type: connect.TypeNotice,
Dest: claims.CustomerPid,
Tag: connect.IDCustomer,
RoomId: c.RoomId,
From: c.Id,
Data: map[string]interface{}{
"content": "子账号上线",
"nickname": "",
"username": claims.Username,
},
}
c.Send(msg)
}
}
c.Write([]byte("登录成功"))
}