sys_user.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. if info.Username != "" {
  75. db.Where("username=?", info.Username)
  76. }
  77. if info.OpenId != "" {
  78. db.Where("open_id=?", info.OpenId)
  79. }
  80. if info.UnionId != "" {
  81. db.Where("union_id=?", info.UnionId)
  82. }
  83. if info.WechatId != "" {
  84. db.Where("wechat_id=?", info.WechatId)
  85. }
  86. var userList []system.SysUser
  87. err = db.Count(&total).Error
  88. err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Order("created_at desc").Find(&userList).Error
  89. return err, userList, total
  90. }
  91. //@author: [piexlmax](https://github.com/piexlmax)
  92. //@function: SetUserAuthority
  93. //@description: 设置一个用户的权限
  94. //@param: uuid uuid.UUID, authorityId string
  95. //@return: err error
  96. func (userService *UserService) SetUserAuthority(id uint, uuid uuid.UUID, authorityId string) (err error) {
  97. assignErr := global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUseAuthority{}).Error
  98. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  99. return errors.New("该用户无此角色")
  100. }
  101. err = global.GVA_DB.Where("uuid = ?", uuid).First(&system.SysUser{}).Update("authority_id", authorityId).Error
  102. return err
  103. }
  104. //@author: [piexlmax](https://github.com/piexlmax)
  105. //@function: SetUserAuthorities
  106. //@description: 设置一个用户的权限
  107. //@param: id uint, authorityIds []string
  108. //@return: err error
  109. func (userService *UserService) SetUserAuthorities(id uint, authorityIds []string) (err error) {
  110. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  111. TxErr := tx.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  112. if TxErr != nil {
  113. return TxErr
  114. }
  115. useAuthority := []system.SysUseAuthority{}
  116. for _, v := range authorityIds {
  117. useAuthority = append(useAuthority, system.SysUseAuthority{
  118. id, v,
  119. })
  120. }
  121. TxErr = tx.Create(&useAuthority).Error
  122. if TxErr != nil {
  123. return TxErr
  124. }
  125. // 返回 nil 提交事务
  126. return nil
  127. })
  128. }
  129. //@author: [piexlmax](https://github.com/piexlmax)
  130. //@function: DeleteUser
  131. //@description: 删除用户
  132. //@param: id float64
  133. //@return: err error
  134. func (userService *UserService) DeleteUser(id float64) (err error) {
  135. var user system.SysUser
  136. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  137. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  138. return err
  139. }
  140. //@author: [piexlmax](https://github.com/piexlmax)
  141. //@function: SetUserInfo
  142. //@description: 设置用户信息
  143. //@param: reqUser model.SysUser
  144. //@return: err error, user model.SysUser
  145. func (userService *UserService) SetUserInfo(reqUser system.SysUser) (err error, user system.SysUser) {
  146. err = global.GVA_DB.Updates(&reqUser).Error
  147. return err, reqUser
  148. }
  149. func (userService *UserService) SetUserName(reqUser system.SysUser) (err error, user system.SysUser) {
  150. err = global.GVA_DB.Where("id=?", reqUser.ID).Save(reqUser).Error
  151. return err, reqUser
  152. }
  153. //@author: [piexlmax](https://github.com/piexlmax)
  154. //@function: GetUserInfo
  155. //@description: 获取用户信息
  156. //@param: uuid uuid.UUID
  157. //@return: err error, user system.SysUser
  158. func (userService *UserService) GetUserInfo(uuid uuid.UUID) (err error, user system.SysUser) {
  159. var reqUser system.SysUser
  160. err = global.GVA_DB.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error
  161. return err, reqUser
  162. }
  163. func (userService *UserService) GetUserInfoByWechatId(wechatId string) (err error, user system.SysUser) {
  164. var reqUser system.SysUser
  165. err = global.GVA_DB.First(&reqUser, "wechat_id = ?", wechatId).Error
  166. return err, reqUser
  167. }
  168. func (userService *UserService) GetUserInfoByUnionId(unionId string) (err error, user system.SysUser) {
  169. var reqUser system.SysUser
  170. err = global.GVA_DB.First(&reqUser, "union_id = ?", unionId).Error
  171. return err, reqUser
  172. }
  173. //@author: [SliverHorn](https://github.com/SliverHorn)
  174. //@function: FindUserById
  175. //@description: 通过id获取用户信息
  176. //@param: id int
  177. //@return: err error, user *model.SysUser
  178. func (userService *UserService) FindUserById(id int) (err error, user system.SysUser) {
  179. var u system.SysUser
  180. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  181. return err, u
  182. }
  183. //@author: [SliverHorn](https://github.com/SliverHorn)
  184. //@function: FindUserByUuid
  185. //@description: 通过uuid获取用户信息
  186. //@param: uuid string
  187. //@return: err error, user *model.SysUser
  188. func (userService *UserService) FindUserByUuid(uuid string) (err error, user *system.SysUser) {
  189. var u system.SysUser
  190. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  191. return errors.New("用户不存在"), &u
  192. }
  193. return nil, &u
  194. }
  195. func (userService *UserService) FindUserByOpenId(openid string) (err error, user *system.SysUser) {
  196. var u system.SysUser
  197. if err = global.GVA_DB.Where("`open_id` = ?", openid).First(&u).Error; err != nil {
  198. return errors.New("用户不存在"), &u
  199. }
  200. return nil, &u
  201. }