admin.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**********************************************
  2. ** @Des: 管理员
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-16 14:17:37
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-17 11:14:07
  7. ***********************************************/
  8. package backgroundc
  9. import (
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/astaxie/beego"
  15. "wuyebaoxiuapi/libs"
  16. "wuyebaoxiuapi/models/backgroundm"
  17. )
  18. type AdminController struct {
  19. BaseController
  20. }
  21. func (self *AdminController) List() {
  22. self.Data["pageTitle"] = "管理员管理"
  23. self.display()
  24. //self.TplName = "admin/list.html"
  25. }
  26. func (self *AdminController) Add() {
  27. self.Data["pageTitle"] = "新增管理员"
  28. // 角色
  29. filters := make([]interface{}, 0)
  30. filters = append(filters, "status", 1)
  31. result, _ := backgroundm.RoleGetList(1, 1000, filters...)
  32. list := make([]map[string]interface{}, len(result))
  33. for k, v := range result {
  34. row := make(map[string]interface{})
  35. row["id"] = v.Id
  36. row["role_name"] = v.RoleName
  37. list[k] = row
  38. }
  39. self.Data["role"] = list
  40. self.display()
  41. }
  42. func (self *AdminController) Edit() {
  43. self.Data["pageTitle"] = "编辑管理员"
  44. id, _ := self.GetInt("id", 0)
  45. Admin, _ := backgroundm.AdminGetById(id)
  46. row := make(map[string]interface{})
  47. row["id"] = Admin.Id
  48. row["login_name"] = Admin.LoginName
  49. row["real_name"] = Admin.RealName
  50. row["phone"] = Admin.Phone
  51. row["email"] = Admin.Email
  52. row["role_ids"] = Admin.RoleIds
  53. self.Data["admin"] = row
  54. role_ids := strings.Split(Admin.RoleIds, ",")
  55. filters := make([]interface{}, 0)
  56. filters = append(filters, "status", 1)
  57. result, _ := backgroundm.RoleGetList(1, 1000, filters...)
  58. list := make([]map[string]interface{}, len(result))
  59. for k, v := range result {
  60. row := make(map[string]interface{})
  61. row["checked"] = 0
  62. for i := 0; i < len(role_ids); i++ {
  63. role_id, _ := strconv.Atoi(role_ids[i])
  64. if role_id == v.Id {
  65. row["checked"] = 1
  66. }
  67. fmt.Println(role_ids[i])
  68. }
  69. row["id"] = v.Id
  70. row["role_name"] = v.RoleName
  71. list[k] = row
  72. }
  73. self.Data["role"] = list
  74. self.display()
  75. }
  76. func (self *AdminController) AjaxSave() {
  77. Admin_id, _ := self.GetInt("id")
  78. if Admin_id == 0 {
  79. Admin := new(backgroundm.Admin)
  80. Admin.LoginName = strings.TrimSpace(self.GetString("login_name"))
  81. Admin.RealName = strings.TrimSpace(self.GetString("real_name"))
  82. Admin.Phone = strings.TrimSpace(self.GetString("phone"))
  83. Admin.Email = strings.TrimSpace(self.GetString("email"))
  84. Admin.RoleIds = strings.TrimSpace(self.GetString("roleids"))
  85. Admin.UpdateTime = time.Now().Unix()
  86. Admin.UpdateId = self.userId
  87. Admin.Status = 1
  88. // 检查登录名是否已经存在
  89. _, err := backgroundm.AdminGetByName(Admin.LoginName)
  90. if err == nil {
  91. self.ajaxMsg("登录名已经存在", MSG_ERR)
  92. }
  93. //新增
  94. pwd, salt := libs.Password(4, "")
  95. Admin.Password = pwd
  96. Admin.Salt = salt
  97. Admin.CreateTime = time.Now().Unix()
  98. Admin.CreateId = self.userId
  99. if _, err := backgroundm.AdminAdd(Admin); err != nil {
  100. self.ajaxMsg(err.Error(), MSG_ERR)
  101. }
  102. self.ajaxMsg("", MSG_OK)
  103. }
  104. Admin, _ := backgroundm.AdminGetById(Admin_id)
  105. //修改
  106. Admin.Id = Admin_id
  107. Admin.UpdateTime = time.Now().Unix()
  108. Admin.UpdateId = self.userId
  109. Admin.LoginName = strings.TrimSpace(self.GetString("login_name"))
  110. Admin.RealName = strings.TrimSpace(self.GetString("real_name"))
  111. Admin.Phone = strings.TrimSpace(self.GetString("phone"))
  112. Admin.Email = strings.TrimSpace(self.GetString("email"))
  113. Admin.RoleIds = strings.TrimSpace(self.GetString("roleids"))
  114. Admin.UpdateTime = time.Now().Unix()
  115. Admin.UpdateId = self.userId
  116. Admin.Status = 1
  117. resetPwd, _ := self.GetInt("reset_pwd")
  118. if resetPwd == 1 {
  119. pwd, salt := libs.Password(4, "")
  120. Admin.Password = pwd
  121. Admin.Salt = salt
  122. }
  123. if err := Admin.Update(); err != nil {
  124. self.ajaxMsg(err.Error(), MSG_ERR)
  125. }
  126. self.ajaxMsg(strconv.Itoa(resetPwd), MSG_OK)
  127. }
  128. func (self *AdminController) AjaxDel() {
  129. Admin_id, _ := self.GetInt("id")
  130. status := strings.TrimSpace(self.GetString("status"))
  131. if Admin_id == 1 {
  132. self.ajaxMsg("超级管理员不允许操作", MSG_ERR)
  133. }
  134. Admin_status := 0
  135. if status == "enable" {
  136. Admin_status = 1
  137. }
  138. Admin, _ := backgroundm.AdminGetById(Admin_id)
  139. Admin.UpdateTime = time.Now().Unix()
  140. Admin.Status = Admin_status
  141. Admin.Id = Admin_id
  142. if err := Admin.Update(); err != nil {
  143. self.ajaxMsg(err.Error(), MSG_ERR)
  144. }
  145. self.ajaxMsg("操作成功", MSG_OK)
  146. }
  147. func (self *AdminController) Table() {
  148. //列表
  149. page, err := self.GetInt("page")
  150. if err != nil {
  151. page = 1
  152. }
  153. limit, err := self.GetInt("limit")
  154. if err != nil {
  155. limit = 30
  156. }
  157. realName := strings.TrimSpace(self.GetString("realName"))
  158. StatusText := make(map[int]string)
  159. StatusText[0] = "<font color='red'>禁用</font>"
  160. StatusText[1] = "正常"
  161. self.pageSize = limit
  162. //查询条件
  163. filters := make([]interface{}, 0)
  164. //
  165. if realName != "" {
  166. filters = append(filters, "real_name__icontains", realName)
  167. }
  168. result, count := backgroundm.AdminGetList(page, self.pageSize, filters...)
  169. list := make([]map[string]interface{}, len(result))
  170. for k, v := range result {
  171. row := make(map[string]interface{})
  172. row["id"] = v.Id
  173. row["login_name"] = v.LoginName
  174. row["real_name"] = v.RealName
  175. row["phone"] = v.Phone
  176. row["email"] = v.Email
  177. row["role_ids"] = v.RoleIds
  178. row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
  179. row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
  180. row["status"] = v.Status
  181. row["status_text"] = StatusText[v.Status]
  182. list[k] = row
  183. }
  184. self.ajaxList("成功", MSG_OK, count, list)
  185. }