sys_user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package controller
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service"
  5. "cold-logistics/app/admin/service/dto"
  6. "cold-logistics/common/actions"
  7. "cold-logistics/common/middleware/handler"
  8. "cold-logistics/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 name query string false "姓名"
  32. // @Param type query int false "类型:管理员1 仓管2 司机3"
  33. // @Param pageSize query int false "页条数"
  34. // @Param page query int false "页码"
  35. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysUser}} "{"code": 200, "data": [...]}"
  36. // @Router /api/sys-user [get]
  37. // @Security Bearer
  38. func (e SysUser) GetPage(c *gin.Context) {
  39. s := service.SysUser{}
  40. req := dto.SysUserGetPageReq{}
  41. err := e.MakeContext(c).
  42. MakeOrm().
  43. Bind(&req, binding.Query).
  44. MakeService(&s.Service).
  45. Errors
  46. if err != nil {
  47. e.Logger.Error(err)
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. //数据权限检查
  52. p := actions.GetPermissionFromContext(c)
  53. list := make([]model.SysUser, 0)
  54. var count int64
  55. err = s.GetPage(&req, p, &list, &count)
  56. if err != nil {
  57. e.Error(500, err, err.Error())
  58. return
  59. }
  60. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  61. }
  62. // Get 通过id获取用户
  63. // @Summary 通过id获取用户
  64. // @Description 通过id获取用户
  65. // @Tags 用户
  66. // @Param id path int true "用户id"
  67. // @Success 200 {object} response.Response{data=model.SysUser} "{"code": 200, "data": [...]}"
  68. // @Router /api/sys-user/{id} [get]
  69. // @Security Bearer
  70. func (e SysUser) Get(c *gin.Context) {
  71. s := service.SysUser{}
  72. req := dto.SysUserGetReq{}
  73. err := e.MakeContext(c).
  74. MakeOrm().
  75. Bind(&req, nil).
  76. MakeService(&s.Service).
  77. Errors
  78. if err != nil {
  79. e.Logger.Error(err)
  80. e.Error(500, err, err.Error())
  81. return
  82. }
  83. //数据权限检查
  84. //p := actions.GetPermissionFromContext(c)
  85. var object model.SysUser
  86. err = s.Get(&req, nil, &object)
  87. if err != nil {
  88. e.Error(http.StatusUnprocessableEntity, err, err.Error())
  89. return
  90. }
  91. e.OK(object, "查询成功")
  92. }
  93. // Insert 创建用户
  94. // @Summary 创建用户
  95. // @Description 创建用户
  96. // @Tags 用户
  97. // @Accept application/json
  98. // @Product application/json
  99. // @Param data body dto.SysUserInsertReq true "body"
  100. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  101. // @Router /api/sys-user [post]
  102. // @Security Bearer
  103. func (e SysUser) Insert(c *gin.Context) {
  104. s := service.SysUser{}
  105. req := dto.SysUserInsertReq{}
  106. err := e.MakeContext(c).
  107. MakeOrm().
  108. Bind(&req, binding.JSON).
  109. MakeService(&s.Service).
  110. Errors
  111. if err != nil {
  112. e.Logger.Error(err)
  113. e.Error(500, err, err.Error())
  114. return
  115. }
  116. // 设置创建人
  117. req.SetCreateBy(user.GetUserId(c))
  118. err = s.Insert(&req)
  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} "{"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. // @Param phone query string true "手机号"
  381. // @Success 200 {string} string "{"code": 200, "data": "18888888888"}"
  382. // @Router /verify-code [get]
  383. // @Security Bearer
  384. func (e SysUser) VerifyCode(c *gin.Context) {
  385. s := service.SysUser{}
  386. req := dto.SysUserGetSMSVerifyCodeReq{}
  387. err := e.MakeContext(c).
  388. MakeService(&s.Service).
  389. Bind(&req, binding.Query).
  390. Errors
  391. if err != nil {
  392. e.Logger.Error(err)
  393. e.Error(500, err, err.Error())
  394. return
  395. }
  396. key := handler.GetVerifyCodeCacheKey(req.Phone)
  397. _, err = e.Cache.Get(key)
  398. // 验证吗缓存已存在
  399. if err == nil {
  400. e.Error(500, err, "验证吗已发送,请注意查收")
  401. return
  402. }
  403. code := pkg.GenerateRandomFigureKey6()
  404. ss := sms.NewSMS(conf.ExtConfig.SubMail.Appid, conf.ExtConfig.SubMail.Signature)
  405. content := fmt.Sprintf("【气瓶追溯管理系统】您的短信验证码:%s,请在10分钟内输入", code)
  406. res, err := ss.Send(req.Phone, content)
  407. if err != nil || res.Status != sms.SUCCESS {
  408. e.Logger.Error("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
  409. e.Error(500, err, "验证吗发送失败,请重试")
  410. return
  411. }
  412. _ = e.Cache.Set(key, code, 600)
  413. e.OK(req.Phone, "发送成功")
  414. }
  415. // GetPageFoeBind 获取用户列表-仓库/车辆绑定
  416. // @Summary 获取用户列表-仓库/车辆绑定
  417. // @Description 获取用户列表-仓库/车辆绑定
  418. // @Tags 用户
  419. // @Param username query string false "用户名"
  420. // @Param name query string false "姓名"
  421. // @Param type query int false "类型:管理员1 仓管2 司机3"
  422. // @Param pageSize query int false "页条数"
  423. // @Param page query int false "页码"
  424. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysUser}} "{"code": 200, "data": [...]}"
  425. // @Router /api/sys-user [get]
  426. // @Security Bearer
  427. func (e SysUser) GetPageFoeBind(c *gin.Context) {
  428. warehouseSvc := service.Warehouse{}
  429. carSvc := service.Car{}
  430. req := dto.SysUserGetPageReq{}
  431. err := e.MakeContext(c).
  432. MakeOrm().
  433. Bind(&req, binding.Query).
  434. MakeService(&warehouseSvc.Service).
  435. MakeService(&carSvc.Service).
  436. Errors
  437. if err != nil {
  438. e.Logger.Error(err)
  439. e.Error(500, err, err.Error())
  440. return
  441. }
  442. //数据权限检查
  443. p := actions.GetPermissionFromContext(c)
  444. list := make([]model.SysUserForBind, 0)
  445. var count int64
  446. if req.Type == 2 {
  447. err = warehouseSvc.GetUserList(&req, p, &list, &count)
  448. if err != nil {
  449. e.Error(500, err, err.Error())
  450. return
  451. }
  452. }
  453. if req.Type == 3 {
  454. err = carSvc.GetUserList(&req, p, &list, &count)
  455. if err != nil {
  456. e.Error(500, err, err.Error())
  457. return
  458. }
  459. }
  460. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  461. }