sys_staff.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package system
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "go_server/base/core"
  6. "go_server/model/common/response"
  7. system2 "go_server/model/system"
  8. "go_server/service/base"
  9. "strings"
  10. )
  11. // 用户登陆时候获取基本信息
  12. func (s *UserService) Info(c *gin.Context) {
  13. userId := c.GetInt64("userId")
  14. if userId == 0 {
  15. response.Resp(c)
  16. return
  17. }
  18. user, ok := base.GetOne[system2.Administrator](s.DB(), "id", userId)
  19. if !ok {
  20. response.Resp(c, response.ResponseCodeFailure)
  21. return
  22. }
  23. role, ok := base.GetOne[system2.Role](s.DB(), "id", user.RoleId)
  24. if !ok {
  25. response.Resp(c, "角色信息不存在")
  26. return
  27. }
  28. res := make(map[string]interface{})
  29. res["avatar"] = user.Avatar
  30. res["nickName"] = user.Nickname
  31. res["roleName"] = role.Name
  32. sysMenus, err := system2.NewMenus().SysTree(s.DB())
  33. if err != nil {
  34. response.Resp(c, err.Error())
  35. return
  36. }
  37. res["sysMenus"] = sysMenus // 系统菜单
  38. // 前端菜单
  39. if role.Menus == "*" {
  40. res["menus"] = sysMenus // 系统菜单
  41. } else {
  42. menuIds := strings.Split(role.Menus, ",")
  43. menus, err := system2.NewMenus().GetUserTree(s.DB(), menuIds)
  44. if err != nil {
  45. response.Resp(c, "菜单信息不存在")
  46. return
  47. }
  48. res["menus"] = menus // 用户菜单
  49. }
  50. sysApis, err := system2.NewApis().SysTree(s.DB())
  51. if err != nil {
  52. response.Resp(c, "菜单信息不存在")
  53. return
  54. }
  55. res["sysApis"] = sysApis // 系统菜单
  56. // 个人api
  57. if role.Apis == "*" {
  58. res["apis"] = "*" // 开放全部菜单
  59. } else {
  60. apiIds := strings.Split(role.Apis, ",")
  61. apis, err := system2.NewApis().GetUserTree(s.DB(), apiIds)
  62. if err != nil {
  63. response.Resp(c, "菜单信息不存在")
  64. return
  65. }
  66. res["apis"] = apis // 用户api权限
  67. }
  68. response.Resp(c, res)
  69. }
  70. // 用户信息设置
  71. func (s *UserService) Set(c *gin.Context) {
  72. type request struct {
  73. Avatar string `json:"avatar"`
  74. Nickname string `json:"nickname"`
  75. Password string `json:"password"`
  76. }
  77. req := new(request)
  78. if err := c.BindJSON(req); err != nil {
  79. response.Resp(c, err.Error())
  80. return
  81. }
  82. userId := c.GetInt64("userId")
  83. user, ok := base.GetOne[system2.Administrator](s.DB(), "id", userId)
  84. if !ok {
  85. response.Resp(c, response.ResponseCodeFailure)
  86. return
  87. }
  88. if req.Avatar != "" {
  89. user.Avatar = req.Avatar
  90. }
  91. if req.Nickname != "" {
  92. user.Nickname = req.Nickname
  93. }
  94. if req.Password != "" {
  95. user.Password, user.Salt = user.EncodePassword(req.Password)
  96. }
  97. if err := s.DB().Model(&system2.Administrator{}).
  98. Where("id", userId).
  99. Updates(map[string]interface{}{
  100. "avatar": user.Avatar, // 头像设置
  101. "nickname": user.Nickname,
  102. "password": user.Password,
  103. "salt": user.Salt,
  104. }).Error; err != nil {
  105. response.Resp(c, response.ResponseCodeFailure)
  106. return
  107. }
  108. response.Resp(c, map[string]interface{}{
  109. "avatar": user.Avatar, // 头像设置
  110. "nickname": user.Nickname,
  111. })
  112. }
  113. // 获取谷歌密钥
  114. func (s *UserService) GetGoogleKey(c *gin.Context) {
  115. userId := c.GetInt64("userId")
  116. googleSecret := core.NewGoogleAuth().GetSecret()[:16]
  117. res := make(map[string]interface{})
  118. res["googleKey"] = googleSecret
  119. res["qrcode"] = core.NewGoogleAuth().GetQrcode(fmt.Sprintf("AmsAdmin-(%d)", userId),
  120. googleSecret)
  121. response.Resp(c, res)
  122. }
  123. // 设置/重置谷歌密钥
  124. func (s *UserService) ReplaceGoogleKey(c *gin.Context) {
  125. type request struct {
  126. GoogleKey string `json:"googleKey" validate:"required"`
  127. GoogleCode string `json:"googleCode" validate:"required"`
  128. OriginalGoogleCode string `json:"originalGoogleCode"`
  129. }
  130. req := new(request)
  131. if err := c.BindJSON(req); err != nil {
  132. response.Resp(c, response.ResponseCodeFailure)
  133. return
  134. }
  135. if req.GoogleCode == "" || req.GoogleKey == "" {
  136. response.Resp(c, response.ResponseCodeFailure)
  137. return
  138. }
  139. userId := c.GetInt64("userId")
  140. user, ok := base.GetOne[system2.Administrator](s.DB(), "id", userId)
  141. if !ok {
  142. response.Resp(c, response.ResponseCodeFailure)
  143. return
  144. }
  145. if user.GoogleKey != "" {
  146. if req.OriginalGoogleCode == "" {
  147. response.Resp(c, response.ResponseCodeFailure)
  148. return
  149. }
  150. if ok, _ := core.NewGoogleAuth().VerifyCode(user.GoogleKey, req.OriginalGoogleCode); !ok {
  151. response.Resp(c, "谷歌验证码错误")
  152. return
  153. }
  154. }
  155. // 保存谷歌秘钥
  156. if err := s.DB().Model(&system2.Administrator{}).
  157. Where("id", userId).
  158. Updates(system2.Administrator{
  159. GoogleKey: req.GoogleKey,
  160. }).Error; err != nil {
  161. response.Resp(c, response.ResponseCodeFailure)
  162. return
  163. }
  164. response.Resp(c)
  165. }
  166. // 取消谷歌验证码
  167. func (s *UserService) CancelGoogleKey(c *gin.Context) {
  168. type request struct {
  169. GoogleCode string `json:"googleCode" validate:"required"`
  170. }
  171. req := new(request)
  172. if err := c.BindJSON(req); err != nil {
  173. response.Resp(c, response.ResponseCodeFailure)
  174. return
  175. }
  176. userId := c.GetInt64("userId")
  177. user, ok := base.GetOne[system2.Administrator](s.DB(), "id", userId)
  178. if !ok {
  179. response.Resp(c, response.ResponseCodeFailure)
  180. return
  181. }
  182. if user.GoogleKey == "" {
  183. response.Resp(c, "未设置谷歌密钥")
  184. return
  185. }
  186. if req.GoogleCode == "" {
  187. response.Resp(c, "请输入谷歌验证码")
  188. return
  189. }
  190. if ok, _ := core.NewGoogleAuth().VerifyCode(user.GoogleKey, req.GoogleCode); !ok {
  191. response.Resp(c, "验证码错误")
  192. return
  193. }
  194. // 保存谷歌秘钥
  195. if err := s.DB().Model(&system2.Administrator{}).
  196. Where("id", userId).
  197. Updates(system2.Administrator{
  198. GoogleKey: "",
  199. }).Error; err != nil {
  200. response.Resp(c, response.ResponseCodeFailure)
  201. return
  202. }
  203. response.Resp(c)
  204. }