User.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\shop\model\auth;
  3. use app\common\model\shop\User as UserModel;
  4. /**
  5. * 角色模型
  6. */
  7. class User extends UserModel
  8. {
  9. public function getList($limit = 20)
  10. {
  11. return $this->with(['userRole.role'])->where('is_delete', '=', 0)->
  12. order(['create_time' => 'desc'])
  13. ->paginate($limit);
  14. }
  15. /**
  16. * 获取所有上级id集
  17. */
  18. public function getTopRoleIds($role_id, &$all = null)
  19. {
  20. static $ids = [];
  21. is_null($all) && $all = $this->getAll();
  22. foreach ($all as $item) {
  23. if ($item['role_id'] == $role_id && $item['parent_id'] > 0) {
  24. $ids[] = $item['parent_id'];
  25. $this->getTopRoleIds($item['parent_id'], $all);
  26. }
  27. }
  28. return $ids;
  29. }
  30. /**
  31. * 获取所有角色
  32. */
  33. private function getAll()
  34. {
  35. $data = $this->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
  36. return $data ? $data->toArray() : [];
  37. }
  38. public function add($data)
  39. {
  40. $this->startTrans();
  41. try {
  42. $arr = [
  43. 'user_name' => trim($data['user_name']),
  44. 'password' => salt_hash($data['password']),
  45. 'real_name' => trim($data['real_name']),
  46. 'role_id' => $data['role_id'],
  47. 'app_id' => self::$app_id
  48. ];
  49. $res = self::create($arr);
  50. $add_arr = [];
  51. $model = new UserRole();
  52. foreach ($data['role_id'] as $val) {
  53. $add_arr[] = [
  54. 'shop_user_id' => $res['shop_user_id'],
  55. 'role_id' => $val,
  56. 'app_id' => self::$app_id,
  57. ];
  58. }
  59. $model->saveAll($add_arr);
  60. // 事务提交
  61. $this->commit();
  62. return true;
  63. } catch (\Exception $e) {
  64. $this->error = $e->getMessage();
  65. $this->rollback();
  66. return false;
  67. }
  68. }
  69. public function getUserName($where, $shop_user_id = 0)
  70. {
  71. if ($shop_user_id > 0) {
  72. return $this->where($where)->where('shop_user_id', '<>', $shop_user_id)->count();
  73. }
  74. return $this->where($where)->count();
  75. }
  76. public function edit($data)
  77. {
  78. $this->startTrans();
  79. try {
  80. $arr = [
  81. 'user_name' => $data['user_name'],
  82. 'password' => salt_hash($data['password']),
  83. 'real_name' => $data['real_name'],
  84. ];
  85. if (empty($data['password'])) {
  86. unset($arr['password']);
  87. }
  88. $where['shop_user_id'] = $data['shop_user_id'];
  89. self::update($arr, $where);
  90. $model = new UserRole();
  91. UserRole::destroy($where);
  92. $add_arr = [];
  93. foreach ($data['access_id'] as $val) {
  94. $add_arr[] = [
  95. 'shop_user_id' => $data['shop_user_id'],
  96. 'role_id' => $val,
  97. 'app_id' => self::$app_id
  98. ];
  99. }
  100. $model->saveAll($add_arr);
  101. // 事务提交
  102. $this->commit();
  103. return true;
  104. } catch (\Exception $e) {
  105. $this->error = $e->getMessage();
  106. $this->rollback();
  107. return false;
  108. }
  109. }
  110. public function getChild($where)
  111. {
  112. return $this->where($where)->count();
  113. }
  114. public function del($where)
  115. {
  116. self::update(['is_delete' => 1], $where);
  117. return UserRole::destroy($where);
  118. }
  119. }