permission.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package v1
  2. import (
  3. "crypto/rand"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/global"
  7. "designs/model"
  8. "encoding/json"
  9. "github.com/gin-gonic/gin"
  10. "math/big"
  11. "time"
  12. )
  13. func AdminList(c *gin.Context) {
  14. form := request.Check(c, &struct {
  15. Offset int `form:"offset" binding:""`
  16. Limit int `form:"limit" binding:""`
  17. Search string `form:"search" binding:""`
  18. }{})
  19. query := global.App.DB.Table("admin")
  20. if form.Search != "" {
  21. query = query.WhereRaw("account LIKE ?", "%"+form.Search+"%")
  22. }
  23. var admin []model.Admin
  24. type adminRes struct {
  25. ID int `json:"id" gorm:"not null;"`
  26. Account string `json:"account" gorm:"not null;"`
  27. Name string `json:"name" gorm:"not null;"`
  28. CreatedAt model.XTime `json:"createdAt" gorm:"column:createdAt;"`
  29. UpdatedAt model.XTime `json:"updatedAt" gorm:"column:updatedAt;"`
  30. Permission interface{} `json:"permission" gorm:""`
  31. }
  32. var count int64
  33. err := query.Count(&count).Error
  34. if err != nil {
  35. response.Fail(c, 501, err.Error())
  36. return
  37. }
  38. err = query.Offset(form.Offset).Select("id", "account", "name", "createdAt", "updatedAt", "permission").Limit(form.Limit).Scan(&admin).Error
  39. if err != nil {
  40. response.Fail(c, 501, err.Error())
  41. return
  42. }
  43. var res []adminRes
  44. for _, v := range admin {
  45. var PermissionSlice []string
  46. json.Unmarshal([]byte(v.Permission), &PermissionSlice)
  47. res = append(res, adminRes{
  48. ID: v.ID,
  49. Account: v.Account,
  50. Name: v.Name,
  51. CreatedAt: v.CreatedAt,
  52. UpdatedAt: v.UpdatedAt,
  53. Permission: PermissionSlice,
  54. })
  55. }
  56. response.Success(c, gin.H{"data": res, "count": count})
  57. }
  58. func RandomString(length int) (string, error) {
  59. const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
  60. b := make([]byte, length)
  61. for i := range b {
  62. n, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
  63. if err != nil {
  64. return "", err
  65. }
  66. b[i] = charset[n.Int64()]
  67. }
  68. return string(b), nil
  69. }
  70. func SetAdmin(c *gin.Context) {
  71. form := request.Check(c, &struct {
  72. Account string `form:"account" binding:"required"`
  73. Name string `form:"name" binding:"required"`
  74. //Password string `form:"password" binding:"required"`
  75. Permission []string `form:"permission" binding:"required"`
  76. }{})
  77. var admin model.Admin
  78. global.App.DB.WhereRaw("account = ?", form.Account).First(&admin)
  79. if admin.ID != 0 {
  80. response.Fail(c, 1001, "账户名称重复")
  81. return
  82. }
  83. password, _ := RandomString(10)
  84. now := model.XTime{Time: time.Now()}
  85. admin.Account = form.Account
  86. admin.Password = password
  87. admin.Name = form.Name
  88. admin.CreatedAt = now
  89. admin.UpdatedAt = now
  90. p, _ := json.Marshal(form.Permission)
  91. admin.Permission = string(p)
  92. err := global.App.DB.Table("admin").Create(&admin).Error
  93. if err != nil {
  94. response.Fail(c, 502, err.Error())
  95. return
  96. }
  97. response.Success(c, gin.H{
  98. "data": map[string]interface{}{
  99. "password": password,
  100. },
  101. })
  102. }
  103. func DeleteAdmin(c *gin.Context) {
  104. form := request.Check(c, &struct {
  105. AdminId int `form:"adminId" binding:"required"`
  106. }{})
  107. if form.AdminId == 1 {
  108. response.Fail(c, 501, "默认管理员无法删除")
  109. return
  110. }
  111. var d interface{}
  112. err := global.App.DB.Table("admin").Where("id", form.AdminId).Delete(d).Error
  113. if err != nil {
  114. response.Fail(c, 502, err.Error())
  115. return
  116. }
  117. response.Success(c, gin.H{})
  118. }
  119. func UpdateAdmin(c *gin.Context) {
  120. form := request.Check(c, &struct {
  121. AdminId int `form:"adminId" binding:"required"`
  122. Name string `form:"name" binding:"required"`
  123. Permission []string `form:"permission" binding:""`
  124. Password string `form:"password" binding:""`
  125. }{})
  126. update := make(map[string]interface{})
  127. if form.AdminId == 1 {
  128. response.Fail(c, 501, "默认管理员无法编辑")
  129. return
  130. }
  131. if form.Permission != nil {
  132. p, _ := json.Marshal(form.Permission)
  133. update["permission"] = string(p)
  134. }
  135. if form.Password != "" {
  136. update["password"] = form.Password
  137. }
  138. if form.Name != "" {
  139. update["name"] = form.Name
  140. }
  141. if len(form.Permission) == 0 {
  142. response.Fail(c, 501, "没有更新")
  143. return
  144. }
  145. update["updatedAt"] = time.Now()
  146. err := global.App.DB.Table("admin").Where("id", form.AdminId).Updates(update).Error
  147. if err != nil {
  148. response.Fail(c, 502, "更新失败"+err.Error())
  149. return
  150. }
  151. response.Success(c, gin.H{})
  152. }