syscfg_account.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "cnphper.com/model"
  12. )
  13. type SyscfgAccountListRet struct {
  14. Errno int `json:"errno"`
  15. Error string `json:"error"`
  16. Data []*model.AccountsRow `json:"data"`
  17. }
  18. type SyscfgAccountGetRet struct {
  19. Errno int `json:"errno"`
  20. Error string `json:"error"`
  21. Data *model.AccountsRow `json:"data"`
  22. }
  23. func syscfg_account(resp http.ResponseWriter, req *http.Request) {
  24. sess, ok := checkLogin(resp, req)
  25. if !ok {
  26. return
  27. }
  28. //视图输出
  29. files := []string{
  30. filepath.Join(Cfg.TmplDir, "syscfg", "account.tmpl"),
  31. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  32. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  33. }
  34. tmpl, err := template.New("account.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  35. if err != nil {
  36. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  37. } else {
  38. tmpl.Execute(resp, struct {
  39. Sess *Session
  40. Req *http.Request
  41. Title string
  42. }{
  43. sess,
  44. req,
  45. "账号管理",
  46. })
  47. }
  48. }
  49. func syscfg_account_list(resp http.ResponseWriter, req *http.Request) {
  50. _, ok := checkLogin(resp, req)
  51. if !ok {
  52. return
  53. }
  54. req.ParseForm()
  55. mdlAccounts := model.NewAccounts(Db)
  56. list, err := mdlAccounts.GetAll()
  57. if err != nil {
  58. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: err.Error()})
  59. resp.Write(json)
  60. } else {
  61. json, _ := json.Marshal(SyscfgAccountListRet{Errno: 0, Error: "", Data: list})
  62. resp.Write(json)
  63. }
  64. }
  65. func syscfg_account_get(resp http.ResponseWriter, req *http.Request) {
  66. _, ok := checkLogin(resp, req)
  67. if !ok {
  68. return
  69. }
  70. req.ParseForm()
  71. idStr := req.Form.Get("id")
  72. if idStr == "" {
  73. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
  74. resp.Write(json)
  75. return
  76. }
  77. id, err := strconv.Atoi(idStr)
  78. if err != nil {
  79. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
  80. resp.Write(json)
  81. return
  82. }
  83. mdlAccounts := model.NewAccounts(Db)
  84. item, err := mdlAccounts.Get(int64(id))
  85. if err != nil {
  86. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()})
  87. resp.Write(json)
  88. } else {
  89. json, _ := json.Marshal(SyscfgAccountGetRet{Errno: 0, Error: "", Data: item})
  90. resp.Write(json)
  91. }
  92. }
  93. func syscfg_account_set(resp http.ResponseWriter, req *http.Request) {
  94. _, ok := checkLogin(resp, req)
  95. if !ok {
  96. return
  97. }
  98. req.ParseForm()
  99. Id := req.PostForm.Get("Id")
  100. Account := req.PostForm.Get("Account")
  101. Name := req.PostForm.Get("Name")
  102. Password := req.PostForm.Get("Password")
  103. IsSuper := req.PostForm.Get("IsSuper")
  104. Disabled := req.PostForm.Get("Disabled")
  105. IdInt, err := strconv.Atoi(Id)
  106. if err != nil {
  107. IdInt = 0
  108. }
  109. if Account == "" {
  110. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "账号不能为空!"})
  111. resp.Write(json)
  112. return
  113. }
  114. reg := regexp.MustCompile(`^[A-Za-z]\w{1,19}$`)
  115. if !reg.MatchString(Account) {
  116. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "账号格式不正确!"})
  117. resp.Write(json)
  118. return
  119. }
  120. if Name == "" {
  121. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "姓名不能为空!"})
  122. resp.Write(json)
  123. return
  124. }
  125. if IdInt == 0 {
  126. if Password == "" {
  127. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "密码不能为空!"})
  128. resp.Write(json)
  129. return
  130. }
  131. if len(Password) < 6 {
  132. json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "密码长度不能小于6!"})
  133. resp.Write(json)
  134. return
  135. }
  136. }
  137. if IsSuper == "" {
  138. json, _ := json.Marshal(ErrorRet{Errno: 6, Error: "请选择是否管理员!"})
  139. resp.Write(json)
  140. return
  141. }
  142. if Disabled == "" {
  143. json, _ := json.Marshal(ErrorRet{Errno: 7, Error: "状态不能为空!"})
  144. resp.Write(json)
  145. return
  146. }
  147. IsSuperInt, err := strconv.Atoi(IsSuper)
  148. if err != nil {
  149. json, _ := json.Marshal(ErrorRet{Errno: 8, Error: err.Error()})
  150. resp.Write(json)
  151. return
  152. }
  153. DisabledInt, err := strconv.Atoi(Disabled)
  154. if err != nil {
  155. json, _ := json.Marshal(ErrorRet{Errno: 9, Error: err.Error()})
  156. resp.Write(json)
  157. return
  158. }
  159. newRow := model.AccountsRow{
  160. Id: int64(IdInt),
  161. Account: Account,
  162. Name: Name,
  163. Password: Password,
  164. IsSuper: IsSuperInt != 0,
  165. Disabled: DisabledInt != 0,
  166. }
  167. mdlAccounts := model.NewAccounts(Db)
  168. if IdInt > 0 {
  169. affected, err := mdlAccounts.Update(&newRow)
  170. if err != nil {
  171. json, _ := json.Marshal(ErrorRet{Errno: 10, Error: err.Error()})
  172. resp.Write(json)
  173. } else if affected > 0 {
  174. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  175. resp.Write(json)
  176. } else {
  177. json, _ := json.Marshal(ErrorRet{Errno: 11, Error: "更新失败!"})
  178. resp.Write(json)
  179. }
  180. } else {
  181. newRowId, err := mdlAccounts.Insert(&newRow)
  182. if err != nil {
  183. json, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()})
  184. resp.Write(json)
  185. } else if newRowId > 0 {
  186. json, _ := json.Marshal(SyscfgRedisAddRet{Errno: 0, Error: "", Data: newRowId})
  187. resp.Write(json)
  188. } else {
  189. json, _ := json.Marshal(ErrorRet{Errno: 23, Error: "新增失败!"})
  190. resp.Write(json)
  191. }
  192. }
  193. }
  194. func syscfg_account_del(resp http.ResponseWriter, req *http.Request) {
  195. _, ok := checkLogin(resp, req)
  196. if !ok {
  197. return
  198. }
  199. req.ParseForm()
  200. idStr := req.Form.Get("id")
  201. if idStr == "" {
  202. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
  203. resp.Write(json)
  204. return
  205. }
  206. id, err := strconv.Atoi(idStr)
  207. if err != nil {
  208. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
  209. resp.Write(json)
  210. return
  211. }
  212. mdlAccounts := model.NewAccounts(Db)
  213. affected, err := mdlAccounts.Delete(int64(id))
  214. if err != nil {
  215. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()})
  216. resp.Write(json)
  217. } else if affected > 0 {
  218. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  219. resp.Write(json)
  220. } else {
  221. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "操作失败!"})
  222. resp.Write(json)
  223. }
  224. }
  225. func syscfg_account_reset_pwd(resp http.ResponseWriter, req *http.Request) {
  226. _, ok := checkLogin(resp, req)
  227. if !ok {
  228. return
  229. }
  230. req.ParseForm()
  231. idStr := req.PostForm.Get("id")
  232. if idStr == "" {
  233. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
  234. resp.Write(json)
  235. return
  236. }
  237. id, err := strconv.Atoi(idStr)
  238. if err != nil {
  239. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
  240. resp.Write(json)
  241. return
  242. }
  243. password := req.PostForm.Get("password")
  244. if len(password) < 6 {
  245. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "密码长度不能小于6位!"})
  246. resp.Write(json)
  247. return
  248. }
  249. mdlAccounts := model.NewAccounts(Db)
  250. affected, err := mdlAccounts.UpdatePassword(int64(id), password)
  251. if err != nil {
  252. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: err.Error()})
  253. resp.Write(json)
  254. } else if affected > 0 {
  255. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  256. resp.Write(json)
  257. } else {
  258. json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "操作失败!"})
  259. resp.Write(json)
  260. }
  261. }