package handler import ( "encoding/json" "github.com/gorilla/csrf" "math" "net/http" "picgo/corelib" "picgo/data" "picgo/model" "strconv" ) func UserPageHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { var ( err error user model.SysUser ) username := r.Context().Value("username").(string) if user, err = data.SysUserGetCacheByUsername(username); err != nil { http.Error(w, "IndexHandler SysUserGetCacheByUsername Error", http.StatusInternalServerError) return } tmpData := map[string]interface{}{ csrf.TemplateTag: csrf.TemplateField(r), } tmpData["Title"] = "用户管理" tmpData["Active"] = r.URL.Path tmpData["IsSuper"] = user.IsSuper w.Header().Add("X-CSRF-Token", csrf.Token(r)) corelib.TemplateHandler(w, tmpData, "layout.html", "view/layout.html", "view/user.html") } else { http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) return } } func UserCreateApiHandler(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var ( err error res model.UserCreateRequest user model.SysUser password string salt string ) if err = json.NewDecoder(r.Body).Decode(&res); err != nil { corelib.Logger.Error("UserCreateApiHandler, 参数获取失败") corelib.WriteJsonResponse(w, 400, "参数错误", nil) return } if (res.Username == "") || (res.Password == "") { corelib.Logger.Error("UserCreateApiHandler, 用户名或者密码为空") corelib.WriteJsonResponse(w, 400, "请输入用户名密码", nil) return } if isExists := data.SysUserExists(res.Username); !isExists { corelib.Logger.Error("UserCreateApiHandler, 用户已经存在") corelib.WriteJsonResponse(w, 10050, "用户已经存在", nil) return } salt = corelib.GenerateSalt() if password, err = corelib.HashPassword(res.Password, salt); err != nil { corelib.Logger.Error("UserCreateApiHandler, 生成密码失败") corelib.WriteJsonResponse(w, 10051, "生成密码失败", nil) return } user.Username = res.Username user.Password = password user.Salt = salt user.IsSuper = 0 if err = data.SysUserCreate(user); err != nil { corelib.Logger.Error("UserCreateApiHandler, 数据保存用户数据失败") corelib.WriteJsonResponse(w, 10052, "新建用户失败", nil) return } w.Header().Set("Content-Type", "application/json") corelib.WriteJsonResponse(w, 200, "用户创建成功", nil) } else { //http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) corelib.WriteJsonResponse(w, 405, "方法错误", nil) return } } // UserPageApiHandler 分页查询 func UserPageApiHandler(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var ( err error page int onePageCount int count int ) query := r.URL.Query() // 搜索条件 search := query.Get("search") // 每页显示多少跳 if onePageCount, err = strconv.Atoi(query.Get("count")); err != nil { corelib.Logger.Error("UserPageApiHandler, 获取count参数失败") corelib.WriteJsonResponse(w, 400, "参数错误", nil) return } // 第几页 if page, err = strconv.Atoi(query.Get("page")); err != nil { corelib.Logger.Error("UserPageApiHandler, 获取page参数失败") corelib.WriteJsonResponse(w, 400, "参数错误", nil) return } numPage := math.Ceil(float64(count) / float64(onePageCount)) paginationData := corelib.GetPaginationData(int(numPage), page, 2) if search != "" { } else { } corelib.Logger.Infoln("paginationData: ", paginationData) } else { corelib.WriteJsonResponse(w, 405, "方法错误", nil) return } } func pagination(page, onePageCount int, search string) { }