sys_user_api.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package admin
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "strconv"
  5. "ulink-admin/frame"
  6. "ulink-admin/modules/system/models/model"
  7. "ulink-admin/modules/system/models/req"
  8. "ulink-admin/modules/system/models/response"
  9. service2 "ulink-admin/modules/system/service"
  10. "ulink-admin/pkg/excels"
  11. "ulink-admin/pkg/jwt/admin"
  12. "ulink-admin/pkg/page"
  13. )
  14. // UserApi 用户操作api
  15. type UserApi struct {
  16. UserService *service2.UserService `inject:""`
  17. RoleService *service2.RoleService `inject:""`
  18. }
  19. // Find @Summary 用户列表查询接口
  20. // @Description 用户列表查询接口
  21. // @Tags 用户相关接口
  22. // @Accept application/json
  23. // @Produce application/json
  24. // @Param Authorization header string false "Bearer 用户令牌"
  25. // @Param object query req.UserQuery false "查询参数"
  26. // @Security ApiKeyAuth
  27. // @Success 200 {object} resp.Response{data=page.Page{list=model.SysUser},msg=string} "分页获取用户列表,返回包括列表,总数,页码,每页数量"
  28. // @Router /user/page [get]
  29. func (a UserApi) Find(c *frame.Context) {
  30. query := &req.UserQuery{}
  31. c.ValidteError(c.ShouldBind(query), query)
  32. list, i := a.UserService.Page(query)
  33. c.Ok(page.Page{
  34. Size: query.PageSize,
  35. Total: i,
  36. List: list,
  37. })
  38. }
  39. // Find @Summary 用户列表查询接口
  40. // @Description 用户列表查询接口
  41. // @Tags 用户相关接口
  42. // @Accept application/json
  43. // @Produce application/json
  44. // @Param Authorization header string false "Bearer 用户令牌"
  45. // @Param object query req.UserQuery false "查询参数"
  46. // @Security ApiKeyAuth
  47. // @Success 200 {object} resp.Response{data=page.Page{list=model.SysUser},msg=string} "分页获取用户列表,返回包括列表,总数,页码,每页数量"
  48. // @Router /user/list [get]
  49. func (a UserApi) List(c *frame.Context) {
  50. query := &req.UserQuery{}
  51. c.ValidteError(c.ShouldBind(query), query)
  52. list := make([]model.SysUser, 0)
  53. a.UserService.List(query, &list)
  54. c.Ok(list)
  55. }
  56. // GetInfo @Summary 用户详情查询接口
  57. // @Description 用户详情查询接口
  58. // @Tags 用户相关接口
  59. // @Accept application/json
  60. // @Produce application/json
  61. // @Param Authorization header string false "Bearer 用户令牌"
  62. // @Param id path int true "id" id
  63. // @Security ApiKeyAuth
  64. // @Success 200 {object} resp.Response{data=response.UserInfo,msg=string} "返回用户详情查询"
  65. // @Router /user/getInfo [get]
  66. func (a UserApi) GetInfo(c *frame.Context) {
  67. param := c.Param("id")
  68. r := new(response.UserInfo)
  69. roleAll := make([]model.SysRole, 0)
  70. //查询角色
  71. a.RoleService.List(&req.RoleQuery{}, &roleAll)
  72. //判断id传入的是否为空
  73. if !gotool.StrUtils.HasEmpty(param) {
  74. parseInt, err := strconv.ParseInt(param, 10, 64)
  75. if err == nil {
  76. //判断当前登录用户是否是admin
  77. m := new(model.SysUser)
  78. if m.IsAdmin(parseInt) {
  79. r.Roles = roleAll
  80. } else {
  81. roles := make([]model.SysRole, 0)
  82. for _, role := range roleAll {
  83. if role.Id != 1 {
  84. roles = append(roles, role)
  85. }
  86. }
  87. r.Roles = roles
  88. }
  89. user := new(model.SysUser)
  90. a.UserService.GetById(parseInt, user)
  91. if user == nil {
  92. frame.Throw(frame.BUSINESS_CODE, "用户查询异常")
  93. }
  94. role := a.RoleService.SelectRoleListByUserId(parseInt)
  95. if role == nil {
  96. frame.Throw(frame.BUSINESS_CODE, "角色查询异常")
  97. }
  98. //根据id获取用户数据
  99. r.User = user
  100. //根据用户ID查询角色id集合
  101. r.RoleIds = role
  102. }
  103. } else {
  104. //id为空不取管理员角色
  105. roles := make([]model.SysRole, 0)
  106. for _, role := range roleAll {
  107. if role.Id != 1 {
  108. roles = append(roles, role)
  109. }
  110. }
  111. r.Roles = roles
  112. }
  113. c.Ok(r)
  114. }
  115. // MyInfo @Summary 会员详情查询接口
  116. // @Description 会员详情等查询接口
  117. // @Tags 用户相关接口
  118. // @Accept application/json
  119. // @Produce application/json
  120. // @Param Authorization header string false "Bearer 用户令牌"
  121. // @Security ApiKeyAuth
  122. // @Success 200 {object} resp.Response{data=response.MemberInfo,msg=string} "返回会员详情查询"
  123. // @Router /app/user/myInfo [get]
  124. func (a UserApi) MyInfo(c *frame.Context) {
  125. userId := admin.GetUserInfo(c).Id
  126. memberInfo := new(response.MemberInfo)
  127. user := new(model.SysUser)
  128. a.UserService.GetById(userId, user)
  129. //查询会员信息
  130. memberInfo.User = user
  131. if memberInfo.User == nil {
  132. c.Error("用户查询异常")
  133. return
  134. }
  135. c.Ok(memberInfo)
  136. }
  137. // AuthRole 根据用户编号获取授权角色
  138. func (a UserApi) AuthRole(c *frame.Context) {
  139. /* m := make(map[string]interface{})
  140. id := c.Param("id")
  141. parseInt, err := strconv.ParseInt(id, 10, 64)
  142. if err != nil {
  143. gotool.Logs.ErrorLog().Println(err)
  144. c.JSON(500, resp.ErrorResp(err))
  145. }
  146. user := a.UserService.GetUserById(parseInt)
  147. if user == nil {
  148. resp.Error(c, "用户查询异常")
  149. return
  150. }
  151. //查询角色
  152. roles := a.RoleService.GetRoleListByUserId(parseInt)
  153. if roles == nil {
  154. resp.Error(c, "角色查询异常")
  155. return
  156. }
  157. flag := model.SysUser{}.IsAdmin(parseInt)
  158. if flag {
  159. m["roles"] = roles
  160. } else {
  161. roleList := make([]model.SysRole, 0)
  162. for _, role := range *roles {
  163. if role.Id != 1 {
  164. roleList = append(roleList, role)
  165. }
  166. }
  167. m["roles"] = roleList
  168. }
  169. allRoles := a.RoleService.FindAll()
  170. if allRoles == nil {
  171. resp.Error(c, "角色查询异常")
  172. return
  173. }
  174. if flag {
  175. m["allRoles"] = allRoles
  176. } else {
  177. roleList := make([]model.SysRole, 0)
  178. for _, role := range allRoles {
  179. if role.Id != 1 {
  180. roleList = append(roleList, *role)
  181. }
  182. }
  183. m["allRoles"] = roleList
  184. }
  185. m["user"] = user
  186. c.JSON(200, resp.Success(m))*/
  187. }
  188. // Add @Summary 新增用户接口
  189. // @Description 新增用户接口
  190. // @Tags 用户相关接口
  191. // @Accept application/json
  192. // @Produce application/json
  193. // @Param Authorization header string false "Bearer 用户令牌"
  194. // @Param data body model.SysUser true "用户实体对象"
  195. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  196. // @Router /user/add [post]
  197. func (a UserApi) Add(c *frame.Context) {
  198. userBody := &req.UserBody{}
  199. c.ValidteError(c.ShouldBind(userBody), userBody)
  200. userBody.DelFlag = "0"
  201. userBody.Balance = "0.00"
  202. //添加用户
  203. a.UserService.Insert(userBody)
  204. }
  205. // Edit @Summary 修改用户接口
  206. // @Description 修改用户接口
  207. // @Tags 用户相关接口
  208. // @Accept application/json
  209. // @Produce application/json
  210. // @Param Authorization header string false "Bearer 用户令牌"
  211. // @Param data body model.SysUser true "用户实体对象"
  212. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  213. // @Router /user/edit [put]
  214. func (a UserApi) Edit(c *frame.Context) {
  215. userBody := &req.UserBody{}
  216. c.ValidteError(c.ShouldBind(userBody), userBody)
  217. //进行用户修改操作
  218. a.UserService.Edit(userBody)
  219. }
  220. // Remove @Summary 删除用户接口
  221. // @Description 删除用户接口
  222. // @Tags 用户相关接口
  223. // @Accept application/json
  224. // @Produce application/json
  225. // @Param Authorization header string false "Bearer 用户令牌"
  226. // @Param id path int true "id" id
  227. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  228. // @Router /user/remove [delete]
  229. func (a UserApi) Remove(c *frame.Context) {
  230. param := c.Param("id")
  231. id, err := strconv.ParseInt(param, 10, 64)
  232. if err != nil {
  233. gotool.Logs.ErrorLog().Println(err)
  234. c.Error("参数错误")
  235. return
  236. }
  237. a.UserService.Remove(id)
  238. }
  239. // ResetPwd @Summary 重置用户密码接口
  240. // @Description 重置用户密码接口
  241. // @Tags 用户相关接口
  242. // @Accept application/json
  243. // @Produce application/json
  244. // @Param Authorization header string false "Bearer 用户令牌"
  245. // @Param data body model.SysUser true "用户实体对象"
  246. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  247. // @Router /user/resetPwd [put]
  248. func (a UserApi) ResetPwd(c *frame.Context) {
  249. userBody := &req.UserBody{}
  250. c.ValidteError(c.ShouldBind(userBody), userBody)
  251. if a.UserService.CheckUserAllowed(userBody) {
  252. c.Error("不允许操作超级管理员用户")
  253. return
  254. }
  255. userBody.Password = gotool.BcryptUtils.Generate(userBody.Password)
  256. //进行密码修改
  257. a.UserService.ResetPwd(userBody)
  258. }
  259. // Export 导出excel
  260. func (a UserApi) Export(c *frame.Context) {
  261. query := &req.UserQuery{}
  262. c.ValidteError(c.ShouldBind(query), query)
  263. items := make([]interface{}, 0)
  264. list, _ := a.UserService.Page(query)
  265. for _, userResponse := range list {
  266. items = append(items, *userResponse)
  267. }
  268. _, file := excels.ExportExcel(items, "用户表")
  269. c.Header("Content-Type", "application/octet-stream")
  270. c.Header("Content-Disposition", "attachment; filename="+gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
  271. c.Header("Content-Transfer-Encoding", "binary")
  272. c.Header("FileName", gotool.IdUtils.IdUUIDToRan(false)+".xlsx")
  273. _ = file.Write(c.Writer)
  274. }
  275. // Profile 查询个人信息
  276. // @Summary 查询个人信息接口
  277. // @Description 查询个人信息接口
  278. // @Tags 用户相关接口
  279. // @Accept application/json
  280. // @Produce application/json
  281. // @Param Authorization header string false "Bearer 用户令牌"
  282. // @Param id path int true "id" id
  283. // @Security ApiKeyAuth
  284. // @Success 200 {object} resp.Response{data=map[string]interface{},msg=string} "返回个人详情查询"
  285. // @Router /user/profile [get]
  286. func (a UserApi) Profile(c *frame.Context) {
  287. m := make(map[string]interface{})
  288. info := admin.GetUserInfo(c)
  289. user := new(model.SysUser)
  290. a.UserService.GetById(info.Id, user)
  291. m["user"] = user
  292. // 查询所属角色组
  293. m["roleGroup"] = a.RoleService.SelectRolesByUserName(info.UserName)
  294. c.Ok(m)
  295. }
  296. // UpdateProfile 修改个人数据
  297. // @Summary 修改个人数据接口
  298. // @Description 修改个人数据接口
  299. // @Tags 用户相关接口
  300. // @Accept application/json
  301. // @Produce application/json
  302. // @Param Authorization header string false "Bearer 用户令牌"
  303. // @Param data body model.SysUser true "用户实体对象"
  304. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  305. // @Router /user/profile [put]
  306. func (a UserApi) UpdateProfile(c *frame.Context) {
  307. user := &req.UserBody{}
  308. c.ValidteError(c.ShouldBind(user), user)
  309. a.UserService.EditProfile(user)
  310. }
  311. // ChangeAuthRole 修改状态
  312. // @Summary 修改状态接口
  313. // @Description 修改状态接口
  314. // @Tags 用户相关接口
  315. // @Accept application/json
  316. // @Produce application/json
  317. // @Param Authorization header string false "Bearer 用户令牌"
  318. // @Param id query int true "id" id
  319. // @Param status query string true "status" status
  320. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  321. // @Router /user/authRole [put]
  322. func (a UserApi) ChangeAuthRole(c *frame.Context) {
  323. userBody := &req.UserBody{}
  324. c.ValidteError(c.ShouldBind(userBody), userBody)
  325. a.UserService.UpdateAuthRole(userBody)
  326. }
  327. // ChangeStatus 修改状态
  328. // @Summary 修改状态接口
  329. // @Description 修改状态接口
  330. // @Tags 用户相关接口
  331. // @Accept application/json
  332. // @Produce application/json
  333. // @Param Authorization header string false "Bearer 用户令牌"
  334. // @Param id query int true "id" id
  335. // @Param status query string true "status" status
  336. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  337. // @Router /user/changeStatus [put]
  338. func (a UserApi) ChangeStatus(c *frame.Context) {
  339. param := c.Query("id")
  340. status := c.Query("status")
  341. id, err := strconv.ParseInt(param, 10, 64)
  342. if err != nil {
  343. c.Error("参数错误")
  344. return
  345. }
  346. a.UserService.UpdateStatus(id, status)
  347. }
  348. // UpdatePwd 修改个人密码
  349. // @Summary 修改个人密码接口
  350. // @Description 修改个人密码接口
  351. // @Tags 用户相关接口
  352. // @Accept application/json
  353. // @Produce application/json
  354. // @Param Authorization header string false "Bearer 用户令牌"
  355. // @Param oldPassword query string true "oldPassword" oldPassword
  356. // @Param newPassword query string true "newPassword" newPassword
  357. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  358. // @Router /user/profile/updatePwd [put]
  359. func (a UserApi) UpdatePwd(c *frame.Context) {
  360. oldPassword := c.Query("oldPassword")
  361. newPassword := c.Query("newPassword")
  362. info := admin.GetUserInfo(c)
  363. name := a.UserService.GetUserByUserName(info.UserName)
  364. hash := gotool.BcryptUtils.CompareHash(name.Password, oldPassword)
  365. if !hash {
  366. c.Error("修改密码失败,旧密码错误")
  367. return
  368. }
  369. generate := gotool.BcryptUtils.Generate(oldPassword)
  370. compareHash := gotool.BcryptUtils.CompareHash(generate, newPassword)
  371. if compareHash {
  372. c.Error("新密码不能与旧密码相同")
  373. return
  374. }
  375. a.UserService.UpdatePwd(info.Id, gotool.BcryptUtils.Generate(newPassword))
  376. }
  377. // Avatar 修改头像
  378. // UpdatePwd 修改个人密码
  379. // @Summary 修改个人密码接口
  380. // @Description 修改个人密码接口
  381. // @Tags 用户相关接口
  382. // @Accept application/json
  383. // @Produce application/json
  384. // @Param Authorization header string false "Bearer 用户令牌"
  385. // @Param file formData file true "avatarfile"
  386. // @Success 200 {object} resp.Response{msg=string} "操作状态"
  387. // @Router /user/profile/avatar [put]
  388. func (a UserApi) Avatar(c *frame.Context) {
  389. img := c.Request.FormValue("img")
  390. // 进行存储
  391. info := admin.GetUserInfo(c)
  392. info.Avatar = img
  393. a.UserService.UpdateAvatar(info)
  394. }