sys_user.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package service
  2. import (
  3. "errors"
  4. "go-admin/app/admin/models"
  5. "go-admin/app/admin/service/dto"
  6. log "github.com/go-admin-team/go-admin-core/logger"
  7. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  8. "github.com/go-admin-team/go-admin-core/sdk/service"
  9. "gorm.io/gorm"
  10. "go-admin/common/actions"
  11. cDto "go-admin/common/dto"
  12. )
  13. type SysUser struct {
  14. service.Service
  15. }
  16. // GetPage 获取SysUser列表
  17. func (e *SysUser) GetPage(c *dto.SysUserGetPageReq, p *actions.DataPermission, list *[]models.SysUser, count *int64) error {
  18. var err error
  19. var data models.SysUser
  20. err = e.Orm.Debug().Preload("Dept").
  21. Scopes(
  22. cDto.MakeCondition(c.GetNeedSearch()),
  23. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  24. actions.Permission(data.TableName(), p),
  25. ).
  26. Find(list).Limit(-1).Offset(-1).
  27. Count(count).Error
  28. if err != nil {
  29. e.Log.Errorf("db error: %s", err)
  30. return err
  31. }
  32. return nil
  33. }
  34. // Get 获取SysUser对象
  35. func (e *SysUser) Get(d *dto.SysUserById, p *actions.DataPermission, model *models.SysUser) error {
  36. var data models.SysUser
  37. err := e.Orm.Model(&data).Debug().
  38. Scopes(
  39. actions.Permission(data.TableName(), p),
  40. ).
  41. First(model, d.GetId()).Error
  42. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  43. err = errors.New("查看对象不存在或无权查看")
  44. e.Log.Errorf("db error: %s", err)
  45. return err
  46. }
  47. if err != nil {
  48. e.Log.Errorf("db error: %s", err)
  49. return err
  50. }
  51. return nil
  52. }
  53. // Insert 创建SysUser对象
  54. func (e *SysUser) Insert(c *dto.SysUserInsertReq) error {
  55. var err error
  56. var data models.SysUser
  57. var i int64
  58. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  59. if err != nil {
  60. e.Log.Errorf("db error: %s", err)
  61. return err
  62. }
  63. if i > 0 {
  64. err := errors.New("用户名已存在!")
  65. e.Log.Errorf("db error: %s", err)
  66. return err
  67. }
  68. c.Generate(&data)
  69. err = e.Orm.Create(&data).Error
  70. if err != nil {
  71. e.Log.Errorf("db error: %s", err)
  72. return err
  73. }
  74. return nil
  75. }
  76. // Update 修改SysUser对象
  77. func (e *SysUser) Update(c *dto.SysUserUpdateReq, p *actions.DataPermission) error {
  78. var err error
  79. var model models.SysUser
  80. db := e.Orm.Scopes(
  81. actions.Permission(model.TableName(), p),
  82. ).First(&model, c.GetId())
  83. if err = db.Error; err != nil {
  84. e.Log.Errorf("Service UpdateSysUser error: %s", err)
  85. return err
  86. }
  87. if db.RowsAffected == 0 {
  88. return errors.New("无权更新该数据")
  89. }
  90. c.Generate(&model)
  91. update := e.Orm.Model(&model).Where("user_id = ?", &model.UserId).Omit("password", "salt").Updates(&model)
  92. if err = update.Error; err != nil {
  93. e.Log.Errorf("db error: %s", err)
  94. return err
  95. }
  96. if update.RowsAffected == 0 {
  97. err = errors.New("update userinfo error")
  98. log.Warnf("db update error")
  99. return err
  100. }
  101. return nil
  102. }
  103. // UpdateAvatar 更新用户头像
  104. func (e *SysUser) UpdateAvatar(c *dto.UpdateSysUserAvatarReq, p *actions.DataPermission) error {
  105. var err error
  106. var model models.SysUser
  107. db := e.Orm.Scopes(
  108. actions.Permission(model.TableName(), p),
  109. ).First(&model, c.GetId())
  110. if err = db.Error; err != nil {
  111. e.Log.Errorf("Service UpdateSysUser error: %s", err)
  112. return err
  113. }
  114. if db.RowsAffected == 0 {
  115. return errors.New("无权更新该数据")
  116. }
  117. err = e.Orm.Table(model.TableName()).Where("user_id =? ", c.UserId).Updates(c).Error
  118. if err != nil {
  119. e.Log.Errorf("Service UpdateSysUser error: %s", err)
  120. return err
  121. }
  122. return nil
  123. }
  124. // UpdateStatus 更新用户状态
  125. func (e *SysUser) UpdateStatus(c *dto.UpdateSysUserStatusReq, p *actions.DataPermission) error {
  126. var err error
  127. var model models.SysUser
  128. db := e.Orm.Scopes(
  129. actions.Permission(model.TableName(), p),
  130. ).First(&model, c.GetId())
  131. if err = db.Error; err != nil {
  132. e.Log.Errorf("Service UpdateSysUser error: %s", err)
  133. return err
  134. }
  135. if db.RowsAffected == 0 {
  136. return errors.New("无权更新该数据")
  137. }
  138. err = e.Orm.Table(model.TableName()).Where("user_id =? ", c.UserId).Updates(c).Error
  139. if err != nil {
  140. e.Log.Errorf("Service UpdateSysUser error: %s", err)
  141. return err
  142. }
  143. return nil
  144. }
  145. // ResetPwd 重置用户密码
  146. func (e *SysUser) ResetPwd(c *dto.ResetSysUserPwdReq, p *actions.DataPermission) error {
  147. var err error
  148. var model models.SysUser
  149. db := e.Orm.Scopes(
  150. actions.Permission(model.TableName(), p),
  151. ).First(&model, c.GetId())
  152. if err = db.Error; err != nil {
  153. e.Log.Errorf("At Service ResetSysUserPwd error: %s", err)
  154. return err
  155. }
  156. if db.RowsAffected == 0 {
  157. return errors.New("无权更新该数据")
  158. }
  159. c.Generate(&model)
  160. err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&model).Error
  161. if err != nil {
  162. e.Log.Errorf("At Service ResetSysUserPwd error: %s", err)
  163. return err
  164. }
  165. return nil
  166. }
  167. // Remove 删除SysUser
  168. func (e *SysUser) Remove(c *dto.SysUserById, p *actions.DataPermission) error {
  169. var err error
  170. var data models.SysUser
  171. db := e.Orm.Model(&data).
  172. Scopes(
  173. actions.Permission(data.TableName(), p),
  174. ).Delete(&data, c.GetId())
  175. if err = db.Error; err != nil {
  176. e.Log.Errorf("Error found in RemoveSysUser : %s", err)
  177. return err
  178. }
  179. if db.RowsAffected == 0 {
  180. return errors.New("无权删除该数据")
  181. }
  182. return nil
  183. }
  184. // UpdatePwd 修改SysUser对象密码
  185. func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string, p *actions.DataPermission) error {
  186. var err error
  187. if newPassword == "" {
  188. return nil
  189. }
  190. c := &models.SysUser{}
  191. err = e.Orm.Model(c).
  192. Scopes(
  193. actions.Permission(c.TableName(), p),
  194. ).Select("UserId", "Password", "Salt").
  195. First(c, id).Error
  196. if err != nil {
  197. if errors.Is(err, gorm.ErrRecordNotFound) {
  198. return errors.New("无权更新该数据")
  199. }
  200. e.Log.Errorf("db error: %s", err)
  201. return err
  202. }
  203. var ok bool
  204. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  205. if err != nil {
  206. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  207. return err
  208. }
  209. if !ok {
  210. err = errors.New("incorrect Password")
  211. e.Log.Warnf("user[%d] %s", id, err.Error())
  212. return err
  213. }
  214. c.Password = newPassword
  215. db := e.Orm.Model(c).Where("user_id = ?", id).
  216. Select("Password", "Salt").
  217. Updates(c)
  218. if err = db.Error; err != nil {
  219. e.Log.Errorf("db error: %s", err)
  220. return err
  221. }
  222. if db.RowsAffected == 0 {
  223. err = errors.New("set password error")
  224. log.Warnf("db update error")
  225. return err
  226. }
  227. return nil
  228. }
  229. func (e *SysUser) GetProfile(c *dto.SysUserById, user *models.SysUser, roles *[]models.SysRole, posts *[]models.SysPost) error {
  230. err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
  231. if err != nil {
  232. return err
  233. }
  234. err = e.Orm.Find(roles, user.RoleId).Error
  235. if err != nil {
  236. return err
  237. }
  238. err = e.Orm.Find(posts, user.PostIds).Error
  239. if err != nil {
  240. return err
  241. }
  242. return nil
  243. }