sys_user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package controller
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service"
  5. "cold-delivery/app/admin/service/dto"
  6. "cold-delivery/common/actions"
  7. "cold-delivery/common/middleware/handler"
  8. "cold-delivery/conf"
  9. "errors"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "github.com/gin-gonic/gin/binding"
  13. "go.uber.org/zap"
  14. "gogs.baozhida.cn/zoie/OAuth-core/api"
  15. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  16. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  17. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  18. "gogs.baozhida.cn/zoie/OAuth-core/pkg/sms"
  19. "golang.org/x/crypto/bcrypt"
  20. "net/http"
  21. )
  22. // SysUser 定义用户控制器
  23. type SysUser struct {
  24. api.Api
  25. }
  26. // GetPage 获取系统用户列表
  27. // @Summary 获取系统用户列表
  28. // @Description 获取系统用户列表
  29. // @Tags 用户
  30. // @Param username query string false "用户名"
  31. // @Param status query string false "状态 1-停用 2-正常"
  32. // @Param pageSize query int false "页条数"
  33. // @Param page query int false "页码"
  34. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysUser}} "{"code": 200, "data": [...]}"
  35. // @Router /api/sys-user [get]
  36. // @Security Bearer
  37. func (e SysUser) GetPage(c *gin.Context) {
  38. s := service.SysUser{}
  39. req := dto.SysUserGetPageReq{}
  40. err := e.MakeContext(c).
  41. MakeOrm().
  42. Bind(&req, binding.Query).
  43. MakeService(&s.Service).
  44. Errors
  45. if err != nil {
  46. e.Logger.Error(err)
  47. e.Error(500, err, err.Error())
  48. return
  49. }
  50. //数据权限检查
  51. p := actions.GetPermissionFromContext(c)
  52. list := make([]model.SysUser, 0)
  53. var count int64
  54. err = s.GetPage(&req, p, &list, &count)
  55. if err != nil {
  56. e.Error(500, err, err.Error())
  57. return
  58. }
  59. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  60. }
  61. // Get 通过id获取用户
  62. // @Summary 通过id获取用户
  63. // @Description 通过id获取用户
  64. // @Tags 用户
  65. // @Param id path int true "用户id"
  66. // @Success 200 {object} response.Response{data=model.SysUser} "{"code": 200, "data": [...]}"
  67. // @Router /api/sys-user/{id} [get]
  68. // @Security Bearer
  69. func (e SysUser) Get(c *gin.Context) {
  70. s := service.SysUser{}
  71. req := dto.SysUserGetReq{}
  72. err := e.MakeContext(c).
  73. MakeOrm().
  74. Bind(&req, nil).
  75. MakeService(&s.Service).
  76. Errors
  77. if err != nil {
  78. e.Logger.Error(err)
  79. e.Error(500, err, err.Error())
  80. return
  81. }
  82. //数据权限检查
  83. //p := actions.GetPermissionFromContext(c)
  84. var object model.SysUser
  85. err = s.Get(&req, nil, &object)
  86. if err != nil {
  87. e.Error(http.StatusUnprocessableEntity, err, err.Error())
  88. return
  89. }
  90. e.OK(object, "查询成功")
  91. }
  92. // Insert 创建用户
  93. // @Summary 创建用户
  94. // @Description 创建用户
  95. // @Tags 用户
  96. // @Accept application/json
  97. // @Product application/json
  98. // @Param data body dto.SysUserInsertReq true "用户数据"
  99. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  100. // @Router /api/sys-user [post]
  101. // @Security Bearer
  102. func (e SysUser) Insert(c *gin.Context) {
  103. s := service.SysUser{}
  104. req := dto.SysUserInsertReq{}
  105. err := e.MakeContext(c).
  106. MakeOrm().
  107. Bind(&req, binding.JSON).
  108. MakeService(&s.Service).
  109. Errors
  110. if err != nil {
  111. e.Logger.Error(err)
  112. e.Error(500, err, err.Error())
  113. return
  114. }
  115. p := actions.GetPermissionFromContext(c)
  116. // 设置创建人
  117. req.SetCreateBy(user.GetUserId(c))
  118. err = s.Insert(&req, p)
  119. if err != nil {
  120. e.Error(500, err, err.Error())
  121. return
  122. }
  123. e.OK(req.GetId(), "添加成功")
  124. }
  125. // Update 修改用户数据
  126. // @Summary 修改用户数据
  127. // @Description 修改用户数据
  128. // @Tags 用户
  129. // @Accept application/json
  130. // @Product application/json
  131. // @Param data body dto.SysUserUpdateReq true "body"
  132. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  133. // @Router /api/sys-user [put]
  134. // @Security Bearer
  135. func (e SysUser) Update(c *gin.Context) {
  136. s := service.SysUser{}
  137. req := dto.SysUserUpdateReq{}
  138. err := e.MakeContext(c).
  139. MakeOrm().
  140. Bind(&req).
  141. MakeService(&s.Service).
  142. Errors
  143. if err != nil {
  144. e.Logger.Error(err)
  145. e.Error(500, err, err.Error())
  146. return
  147. }
  148. req.SetUpdateBy(user.GetUserId(c))
  149. //数据权限检查
  150. //p := actions.GetPermissionFromContext(c)
  151. err = s.Update(&req, nil)
  152. if err != nil {
  153. e.Error(500, err, err.Error())
  154. return
  155. }
  156. e.OK(req.GetId(), "修改成功")
  157. }
  158. // Delete 通过id删除用户数据
  159. // @Summary 通过id删除用户数据
  160. // @Description 通过id删除用户数据
  161. // @Tags 用户
  162. // @Param data body dto.SysUserDeleteReq true "body"
  163. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  164. // @Router /api/sys-user [delete]
  165. // @Security Bearer
  166. func (e SysUser) Delete(c *gin.Context) {
  167. s := service.SysUser{}
  168. req := dto.SysUserDeleteReq{}
  169. err := e.MakeContext(c).
  170. MakeOrm().
  171. Bind(&req, binding.JSON).
  172. MakeService(&s.Service).
  173. Errors
  174. if err != nil {
  175. e.Logger.Error(err)
  176. e.Error(500, err, err.Error())
  177. return
  178. }
  179. userId := user.GetUserId(c)
  180. if userId == req.Id {
  181. err := errors.New("禁止删除自己")
  182. e.Error(500, err, err.Error())
  183. return
  184. }
  185. // 设置编辑人
  186. req.SetUpdateBy(userId)
  187. //数据权限检查
  188. p := actions.GetPermissionFromContext(c)
  189. err = s.Remove(&req, p)
  190. if err != nil {
  191. e.Error(500, err, err.Error())
  192. return
  193. }
  194. e.OK(req.GetId(), "删除成功")
  195. }
  196. // UpdateStatus 修改用户状态
  197. // @Summary 修改用户状态
  198. // @Description 修改用户状态
  199. // @Tags 用户
  200. // @Accept application/json
  201. // @Product application/json
  202. // @Param data body dto.UpdateSysUserStatusReq true "body"
  203. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  204. // @Router /api/user/status [put]
  205. // @Security Bearer
  206. func (e SysUser) UpdateStatus(c *gin.Context) {
  207. s := service.SysUser{}
  208. req := dto.UpdateSysUserStatusReq{}
  209. err := e.MakeContext(c).
  210. MakeOrm().
  211. Bind(&req, binding.JSON, nil).
  212. MakeService(&s.Service).
  213. Errors
  214. if err != nil {
  215. e.Logger.Error(err)
  216. e.Error(500, err, err.Error())
  217. return
  218. }
  219. req.SetUpdateBy(user.GetUserId(c))
  220. //数据权限检查
  221. p := actions.GetPermissionFromContext(c)
  222. err = s.UpdateStatus(&req, p)
  223. if err != nil {
  224. e.Logger.Error(err)
  225. e.Error(500, err, err.Error())
  226. return
  227. }
  228. e.OK(req.GetId(), "修改成功")
  229. }
  230. // ResetPwd 重置用户密码
  231. // @Summary 重置用户密码
  232. // @Description 重置用户密码
  233. // @Tags 用户
  234. // @Accept application/json
  235. // @Product application/json
  236. // @Param data body dto.ResetSysUserPwdReq true "body"
  237. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  238. // @Router /api/user/pwd/reset [put]
  239. // @Security Bearer
  240. func (e SysUser) ResetPwd(c *gin.Context) {
  241. s := service.SysUser{}
  242. req := dto.ResetSysUserPwdReq{}
  243. err := e.MakeContext(c).
  244. MakeOrm().
  245. Bind(&req, binding.JSON).
  246. MakeService(&s.Service).
  247. Errors
  248. if err != nil {
  249. e.Logger.Error(err)
  250. e.Error(500, err, err.Error())
  251. return
  252. }
  253. req.SetUpdateBy(user.GetUserId(c))
  254. //数据权限检查
  255. //p := actions.GetPermissionFromContext(c)
  256. err = s.ResetPwd(&req, nil)
  257. if err != nil {
  258. e.Logger.Error(err)
  259. e.Error(500, err, err.Error())
  260. return
  261. }
  262. e.OK(req.GetId(), "修改成功")
  263. }
  264. // UpdatePwd 修改密码
  265. // @Summary 修改密码
  266. // @Description 修改密码
  267. // @Tags 个人中心
  268. // @Accept application/json
  269. // @Product application/json
  270. // @Param data body dto.SysUserPassWord true "body"
  271. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  272. // @Router /api/user/pwd/set [put]
  273. // @Security Bearer
  274. func (e SysUser) UpdatePwd(c *gin.Context) {
  275. s := service.SysUser{}
  276. req := dto.SysUserPassWord{}
  277. err := e.MakeContext(c).
  278. MakeOrm().
  279. Bind(&req).
  280. MakeService(&s.Service).
  281. Errors
  282. if err != nil {
  283. e.Logger.Error(err)
  284. e.Error(500, err, err.Error())
  285. return
  286. }
  287. var hash []byte
  288. if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
  289. req.NewPassword = string(hash)
  290. }
  291. err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword)
  292. if err != nil {
  293. e.Error(http.StatusForbidden, err, err.Error())
  294. return
  295. }
  296. e.OK(nil, "密码修改成功")
  297. }
  298. // GetProfile 获取个人中心用户
  299. // @Summary 获取个人中心用户
  300. // @Description 获取个人中心用户
  301. // @Tags 个人中心
  302. // @Success 200 {object} response.Response{user=model.SysUser,role=model.SysRole} "{"code": 200, "data": {"user":[...],"role":[...]}}"
  303. // @Router /api/user/profile [get]
  304. // @Security Bearer
  305. func (e SysUser) GetProfile(c *gin.Context) {
  306. s := service.SysUser{}
  307. req := dto.SysUserById{}
  308. err := e.MakeContext(c).
  309. MakeOrm().
  310. MakeService(&s.Service).
  311. Errors
  312. if err != nil {
  313. e.Logger.Error(err)
  314. e.Error(500, err, err.Error())
  315. return
  316. }
  317. req.Id = user.GetUserId(c)
  318. sysUser := model.SysUser{}
  319. err = s.GetProfile(&req, &sysUser)
  320. if err != nil {
  321. e.Logger.Errorf("get user profile error, %s", err.Error())
  322. e.Error(500, err, "获取用户信息失败")
  323. return
  324. }
  325. e.OK(gin.H{
  326. "user": sysUser,
  327. }, "查询成功")
  328. }
  329. // GetInfo 获取个人信息
  330. // @Summary 获取个人信息
  331. // @Description 获取个人信息
  332. // @Tags 个人中心
  333. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  334. // @Router /api/user/info [get]
  335. // @Security Bearer
  336. func (e SysUser) GetInfo(c *gin.Context) {
  337. req := dto.SysUserGetReq{}
  338. s := service.SysUser{}
  339. r := service.SysRole{}
  340. err := e.MakeContext(c).
  341. MakeOrm().
  342. MakeService(&r.Service).
  343. MakeService(&s.Service).
  344. Errors
  345. if err != nil {
  346. e.Logger.Error(err)
  347. e.Error(500, err, err.Error())
  348. return
  349. }
  350. var roles = make([]string, 1)
  351. roles[0] = user.GetRoleName(c)
  352. var permissions = make([]string, 1)
  353. permissions[0] = "*:*:*"
  354. var buttons = make([]string, 1)
  355. buttons[0] = "*:*:*"
  356. var mp = make(map[string]interface{})
  357. mp["roles"] = roles
  358. list, _ := r.GetById(user.GetRoleId(c))
  359. mp["permissions"] = list
  360. mp["buttons"] = list
  361. sysUser := model.SysUser{}
  362. req.Id = user.GetUserId(c)
  363. err = s.Get(&req, nil, &sysUser)
  364. if err != nil {
  365. e.Logger.Errorf("get user info error, %s", err.Error())
  366. e.Error(http.StatusUnauthorized, err, err.Error())
  367. return
  368. }
  369. mp["userName"] = sysUser.Username
  370. mp["userId"] = sysUser.Id
  371. mp["deptId"] = sysUser.DeptId
  372. mp["name"] = sysUser.NickName
  373. mp["code"] = 200
  374. e.OK(mp, "查询成功")
  375. }
  376. // VerifyCode 获取短信验证码
  377. // @Summary 获取短信验证码
  378. // @Description 获取短信验证码
  379. // @Tags 登录
  380. // @Accept application/json
  381. // @Product application/json
  382. // @Success 200 {string} string "{"code": 200, "data": "18888888888"}"
  383. // @Router /verify-code [get]
  384. // @Security Bearer
  385. func (e SysUser) VerifyCode(c *gin.Context) {
  386. s := service.SysUser{}
  387. req := dto.SysUserGetSMSVerifyCodeReq{}
  388. err := e.MakeContext(c).
  389. MakeService(&s.Service).
  390. Bind(&req, binding.Query).
  391. Errors
  392. if err != nil {
  393. e.Logger.Error(err)
  394. e.Error(500, err, err.Error())
  395. return
  396. }
  397. key := handler.GetVerifyCodeCacheKey(req.Phone)
  398. _, err = e.Cache.Get(key)
  399. // 验证吗缓存已存在
  400. if err == nil {
  401. e.Error(500, err, "验证吗已发送,请注意查收")
  402. return
  403. }
  404. code := pkg.GenerateRandomFigureKey6()
  405. ss := sms.NewSMS(conf.ExtConfig.SubMail.Appid, conf.ExtConfig.SubMail.Signature)
  406. content := fmt.Sprintf("【气瓶追溯管理系统】您的短信验证码:%s,请在10分钟内输入", code)
  407. res, err := ss.Send(req.Phone, content)
  408. if err != nil || res.Status != sms.SUCCESS {
  409. e.Logger.Error("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
  410. e.Error(500, err, "验证码发送失败,请重试")
  411. return
  412. }
  413. _ = e.Cache.Set(key, code, 600)
  414. e.OK(req.Phone, "发送成功")
  415. }