Exemplo n.º 1
0
/*
 用户注册
 101	账号不能为空
 102	密码不能为空
 103	用户名已存在
 104	昵称不能为空
 105	注册失败
*/
func register(resp http.ResponseWriter, account string, password string, nick string, avatar string) {
	if account == "" {
		resp.Write(common.NewIMResponseSimple(101, "账号不能为空", "").Encode())
	} else if password == "" {
		resp.Write(common.NewIMResponseSimple(102, "密码不能为空", "").Encode())
	} else if nick == "" {
		resp.Write(common.NewIMResponseSimple(103, "昵称不能为空", "").Encode())
	} else {
		num, err := model.CheckAccount(account)
		if err != nil {
			resp.Write(common.NewIMResponseSimple(103, err.Error(), "").Encode())
			return
		}
		if num > 0 {
			resp.Write(common.NewIMResponseSimple(104, "用户名已存在", "").Encode())
		} else {
			_, err := model.SaveUser(account, password, nick, avatar)
			if err != nil {
				resp.Write(common.NewIMResponseSimple(104, err.Error(), "").Encode())
				return
			}
			if num > 0 {
				resp.Write(common.NewIMResponseSimple(0, "注册成功", "").Encode())
			} else {
				resp.Write(common.NewIMResponseSimple(105, "注册失败", "").Encode())
			}
		}
	}
}
Exemplo n.º 2
0
// 登录主方法
func login(resp http.ResponseWriter, account string, password string, ip string) {
	if account == "" {
		resp.Write(common.NewIMResponseSimple(101, "账号不能为空", "").Encode())
	} else if password == "" {
		resp.Write(common.NewIMResponseSimple(102, "密码不能为空", "").Encode())
	} else {
		num, err := model.CheckAccount(account)
		if err != nil {
			resp.Write(common.NewIMResponseSimple(100, err.Error(), "").Encode())
			return
		}
		if num > 0 {
			user, err := model.LoginUser(account, password)
			if err != nil {
				resp.Write(common.NewIMResponseSimple(100, err.Error(), "").Encode())
				return
			}
			if !strings.EqualFold(user.Id, "") {
				token := uuid.New()
				if _, err := model.SaveLogin(user.Id, token, ip); err != nil {
					resp.Write(common.NewIMResponseSimple(100, err.Error(), "").Encode())
				} else {
					// returnData := make(map[string]string)
					// returnData["id"] = user.Id
					// returnData["nick"] = user.Nick
					// returnData["avatar"] = user.Avatar
					// returnData["status"] = user.Status
					// returnData["token"] = token //token uuid 带 横杠
					// returnData["sign"]=user.Sign
					user.Token = token
					resp.Write(common.NewIMResponseData(util.SetData("user", user), "LOGIN_RETURN").Encode())
				}
			} else {
				resp.Write(common.NewIMResponseSimple(104, "密码错误", "").Encode())
			}
		} else {
			resp.Write(common.NewIMResponseSimple(103, "账号不存在", "").Encode())
		}
	}
}