示例#1
0
文件: user.go 项目: jackyxie/btcrobot
// 执行添加新用户(异步请求,返回json)
func AddUserHandler(rw http.ResponseWriter, req *http.Request) {
	// 入库
	errMsg, err := service.CreateUser(req.Form)
	if err != nil {
		fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
		return
	}
	fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}
示例#2
0
// 用户注册
// uri: /account/register{json:(|.json)}
func RegisterHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	username := req.FormValue("username")
	// 请求注册页面
	if username == "" || req.Method != "POST" || vars["json"] == "" {
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/register.html")
		return
	}

	// 入库
	errMsg, err := service.CreateUser(req.Form)
	if err != nil {
		fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
		return
	}
	// 注册成功,自动为其登录
	setCookie(rw, req, req.FormValue("username"))
	// 发送欢迎邮件
	go service.SendWelcomeMail([]string{req.FormValue("email")})
	fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}
示例#3
0
// 用户注册
// uri: /account/register{json:(|.json)}
func RegisterHandler(rw http.ResponseWriter, req *http.Request) {
	if _, ok := filter.CurrentUser(req); ok {
		util.Redirect(rw, req, "/")
		return
	}

	vars := mux.Vars(req)
	username := req.PostFormValue("username")
	// 请求注册页面
	if username == "" || req.Method != "POST" || vars["json"] == "" {
		filter.SetData(req, map[string]interface{}{"captchaId": captcha.NewLen(4)})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/register.html")
		return
	}

	// 校验验证码
	if !captcha.VerifyString(req.PostFormValue("captchaid"), req.PostFormValue("captchaSolution")) {
		fmt.Fprint(rw, `{"ok": 0, "error":"验证码错误"}`)
		return
	}

	// 入库
	errMsg, err := service.CreateUser(req.PostForm)
	if err != nil {
		// bugfix:http://studygolang.com/topics/255
		if errMsg == "" {
			errMsg = err.Error()
		}
		fmt.Fprint(rw, `{"ok": 0, "error":"`, errMsg, `"}`)
		return
	}

	// 注册成功,自动为其登录
	setCookie(rw, req, req.PostFormValue("username"))
	// 发送欢迎邮件
	go sendWelcomeMail([]string{req.PostFormValue("email")})
	fmt.Fprint(rw, `{"ok": 1, "msg":"注册成功"}`)
}
示例#4
0
// 执行添加新用户(异步请求,返回json)
func AddUserHandler(rw http.ResponseWriter, req *http.Request) {
	rules := map[string]map[string]map[string]string{
		"username": {
			"require": {"error": "用户名不能为空!"},
			"length":  {"range": "4,20", "error": "用户名长度必须在%d个字符和%d个字符之间"},
		},
		"email": {
			"require": {"error": "邮箱不能为空!"},
			"email":   {"error": "邮箱格式不正确!"},
		},
		"passwd": {
			"require": {"error": "密码不能为空!"},
			"length":  {"range": "6,32", "error": "密码长度必须在%d个字符和%d个字符之间"},
		},
		"pass2": {
			"require": {"error": "确认密码不能为空!"},
			"compare": {"field": "passwd", "rule": "=", "error": "两次密码不一致"},
		},
	}
	req.ParseForm()
	errMsg := util.Validate(req.Form, rules)

	header := rw.Header()
	header.Set("Content-Type", "application/json; charset=utf-8")
	if errMsg != "" {
		fmt.Fprint(rw, `{"errno": 1,"error":"`, errMsg, `"}`)
		return
	}

	// 入库
	errMsg, err := service.CreateUser(req.Form)
	if err != nil {
		fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
		return
	}
	fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}