Exemple #1
0
// 用户编辑个人信息
func AccountEditHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	username := req.FormValue("username")
	curUser, _ := filter.CurrentUser(req)
	if username == "" || req.Method != "POST" || vars["json"] == "" {
		// 获取用户信息
		user := service.FindUserByUsername(curUser["username"].(string))
		// 设置模板数据
		filter.SetData(req, map[string]interface{}{"activeUsers": "active", "user": user})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/edit.html")
		return
	}

	// 只能编辑自己的信息
	if username != curUser["username"].(string) {
		fmt.Fprint(rw, `{"errno": 1, "error": "非法请求"}`)
		return
	}

	// open传递过来的是“on”或没传递
	if req.FormValue("open") == "on" {
		req.Form.Set("open", "1")
	} else {
		req.Form.Set("open", "0")
	}
	// 更新个人信息
	errMsg, err := service.UpdateUser(req.Form)
	if err != nil {
		fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
		return
	}
	fmt.Fprint(rw, `{"errno": 0, "msg":"个人资料更新成功!"}`)
}
Exemple #2
0
// 用户编辑个人信息
func AccountEditHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	curUser, _ := filter.CurrentUser(req)
	if req.Method != "POST" || vars["json"] == "" {
		// 获取用户信息
		user := service.FindUserByUsername(curUser["username"].(string))
		// 设置模板数据
		filter.SetData(req, map[string]interface{}{"user": user, "default_avatars": service.DefaultAvatars})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/edit.html")
		return
	}

	req.PostForm.Set("username", curUser["username"].(string))

	if req.PostFormValue("open") != "1" {
		req.PostForm.Set("open", "0")
	}

	// 更新个人信息
	errMsg, err := service.UpdateUser(req.PostForm)
	if err != nil {
		fmt.Fprint(rw, `{"ok": 0, "error":"`, errMsg, `"}`)
		return
	}
	fmt.Fprint(rw, `{"ok": 1, "msg":"个人资料更新成功!"}`)
}