sys_user.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package system
  2. import (
  3. "errors"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  8. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  9. uuid "github.com/satori/go.uuid"
  10. "gorm.io/gorm"
  11. )
  12. //@author: [piexlmax](https://github.com/piexlmax)
  13. //@function: Register
  14. //@description: 用户注册
  15. //@param: u model.SysUser
  16. //@return: err error, userInter model.SysUser
  17. type UserService struct {
  18. }
  19. func (userService *UserService) Register(u system.SysUser) (err error, userInter system.SysUser) {
  20. var user system.SysUser
  21. if !errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
  22. return errors.New("用户名已注册"), userInter
  23. }
  24. // 否则 附加uuid 密码md5简单加密 注册
  25. u.Password = utils.MD5V([]byte(u.Password))
  26. u.UUID = uuid.NewV4()
  27. err = global.GVA_DB.Create(&u).Error
  28. return err, u
  29. }
  30. //@author: [piexlmax](https://github.com/piexlmax)
  31. //@function: Login
  32. //@description: 用户登录
  33. //@param: u *model.SysUser
  34. //@return: err error, userInter *model.SysUser
  35. func (userService *UserService) Login(u *system.SysUser) (err error, userInter *system.SysUser) {
  36. var user system.SysUser
  37. u.Password = utils.MD5V([]byte(u.Password))
  38. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authorities").Preload("Authority").First(&user).Error
  39. return err, &user
  40. }
  41. //@author: [piexlmax](https://github.com/piexlmax)
  42. //@function: ChangePassword
  43. //@description: 修改用户密码
  44. //@param: u *model.SysUser, newPassword string
  45. //@return: err error, userInter *model.SysUser
  46. func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (err error, userInter *system.SysUser) {
  47. var user system.SysUser
  48. u.Password = utils.MD5V([]byte(u.Password))
  49. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  50. return err, u
  51. }
  52. //@author: [piexlmax](https://github.com/piexlmax)
  53. //@function: GetUserInfoList
  54. //@description: 分页获取数据
  55. //@param: info request.PageInfo
  56. //@return: err error, list interface{}, total int64
  57. func (userService *UserService) GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  58. limit := info.PageSize
  59. offset := info.PageSize * (info.Page - 1)
  60. db := global.GVA_DB.Model(&system.SysUser{})
  61. var userList []system.SysUser
  62. err = db.Count(&total).Error
  63. err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Find(&userList).Error
  64. return err, userList, total
  65. }
  66. func (userService *UserService) GetUserPInfoList(info systemReq.UserSearch) (err error, list interface{}, total int64) {
  67. limit := info.PageSize
  68. offset := info.PageSize * (info.Page - 1)
  69. db := global.GVA_DB.Model(&system.SysUser{})
  70. db.Where("id != 1")
  71. if info.AuthorityId != "" {
  72. db.Where("authority_id=?", info.AuthorityId)
  73. }
  74. var userList []system.SysUser
  75. err = db.Count(&total).Error
  76. err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Order("created_at desc").Find(&userList).Error
  77. return err, userList, total
  78. }
  79. //@author: [piexlmax](https://github.com/piexlmax)
  80. //@function: SetUserAuthority
  81. //@description: 设置一个用户的权限
  82. //@param: uuid uuid.UUID, authorityId string
  83. //@return: err error
  84. func (userService *UserService) SetUserAuthority(id uint, uuid uuid.UUID, authorityId string) (err error) {
  85. assignErr := global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUseAuthority{}).Error
  86. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  87. return errors.New("该用户无此角色")
  88. }
  89. err = global.GVA_DB.Where("uuid = ?", uuid).First(&system.SysUser{}).Update("authority_id", authorityId).Error
  90. return err
  91. }
  92. //@author: [piexlmax](https://github.com/piexlmax)
  93. //@function: SetUserAuthorities
  94. //@description: 设置一个用户的权限
  95. //@param: id uint, authorityIds []string
  96. //@return: err error
  97. func (userService *UserService) SetUserAuthorities(id uint, authorityIds []string) (err error) {
  98. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  99. TxErr := tx.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  100. if TxErr != nil {
  101. return TxErr
  102. }
  103. useAuthority := []system.SysUseAuthority{}
  104. for _, v := range authorityIds {
  105. useAuthority = append(useAuthority, system.SysUseAuthority{
  106. id, v,
  107. })
  108. }
  109. TxErr = tx.Create(&useAuthority).Error
  110. if TxErr != nil {
  111. return TxErr
  112. }
  113. // 返回 nil 提交事务
  114. return nil
  115. })
  116. }
  117. //@author: [piexlmax](https://github.com/piexlmax)
  118. //@function: DeleteUser
  119. //@description: 删除用户
  120. //@param: id float64
  121. //@return: err error
  122. func (userService *UserService) DeleteUser(id float64) (err error) {
  123. var user system.SysUser
  124. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  125. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  126. return err
  127. }
  128. //@author: [piexlmax](https://github.com/piexlmax)
  129. //@function: SetUserInfo
  130. //@description: 设置用户信息
  131. //@param: reqUser model.SysUser
  132. //@return: err error, user model.SysUser
  133. func (userService *UserService) SetUserInfo(reqUser system.SysUser) (err error, user system.SysUser) {
  134. err = global.GVA_DB.Updates(&reqUser).Error
  135. return err, reqUser
  136. }
  137. func (userService *UserService) SetUserName(reqUser system.SysUser) (err error, user system.SysUser) {
  138. err = global.GVA_DB.Where("id=?", reqUser.ID).Save(reqUser).Error
  139. return err, reqUser
  140. }
  141. //@author: [piexlmax](https://github.com/piexlmax)
  142. //@function: GetUserInfo
  143. //@description: 获取用户信息
  144. //@param: uuid uuid.UUID
  145. //@return: err error, user system.SysUser
  146. func (userService *UserService) GetUserInfo(uuid uuid.UUID) (err error, user system.SysUser) {
  147. var reqUser system.SysUser
  148. err = global.GVA_DB.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error
  149. return err, reqUser
  150. }
  151. func (userService *UserService) GetUserInfoByWechatId(wechatId string) (err error, user system.SysUser) {
  152. var reqUser system.SysUser
  153. err = global.GVA_DB.First(&reqUser, "wechat_id = ?", wechatId).Error
  154. return err, reqUser
  155. }
  156. func (userService *UserService) GetUserInfoByUnionId(unionId string) (err error, user system.SysUser) {
  157. var reqUser system.SysUser
  158. err = global.GVA_DB.First(&reqUser, "union_id = ?", unionId).Error
  159. return err, reqUser
  160. }
  161. //@author: [SliverHorn](https://github.com/SliverHorn)
  162. //@function: FindUserById
  163. //@description: 通过id获取用户信息
  164. //@param: id int
  165. //@return: err error, user *model.SysUser
  166. func (userService *UserService) FindUserById(id int) (err error, user system.SysUser) {
  167. var u system.SysUser
  168. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  169. return err, u
  170. }
  171. //@author: [SliverHorn](https://github.com/SliverHorn)
  172. //@function: FindUserByUuid
  173. //@description: 通过uuid获取用户信息
  174. //@param: uuid string
  175. //@return: err error, user *model.SysUser
  176. func (userService *UserService) FindUserByUuid(uuid string) (err error, user *system.SysUser) {
  177. var u system.SysUser
  178. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  179. return errors.New("用户不存在"), &u
  180. }
  181. return nil, &u
  182. }
  183. func (userService *UserService) FindUserByOpenId(openid string) (err error, user *system.SysUser) {
  184. var u system.SysUser
  185. if err = global.GVA_DB.Where("`open_id` = ?", openid).First(&u).Error; err != nil {
  186. return errors.New("用户不存在"), &u
  187. }
  188. return nil, &u
  189. }