sys_user.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package service
  2. import (
  3. "Medical_OAuth/app/admin/model"
  4. "Medical_OAuth/app/admin/service/dto"
  5. "Medical_OAuth/common/actions"
  6. cDto "Medical_OAuth/common/dto"
  7. "Medical_OAuth/common/global"
  8. "errors"
  9. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
  11. "gogs.baozhida.cn/zoie/OAuth-core/service"
  12. "gorm.io/gorm"
  13. )
  14. type SysUser struct {
  15. service.Service
  16. }
  17. // GetPage 获取SysUser列表
  18. func (e *SysUser) GetPage(c *dto.SysUserGetPageReq, p *actions.DataPermission, list *[]model.SysUser, count *int64) error {
  19. var err error
  20. var data model.SysUser
  21. var userList []model.SysUser
  22. err = e.Orm.
  23. Scopes(
  24. cDto.MakeCondition(c.GetNeedSearch()),
  25. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  26. actions.UserPermission(data.TableName(), p),
  27. ).
  28. Find(&userList).Limit(-1).Offset(-1).
  29. Count(count).Error
  30. if err != nil {
  31. e.Log.Errorf("db error: %s", err)
  32. return global.GetFailedErr
  33. }
  34. for i := 0; i < len(userList); i++ {
  35. var dept model.SysDept
  36. var role model.SysRole
  37. //var post model.SysPost
  38. err = e.Orm.First(&dept, userList[i].DeptId).Error
  39. userList[i].Dept = dept
  40. err = e.Orm.First(&role, userList[i].RoleId).Error
  41. userList[i].Role = role
  42. //err = e.Orm.First(&post, userList[i].PostId).Error
  43. //userList[i].Post = post
  44. }
  45. *list = userList
  46. return nil
  47. }
  48. func (e *SysUser) GetCount(c *dto.SysUserGetCountReq, count *int64) error {
  49. var err error
  50. var data model.SysUser
  51. err = e.Orm.Model(&data).
  52. Scopes(
  53. cDto.MakeCondition(c.GetNeedSearch()),
  54. ).Count(count).Error
  55. if err != nil {
  56. e.Log.Errorf("db error: %s", err)
  57. return global.GetFailedErr
  58. }
  59. return nil
  60. }
  61. // Get 获取SysUser对象
  62. func (e *SysUser) Get(d *dto.SysUserGetReq, p *actions.DataPermission, userModel *model.SysUser) error {
  63. var data model.SysUser
  64. var dept model.SysDept
  65. var role model.SysRole
  66. //var post model.SysPost
  67. err := e.Orm.Model(&data).
  68. Scopes(actions.UserPermission(data.TableName(), p)).
  69. First(userModel, d.GetId()).Error
  70. if err != nil {
  71. e.Log.Errorf("db error: %s", err)
  72. if errors.Is(err, gorm.ErrRecordNotFound) {
  73. return global.GetNotFoundOrNoPermissionErr
  74. }
  75. return global.GetFailedErr
  76. }
  77. err = e.Orm.First(&dept, userModel.DeptId).Error
  78. userModel.Dept = dept
  79. err = e.Orm.First(&role, userModel.RoleId).Error
  80. userModel.Role = role
  81. //err = e.Orm.First(&post, userModel.PostId).Error
  82. //userModel.Post = post
  83. return nil
  84. }
  85. // Insert 创建SysUser对象
  86. func (e *SysUser) Insert(c *dto.SysUserInsertReq) error {
  87. var err error
  88. var data model.SysUser
  89. var i int64
  90. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  91. if err != nil {
  92. e.Log.Errorf("db error: %s", err)
  93. return global.CreateFailedErr
  94. }
  95. if i > 0 {
  96. err = errors.New("用户名已存在!")
  97. e.Log.Errorf("db error: %s", err)
  98. return err
  99. }
  100. var uuid string
  101. for {
  102. uuid = utils.GetUUID()
  103. var j int64
  104. err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error
  105. if err != nil {
  106. continue
  107. }
  108. if j == 0 {
  109. break
  110. }
  111. }
  112. c.Generate(&data)
  113. data.Uuid = uuid
  114. err = e.Orm.Create(&data).Error
  115. if err != nil {
  116. e.Log.Errorf("db error: %s", err)
  117. return global.CreateFailedErr
  118. }
  119. c.Id = data.Id
  120. return nil
  121. }
  122. // Update 修改SysUser对象
  123. func (e *SysUser) Update(c *dto.SysUserUpdateReq, p *actions.DataPermission) error {
  124. var err error
  125. var userModel model.SysUser
  126. err = e.Orm.
  127. Scopes(actions.UserPermission(userModel.TableName(), p)).
  128. First(&userModel, c.GetId()).Error
  129. if err != nil {
  130. e.Log.Errorf("db error: %s", err)
  131. if errors.Is(err, gorm.ErrRecordNotFound) {
  132. return global.UpdateNotFoundOrNoPermissionErr
  133. }
  134. return global.UpdateFailedErr
  135. }
  136. c.Generate(&userModel)
  137. err = e.Orm.Model(&userModel).Where("id = ?", &userModel.Id).
  138. Omit("password", "salt").Updates(&userModel).Error
  139. if err != nil {
  140. e.Log.Errorf("db error: %s", err)
  141. return global.UpdateFailedErr
  142. }
  143. return nil
  144. }
  145. // UpdateStatus 更新用户状态
  146. func (e *SysUser) UpdateStatus(c *dto.UpdateSysUserStatusReq, p *actions.DataPermission) error {
  147. var userModel model.SysUser
  148. err := e.Orm.
  149. Scopes(actions.UserPermission(userModel.TableName(), p)).
  150. First(&userModel, c.GetId()).Error
  151. if err != nil {
  152. e.Log.Errorf("db error: %s", err)
  153. if errors.Is(err, gorm.ErrRecordNotFound) {
  154. return global.UpdateNotFoundOrNoPermissionErr
  155. }
  156. return global.UpdateFailedErr
  157. }
  158. err = e.Orm.Table(userModel.TableName()).Where("id = ? ", c.Id).Updates(c).Error
  159. if err != nil {
  160. e.Log.Errorf("db error: %s", err)
  161. return global.UpdateFailedErr
  162. }
  163. return nil
  164. }
  165. // ResetPwd 重置用户密码
  166. func (e *SysUser) ResetPwd(c *dto.ResetSysUserPwdReq, p *actions.DataPermission) error {
  167. var userModel model.SysUser
  168. err := e.Orm.
  169. Scopes(actions.UserPermission(userModel.TableName(), p)).
  170. First(&userModel, c.GetId()).Error
  171. if err != nil {
  172. e.Log.Errorf("db error: %s", err)
  173. if errors.Is(err, gorm.ErrRecordNotFound) {
  174. return global.UpdateNotFoundOrNoPermissionErr
  175. }
  176. return global.UpdateFailedErr
  177. }
  178. c.Generate(&userModel)
  179. err = e.Orm.Omit("username", "nick_name", "phone", "role_id", "avatar", "sex").Save(&userModel).Error
  180. if err != nil {
  181. e.Log.Errorf("db error: %s", err)
  182. return global.UpdateFailedErr
  183. }
  184. return nil
  185. }
  186. // Remove 删除SysUser
  187. func (e *SysUser) Remove(c *dto.SysUserDeleteReq, p *actions.DataPermission) error {
  188. var data model.SysUser
  189. db := e.Orm.Model(&data).
  190. Scopes(actions.UserPermission(data.TableName(), p)).
  191. Find(&data, c.GetId())
  192. if err := db.Error; err != nil {
  193. e.Log.Errorf("db error: %s", err)
  194. return err
  195. }
  196. if db.RowsAffected == 0 {
  197. return global.DeleteNotFoundOrNoPermissionErr
  198. }
  199. if err := e.Orm.Delete(&data, c.GetId()).Error; err != nil {
  200. e.Log.Errorf("db error: %s", err)
  201. return err
  202. }
  203. return nil
  204. }
  205. // UpdatePwd 修改SysUser对象密码
  206. func (e *SysUser) UpdatePwd(id int, oldPassword, newPassword string) error {
  207. var err error
  208. if newPassword == "" {
  209. return nil
  210. }
  211. c := &model.SysUser{}
  212. err = e.Orm.Model(c).
  213. Select("id", "password", "salt").
  214. First(c, id).Error
  215. if err != nil {
  216. e.Log.Errorf("db error: %s", err)
  217. if errors.Is(err, gorm.ErrRecordNotFound) {
  218. return global.UpdateNotFoundOrNoPermissionErr
  219. }
  220. return errors.New("密码修改失败")
  221. }
  222. var ok bool
  223. ok, err = pkg.CompareHashAndPassword(c.Password, oldPassword)
  224. if err != nil {
  225. e.Log.Errorf("CompareHashAndPassword error, %s", err.Error())
  226. return errors.New("密码修改失败")
  227. }
  228. if !ok {
  229. err = errors.New("incorrect Password")
  230. e.Log.Warnf("user[%d] %s", id, err.Error())
  231. return err
  232. }
  233. c.Password = newPassword
  234. db := e.Orm.Model(c).Where("id = ?", id).
  235. Select("Password", "Salt").
  236. Updates(c)
  237. if err = db.Error; err != nil {
  238. e.Log.Errorf("db error: %s", err)
  239. return errors.New("密码修改失败")
  240. }
  241. return nil
  242. }
  243. func (e *SysUser) GetProfile(c *dto.SysUserById, user *model.SysUser) error {
  244. err := e.Orm.First(user, c.GetId()).Error
  245. if err != nil {
  246. return err
  247. }
  248. var dept model.SysDept
  249. var role model.SysRole
  250. //var post model.SysPost
  251. err = e.Orm.First(&dept, user.DeptId).Error
  252. if err != nil {
  253. return err
  254. }
  255. user.Dept = dept
  256. err = e.Orm.First(&role, user.RoleId).Error
  257. if err != nil {
  258. return err
  259. }
  260. user.Role = role
  261. //err = e.Orm.First(&post, user.PostId).Error
  262. //if err != nil {
  263. // return err
  264. //}
  265. //user.Post = post
  266. return nil
  267. }
  268. // 公开用户注册
  269. func (e *SysUser) Register(c *dto.SysUserRegisterReq) error {
  270. var err error
  271. var registerAuth model.SysRegisterAuth
  272. err = e.Orm.Where("ur_key = ?", c.URKey).First(&registerAuth).Error
  273. if err != nil {
  274. e.Log.Errorf("db error: %s", err)
  275. if errors.Is(err, gorm.ErrRecordNotFound) {
  276. return errors.New("URKey不存在!")
  277. }
  278. return global.CreateFailedErr
  279. }
  280. if registerAuth.Status == 1 {
  281. return errors.New("URKey已停用!")
  282. }
  283. if registerAuth.SMSVerify == 2 {
  284. if len(c.Phone) == 0 {
  285. return errors.New("手机号码不能为空!")
  286. }
  287. if len(c.Code) == 0 {
  288. return errors.New("短信验证码不能为空!")
  289. }
  290. code, err := e.Cache.Get(model.GetVerifyCodeCacheKey(c.Phone))
  291. if err != nil || code != c.Code {
  292. return errors.New("短信验证码错误")
  293. }
  294. }
  295. var data model.SysUser
  296. var i int64
  297. err = e.Orm.Model(&data).Where("username = ?", c.Username).Count(&i).Error
  298. if err != nil {
  299. e.Log.Errorf("db error: %s", err)
  300. return global.CreateFailedErr
  301. }
  302. if i > 0 {
  303. err = errors.New("用户名已存在!")
  304. e.Log.Errorf("db error: %s", err)
  305. return err
  306. }
  307. var uuid string
  308. for {
  309. uuid = utils.GetUUID()
  310. var j int64
  311. err = e.Orm.Model(&data).Where("uuid = ?", uuid).Count(&j).Error
  312. if err != nil {
  313. continue
  314. }
  315. if j == 0 {
  316. break
  317. }
  318. }
  319. c.Generate(&data)
  320. data.Uuid = uuid
  321. data.RoleId = registerAuth.RoleId
  322. data.DeptId = registerAuth.DeptId
  323. data.Status = "2"
  324. err = e.Orm.Create(&data).Error
  325. if err != nil {
  326. e.Log.Errorf("db error: %s", err)
  327. return global.CreateFailedErr
  328. }
  329. c.Id = data.Id
  330. return nil
  331. }