User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\supplier\model\auth;
  3. use app\common\model\supplier\User as SupplierUserModel;
  4. use app\common\model\user\User as UserModel;
  5. /**
  6. * 角色模型
  7. */
  8. class User extends SupplierUserModel
  9. {
  10. public function getList($limit = 20,$shop_supplier_id)
  11. {
  12. return $this->with(['userRole.role'])->where('is_delete', '=', 0)
  13. ->where('shop_supplier_id','=',$shop_supplier_id)
  14. ->order(['create_time' => 'desc'])
  15. ->paginate($limit);
  16. }
  17. /**
  18. * 获取所有上级id集
  19. */
  20. public function getTopRoleIds($role_id, &$all = null)
  21. {
  22. static $ids = [];
  23. is_null($all) && $all = $this->getAll();
  24. foreach ($all as $item) {
  25. if ($item['role_id'] == $role_id && $item['parent_id'] > 0) {
  26. $ids[] = $item['parent_id'];
  27. $this->getTopRoleIds($item['parent_id'], $all);
  28. }
  29. }
  30. return $ids;
  31. }
  32. /**
  33. * 获取所有角色
  34. */
  35. private function getAll()
  36. {
  37. $data = $this->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
  38. return $data ? $data->toArray() : [];
  39. }
  40. public function add($data)
  41. {
  42. $this->startTrans();
  43. try {
  44. // 用户是否已绑定
  45. $user = null;
  46. if($data['user_id'] > 0){
  47. $user = UserModel::detail($data['user_id']);
  48. if($user['user_type'] != 1){
  49. $this->error = '该用户已绑定,或绑定的商户正在审核';
  50. return false;
  51. }
  52. }
  53. $arr = [
  54. 'user_name' => trim($data['user_name']),
  55. 'password' => salt_hash($data['password']),
  56. 'real_name' => trim($data['real_name']),
  57. 'user_id' => $data['user_id'],
  58. 'app_id' => self::$app_id,
  59. 'shop_supplier_id' => $data['shop_supplier_id']
  60. ];
  61. $res = self::create($arr);
  62. $add_arr = [];
  63. $model = new UserRole();
  64. foreach ($data['access_id'] as $val) {
  65. $add_arr[] = [
  66. 'supplier_user_id' => $res['supplier_user_id'],
  67. 'role_id' => $val,
  68. 'app_id' => self::$app_id,
  69. ];
  70. }
  71. $model->saveAll($add_arr);
  72. // 后台添加的直接算审核通过
  73. if($user){
  74. $user->save([
  75. 'user_type' => 2
  76. ]);
  77. }
  78. // 事务提交
  79. $this->commit();
  80. return true;
  81. } catch (\Exception $e) {
  82. $this->error = $e->getMessage();
  83. $this->rollback();
  84. return false;
  85. }
  86. }
  87. public function getUserName($where)
  88. {
  89. return $this->where($where)->count();
  90. }
  91. public function edit($data)
  92. {
  93. $this->startTrans();
  94. try {
  95. // 用户是否已绑定
  96. $user = null;
  97. $old_user_id = 0;
  98. if($this['user']){
  99. $old_user_id = $this['user']['user_id'];
  100. }
  101. $userChange = false;
  102. if($this['user'] && $data['user_id'] > 0 && $data['user_id'] != $this['user']['user_id']){
  103. $user = UserModel::detail($data['user_id']);
  104. if($user['user_type'] != 1){
  105. $this->error = '该用户已绑定,或绑定的商户正在审核';
  106. return false;
  107. }
  108. $userChange = true;
  109. }
  110. $arr = [
  111. 'user_name' => $data['user_name'],
  112. 'user_id' => $data['user_id'],
  113. 'real_name' => $data['real_name'],
  114. ];
  115. if (!empty($data['password'])) {
  116. $arr['password'] = salt_hash($data['password']);
  117. }
  118. $this->save($arr);
  119. $model = new UserRole();
  120. $where['supplier_user_id'] = $data['supplier_user_id'];
  121. UserRole::destroy($where);
  122. $add_arr = [];
  123. foreach ($data['access_id'] as $val) {
  124. $add_arr[] = [
  125. 'supplier_user_id' => $data['supplier_user_id'],
  126. 'role_id' => $val,
  127. 'app_id' => self::$app_id
  128. ];
  129. }
  130. $model->saveAll($add_arr);
  131. // 后台添加的直接算审核通过
  132. if($userChange){
  133. $user->save([
  134. 'user_type' => 2
  135. ]);
  136. //取消原来的
  137. if ($old_user_id > 0){
  138. (new UserModel())->where('user_id', '=', $old_user_id)->update([
  139. 'user_type' => 1
  140. ]);
  141. }
  142. }
  143. // 事务提交
  144. $this->commit();
  145. return true;
  146. } catch (\Exception $e) {
  147. $this->error = $e->getMessage();
  148. $this->rollback();
  149. return false;
  150. }
  151. }
  152. public function getChild($where)
  153. {
  154. return $this->where($where)->count();
  155. }
  156. public function del($where)
  157. {
  158. return self::update(['is_delete' => 1], $where);
  159. }
  160. }