user_controller.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package controllers
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/service/system"
  5. "xps/viewmodels"
  6. )
  7. type UserController struct {
  8. Base
  9. Service system.UserService
  10. }
  11. func (c *UserController) BeforeActivation(b mvc.BeforeActivation) {
  12. b.Handle("GET", "/page", "GetPage" )
  13. b.Handle("GET", "/", "GetList" )
  14. b.Handle("GET", "/{groupId:int64}/{accountId:int64}", "GetById" )
  15. b.Handle("POST", "/add", "Create" )
  16. b.Handle("PUT", "/update", "Update" )
  17. b.Handle("PUT", "/updatePassword/{pwd: string}", "UpdatePassword" )
  18. b.Handle("PUT", "/resetPassword/{userId:int64}", "ResetPassword" )
  19. b.Handle("PUT", "/updateStatus/{userId:int64}/{status:int64}", "UpdateStatus" )
  20. b.Handle("DELETE", "/{accountId:int64}", "DeleteById" )
  21. b.Handle("GET", "/IsUserNameExist/{userName: string}", "IsUserNameExist" )
  22. b.Handle("GET", "/IsUserPhoneExist/{userPhone: string}", "IsUserPhoneExist")
  23. b.Handle("GET", "/IsStaffNoExist/{staffNo: string}", "IsStaffNoExist" )
  24. b.Handle("GET", "/IsEmailExist/{email: string}", "IsEmailExist" )
  25. b.Handle("GET", "/select", "SelectUser" )
  26. }
  27. func (c *UserController) GetPage()*JsonResult {
  28. params := c.buildParams()
  29. data, err := c.Service.GetPage(params)
  30. if err != nil {
  31. return ResultErr(err.Error(), nil)
  32. } else {
  33. return ResultOk("获取成功", data)
  34. }
  35. }
  36. func (c *UserController) GetList() *JsonResult {
  37. params := c.buildParams()
  38. data, err := c.Service.GetList(params)
  39. if err != nil {
  40. return ResultErr(err.Error(), nil)
  41. } else {
  42. return ResultOk("获取成功", data)
  43. }
  44. }
  45. func (c *UserController) GetById(groupId, accountId int64)*JsonResult {
  46. ocId := c.GetCurOcId()
  47. data, err := c.Service.GetById(ocId, groupId, accountId)
  48. if err != nil {
  49. return ResultErr(err.Error(), nil)
  50. } else {
  51. return ResultOk("获取成功", data)
  52. }
  53. }
  54. func (c *UserController) Create() *JsonResult {
  55. ocId := c.GetCurOcId()
  56. ocTypeId := c.GetOcTypeId()
  57. user := &viewmodels.UserInfo{}
  58. if err := c.Ctx.ReadJSON(user); err != nil {
  59. return ResultErr("读取数据失败", nil)
  60. }
  61. user.OcId = ocId
  62. if err := c.Service.Create(ocTypeId, user); err != nil {
  63. return ResultErr(err.Error(), nil)
  64. } else {
  65. return ResultOk("新增成功", nil)
  66. }
  67. }
  68. func (c *UserController) Update() *JsonResult {
  69. user := &viewmodels.UserInfo{}
  70. if err := c.Ctx.ReadJSON(user); err != nil {
  71. return ResultErr("读取数据失败", nil)
  72. }
  73. if err := c.Service.Update(user); err != nil {
  74. return ResultErr(err.Error(), nil)
  75. } else {
  76. return ResultOk("更新成功", nil)
  77. }
  78. }
  79. func (c *UserController) UpdatePassword(psw string)*JsonResult{
  80. ocId := c.GetCurOcId()
  81. userId := c.GetCurUserId()
  82. if err := c.Service.UpdatePassword(ocId, userId, psw); err != nil {
  83. return ResultErr(err.Error(), nil)
  84. } else {
  85. return ResultOk("密码更新成功", nil)
  86. }
  87. }
  88. func (c *UserController) ResetPassword(userId int64)*JsonResult{
  89. ocId := c.GetCurOcId()
  90. if err := c.Service.ResetPassword(ocId, userId); err != nil {
  91. return ResultErr(err.Error(), nil)
  92. } else {
  93. return ResultOk("密码重置成功", nil)
  94. }
  95. }
  96. func (c *UserController) UpdateStatus(userId, status int64)*JsonResult{
  97. ocId := c.GetCurOcId()
  98. if err := c.Service.ChangeStatus(ocId, userId, status); err != nil {
  99. return ResultErr(err.Error(), nil)
  100. } else {
  101. return ResultOk("状态变更成功", nil)
  102. }
  103. }
  104. func (c *UserController) DeleteById(accountId int64)*JsonResult {
  105. ocId := c.GetCurOcId()
  106. userId := c.GetCurUserId()
  107. m := make(map[string]interface{})
  108. m["ocId"] = ocId
  109. m["accountId"] = accountId
  110. m["deletedBy"] = userId
  111. if err := c.Service.Delete(m); err != nil {
  112. return ResultErr(err.Error(), nil)
  113. } else {
  114. return ResultOk("删除成功", nil)
  115. }
  116. }
  117. func (c *UserController) IsUserNameExist(userName string) *JsonResult {
  118. ocId := c.GetCurOcId()
  119. bExist, err := c.Service.IsUserNameExist(ocId, userName)
  120. if err != nil {
  121. return ResultErr(err.Error(), nil)
  122. }
  123. if bExist {
  124. return ResultErr("帐号名称已存在", nil)
  125. } else {
  126. return ResultOk("帐号名称不重复", nil)
  127. }
  128. }
  129. func (c *UserController) IsUserPhoneExist(userPhone string) *JsonResult {
  130. ocId := c.GetCurOcId()
  131. bExist, err := c.Service.IsUserPhoneExist(ocId, userPhone)
  132. if err != nil {
  133. return ResultErr(err.Error(), nil)
  134. }
  135. if bExist {
  136. return ResultErr("电话号码已注册", nil)
  137. } else {
  138. return ResultOk("电话号码无重复", nil)
  139. }
  140. }
  141. func (c *UserController) IsStaffNoExist(staffNo string) *JsonResult {
  142. ocId := c.GetCurOcId()
  143. bExist, err := c.Service.IsStaffNoExist(ocId, staffNo)
  144. if err != nil {
  145. return ResultErr(err.Error(), nil)
  146. }
  147. if bExist {
  148. return ResultErr("工号已存在", nil)
  149. } else {
  150. return ResultOk("工号无重复", nil)
  151. }
  152. }
  153. func (c *UserController) IsEmailExist(email string) *JsonResult {
  154. ocId := c.GetCurOcId()
  155. bExist, err := c.Service.IsEmailExist(ocId, email)
  156. if err != nil {
  157. return ResultErr(err.Error(), nil)
  158. }
  159. if bExist {
  160. return ResultErr("email已存在", nil)
  161. } else {
  162. return ResultOk("email无重复", nil)
  163. }
  164. }
  165. func (c *UserController) SelectUser() *JsonResult {
  166. ocId := c.GetCurOcId()
  167. data, err := c.Service.SelectUser(ocId)
  168. if err != nil {
  169. return ResultErr(err.Error(), nil)
  170. } else {
  171. return ResultOk("获取成功", data)
  172. }
  173. }