Exemple #1
0
func TestUpdateUser(t *testing.T) {
	phone := "18801234567"
	secret := "8428d916f8cca9ba5971bf58b34d38da20bc3dff"
	phone2 := "18801234568"

	// insert two
	user, err := models.CreateUserByPhone(&phone, secret)
	assert.Zero(t, err)
	user2, err := models.CreateUserByPhone(&phone2, secret)
	assert.Zero(t, err)

	weixin := "wx:piasy_umumu"
	user.WeiXin = &weixin
	user.Nickname = "Piasy"
	user.Gender = 1
	err = models.UpdateUser(user)
	assert.Zero(t, err)

	got, err := models.GetUserByToken(user.Token)
	assert.Zero(t, err)
	assertUserEquals(t, user, got)

	user.Phone = &phone2
	err = models.UpdateUser(user)
	assert.Equal(t, utils.ERROR_CODE_SYSTEM_ERROR, err)

	deleteUser(t, user.Id)
	deleteUser(t, user2.Id)
}
Exemple #2
0
// @Title UpdateUserInfo
// @Description 修改用户自己的信息, 修改哪些字段就传哪些字段, 成功后返回所有字段, <br/>注意: N位中英数限制, 指只能有汉字/英文字母/阿拉伯数字, 不能有标点符号, 特殊符号, 每个汉字/字母/数字长度都算1
// @Param	token		query 	string	true		"Token"
// @Param	phone		query 	string	false		"更换绑定手机号"
// @Param	code		query 	string	false		"手机验证码, 换绑手机号时需要"
// @Param	wx_openid		query 	string	false		"微信授权的openid"
// @Param	wx_token		query 	string	false		"微信授权的token"
// @Param	qq_openid		query 	string	false		"QQ授权的openid"
// @Param	qq_token		query 	string	false		"QQ授权的token"
// @Param	wb_token		query 	string	false		"微博授权的token"
// @Param	nickname		query 	string	false		"昵称, 小于12位中英数"
// @Param	gender		query 	int	false		"性别, 1为男, 2为女"
// @Param	avatar		query 	string	false		"头像url, 通过上传头像接口上传成功后获得"
// @Success 201 {object} models.User
// @Failure 401 token无效
// @Failure 403 参数错误:缺失或格式错误
// @Failure 500 系统错误
// @router / [patch]
func (this *UsersController) Patch() {
	token := this.GetString("token")
	phone := this.GetString("phone")
	code := this.GetString("code")
	wx_openid := this.GetString("wx_openid")
	wx_token := this.GetString("wx_token")
	qq_openid := this.GetString("qq_openid")
	qq_token := this.GetString("qq_token")
	wb_token := this.GetString("wb_token")
	nickname := this.GetString("nickname")
	gender, errGender := this.GetInt("gender", 0)
	avatar := this.GetString("avatar")
	if (phone != "" && !utils.IsValidPhone(phone)) || errGender != nil {
		// has phone, but invalid; parse gender/birthday error
		this.Ctx.ResponseWriter.WriteHeader(403)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
	} else if user, err := models.GetUserByToken(token); err > 0 {
		// invalid token
		this.Ctx.ResponseWriter.WriteHeader(401)
		this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
	} else {
		for {
			// has valid phone
			if phone != "" {
				if code == "" {
					// has valid phone but not valid code
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_VERIFY_CODE_MISMATCH, this.Ctx.Request.URL.String())
					break

				}
				if err = models.CheckVerifyCode(phone, code); err > 0 {
					// code mismatch
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// valid
				user.Phone = &phone
			}
			// has wx_openid
			if wx_openid != "" {
				if wx_token == "" {
					// empty wx_token
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
					break
				}
				authUser, err := utils.AuthWithWeiXin(wx_openid, wx_token)
				if err > 0 {
					// auth fail
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// verified
				user.WeiXin = &authUser.Openid
				user.WeiXinNickName = authUser.Nickname
			}
			// has wb_token
			if wb_token != "" {
				authUser, err := utils.AuthWithWeiBo(wb_token)
				if err > 0 {
					// auth fail
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// verified
				user.WeiBo = &authUser.Openid
				user.WeiBoNickName = authUser.Nickname
			}
			// has qq_openid
			if qq_openid != "" {
				if qq_token == "" {
					// empty qq_token
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
					break
				}
				authUser, err := utils.AuthWithQQ(qq_openid, qq_token, QQ_OAUTH_CONSUMER_KEY)
				if err > 0 {
					// auth fail
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// verified
				user.QQ = &authUser.Openid
				user.QQNickName = authUser.Nickname
			}
			if nickname != "" {
				if !utils.IsLegalRestrictedStringWithLength(nickname, utils.USER_NICKNAME_MEX_LEN) {
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_INVALID_NICKNAME, this.Ctx.Request.URL.String())
					break
				}
				user.Nickname = nickname
			}
			if gender > 0 {
				if gender != 1 && gender != 2 {
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_INVALID_GENDER_VALUE, this.Ctx.Request.URL.String())
					break
				}
				user.Gender = gender
			}
			if avatar != "" {
				if len(avatar) > utils.USER_AVATAR_MEX_LEN {
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_INVALID_AVATAR, this.Ctx.Request.URL.String())
					break
				}
				user.Avatar = avatar
			}

			err = models.UpdateUser(user)
			if err > 0 {
				this.Ctx.ResponseWriter.WriteHeader(403)
				this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
				break
			}

			// success
			this.Ctx.ResponseWriter.WriteHeader(201)
			this.Data["json"] = user
			break
		}
	}
	this.ServeJSON()
}