You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.2 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. "gin-vue-admin/utils"
  8. uuid "github.com/satori/go.uuid"
  9. )
  10. // @title Register
  11. // @description register, 用户注册
  12. // @auth (2020/04/05 20:22)
  13. // @param u model.SysUser
  14. // @return err error
  15. // @return userInter *SysUser
  16. func Register(u model.SysUser) (err error, userInter model.SysUser) {
  17. var user model.SysUser
  18. // 判断用户名是否注册
  19. notRegister := global.GVA_DB.Where("username = ?", u.Username).First(&user).RecordNotFound()
  20. // notRegister为false表明读取到了 不能注册
  21. if !notRegister {
  22. return errors.New("用户名已注册"), userInter
  23. } else {
  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. }
  29. return err, u
  30. }
  31. // @title Login
  32. // @description login, 用户登录
  33. // @auth (2020/04/05 20:22)
  34. // @param u *model.SysUser
  35. // @return err error
  36. // @return userInter *SysUser
  37. func Login(u *model.SysUser) (err error, userInter *model.SysUser) {
  38. var user model.SysUser
  39. u.Password = utils.MD5V([]byte(u.Password))
  40. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authority").First(&user).Error
  41. return err, &user
  42. }
  43. // @title ChangePassword
  44. // @description change the password of a certain user, 修改用户密码
  45. // @auth (2020/04/05 20:22)
  46. // @param u *model.SysUser
  47. // @param newPassword string
  48. // @return err error
  49. // @return userInter *SysUser
  50. func ChangePassword(u *model.SysUser, newPassword string) (err error, userInter *model.SysUser) {
  51. var user model.SysUser
  52. // TODO:后期修改jwt+password模式
  53. u.Password = utils.MD5V([]byte(u.Password))
  54. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  55. return err, u
  56. }
  57. // @title GetInfoList
  58. // @description get user list by pagination, 分页获取数据
  59. // @auth (2020/04/05 20:22)
  60. // @param info request.PageInfo
  61. // @return err error
  62. // @return list interface{}
  63. // @return total int
  64. func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int) {
  65. limit := info.PageSize
  66. offset := info.PageSize * (info.Page - 1)
  67. db := global.GVA_DB
  68. var userList []model.SysUser
  69. err = db.Find(&userList).Count(&total).Error
  70. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  71. return err, userList, total
  72. }
  73. // @title SetUserAuthority
  74. // @description set the authority of a certain user, 设置一个用户的权限
  75. // @auth (2020/04/05 20:22)
  76. // @param uuid UUID
  77. // @param authorityId string
  78. // @return err error
  79. func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  80. err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
  81. return err
  82. }
  83. // @title SetUserAuthority
  84. // @description set the authority of a certain user, 设置一个用户的权限
  85. // @auth (2020/04/05 20:22)
  86. // @param uuid UUID
  87. // @param authorityId string
  88. // @return err error
  89. func DeleteUser(id float64) (err error) {
  90. var user model.SysUser
  91. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  92. return err
  93. }
  94. // @title UploadHeaderImg
  95. // @description upload avatar, 用户头像上传更新地址
  96. // @auth (2020/04/05 20:22)
  97. // @param uuid UUID
  98. // @param filePath string
  99. // @return err error
  100. // @return userInter *SysUser
  101. func UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *model.SysUser) {
  102. var user model.SysUser
  103. err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  104. return err, &user
  105. }