user.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package user
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/baiy/Cadmin-server-go/models"
  7. "github.com/baiy/Cadmin-server-go/models/menuRelate"
  8. "github.com/baiy/Cadmin-server-go/models/requestRelate"
  9. "github.com/baiy/Cadmin-server-go/models/userGroupRelate"
  10. "github.com/baiy/Cadmin-server-go/models/userRelate"
  11. "github.com/baiy/Cadmin-server-go/models/utils"
  12. "github.com/doug-martin/goqu/v9"
  13. )
  14. const (
  15. Enable = 1 // 启用
  16. Disable = 2 // 禁用
  17. )
  18. type Model struct {
  19. models.Model
  20. Username string `json:"username"`
  21. Password string `json:"-"`
  22. LastLoginIp string `json:"last_login_ip"`
  23. LastLoginTime *utils.Time `json:"last_login_time"`
  24. Status int `json:"status"`
  25. Description string `json:"description"`
  26. Phone string `json:"phone"`
  27. }
  28. func (m *Model) IsDisabled() bool {
  29. return m.Status == Disable
  30. }
  31. func (m *Model) LoginUpdate(ip string) {
  32. _, _ = models.Db.Update("admin_user").Where(goqu.Ex{
  33. "id": m.Id,
  34. }).Set(map[string]interface{}{
  35. "last_login_ip": ip,
  36. "last_login_time": time.Now().Format("2006-01-02 15:04:05"),
  37. }).Executor().Exec()
  38. }
  39. func (m Model) UserGroupIds() []int {
  40. return userRelate.GroupIds([]int{m.Id})
  41. }
  42. func (m Model) AuthIds() []int {
  43. return userGroupRelate.AuthIds(m.UserGroupIds())
  44. }
  45. func (m Model) MenuIds() []int {
  46. return menuRelate.MenuIds(m.AuthIds())
  47. }
  48. func (m Model) RequestIds() []int {
  49. return requestRelate.RequestIds(m.AuthIds())
  50. }
  51. func Add(username, password string, status int, description string, phone string) error {
  52. exist, _ := GetByUserName(username)
  53. if exist.Id > 0 {
  54. return errors.New(fmt.Sprintf("[%s] 用户已经存在", username))
  55. }
  56. phoneExist, _ := GetByPhone(phone)
  57. if phoneExist.Id > 0 {
  58. return errors.New(fmt.Sprintf("[%s] 手机号已经存在", phone))
  59. }
  60. _, err := models.Db.Insert("admin_user").Rows(
  61. goqu.Record{"username": username, "password": password, "status": status, "description": description, "phone": phone},
  62. ).Executor().Exec()
  63. return err
  64. }
  65. func Updata(id int, username, password string, status int, description string, phone string) error {
  66. exist, _ := GetByUserName(username)
  67. if exist.Id > 0 && exist.Id != id {
  68. return errors.New(fmt.Sprintf("[%s] 用户已经存在", username))
  69. }
  70. phoneExist, _ := GetByPhone(phone)
  71. if phoneExist.Id > 0 && phoneExist.Id != id {
  72. return errors.New(fmt.Sprintf("[%s] 手机号已经存在", phone))
  73. }
  74. record := goqu.Record{"username": username, "status": status, "description": description, "phone": phone}
  75. if password != "" {
  76. record["password"] = password
  77. }
  78. _, err := models.Db.Update("admin_user").Set(record).Where(goqu.Ex{
  79. "id": id,
  80. }).Executor().Exec()
  81. return err
  82. }
  83. func SelfUpdata(id int, username, password string) error {
  84. exist, _ := GetByUserName(username)
  85. if exist.Id > 0 && exist.Id != id {
  86. return errors.New(fmt.Sprintf("[%s] 用户已经存在", username))
  87. }
  88. record := goqu.Record{"username": username}
  89. if password != "" {
  90. record["password"] = password
  91. }
  92. _, err := models.Db.Update("admin_user").Set(record).Where(goqu.Ex{
  93. "id": id,
  94. }).Executor().Exec()
  95. return err
  96. }
  97. func Remove(id int) error {
  98. _, err := models.Db.Delete("admin_user").Where(goqu.Ex{
  99. "id": id,
  100. }).Executor().Exec()
  101. if err == nil {
  102. _ = userRelate.Remove(0, id)
  103. }
  104. return err
  105. }
  106. // 检查用户权限
  107. func CheckAuth(id int, requestId int) error {
  108. userGroupIds := userRelate.GroupIds([]int{id})
  109. if len(userGroupIds) == 0 {
  110. return errors.New("用户未分配用户组")
  111. }
  112. authIds := requestRelate.AuthIds([]int{requestId})
  113. if len(authIds) == 0 {
  114. return errors.New("请求未分配权限组")
  115. }
  116. if !userGroupRelate.Check(authIds, userGroupIds) {
  117. return errors.New("暂无权限")
  118. }
  119. return nil
  120. }
  121. func All() ([]*Model, error) {
  122. m := make([]*Model, 0)
  123. err := models.Db.From("admin_user").ScanStructs(&m)
  124. return m, err
  125. }
  126. func GetById(id int) (model *Model, err error) {
  127. model = new(Model)
  128. found, err := models.Db.From("admin_user").Where(goqu.Ex{
  129. "id": id,
  130. }).ScanStruct(model)
  131. if err == nil {
  132. if !found {
  133. err = errors.New("用户不存在")
  134. }
  135. }
  136. return
  137. }
  138. func GetByUserName(username string) (model *Model, err error) {
  139. model = new(Model)
  140. found, err := models.Db.From("admin_user").Where(goqu.Ex{
  141. "username": username,
  142. }).ScanStruct(model)
  143. if err == nil {
  144. if !found {
  145. err = errors.New("用户不存在")
  146. }
  147. }
  148. return
  149. }
  150. func GetByPhone(phone string) (model *Model, err error) {
  151. model = new(Model)
  152. found, err := models.Db.From("admin_user").Where(goqu.Ex{
  153. "phone": phone,
  154. }).ScanStruct(model)
  155. if err == nil {
  156. if !found {
  157. err = errors.New("手机号不存在")
  158. }
  159. }
  160. return
  161. }
  162. func GetLists(ids []int) ([]*Model, error) {
  163. model := make([]*Model, 0)
  164. if len(ids) == 0 {
  165. return model, nil
  166. }
  167. err := models.Db.From("admin_user").Where(goqu.Ex{
  168. "id": ids,
  169. }).ScanStructs(&model)
  170. return model, err
  171. }