User.php 5.3 KB

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