sys_user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package apis
  2. import (
  3. "go-admin/app/admin/models"
  4. "net/http"
  5. "github.com/gin-gonic/gin/binding"
  6. "golang.org/x/crypto/bcrypt"
  7. "github.com/gin-gonic/gin"
  8. "github.com/go-admin-team/go-admin-core/sdk/api"
  9. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  10. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  11. "github.com/google/uuid"
  12. "go-admin/app/admin/service"
  13. "go-admin/app/admin/service/dto"
  14. "go-admin/common/actions"
  15. )
  16. type SysUser struct {
  17. api.Api
  18. }
  19. // GetPage
  20. // @Summary 列表用户信息数据
  21. // @Description 获取JSON
  22. // @Tags 用户
  23. // @Param username query string false "username"
  24. // @Success 200 {string} {object} response.Response "{"code": 200, "data": [...]}"
  25. // @Router /api/v1/sys-user [get]
  26. // @Security Bearer
  27. func (e SysUser) GetPage(c *gin.Context) {
  28. s := service.SysUser{}
  29. req := dto.SysUserGetPageReq{}
  30. err := e.MakeContext(c).
  31. MakeOrm().
  32. Bind(&req).
  33. MakeService(&s.Service).
  34. Errors
  35. if err != nil {
  36. e.Logger.Error(err)
  37. e.Error(500, err, err.Error())
  38. return
  39. }
  40. //数据权限检查
  41. p := actions.GetPermissionFromContext(c)
  42. list := make([]models.SysUser, 0)
  43. var count int64
  44. err = s.GetPage(&req, p, &list, &count)
  45. if err != nil {
  46. e.Error(500, err, "查询失败")
  47. return
  48. }
  49. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  50. }
  51. // Get
  52. // @Summary 获取用户
  53. // @Description 获取JSON
  54. // @Tags 用户
  55. // @Param userId path int true "用户编码"
  56. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  57. // @Router /api/v1/sys-user/{userId} [get]
  58. // @Security Bearer
  59. func (e SysUser) Get(c *gin.Context) {
  60. s := service.SysUser{}
  61. req := dto.SysUserById{}
  62. err := e.MakeContext(c).
  63. MakeOrm().
  64. Bind(&req, nil).
  65. MakeService(&s.Service).
  66. Errors
  67. if err != nil {
  68. e.Logger.Error(err)
  69. e.Error(500, err, err.Error())
  70. return
  71. }
  72. var object models.SysUser
  73. //数据权限检查
  74. p := actions.GetPermissionFromContext(c)
  75. err = s.Get(&req, p, &object)
  76. if err != nil {
  77. e.Error(http.StatusUnprocessableEntity, err, "查询失败")
  78. return
  79. }
  80. e.OK(object, "查询成功")
  81. }
  82. // Insert
  83. // @Summary 创建用户
  84. // @Description 获取JSON
  85. // @Tags 用户
  86. // @Accept application/json
  87. // @Product application/json
  88. // @Param data body dto.SysUserInsertReq true "用户数据"
  89. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  90. // @Router /api/v1/sys-user [post]
  91. // @Security Bearer
  92. func (e SysUser) Insert(c *gin.Context) {
  93. s := service.SysUser{}
  94. req := dto.SysUserInsertReq{}
  95. err := e.MakeContext(c).
  96. MakeOrm().
  97. Bind(&req, binding.JSON).
  98. MakeService(&s.Service).
  99. Errors
  100. if err != nil {
  101. e.Logger.Error(err)
  102. e.Error(500, err, err.Error())
  103. return
  104. }
  105. // 设置创建人
  106. req.SetCreateBy(user.GetUserId(c))
  107. err = s.Insert(&req)
  108. if err != nil {
  109. e.Logger.Error(err)
  110. e.Error(500, err, err.Error())
  111. return
  112. }
  113. e.OK(req.GetId(), "创建成功")
  114. }
  115. // Update
  116. // @Summary 修改用户数据
  117. // @Description 获取JSON
  118. // @Tags 用户
  119. // @Accept application/json
  120. // @Product application/json
  121. // @Param data body dto.SysUserUpdateReq true "body"
  122. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  123. // @Router /api/v1/sys-user/{userId} [put]
  124. // @Security Bearer
  125. func (e SysUser) Update(c *gin.Context) {
  126. s := service.SysUser{}
  127. req := dto.SysUserUpdateReq{}
  128. err := e.MakeContext(c).
  129. MakeOrm().
  130. Bind(&req).
  131. MakeService(&s.Service).
  132. Errors
  133. if err != nil {
  134. e.Logger.Error(err)
  135. e.Error(500, err, err.Error())
  136. return
  137. }
  138. req.SetUpdateBy(user.GetUserId(c))
  139. //数据权限检查
  140. p := actions.GetPermissionFromContext(c)
  141. err = s.Update(&req, p)
  142. if err != nil {
  143. e.Logger.Error(err)
  144. return
  145. }
  146. e.OK(req.GetId(), "更新成功")
  147. }
  148. // Delete
  149. // @Summary 删除用户数据
  150. // @Description 删除数据
  151. // @Tags 用户
  152. // @Param userId path int true "userId"
  153. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  154. // @Router /api/v1/sys-user/{userId} [delete]
  155. // @Security Bearer
  156. func (e SysUser) Delete(c *gin.Context) {
  157. s := service.SysUser{}
  158. req := dto.SysUserById{}
  159. err := e.MakeContext(c).
  160. MakeOrm().
  161. Bind(&req, binding.JSON).
  162. MakeService(&s.Service).
  163. Errors
  164. if err != nil {
  165. e.Logger.Error(err)
  166. e.Error(500, err, err.Error())
  167. return
  168. }
  169. // 设置编辑人
  170. req.SetUpdateBy(user.GetUserId(c))
  171. // 数据权限检查
  172. p := actions.GetPermissionFromContext(c)
  173. err = s.Remove(&req, p)
  174. if err != nil {
  175. e.Logger.Error(err)
  176. return
  177. }
  178. e.OK(req.GetId(), "删除成功")
  179. }
  180. // InsetAvatar
  181. // @Summary 修改头像
  182. // @Description 获取JSON
  183. // @Tags 个人中心
  184. // @Accept multipart/form-data
  185. // @Param file formData file true "file"
  186. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  187. // @Router /api/v1/user/avatar [post]
  188. // @Security Bearer
  189. func (e SysUser) InsetAvatar(c *gin.Context) {
  190. s := service.SysUser{}
  191. req := dto.UpdateSysUserAvatarReq{}
  192. err := e.MakeContext(c).
  193. MakeOrm().
  194. MakeService(&s.Service).
  195. Errors
  196. if err != nil {
  197. e.Logger.Error(err)
  198. e.Error(500, err, err.Error())
  199. return
  200. }
  201. // 数据权限检查
  202. p := actions.GetPermissionFromContext(c)
  203. form, _ := c.MultipartForm()
  204. files := form.File["upload[]"]
  205. guid := uuid.New().String()
  206. filPath := "static/uploadfile/" + guid + ".jpg"
  207. for _, file := range files {
  208. e.Logger.Debugf("upload avatar file: %s", file.Filename)
  209. // 上传文件至指定目录
  210. err = c.SaveUploadedFile(file, filPath)
  211. if err != nil {
  212. e.Logger.Errorf("save file error, %s", err.Error())
  213. e.Error(500, err, "")
  214. return
  215. }
  216. }
  217. req.UserId = p.UserId
  218. req.Avatar = "/" + filPath
  219. err = s.UpdateAvatar(&req, p)
  220. if err != nil {
  221. e.Logger.Error(err)
  222. return
  223. }
  224. e.OK(filPath, "修改成功")
  225. }
  226. // UpdateStatus 修改用户状态
  227. // @Summary 修改用户状态
  228. // @Description 获取JSON
  229. // @Tags 用户
  230. // @Accept application/json
  231. // @Product application/json
  232. // @Param data body dto.UpdateSysUserStatusReq true "body"
  233. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  234. // @Router /api/v1/user/status [put]
  235. // @Security Bearer
  236. func (e SysUser) UpdateStatus(c *gin.Context) {
  237. s := service.SysUser{}
  238. req := dto.UpdateSysUserStatusReq{}
  239. err := e.MakeContext(c).
  240. MakeOrm().
  241. Bind(&req, binding.JSON, nil).
  242. MakeService(&s.Service).
  243. Errors
  244. if err != nil {
  245. e.Logger.Error(err)
  246. e.Error(500, err, err.Error())
  247. return
  248. }
  249. req.SetUpdateBy(user.GetUserId(c))
  250. //数据权限检查
  251. p := actions.GetPermissionFromContext(c)
  252. err = s.UpdateStatus(&req, p)
  253. if err != nil {
  254. e.Logger.Error(err)
  255. return
  256. }
  257. e.OK(req.GetId(), "更新成功")
  258. }
  259. // ResetPwd 重置用户密码
  260. // @Summary 重置用户密码
  261. // @Description 获取JSON
  262. // @Tags 用户
  263. // @Accept application/json
  264. // @Product application/json
  265. // @Param data body dto.ResetSysUserPwdReq true "body"
  266. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  267. // @Router /api/v1/user/pwd/reset [put]
  268. // @Security Bearer
  269. func (e SysUser) ResetPwd(c *gin.Context) {
  270. s := service.SysUser{}
  271. req := dto.ResetSysUserPwdReq{}
  272. err := e.MakeContext(c).
  273. MakeOrm().
  274. Bind(&req, binding.JSON).
  275. MakeService(&s.Service).
  276. Errors
  277. if err != nil {
  278. e.Logger.Error(err)
  279. e.Error(500, err, err.Error())
  280. return
  281. }
  282. req.SetUpdateBy(user.GetUserId(c))
  283. //数据权限检查
  284. p := actions.GetPermissionFromContext(c)
  285. err = s.ResetPwd(&req, p)
  286. if err != nil {
  287. e.Logger.Error(err)
  288. return
  289. }
  290. e.OK(req.GetId(), "更新成功")
  291. }
  292. // UpdatePwd
  293. // @Summary 修改密码
  294. // @Description 获取JSON
  295. // @Tags 用户
  296. // @Accept application/json
  297. // @Product application/json
  298. // @Param data body dto.PassWord true "body"
  299. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  300. // @Router /api/v1/user/pwd/set [put]
  301. // @Security Bearer
  302. func (e SysUser) UpdatePwd(c *gin.Context) {
  303. s := service.SysUser{}
  304. req := dto.PassWord{}
  305. err := e.MakeContext(c).
  306. MakeOrm().
  307. Bind(&req).
  308. MakeService(&s.Service).
  309. Errors
  310. if err != nil {
  311. e.Logger.Error(err)
  312. e.Error(500, err, err.Error())
  313. return
  314. }
  315. // 数据权限检查
  316. p := actions.GetPermissionFromContext(c)
  317. var hash []byte
  318. if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
  319. req.NewPassword = string(hash)
  320. }
  321. err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword, p)
  322. if err != nil {
  323. e.Logger.Error(err)
  324. e.Error(http.StatusForbidden, err, "密码修改失败")
  325. return
  326. }
  327. e.OK(nil, "密码修改成功")
  328. }
  329. // GetProfile
  330. // @Summary 获取个人中心用户
  331. // @Description 获取JSON
  332. // @Tags 个人中心
  333. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  334. // @Router /api/v1/user/profile [get]
  335. // @Security Bearer
  336. func (e SysUser) GetProfile(c *gin.Context) {
  337. s := service.SysUser{}
  338. req := dto.SysUserById{}
  339. err := e.MakeContext(c).
  340. MakeOrm().
  341. MakeService(&s.Service).
  342. Errors
  343. if err != nil {
  344. e.Logger.Error(err)
  345. e.Error(500, err, err.Error())
  346. return
  347. }
  348. req.Id = user.GetUserId(c)
  349. sysUser := models.SysUser{}
  350. roles := make([]models.SysRole, 0)
  351. posts := make([]models.SysPost, 0)
  352. err = s.GetProfile(&req, &sysUser, &roles, &posts)
  353. if err != nil {
  354. e.Logger.Errorf("get user profile error, %s", err.Error())
  355. e.Error(500, err, "获取用户信息失败")
  356. return
  357. }
  358. e.OK(gin.H{
  359. "user": sysUser,
  360. "roles": roles,
  361. "posts": posts,
  362. }, "查询成功")
  363. }
  364. // GetInfo
  365. // @Summary 获取个人信息
  366. // @Description 获取JSON
  367. // @Tags 个人中心
  368. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  369. // @Router /api/v1/getinfo [get]
  370. // @Security Bearer
  371. func (e SysUser) GetInfo(c *gin.Context) {
  372. req := dto.SysUserById{}
  373. s := service.SysUser{}
  374. r := service.SysRole{}
  375. err := e.MakeContext(c).
  376. MakeOrm().
  377. MakeService(&r.Service).
  378. MakeService(&s.Service).
  379. Errors
  380. if err != nil {
  381. e.Logger.Error(err)
  382. e.Error(500, err, err.Error())
  383. return
  384. }
  385. p := actions.GetPermissionFromContext(c)
  386. var roles = make([]string, 1)
  387. roles[0] = user.GetRoleName(c)
  388. var permissions = make([]string, 1)
  389. permissions[0] = "*:*:*"
  390. var buttons = make([]string, 1)
  391. buttons[0] = "*:*:*"
  392. var mp = make(map[string]interface{})
  393. mp["roles"] = roles
  394. if user.GetRoleName(c) == "admin" || user.GetRoleName(c) == "系统管理员" {
  395. mp["permissions"] = permissions
  396. mp["buttons"] = buttons
  397. } else {
  398. list, _ := r.GetById(user.GetRoleId(c))
  399. mp["permissions"] = list
  400. mp["buttons"] = list
  401. }
  402. sysUser := models.SysUser{}
  403. req.Id = user.GetUserId(c)
  404. err = s.Get(&req, p, &sysUser)
  405. if err != nil {
  406. e.Error(http.StatusUnauthorized, err, "登录失败")
  407. return
  408. }
  409. mp["introduction"] = " am a super administrator"
  410. mp["avatar"] = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
  411. if sysUser.Avatar != "" {
  412. mp["avatar"] = sysUser.Avatar
  413. }
  414. mp["userName"] = sysUser.NickName
  415. mp["userId"] = sysUser.UserId
  416. mp["deptId"] = sysUser.DeptId
  417. mp["name"] = sysUser.NickName
  418. mp["code"] = 200
  419. e.OK(mp, "")
  420. }