sys_user.go 7.9 KB

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