user.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package system
  2. import (
  3. "errors"
  4. "go-nc/configs/global"
  5. "go-nc/internal/utils"
  6. "go-nc/model"
  7. "go-nc/model/common"
  8. "github.com/gin-gonic/gin"
  9. "github.com/jinzhu/copier"
  10. )
  11. // 登录
  12. func SysLogin(c *gin.Context) {
  13. var param SysLoginType
  14. if err := c.ShouldBindJSON(&param); err != nil {
  15. c.Error(errors.New("缺少参数:" + err.Error()))
  16. return
  17. }
  18. err := utils.ValidateStruct(param)
  19. if err != nil {
  20. c.Error(err)
  21. return
  22. }
  23. pwd := utils.DecryptDES_ECB(param.Password, "")
  24. db_pwd := utils.EncryptDES_ECB(pwd, "limingYa")
  25. param.Password = db_pwd
  26. var data model.Sys_user
  27. global.App.DB.Where("username = ? AND password = ?", param.Username, param.Password).First(&data)
  28. if data.Id == 0 {
  29. c.Error(errors.New("用户名或密码错误!"))
  30. return
  31. }
  32. data.Password = ""
  33. token, err := utils.GenerateToken(utils.UserInfo{
  34. Id: data.Id,
  35. UserName: data.Username,
  36. UserType: data.UserType,
  37. })
  38. if err != nil {
  39. c.Error(err)
  40. return
  41. }
  42. type Res struct {
  43. model.Sys_user
  44. Token string `json:"token"`
  45. }
  46. c.Set("res_data", Res{data, token})
  47. }
  48. // 创建用户
  49. func SysRegister(c *gin.Context) {
  50. var param SysUserType
  51. if err := c.ShouldBindJSON(&param); err != nil {
  52. c.Error(errors.New("缺少参数:" + err.Error()))
  53. return
  54. }
  55. err := utils.ValidateStruct(param)
  56. if err != nil {
  57. c.Error(err)
  58. return
  59. }
  60. if len(param.RoleIds) == 0 {
  61. c.Error(errors.New("请选择角色!"))
  62. return
  63. }
  64. // 事物操作
  65. tx := global.App.DB.Begin()
  66. // 判断用户名是否存在
  67. var user model.Sys_user
  68. if err := tx.Where("username = ?", param.Username).First(&user).Error; err == nil {
  69. tx.Rollback()
  70. c.Error(errors.New("该用户名已存在!!"))
  71. return
  72. }
  73. if user.Id != 0 {
  74. tx.Rollback()
  75. c.Error(errors.New("该用户名已存在!"))
  76. return
  77. }
  78. // 创建用户&密码加密
  79. pwd := utils.EncryptDES_ECB(param.Password, "limingYa")
  80. param.Password = pwd
  81. param.UserType = "1"
  82. copier.Copy(&user, &param)
  83. if err := tx.Create(&user).Error; err != nil {
  84. tx.Rollback()
  85. c.Error(err)
  86. return
  87. }
  88. // 批量创建用户角色关系。不要循环插入
  89. var userRoleList []model.Sys_user_role
  90. for _, v := range param.RoleIds {
  91. userRoleList = append(userRoleList, model.Sys_user_role{
  92. UserId: user.Id,
  93. RoleId: v,
  94. })
  95. }
  96. if err := tx.Create(&userRoleList).Error; err != nil {
  97. tx.Rollback()
  98. c.Error(err)
  99. return
  100. }
  101. tx.Commit()
  102. c.Set("res_data", "添加成功")
  103. }
  104. // 数据换加密后内容api
  105. func EncryptDataAPi(c *gin.Context) {
  106. pwd := c.Query("pwd")
  107. data := utils.EncryptDES_ECB(pwd, "")
  108. c.Set("res_data", data)
  109. }
  110. // 获取用户列表
  111. func GetUserList(c *gin.Context) {
  112. type Param struct {
  113. Name string `json:"username"`
  114. common.Pagination
  115. }
  116. var param Param
  117. param.Pagination = common.NewPagination()
  118. if err := c.ShouldBindJSON(&param); err != nil {
  119. c.Error(errors.New("缺少参数:" + err.Error()))
  120. return
  121. }
  122. // 先查询总记录数
  123. var total int64
  124. var userList []model.Sys_user
  125. if err := global.App.DB.Model(&model.Sys_user{}).Where("name LIKE ? AND user_type = 1", "%"+param.Name+"%").Count(&total).Error; err != nil {
  126. c.Error(err)
  127. return
  128. }
  129. // 使用分页查询
  130. if err := global.App.DB.Where("name LIKE ? AND user_type = 1", "%"+param.Name+"%").
  131. Limit(param.PageSize).
  132. Offset((param.Current - 1) * param.PageSize).
  133. Find(&userList).Error; err != nil {
  134. c.Error(err)
  135. return
  136. }
  137. // 查询用户角色
  138. type User struct {
  139. model.Sys_user
  140. RoleList []uint `json:"roleIds"`
  141. }
  142. var userRoleList []User
  143. // 复制数据
  144. copier.Copy(&userRoleList, &userList)
  145. // 查询角色
  146. for i, user := range userRoleList {
  147. var roleList []model.Sys_user_role
  148. if err := global.App.DB.Where("user_id = ?", user.Id).Find(&roleList).Error; err != nil {
  149. c.Error(err)
  150. return
  151. }
  152. // userIds
  153. var userRoleIds []uint
  154. for _, v := range roleList {
  155. userRoleIds = append(userRoleIds, v.RoleId)
  156. }
  157. userRoleList[i].Password = ""
  158. userRoleList[i].RoleList = userRoleIds
  159. }
  160. records := make([]interface{}, len(userRoleList))
  161. for i, user := range userRoleList {
  162. records[i] = user
  163. }
  164. // 返回数据
  165. data := common.Pagination{
  166. Records: records,
  167. Current: param.Current,
  168. PageSize: param.PageSize,
  169. Total: total,
  170. }
  171. c.Set("res_data", data)
  172. }
  173. // 删除用户
  174. func DeleteUser(c *gin.Context) {
  175. type Param struct {
  176. Id uint `json:"id" validate:"required"`
  177. }
  178. var param Param
  179. if err := c.ShouldBindJSON(&param); err != nil {
  180. c.Error(errors.New("缺少参数!"))
  181. return
  182. }
  183. err := utils.ValidateStruct(param)
  184. if err != nil {
  185. c.Error(err)
  186. return
  187. }
  188. // 事物操作
  189. tx := global.App.DB.Begin()
  190. // 删除用户
  191. var user model.Sys_user
  192. user.Id = param.Id
  193. if err := tx.Unscoped().Delete(&user).Error; err != nil {
  194. tx.Rollback()
  195. c.Error(err)
  196. return
  197. }
  198. // 删除用户角色关系
  199. var userRoleList model.Sys_user_role
  200. if err := tx.Where("user_id = ?", param.Id).Unscoped().Delete(&userRoleList).Error; err != nil {
  201. tx.Rollback()
  202. c.Error(err)
  203. return
  204. }
  205. tx.Commit()
  206. c.Set("res_data", "删除成功!")
  207. }
  208. // 更新用户
  209. func UpdateUser(c *gin.Context) {
  210. type Param struct {
  211. Id uint `json:"id" validate:"required"`
  212. Username string `json:"username" validate:"required"`
  213. State string `json:"state" validate:"required"`
  214. Name string `json:"name" validate:"required"`
  215. Password string `json:"password" `
  216. RoleIds []uint `json:"roleIds" validate:"required"`
  217. }
  218. var param Param
  219. if err := c.ShouldBindJSON(&param); err != nil {
  220. c.Error(errors.New("缺少参数:" + err.Error()))
  221. return
  222. }
  223. err := utils.ValidateStruct(param)
  224. if err != nil {
  225. c.Error(err)
  226. return
  227. }
  228. if len(param.RoleIds) == 0 {
  229. c.Error(errors.New("请选择角色!"))
  230. return
  231. }
  232. if param.Password != "" {
  233. pwd := utils.DecryptDES_ECB(param.Password, "")
  234. param.Password = utils.EncryptDES_ECB(pwd, "limingYa")
  235. }
  236. // 更新用户
  237. tx := global.App.DB.Begin()
  238. user := model.Sys_user{}
  239. copier.Copy(&user, &param)
  240. if err := tx.Updates(&user).Error; err != nil {
  241. tx.Rollback()
  242. c.Error(err)
  243. return
  244. }
  245. // 更新用户和角色关系
  246. // 删除用户角色关系
  247. if err := tx.Where("user_id = ?", param.Id).Unscoped().Delete(&model.Sys_user_role{}).Error; err != nil {
  248. tx.Rollback()
  249. c.Error(err)
  250. return
  251. }
  252. // 批量创建用户角色关系。不要循环插入
  253. var userRoleList []model.Sys_user_role
  254. for _, v := range param.RoleIds {
  255. userRoleList = append(userRoleList, model.Sys_user_role{
  256. UserId: user.Id,
  257. RoleId: v,
  258. })
  259. }
  260. if err := tx.Create(&userRoleList).Error; err != nil {
  261. tx.Rollback()
  262. c.Error(err)
  263. return
  264. }
  265. tx.Commit()
  266. c.Set("res_data", "更新成功!")
  267. }