user_controller.go 6.5 KB

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