User.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\supplier\controller\auth;
  3. use app\supplier\model\supplier\Access as AccessModel;
  4. use app\common\model\settings\Setting as SettingModel;
  5. use app\supplier\controller\Controller;
  6. use app\supplier\model\auth\User as UserModel;
  7. use app\supplier\model\auth\Role;
  8. use app\supplier\model\auth\User as AuthUserModel;
  9. /**
  10. * 管理员
  11. */
  12. class User extends Controller
  13. {
  14. /**
  15. * 首页列表
  16. * @return \think\response\Json
  17. */
  18. public function index()
  19. {
  20. $model = new UserModel();
  21. $list = $model->getList($this->postData(),$this->getSupplierId());
  22. // 角色列表
  23. $roleList = (new Role())->getTreeData($this->getSupplierId());
  24. return $this->renderSuccess('', compact('list', 'roleList'));
  25. }
  26. /**
  27. * 新增
  28. * @return \think\response\Json
  29. */
  30. public function add()
  31. {
  32. $data = $this->postData();
  33. $data['shop_supplier_id'] = $this->getSupplierId();
  34. $model = new UserModel();
  35. $num = $model->getUserName(['is_delete'=>0,'user_name' => $data['user_name']]);
  36. if ($num > 0) {
  37. return $this->renderError('用户名已存在');
  38. }
  39. if (!isset($data['access_id'])) {
  40. return $this->renderError('请选择所属角色');
  41. }
  42. if ($data['confirm_password'] != $data['password']) {
  43. return $this->renderError('确认密码和登录密码不一致');
  44. }
  45. $model = new UserModel();
  46. if ($model->add($data)) {
  47. return $this->renderSuccess('添加成功');
  48. }
  49. return $this->renderError($model->getError()?:'添加失败');
  50. }
  51. /**
  52. * 修改信息
  53. * @param $shop_user_id
  54. * @return \think\response\Json
  55. */
  56. public function editInfo($shop_user_id)
  57. {
  58. $info = UserModel::detail(['supplier_user_id' => $shop_user_id], ['userRole', 'user']);
  59. $role_arr = array_column($info->toArray()['userRole'], 'role_id');
  60. $model = new Role();
  61. // 角色列表
  62. $roleList = $model->getTreeData($this->getSupplierId());
  63. // 绑定用户
  64. $user = $info['user'];
  65. return $this->renderSuccess('', compact('info', 'roleList', 'role_arr', 'user'));
  66. }
  67. /**
  68. * 编辑
  69. * @param $shop_user_id
  70. * @return \think\response\Json
  71. */
  72. public function edit($supplier_user_id)
  73. {
  74. $data = $this->postData();
  75. if($this->request->isGet()){
  76. return $this->editInfo($supplier_user_id);
  77. }
  78. $model = UserModel::detail($supplier_user_id, ['user']);
  79. if($data['user_name'] != $model['user_name']){
  80. $num = $model->getUserName(['is_delete'=> 0,'user_name' => $data['user_name']]);
  81. if ($num > 0) {
  82. return $this->renderError('用户名已存在');
  83. }
  84. }
  85. if (!isset($data['access_id'])) {
  86. return $this->renderError('请选择所属角色');
  87. }
  88. if (isset($data['password']) && !empty($data['password'])) {
  89. if (!isset($data['confirm_password'])) {
  90. return $this->renderError('请输入确认密码');
  91. } else {
  92. if ($data['confirm_password'] != $data['password']) {
  93. return $this->renderError('确认密码和登录密码不一致');
  94. }
  95. }
  96. }
  97. if (empty($data['password'])) {
  98. if (isset($data['confirm_password']) && !empty($data['confirm_password'])) {
  99. return $this->renderError('请输入登录密码');
  100. }
  101. }
  102. // 更新记录
  103. if ($model->edit($data)) {
  104. return $this->renderSuccess('更新成功');
  105. }
  106. return $this->renderError($model->getError()?:'更新失败');
  107. }
  108. /**
  109. * 删除
  110. */
  111. public function delete($supplier_user_id)
  112. {
  113. $model = new UserModel();
  114. if ($model->del([
  115. 'supplier_user_id' => $supplier_user_id,
  116. 'shop_supplier_id' => $this->getSupplierId()
  117. ])) {
  118. return $this->renderSuccess('删除成功');
  119. }
  120. return $this->renderError('删除失败');
  121. }
  122. /**
  123. * 获取角色菜单信息
  124. */
  125. public function getRoleList()
  126. {
  127. $user = $this->supplier['user'];
  128. $user_info = (new AuthUserModel())->find($user['supplier_user_id']);
  129. if ($user_info['is_super'] == 1) {
  130. $model = new AccessModel();
  131. $menus = $model->getList();
  132. } else {
  133. $model = new AccessModel();
  134. $menus = $model->getListByUser($user['supplier_user_id']);
  135. foreach ($menus as $key => $val) {
  136. if ($val['redirect_name'] != $val['children'][0]['path']) {
  137. $menus[$key]['redirect_name'] = $menus[$key]['children'][0]['path'];
  138. }
  139. }
  140. }
  141. return $this->renderSuccess('', compact('menus'));
  142. }
  143. /**
  144. * 获取用户信息
  145. */
  146. public function getUserInfo()
  147. {
  148. $supplier = session('jjjshop_supplier');
  149. $user = [];
  150. if (!empty($supplier)) {
  151. $user = $supplier['user'];
  152. }
  153. // 商城名称
  154. $shop_name = SettingModel::getItem('store')['name'];
  155. //当前系统版本
  156. $version = get_version();
  157. return $this->renderSuccess('', compact('user', 'shop_name', 'version'));
  158. }
  159. }