sys_user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. package controller
  2. import (
  3. "Medical_OAuth/app/admin/model"
  4. "Medical_OAuth/app/admin/service"
  5. "Medical_OAuth/app/admin/service/dto"
  6. "Medical_OAuth/common/actions"
  7. "Medical_OAuth/common/middleware/handler"
  8. "Medical_OAuth/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 deptId query int false "部门id"
  32. // @Param roleId query string false "角色id"
  33. // @Param postId query string false "岗位id"
  34. // @Param status query string false "状态 1-停用 2-正常"
  35. // @Param pageSize query int false "页条数"
  36. // @Param pageIndex query int false "页码"
  37. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysUser}} "{"code": 200, "data": [...]}"
  38. // @Router /api/sys-user [get]
  39. // @Security Bearer
  40. func (e SysUser) GetPage(c *gin.Context) {
  41. s := service.SysUser{}
  42. req := dto.SysUserGetPageReq{}
  43. err := e.MakeContext(c).
  44. MakeOrm().
  45. Bind(&req, binding.Query).
  46. MakeService(&s.Service).
  47. Errors
  48. if err != nil {
  49. e.Logger.Error(err)
  50. e.Error(500, err, err.Error())
  51. return
  52. }
  53. //数据权限检查
  54. p := actions.GetPermissionFromContext(c)
  55. list := make([]model.SysUser, 0)
  56. var count int64
  57. err = s.GetPage(&req, p, &list, &count)
  58. if err != nil {
  59. e.Error(500, err, err.Error())
  60. return
  61. }
  62. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  63. }
  64. // Get 通过id获取用户
  65. // @Summary 通过id获取用户
  66. // @Description 通过id获取用户
  67. // @Tags 用户
  68. // @Param id path int true "用户id"
  69. // @Success 200 {object} response.Response{data=model.SysUser} "{"code": 200, "data": [...]}"
  70. // @Router /api/sys-user/{id} [get]
  71. // @Security Bearer
  72. func (e SysUser) Get(c *gin.Context) {
  73. s := service.SysUser{}
  74. req := dto.SysUserGetReq{}
  75. err := e.MakeContext(c).
  76. MakeOrm().
  77. Bind(&req, nil).
  78. MakeService(&s.Service).
  79. Errors
  80. if err != nil {
  81. e.Logger.Error(err)
  82. e.Error(500, err, err.Error())
  83. return
  84. }
  85. //数据权限检查
  86. p := actions.GetPermissionFromContext(c)
  87. var object model.SysUser
  88. err = s.Get(&req, p, &object)
  89. if err != nil {
  90. e.Error(http.StatusUnprocessableEntity, err, err.Error())
  91. return
  92. }
  93. e.OK(object, "查询成功")
  94. }
  95. // Insert 创建用户
  96. // @Summary 创建用户
  97. // @Description 创建用户
  98. // @Tags 用户
  99. // @Accept application/json
  100. // @Product application/json
  101. // @Param data body dto.SysUserInsertReq true "用户数据"
  102. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  103. // @Router /api/sys-user [post]
  104. // @Security Bearer
  105. func (e SysUser) Insert(c *gin.Context) {
  106. s := service.SysUser{}
  107. req := dto.SysUserInsertReq{}
  108. err := e.MakeContext(c).
  109. MakeOrm().
  110. Bind(&req, binding.JSON).
  111. MakeService(&s.Service).
  112. Errors
  113. if err != nil {
  114. e.Logger.Error(err)
  115. e.Error(500, err, err.Error())
  116. return
  117. }
  118. p := actions.GetPermissionFromContext(c)
  119. req.DeptId = p.DeptId
  120. req.RoleId = 1
  121. req.Status = "2"
  122. // 设置创建人
  123. req.SetCreateBy(user.GetUserId(c))
  124. err = s.Insert(&req)
  125. if err != nil {
  126. e.Error(500, err, err.Error())
  127. return
  128. }
  129. e.OK(req.GetId(), "创建成功")
  130. }
  131. // Update 修改用户数据
  132. // @Summary 修改用户数据
  133. // @Description 修改用户数据
  134. // @Tags 用户
  135. // @Accept application/json
  136. // @Product application/json
  137. // @Param id path int true "用户id"
  138. // @Param data body dto.SysUserUpdateReq true "body"
  139. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  140. // @Router /api/sys-user [put]
  141. // @Security Bearer
  142. func (e SysUser) Update(c *gin.Context) {
  143. s := service.SysUser{}
  144. req := dto.SysUserUpdateReq{}
  145. err := e.MakeContext(c).
  146. MakeOrm().
  147. Bind(&req).
  148. MakeService(&s.Service).
  149. Errors
  150. if err != nil {
  151. e.Logger.Error(err)
  152. e.Error(500, err, err.Error())
  153. return
  154. }
  155. req.SetUpdateBy(user.GetUserId(c))
  156. //数据权限检查
  157. p := actions.GetPermissionFromContext(c)
  158. err = s.Update(&req, p)
  159. if err != nil {
  160. e.Error(500, err, err.Error())
  161. return
  162. }
  163. e.OK(req.GetId(), "更新成功")
  164. }
  165. // Delete 通过id删除用户数据
  166. // @Summary 通过id删除用户数据
  167. // @Description 通过id删除用户数据
  168. // @Tags 用户
  169. // @Param data body dto.SysUserDeleteReq true "body"
  170. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  171. // @Router /api/sys-user [delete]
  172. // @Security Bearer
  173. func (e SysUser) Delete(c *gin.Context) {
  174. s := service.SysUser{}
  175. req := dto.SysUserDeleteReq{}
  176. err := e.MakeContext(c).
  177. MakeOrm().
  178. Bind(&req, binding.JSON).
  179. MakeService(&s.Service).
  180. Errors
  181. if err != nil {
  182. e.Logger.Error(err)
  183. e.Error(500, err, err.Error())
  184. return
  185. }
  186. userId := user.GetUserId(c)
  187. if userId == req.Id {
  188. err := errors.New("禁止删除自己")
  189. e.Error(500, err, err.Error())
  190. return
  191. }
  192. // 设置编辑人
  193. req.SetUpdateBy(userId)
  194. //数据权限检查
  195. p := actions.GetPermissionFromContext(c)
  196. err = s.Remove(&req, p)
  197. if err != nil {
  198. e.Error(500, err, err.Error())
  199. return
  200. }
  201. e.OK(req.GetId(), "删除成功")
  202. }
  203. // UpdateStatus 修改用户状态
  204. // @Summary 修改用户状态
  205. // @Description 修改用户状态
  206. // @Tags 用户
  207. // @Accept application/json
  208. // @Product application/json
  209. // @Param data body dto.UpdateSysUserStatusReq true "body"
  210. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  211. // @Router /api/user/status [put]
  212. // @Security Bearer
  213. func (e SysUser) UpdateStatus(c *gin.Context) {
  214. s := service.SysUser{}
  215. req := dto.UpdateSysUserStatusReq{}
  216. err := e.MakeContext(c).
  217. MakeOrm().
  218. Bind(&req, binding.JSON, nil).
  219. MakeService(&s.Service).
  220. Errors
  221. if err != nil {
  222. e.Logger.Error(err)
  223. e.Error(500, err, err.Error())
  224. return
  225. }
  226. req.SetUpdateBy(user.GetUserId(c))
  227. //数据权限检查
  228. p := actions.GetPermissionFromContext(c)
  229. err = s.UpdateStatus(&req, p)
  230. if err != nil {
  231. e.Logger.Error(err)
  232. e.Error(500, err, err.Error())
  233. return
  234. }
  235. e.OK(req.GetId(), "更新成功")
  236. }
  237. // ResetPwd 重置用户密码
  238. // @Summary 重置用户密码
  239. // @Description 重置用户密码
  240. // @Tags 用户
  241. // @Accept application/json
  242. // @Product application/json
  243. // @Param data body dto.ResetSysUserPwdReq true "body"
  244. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  245. // @Router /api/user/pwd/reset [put]
  246. // @Security Bearer
  247. func (e SysUser) ResetPwd(c *gin.Context) {
  248. s := service.SysUser{}
  249. req := dto.ResetSysUserPwdReq{}
  250. err := e.MakeContext(c).
  251. MakeOrm().
  252. Bind(&req, binding.JSON).
  253. MakeService(&s.Service).
  254. Errors
  255. if err != nil {
  256. e.Logger.Error(err)
  257. e.Error(500, err, err.Error())
  258. return
  259. }
  260. req.SetUpdateBy(user.GetUserId(c))
  261. //数据权限检查
  262. p := actions.GetPermissionFromContext(c)
  263. err = s.ResetPwd(&req, p)
  264. if err != nil {
  265. e.Logger.Error(err)
  266. e.Error(500, err, err.Error())
  267. return
  268. }
  269. e.OK(req.GetId(), "更新成功")
  270. }
  271. // UpdatePwd 修改密码
  272. // @Summary 修改密码
  273. // @Description 修改密码
  274. // @Tags 个人中心
  275. // @Accept application/json
  276. // @Product application/json
  277. // @Param data body dto.PassWord true "body"
  278. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  279. // @Router /api/user/pwd/set [put]
  280. // @Security Bearer
  281. func (e SysUser) UpdatePwd(c *gin.Context) {
  282. s := service.SysUser{}
  283. req := dto.PassWord{}
  284. err := e.MakeContext(c).
  285. MakeOrm().
  286. Bind(&req).
  287. MakeService(&s.Service).
  288. Errors
  289. if err != nil {
  290. e.Logger.Error(err)
  291. e.Error(500, err, err.Error())
  292. return
  293. }
  294. var hash []byte
  295. if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
  296. req.NewPassword = string(hash)
  297. }
  298. err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword)
  299. if err != nil {
  300. e.Error(http.StatusForbidden, err, err.Error())
  301. return
  302. }
  303. e.OK(nil, "密码修改成功")
  304. }
  305. // GetProfile 获取个人中心用户
  306. // @Summary 获取个人中心用户
  307. // @Description 获取个人中心用户
  308. // @Tags 个人中心
  309. // @Success 200 {object} response.Response{user=model.SysUser,role=model.SysRole} "{"code": 200, "data": {"user":[...],"role":[...]}}"
  310. // @Router /api/user/profile [get]
  311. // @Security Bearer
  312. func (e SysUser) GetProfile(c *gin.Context) {
  313. s := service.SysUser{}
  314. req := dto.SysUserById{}
  315. err := e.MakeContext(c).
  316. MakeOrm().
  317. MakeService(&s.Service).
  318. Errors
  319. if err != nil {
  320. e.Logger.Error(err)
  321. e.Error(500, err, err.Error())
  322. return
  323. }
  324. req.Id = user.GetUserId(c)
  325. sysUser := model.SysUser{}
  326. err = s.GetProfile(&req, &sysUser)
  327. if err != nil {
  328. e.Logger.Errorf("get user profile error, %s", err.Error())
  329. e.Error(500, err, "获取用户信息失败")
  330. return
  331. }
  332. e.OK(gin.H{
  333. "user": sysUser,
  334. }, "查询成功")
  335. }
  336. // GetInfo 获取个人信息
  337. // @Summary 获取个人信息
  338. // @Description 获取个人信息
  339. // @Tags 个人中心
  340. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  341. // @Router /api/user/info [get]
  342. // @Security Bearer
  343. func (e SysUser) GetInfo(c *gin.Context) {
  344. req := dto.SysUserGetReq{}
  345. s := service.SysUser{}
  346. r := service.SysRole{}
  347. err := e.MakeContext(c).
  348. MakeOrm().
  349. MakeService(&r.Service).
  350. MakeService(&s.Service).
  351. Errors
  352. if err != nil {
  353. e.Logger.Error(err)
  354. e.Error(500, err, err.Error())
  355. return
  356. }
  357. var roles = make([]string, 1)
  358. roles[0] = user.GetRoleName(c)
  359. var permissions = make([]string, 1)
  360. permissions[0] = "*:*:*"
  361. var buttons = make([]string, 1)
  362. buttons[0] = "*:*:*"
  363. var mp = make(map[string]interface{})
  364. mp["roles"] = roles
  365. list, _ := r.GetById(user.GetRoleId(c))
  366. mp["permissions"] = list
  367. mp["buttons"] = list
  368. sysUser := model.SysUser{}
  369. req.Id = user.GetUserId(c)
  370. err = s.Get(&req, nil, &sysUser)
  371. if err != nil {
  372. e.Logger.Errorf("get user info error, %s", err.Error())
  373. e.Error(http.StatusUnauthorized, err, err.Error())
  374. return
  375. }
  376. mp["userName"] = sysUser.Username
  377. mp["userId"] = sysUser.Id
  378. mp["deptId"] = sysUser.DeptId
  379. mp["name"] = sysUser.NickName
  380. mp["code"] = 200
  381. e.OK(mp, "查询成功")
  382. }
  383. // Register 公开用户注册
  384. // @Summary 公开用户注册
  385. // @Description 公开用户注册
  386. // @Tags 用户
  387. // @Accept application/json
  388. // @Product application/json
  389. // @Param id body dto.SysUserRegisterReq true "请求参数"
  390. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  391. // @Router /api/user/register [post]
  392. // @Security Bearer
  393. func (e SysUser) Register(c *gin.Context) {
  394. s := service.SysUser{}
  395. req := dto.SysUserRegisterReq{}
  396. err := e.MakeContext(c).
  397. MakeOrm().
  398. Bind(&req, binding.JSON).
  399. MakeService(&s.Service).
  400. Errors
  401. if err != nil {
  402. e.Logger.Error(err)
  403. e.Error(500, err, err.Error())
  404. return
  405. }
  406. req.SetUpdateBy(user.GetUserId(c))
  407. err = s.Register(&req)
  408. if err != nil {
  409. e.Error(500, err, err.Error())
  410. return
  411. }
  412. e.OK(req.GetId(), "注册成功")
  413. }
  414. // VerifyCode 获取短信验证码
  415. // @Summary 获取短信验证码
  416. // @Description 获取短信验证码
  417. // @Tags 登录
  418. // @Accept application/json
  419. // @Product application/json
  420. // @Success 200 {string} string "{"code": 200, "data": "18888888888"}"
  421. // @Router /verify-code [post]
  422. // @Security Bearer
  423. func (e SysUser) VerifyCode(c *gin.Context) {
  424. s := service.SysUser{}
  425. req := dto.SysUserGetSMSVerifyCodeReq{}
  426. err := e.MakeContext(c).
  427. MakeService(&s.Service).
  428. Bind(&req, binding.Query).
  429. Errors
  430. if err != nil {
  431. e.Logger.Error(err)
  432. e.Error(500, err, err.Error())
  433. return
  434. }
  435. key := model.GetVerifyCodeCacheKey(req.Phone)
  436. _, err = e.Cache.Get(key)
  437. // 验证吗缓存已存在
  438. if err == nil {
  439. e.Error(500, err, "验证吗已发送,请注意查收")
  440. return
  441. }
  442. code := pkg.GenerateRandomFigureKey6()
  443. ss := sms.NewSMS(conf.ExtConfig.SubMail.Appid, conf.ExtConfig.SubMail.Signature)
  444. content := fmt.Sprintf("【宝智达统一身份认证】您的短信验证码:%s,请在10分钟内输入", code)
  445. res, err := ss.Send(req.Phone, content)
  446. if err != nil || res.Status != sms.SUCCESS {
  447. e.Logger.Error("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
  448. e.Error(500, err, "验证吗发送失败,请重试")
  449. return
  450. }
  451. _ = e.Cache.Set(key, code, 600)
  452. e.OK(req.Phone, "发送成功")
  453. }
  454. // GetNewestToken 获取最新token
  455. // @Summary 获取最新token
  456. // @Description 获取最新token,提供给接入服务访问,用于单一认证检查
  457. // @Tags 用户
  458. // @Param serviceId header int true "服务id"
  459. // @Param userId path int true "用户id"
  460. // @Success 200 {string} string "{"code": 200, "data": "...token..."}"
  461. // @Router /newest-token [get]
  462. func (e SysUser) GetNewestToken(c *gin.Context) {
  463. s := service.SysUser{}
  464. req := dto.GetNewestTokenReq{}
  465. err := e.MakeContext(c).
  466. MakeService(&s.Service).
  467. Bind(&req, nil).
  468. Errors
  469. if err != nil {
  470. e.Logger.Error(err)
  471. e.Error(500, err, err.Error())
  472. return
  473. }
  474. key, err := handler.GetNewestToken(c, req.UserId)
  475. if err != nil {
  476. e.Error(500, err, err.Error())
  477. return
  478. }
  479. e.OK(key, "查询成功")
  480. }