123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- package main
- import (
- "encoding/json"
- "fmt"
- "html/template"
- "io"
- "net/http"
- "path/filepath"
- "regexp"
- "strconv"
- "cnphper.com/model"
- )
- type SyscfgAccountListRet struct {
- Errno int `json:"errno"`
- Error string `json:"error"`
- Data []*model.AccountsRow `json:"data"`
- }
- type SyscfgAccountGetRet struct {
- Errno int `json:"errno"`
- Error string `json:"error"`
- Data *model.AccountsRow `json:"data"`
- }
- func syscfg_account(resp http.ResponseWriter, req *http.Request) {
- sess, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- //视图输出
- files := []string{
- filepath.Join(Cfg.TmplDir, "syscfg", "account.tmpl"),
- filepath.Join(Cfg.TmplDir, "header.tmpl"),
- filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
- }
- tmpl, err := template.New("account.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
- if err != nil {
- io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
- } else {
- tmpl.Execute(resp, struct {
- Sess *Session
- Req *http.Request
- Title string
- }{
- sess,
- req,
- "账号管理",
- })
- }
- }
- func syscfg_account_list(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- req.ParseForm()
- mdlAccounts := model.NewAccounts(Db)
- list, err := mdlAccounts.GetAll()
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 1, Error: err.Error()})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(SyscfgAccountListRet{Errno: 0, Error: "", Data: list})
- resp.Write(json)
- }
- }
- func syscfg_account_get(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- req.ParseForm()
- idStr := req.Form.Get("id")
- if idStr == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
- resp.Write(json)
- return
- }
- id, err := strconv.Atoi(idStr)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
- resp.Write(json)
- return
- }
- mdlAccounts := model.NewAccounts(Db)
- item, err := mdlAccounts.Get(int64(id))
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(SyscfgAccountGetRet{Errno: 0, Error: "", Data: item})
- resp.Write(json)
- }
- }
- func syscfg_account_set(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- req.ParseForm()
- Id := req.PostForm.Get("Id")
- Account := req.PostForm.Get("Account")
- Name := req.PostForm.Get("Name")
- Password := req.PostForm.Get("Password")
- IsSuper := req.PostForm.Get("IsSuper")
- Disabled := req.PostForm.Get("Disabled")
- IdInt, err := strconv.Atoi(Id)
- if err != nil {
- IdInt = 0
- }
- if Account == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "账号不能为空!"})
- resp.Write(json)
- return
- }
- reg := regexp.MustCompile(`^[A-Za-z]\w{1,19}$`)
- if !reg.MatchString(Account) {
- json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "账号格式不正确!"})
- resp.Write(json)
- return
- }
- if Name == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "姓名不能为空!"})
- resp.Write(json)
- return
- }
- if IdInt == 0 {
- if Password == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "密码不能为空!"})
- resp.Write(json)
- return
- }
- if len(Password) < 6 {
- json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "密码长度不能小于6!"})
- resp.Write(json)
- return
- }
- }
- if IsSuper == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 6, Error: "请选择是否管理员!"})
- resp.Write(json)
- return
- }
- if Disabled == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 7, Error: "状态不能为空!"})
- resp.Write(json)
- return
- }
- IsSuperInt, err := strconv.Atoi(IsSuper)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 8, Error: err.Error()})
- resp.Write(json)
- return
- }
- DisabledInt, err := strconv.Atoi(Disabled)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 9, Error: err.Error()})
- resp.Write(json)
- return
- }
- newRow := model.AccountsRow{
- Id: int64(IdInt),
- Account: Account,
- Name: Name,
- Password: Password,
- IsSuper: IsSuperInt != 0,
- Disabled: DisabledInt != 0,
- }
- mdlAccounts := model.NewAccounts(Db)
- if IdInt > 0 {
- affected, err := mdlAccounts.Update(&newRow)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 10, Error: err.Error()})
- resp.Write(json)
- } else if affected > 0 {
- json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(ErrorRet{Errno: 11, Error: "更新失败!"})
- resp.Write(json)
- }
- } else {
- newRowId, err := mdlAccounts.Insert(&newRow)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()})
- resp.Write(json)
- } else if newRowId > 0 {
- json, _ := json.Marshal(SyscfgRedisAddRet{Errno: 0, Error: "", Data: newRowId})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(ErrorRet{Errno: 23, Error: "新增失败!"})
- resp.Write(json)
- }
- }
- }
- func syscfg_account_del(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- req.ParseForm()
- idStr := req.Form.Get("id")
- if idStr == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
- resp.Write(json)
- return
- }
- id, err := strconv.Atoi(idStr)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
- resp.Write(json)
- return
- }
- mdlAccounts := model.NewAccounts(Db)
- affected, err := mdlAccounts.Delete(int64(id))
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()})
- resp.Write(json)
- } else if affected > 0 {
- json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "操作失败!"})
- resp.Write(json)
- }
- }
- func syscfg_account_reset_pwd(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- req.ParseForm()
- idStr := req.PostForm.Get("id")
- if idStr == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
- resp.Write(json)
- return
- }
- id, err := strconv.Atoi(idStr)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
- resp.Write(json)
- return
- }
- password := req.PostForm.Get("password")
- if len(password) < 6 {
- json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "密码长度不能小于6位!"})
- resp.Write(json)
- return
- }
- mdlAccounts := model.NewAccounts(Db)
- affected, err := mdlAccounts.UpdatePassword(int64(id), password)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 4, Error: err.Error()})
- resp.Write(json)
- } else if affected > 0 {
- json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "操作失败!"})
- resp.Write(json)
- }
- }
|