package ws import ( "fmt" "hudongzhuanjia/models" "hudongzhuanjia/utils" "strconv" ) func init() { N.SetMiddleware(LogicLogin, LoginMid) } func LoginMid(c *Client, msg *Message) { var token string var ok bool if token, ok = msg.Data["token"].(string); !ok || token == "" { if signerId, ok := msg.Data["signer_id"]; ok { c.AccountId, _ = strconv.Atoi(signerId.(string)) } else { c.AccountId = utils.RandomInt(6) } c.Online = true c.AccountType = "guest" c.Id = fmt.Sprintf("%s:%d", c.AccountType, c.AccountId) } else { _type, id, err := models.ParseToken(token) if err != nil { c.WriteJson(map[string]interface{}{ "msg": "token失效", "code": 507, }) return } c.Online = true c.AccountId = id c.AccountType = _type c.Id = fmt.Sprintf("%s:%d", c.AccountType, c.AccountId) } if err := c.Register(); err != nil { _ = c.WriteJson(map[string]interface{}{ "msg": err.Error(), "code": 404, }) return } _ = c.WriteJson(map[string]interface{}{ "id": c.Id, "msg": "登录成功", "code": 200, }) }