UsersController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Users;
  5. use app\model\UsersSessions;
  6. use app\model\UsersRoles;
  7. use app\model\Department;
  8. use app\model\Store;
  9. use app\model\Operatingcity;
  10. use app\model\Tuanzhang;
  11. use app\model\Config;
  12. use think\console\command\make\Model;
  13. use think\db\Where;
  14. class UsersController extends Base
  15. {
  16. function index()
  17. {
  18. $weid = weid();
  19. $page = input('post.page', 1, 'intval');
  20. $keyword = trim(input('post.keyword', '', 'serach_in'));
  21. $sex = input('post.sex', '', 'serach_in');
  22. $status = input('post.status', '', 'serach_in');
  23. $role_id = input('post.role_id', '', 'serach_in');
  24. $where = [];
  25. $where['users.weid'] = (int) $weid;
  26. $where['users.sid'] = (int) $this->sid;
  27. $where['users.ocid'] = (int) $this->ocid;
  28. $where['users.tzid'] = (int) $this->tzid;
  29. if (!empty($sex)) {
  30. $where['users.sex'] = $sex;
  31. }
  32. if ($status !== '') {
  33. $where['users.status'] = $status;
  34. }
  35. if (!empty($role_id)) {
  36. $where['users.role_id'] = $role_id;
  37. }
  38. $field = 'id,title,username,sex,touxiang,remark,status,create_time';
  39. $withJoin = [
  40. 'roles' => explode(',', 'title'),
  41. ];
  42. $query = Users::where($where)->withJoin($withJoin, 'left');
  43. $query->where('roles.sid', (int) $this->sid);
  44. $query->where('roles.ocid', (int) $this->ocid);
  45. $query->where('roles.tzid', (int) $this->tzid);
  46. if (!empty($keyword)) {
  47. $query->where('users.title|users.username', 'like', '%' . $keyword . '%');
  48. }
  49. $query->where('users.id', '<>', $this->userInfo['id']);
  50. $res = $query->order('id desc')
  51. ->paginate(getpage())
  52. ->toArray();
  53. $data['data'] = $res;
  54. if ($page == 1) {
  55. $data['field_data']['role_ids'] = UsersRoles::getallarray();
  56. }
  57. return $this->json($data);
  58. }
  59. public function update()
  60. {
  61. $id = $this->request->post('id');
  62. $usersdata = only('title,username,password,sex,touxiang,role_id,remark,status,create_time');
  63. $usersdata['username'] = trim($usersdata['username']);
  64. if ($usersdata['username']) {
  65. $chackuser = Users::where('username', $usersdata['username']);
  66. if (!empty($id)) {
  67. $chackuser->Where('id', '<>', $id);
  68. }
  69. $chackuser = $chackuser->find();
  70. if ($chackuser) {
  71. throw new ValidateException('用户名已被占用');
  72. }
  73. }
  74. if (!empty($usersdata['password'])) {
  75. $usersdata["salt"] = substr(md5(uniqid()), 8, 8);
  76. $usersdata['password'] = pass_hash($usersdata['password'], $usersdata["salt"]);
  77. } else {
  78. unset($usersdata['password']);
  79. }
  80. unset($usersdata['create_time']);
  81. if (empty($id)) {
  82. $usersdata['weid'] = weid();
  83. $usersdata['uuid'] = uniqid(rand(1, 10000));
  84. $usersdata['w7uid'] = 0;
  85. if (!empty($this->sid)) {
  86. $usersdata['sid'] = $this->sid;
  87. }
  88. if (!empty($this->ocid)) {
  89. $usersdata['ocid'] = $this->ocid;
  90. }
  91. if (!empty($this->tzid)) {
  92. $usersdata['tzid'] = $this->tzid;
  93. }
  94. try {
  95. $res = Users::create($usersdata);
  96. } catch (\Exception $e) {
  97. throw new ValidateException($e->getMessage());
  98. }
  99. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  100. } else {
  101. $usersdata['id'] = $id;
  102. try {
  103. Users::update($usersdata);
  104. } catch (\Exception $e) {
  105. throw new ValidateException($e->getMessage());
  106. }
  107. return $this->json(['msg' => '修改成功']);
  108. }
  109. }
  110. function getInfo()
  111. {
  112. $id = $this->request->post('id', '', 'serach_in');
  113. if (!$id) throw new ValidateException('参数错误');
  114. $res = Users::find($id);
  115. if (!empty($res)) {
  116. $res = $res->toArray();
  117. $res['password'] = '';
  118. }
  119. return $this->json(['data' => $res]);
  120. }
  121. /*
  122. * @Description 修改排序开关
  123. */
  124. function listUpdate()
  125. {
  126. $data = only('id,status');
  127. if (!$data['id']) throw new ValidateException('参数错误');
  128. Users::update($data);
  129. return $this->json(['msg' => '操作成功']);
  130. }
  131. /*start*/
  132. /*
  133. * @Description 删除
  134. */
  135. function delete()
  136. {
  137. $idx = $this->request->post('id', '', 'serach_in');
  138. if (!is_array($idx)) {
  139. $idx = explode(',', $idx);
  140. }
  141. if ($idx == 1) {
  142. throw new ValidateException('超级用户禁止删除');
  143. }
  144. if (in_array(1, explode(',', $idx))) {
  145. throw new ValidateException('超级用户禁止删除');
  146. }
  147. return $this->del(new Users());
  148. }
  149. /*
  150. * @Description 禁用
  151. */
  152. public function forbidden()
  153. {
  154. $idx = $this->request->post('id', '', 'serach_in');
  155. if (empty($idx)) throw new ValidateException('参数错误');
  156. $data['status'] = '0';
  157. $res = Users::field('status')
  158. ->where(['id' => explode(',', $idx)])
  159. ->update($data);
  160. return $this->json(['msg' => '操作成功']);
  161. }
  162. //获取用户信息 菜单信息 以及菜单对应的组件
  163. //vue2
  164. public function getUserInfo()
  165. {
  166. if (!$this->userInfo['id']) {
  167. throw new ValidateException('用户Id不存在');
  168. }
  169. $config = Config::getconfig();
  170. $userInfo = Users::field('*')->where('id', $this->userInfo['id'])->find();
  171. if (!$userInfo) {
  172. throw new ValidateException('用户信息不存在');
  173. } else {
  174. $userInfo = $userInfo->toArray();
  175. if ($userInfo['weid'] > 0 && $userInfo['weid'] != weid()) {
  176. throw new ValidateException('您不是本平台管理员');
  177. }
  178. }
  179. $rolesInfo = UsersRoles::getinfo($userInfo);
  180. $menu = $this->getMyMenus($rolesInfo, $this->getTotalMenus());
  181. $components = $this->getComponents($menu, $userInfo['role_id']);
  182. if ($this->console == 1) {
  183. $sitesetup = Config::getconfig('sitesetup');
  184. $userInfo['sys_title'] = $sitesetup['sys_title'];
  185. $userInfo['logo'] = toimg($sitesetup['logo']);
  186. } else {
  187. if (!empty($this->userInfo['sys_title'])) {
  188. $userInfo['sys_title'] = $this->userInfo['sys_title'];
  189. } else {
  190. $userInfo['sys_title'] = $config['sys_title'];
  191. }
  192. $userInfo['logo'] = toimg($config['logo']);
  193. }
  194. $userInfo['w7copyright'] = $this->userInfo['w7copyright'];
  195. $userInfo['weid'] = weid();
  196. if (!empty($this->sid)) {
  197. $userInfo['store_title'] = Store::getTitle($this->sid);
  198. }
  199. if (!empty($this->ocid)) {
  200. $userInfo['city_title'] = Operatingcity::getTitle($this->ocid);
  201. }
  202. if (!empty($this->tzid)) {
  203. $userInfo['tuanzhang_title'] = Tuanzhang::getTitle($this->tzid);
  204. $userInfo['tuanzhang_sid'] = Store::getidbytzid($this->tzid);
  205. }
  206. $data['menu'] = $menu;
  207. $data['components'] = $components;
  208. if (!empty($rolesInfo['access'])) {
  209. $data['actions'] = explode(',', $rolesInfo['access']);
  210. } else {
  211. $data['actions'] = [];
  212. }
  213. $userInfo['is_console'] = $rolesInfo['is_console'];
  214. $userInfo['roles'] = $components;
  215. $userInfo['avatar'] = $userInfo['touxiang'];
  216. $data['data'] = $userInfo;
  217. return $this->json($data);
  218. }
  219. //获取用户信息 菜单信息 以及菜单对应的组件
  220. //vue3
  221. public function getUserInfoVue3()
  222. {
  223. if (!$this->userInfo['id']) {
  224. throw new ValidateException('用户Id不存在');
  225. }
  226. $config = Config::getconfig();
  227. $userInfo = Users::field('*')->where('id', $this->userInfo['id'])->find();
  228. if (!$userInfo) {
  229. throw new ValidateException('用户信息不存在');
  230. } else {
  231. $userInfo = $userInfo->toArray();
  232. }
  233. $rolesInfo = UsersRoles::getinfo($userInfo);
  234. $menu = $this->getMyMenus($rolesInfo, $this->getTotalMenus());
  235. $components = $this->getComponents($menu, $userInfo['role_id']);
  236. if ($this->console == 1) {
  237. $sitesetup = Config::getconfig('sitesetup');
  238. $data['sys_title'] = $sitesetup['sys_title'];
  239. $data['logo'] = toimg($sitesetup['logo']);
  240. } else {
  241. if (!empty($this->userInfo['sys_title'])) {
  242. $data['sys_title'] = $this->userInfo['sys_title'];
  243. } else {
  244. $data['sys_title'] = $config['sys_title'];
  245. }
  246. $data['logo'] = toimg($config['logo']);
  247. }
  248. $data['user'] = $userInfo;
  249. $data['w7copyright'] = $this->userInfo['w7copyright'];
  250. $data['weid'] = weid();
  251. if (!empty($this->sid)) {
  252. $data['store_title'] = Store::getTitle($this->sid);
  253. }
  254. if (!empty($this->ocid)) {
  255. $data['city_title'] = Operatingcity::getTitle($this->ocid);
  256. }
  257. if (!empty($this->tzid)) {
  258. $data['tuanzhang_title'] = Tuanzhang::getTitle($this->tzid);
  259. $data['tuanzhang_sid'] = Store::getidbytzid($this->tzid);
  260. }
  261. $data['menu'] = $menu;
  262. $data['components'] = $components;
  263. if (!empty($rolesInfo['access'])) {
  264. $data['permissions'] = explode(',', $rolesInfo['access']);
  265. } else {
  266. $data['permissions'] = [];
  267. }
  268. $data['is_console'] = $rolesInfo['is_console'];
  269. $data['roles'] = $components;
  270. $data['avatar'] = $data['touxiang'];
  271. $data['data'] = $data;
  272. return $this->json($data);
  273. }
  274. //获取当前角色的菜单
  275. private function getMyMenus($rolesInfo, $totalMenus)
  276. {
  277. if ($rolesInfo['access'] == 'all') {
  278. return $totalMenus;
  279. }
  280. if (!empty($rolesInfo['access'])) {
  281. foreach ($totalMenus as $key => $val) {
  282. if (in_array($val['path'], explode(',', $rolesInfo['access']))) {
  283. $tree[] = array_merge($val, ['children' => $this->getMyMenus($rolesInfo, $val['children'])]);
  284. }
  285. }
  286. }
  287. if (is_array($tree)) {
  288. $tree = array_values($tree);
  289. }
  290. return $tree;
  291. }
  292. //退出
  293. public function logout()
  294. {
  295. $token = $this->getToken();
  296. UsersSessions::where('token', $token)->delete();
  297. return $this->json(['msg' => '退出成功']);
  298. }
  299. /*
  300. * @Description 重置密码
  301. */
  302. public function resetPwd()
  303. {
  304. $data = only('id,password');
  305. if (empty($data['id'])) throw new ValidateException('参数错误');
  306. if (empty($data['password'])) throw new ValidateException('密码不能为空');
  307. $userdata = Users::field('id,title,create_time')->find($data['id']);
  308. $data["salt"] = substr(md5(uniqid()), 8, 8);
  309. $data['password'] = pass_hash($data['password'], $data["salt"]);
  310. $res = Users::update($data);
  311. return $this->json(['msg' => '操作成功']);
  312. }
  313. function getField()
  314. {
  315. $data['role_ids'] = UsersRoles::getpcarray(['ocid' => $this->ocid, 'sid' => $this->sid, 'tzid' => $this->tzid]);
  316. return $this->json(['data' => $data]);
  317. }
  318. }