User.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller;
  4. use app\admin\common\User as CommonUser;
  5. use think\facade\Db;
  6. use app\admin\model\ActionLog;
  7. use think\facade\Validate;
  8. //运营账户
  9. class User extends Base
  10. {
  11. //登录
  12. public function login()
  13. {
  14. $param = $this->request->only(['username', 'password'], 'post', 'trim');
  15. $val = Validate::rule([
  16. 'username|用户名' => 'require',
  17. 'password|密码' => 'require',
  18. ]);
  19. if (!$val->check($param)) return json_show(1004, $val->getError());
  20. $user = CommonUser::getIns();
  21. $rs = $user->login(array_merge($param, ['plat_code' => 'cx']));
  22. $rs = json_decode($rs, true);
  23. if (!isset($rs['code']) || $rs['code'] != 0) return json_show(1004, $rs['message']);
  24. switch ($rs['data']['level']){
  25. case 1:
  26. $role = [
  27. 'role_name' => '',
  28. 'role_level' => $rs['data']['level'],
  29. 'role_id' => env('default_admin_addcount_role_id',1),//指定一个默认角色id,1超管
  30. ];
  31. break;
  32. case 2:
  33. //获取默认公司
  34. $default = array_column($rs['data']['company_relaton'], 'companyNo', 'is_main');
  35. if (!isset($default) || !isset($default[1])) return json_show(1004, '没有默认公司,无法登录');
  36. $role = Db::name('user_role')
  37. ->alias('a')
  38. ->field('a.roleid role_id,b.role_name,b.role_level')
  39. ->leftJoin('role b', 'b.id=a.roleid')
  40. ->where(['a.uid' => $rs['data']['id'], 'a.companyNo' => $default[1], 'a.is_del' => 0])
  41. ->findOrEmpty();
  42. if (empty($role)) return json_show(1004, '没有默认角色,无法登录,请联系管理员');
  43. break;
  44. case 3:
  45. //供应商端
  46. $role = [
  47. 'role_name' => '',
  48. 'role_level' => $rs['data']['level'],
  49. 'role_id' => env('default_supplier_addcount_role_id',90),//指定一个默认角色id
  50. ];
  51. break;
  52. }
  53. // if ($rs['data']['level'] == 2) {
  54. //
  55. // $role = Db::name('user_role')
  56. // ->alias('a')
  57. // ->field('a.roleid,b.role_name,b.role_level')
  58. // ->leftJoin('role b', 'b.id=a.roleid')
  59. // ->where(['a.uid' => $rs['data']['id'], 'a.is_main' => 1, 'a.is_del' => 0])
  60. // ->findOrEmpty();
  61. //
  62. // if (empty($role)) return json_show(1004, '没有默认角色,无法登录,请联系管理员');
  63. //
  64. // //数据合并
  65. // $data = array_merge($rs['data'], [
  66. // 'role_name' => $role['role_name'],
  67. // 'role_level' => $role['role_level'],
  68. // 'role_id' => $role['roleid'],
  69. // ]);
  70. //
  71. // } else {
  72. // //超管 or 供应商
  73. // $data = array_merge($rs['data'], [
  74. // 'role_name' => '',
  75. // 'role_level' => $rs['data']['level'],
  76. // 'role_id' => '',
  77. // ]);
  78. // }
  79. $data = array_merge($rs['data'], [
  80. 'role_name' => $role['role_name'],
  81. 'role_level' => $role['role_level'],
  82. 'role_id' => $role['role_id'],
  83. ]);
  84. return json_show(0, '登录成功', $data);
  85. }
  86. public function list()
  87. {
  88. $param = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'uid' => '', 'nuid' => '', 'companyNo' => ''], 'post', 'trim');
  89. $param['level'] = 1;//只筛选超管用户
  90. $userCommon = CommonUser::getIns();
  91. $rs = $userCommon->GetUserlist($param);
  92. $rs = json_decode($rs, true);
  93. return json_show($rs['code'], $rs['message'], $rs['data']);
  94. }
  95. //删除
  96. public function delete()
  97. {
  98. $param = $this->request->only(['ids'], 'post', 'trim');
  99. $val = Validate::rule([
  100. 'ids' => 'require|array|max:100',
  101. ]);
  102. if (!$val->check($param)) return json_show(1004, $val->getError());
  103. if (in_array($this->uid, $param['ids'])) return json_show(1004, '自己不能删除自己');
  104. $userCommon = CommonUser::getIns();
  105. $rs = $userCommon->userDelete($param);
  106. $rs = json_decode($rs, true);
  107. return json_show($rs['code'], $rs['message'], $rs['data']);
  108. }
  109. //详情
  110. public function userInfo()
  111. {
  112. /**
  113. * $post = $this->request->post();
  114. * $token = isset($post['token']) ? trim($post['token']) : "";
  115. * if ($token == "") {
  116. * return app_show(101, 'token不能为空');
  117. * }
  118. * $effetc = VerifyTokens($token);
  119. * if (!empty($effetc) && $effetc['code'] != 0) {
  120. * return app_show($effetc['code'], $effetc['message']);
  121. * }
  122. * $userinfo = GetUserInfo($token);
  123. * if (empty($userinfo) || $userinfo['code'] != 0) {
  124. * return app_show(102, "员工信息不存在");
  125. * }
  126. * $id = isset($post['id']) && $post['id'] !== "" ? trim($post['id']) : "";
  127. * if ($id == "") {
  128. * return error_show(1004, "id不能为空");
  129. * }
  130. * $to = GetInfoById($token, ['id' => $id]);
  131. * if (empty($to) || $to['code'] != 0) {
  132. * return app_show(1002, "员工信息不存在");
  133. * }
  134. * // $nickname = isset($to['data']['nickname']) &&$to['data']['nickname']!="" ? $to['data']['nickname'] :"" ;
  135. * $data = $to['data'];
  136. * $role = Db::name("user_role")->alias('a')->leftJoin('role b', "a.roleid = b.id")->where("a.uid", "=", $data['id'])
  137. * ->field("roleid,role_name,business_all,uid,a.status")->find();
  138. * $data['role_name'] = isset($role['role_name']) ? $role['role_name'] : "";
  139. * $data['roleid'] = isset($role['roleid']) ? $role['roleid'] : "";
  140. * $data['status'] = isset($role['status']) ? $role['status'] : '0';
  141. * $st = Db::name('user_business')->where(['userid' => $role['uid'], "is_del" => 0])->field('businessid,is_main')->select()->toArray();
  142. * $val = [];
  143. * if (!empty($st)) {
  144. * foreach ($st as $itom) {
  145. * $tep = [];
  146. * $business = Db::name('business')->where(['id' => $itom['businessid'], "is_del" => 0])->field("company,companyNo")->find();
  147. * $tep['company'] = isset($business['company']) ? $business['company'] : "";
  148. * $tep['companyNo'] = isset($business['companyNo']) ? $business['companyNo'] : "";
  149. * $tep['businessid'] = $itom['businessid'];
  150. * $tep['is_main'] = $itom['is_main'];
  151. * $val[] = $tep;
  152. * }
  153. * }
  154. * $data['business'] = $val;
  155. * $info = [];
  156. * $stn = Db::name('user_platform')->where(['uid' => $role['uid']])->find();
  157. * if ($stn != false) {
  158. * $plat = isset($stn['platform']) ? explode(",", $stn['platform']) : "";
  159. * if ($plat != "") {
  160. * $platform = Db::name('platform')->where(['id' => $plat, 'is_del' => 0])->column("id,platform_name,platform_code");
  161. * $info = $platform;
  162. * }
  163. *
  164. * }
  165. * $depart = Db::name("depart_user")->where([["uid", "=", $data['id']], ["is_del", "=", 0]])->column("itemid");
  166. * $data['depart'] = "";
  167. *
  168. * if (!empty($depart)) {
  169. * $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")->select()->toArray();
  170. * if (!empty($deparname)) {
  171. * foreach ($deparname as $ky => $item) {
  172. * $deparname[$ky]['depart_code'] = array_filter(explode("-", $item['depart_link']));
  173. * }
  174. * }
  175. * $data['depart'] = $deparname;
  176. * }
  177. * $data['platform'] = $info;
  178. *
  179. * return app_show(0, "获取成功", $data);
  180. * **/
  181. // $id = $this->request->post('id/d', 0, "trim");
  182. $userCommon = CommonUser::getIns();
  183. $rs = json_decode($userCommon->GetUserInfo(['id' => $this->uid]), true);
  184. $rs['data']['level'] = $this->level;
  185. return json_show($rs['code'], $rs['message'], $rs['data']);
  186. }
  187. /**
  188. * @param token
  189. * @param passwd
  190. * @return \think\response\Json
  191. * @throws \think\exception\DbException
  192. */
  193. public function resetPwd()
  194. {
  195. $post = $this->request->post();
  196. $token = isset($post['token']) ? trim($post['token']) : "";
  197. if ($token == "") {
  198. return error_show(101, 'token不能为空');
  199. }
  200. $effetc = VerifyTokens($token);
  201. if (!empty($effetc) && $effetc['code'] != 0) {
  202. return error_show($effetc['code'], $effetc['message']);
  203. }
  204. $newPwd = isset($post['newpwd']) ? trim($post['newpwd']) : "";
  205. if ($newPwd == "") {
  206. return error_show(1001, '新密码不能为空');
  207. }
  208. $oldpwd = isset($post['oldpwd']) ? trim($post['oldpwd']) : "";
  209. if ($oldpwd == "") {
  210. return error_show(1002, "原密码不能为空");
  211. }
  212. $data = resetpasswd($token, ['newpwd' => $newPwd, 'oldpwd' => $oldpwd]);
  213. if (empty($userinfo) || $userinfo['code'] != 0) {
  214. return error_show($userinfo['code'], $userinfo['message']);
  215. } else {
  216. return app_show(0, "密码修改成功");
  217. }
  218. }
  219. //获取所有账号
  220. public function userAll()
  221. {
  222. $post = $this->request->post();
  223. // $token = isset($post['token']) ? trim($post['token']) : "";
  224. // if ($token == "") {
  225. // return app_show(101, 'token不能为空');
  226. // }
  227. // $effetc = VerifyTokens($token);
  228. // if (!empty($effetc) && $effetc['code'] != 0) {
  229. // return app_show($effetc['code'], $effetc['message']);
  230. // }
  231. // if (isset($post['status']) && $post['status'] !== '') {
  232. // $uid = Db::name("user_role")->where(["status" => 1, "is_del" => 0])->column("uid");
  233. // if ($post['status'] == 1) {
  234. // $post['uid'] = $uid;
  235. // } else {
  236. // $post['nouid'] = $uid;
  237. // }
  238. // unset($post['status']);
  239. // }
  240. $post['level'] = 2;
  241. $userCommon = \app\admin\common\User::getIns();
  242. $userlist = $userCommon->handle('userall', $post);
  243. // if (!isset($userlist['code']) || $userlist['code'] != 0)
  244. return json_show($userlist['code'], $userlist['message'], $userlist['data']);
  245. // $userinfo = GetAccountall($token, $post);
  246. // if (empty($userinfo) || $userinfo['code'] != 0) {
  247. // return app_show(1002, "员工信息不存在");
  248. // }
  249. // $all_uid = array_column($userlist['data'],'id');
  250. //所有角色
  251. // $role = Db::name("user_role")
  252. // ->alias('a')
  253. // ->leftJoin('role b', "a.roleid = b.id")
  254. // ->where("a.uid", "in", $all_uid)
  255. // ->column('a.roleid,b.role_name','a.uid');
  256. // $list = [];
  257. // foreach ($userlist['data'] as &$value) {
  258. // $role = Db::name("user_role")
  259. // ->alias('a')
  260. // ->leftJoin('role b', "a.roleid = b.id")
  261. // ->where("a.uid", "=", $value['id'])
  262. // ->field("roleid,role_name,a.status")
  263. // ->find();
  264. // $value['roleid'] = $role[$value['id']]['roleid'] ?? 0;
  265. // $value['role_name'] = $role[$value['id']]['role_name'] ?? '';
  266. // $depart = Db::name("depart_user")->where([["uid", "=", $value['id']], ["is_del", "=", 0]])->column("itemid");
  267. // $value['depart'] = "";
  268. // $value['status'] = isset($role['status']) ? $role['status'] : '0';
  269. // if (!empty($depart)) {
  270. // $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")
  271. // ->select()->toArray();
  272. //
  273. // array_walk($deparname, function (&$v) {
  274. // $v = array_filter(explode("-", $v['depart_link']));
  275. // });
  276. // $value['depart'] = $deparname;
  277. // }
  278. // $list[] = $value;
  279. // }
  280. // return app_show(0, "获取成功", $userlist['data']);
  281. }
  282. /**
  283. * @return \think\response\Json|void
  284. * @throws \think\db\exception\DataNotFoundException
  285. * @throws \think\db\exception\DbException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. * @throws \think\exception\DbException
  288. */
  289. public function userList()
  290. {
  291. $post = $this->request->post();
  292. $token = isset($post['token']) ? trim($post['token']) : "";
  293. if ($token == "") {
  294. return app_show(101, 'token不能为空');
  295. }
  296. $effetc = VerifyTokens($token);
  297. if (!empty($effetc) && $effetc['code'] != 0) {
  298. return app_show($effetc['code'], $effetc['message']);
  299. }
  300. $itemid = isset($post['itemid']) && $post['itemid'] !== "" ? intval($post['itemid']) : "";
  301. if ($itemid !== "") {
  302. $uids = Db::name("depart_user")->where([["itemid", "=", $itemid], ["is_del", "=", 0]])->column("uid");
  303. $post['uid'] = $uids;
  304. }
  305. if (isset($post['status']) && $post['status'] !== '') {
  306. $uid = Db::name("user_role")->where(["status" => 1, "is_del" => 0])->column("uid");
  307. if ($post['status'] == 1) {
  308. $post['uid'] = isset($uids) ? array_unique(array_merge($uids, $uid)) : $uid;
  309. } else {
  310. $post['nouid'] = $uid;
  311. }
  312. unset($post['status']);
  313. }
  314. $userinfo = GetList($token, $post);
  315. if (empty($userinfo) || $userinfo['code'] != 0) {
  316. return app_show($userinfo['code'], $userinfo['msg']);
  317. }
  318. $data = $userinfo['data']['list'];
  319. $list = [];
  320. foreach ($data as $value) {
  321. $role = Db::name("user_role")->alias('a')->leftJoin('role b', "a.roleid = b.id")->where("a.uid", "=", $value['id'])
  322. ->field("roleid,role_name,a.status")->find();
  323. $value['roleid'] = isset($role['roleid']) ? $role['roleid'] : "";
  324. $value['role_name'] = isset($role['role_name']) ? $role['role_name'] : "";
  325. $depart = Db::name("depart_user")->where([["uid", "=", $data['uid']], ["is_del", "=", 0]])->column("itemid");
  326. $value['depart'] = "";
  327. $value['status'] = isset($role['status']) ? $role['status'] : '0';
  328. if (!empty($depart)) {
  329. $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")
  330. ->select()->toArray();
  331. $value['depart'] = $deparname;
  332. $value['depart_code'] = [];
  333. if (!empty($deparname)) {
  334. foreach ($deparname as $item) {
  335. $value['depart_code'][] = array_filter(explode("-", $item['depart_link']));
  336. }
  337. }
  338. }
  339. $list[] = $value;
  340. }
  341. return app_show(0, "获取成功", ["list" => $list, "count" => $userinfo['data']["count"]]);
  342. }
  343. //账户角色列表
  344. public function userRoleList()
  345. {
  346. $post = $this->request->only(["token" => "", "nickname" => "", "relaComNo" => "", "level" => "", "username" => "", "companyNo" => "", "status" => "", "page" => 1, "size" => 10], "post");
  347. if ($this->level == 2) {
  348. // $companyNo = $post['relaComNo'] ?? "";
  349. $post['companyNo'] = $post['relaComNo'] ?? "";
  350. }
  351. $where = [["a.is_del", "=", 0]];
  352. unset($post['relaComNo']);
  353. if ($post['companyNo'] != "") {
  354. $where[] = ["a.companyNo", "=", $post['companyNo']];
  355. }
  356. if ($post['status'] !== '') {
  357. $uid = Db::name("user_role")
  358. ->alias("a")
  359. ->where(["status" => 1, "is_del" => 0])
  360. ->where($where)
  361. ->column("uid");
  362. if ($post['status'] == 1) {
  363. $where[] = ["a.status", "=", 1];
  364. $post['uid'] = $uid;
  365. } else $post['nuid'] = $uid;
  366. unset($post['status']);
  367. }
  368. // if ($post["islevel"] != "") {
  369. // $uid = Db::name("user_role")
  370. // ->alias("a")
  371. // ->leftJoin("role b", "a.roleid=b.id")
  372. // ->where([["b.level", "=", 1], ["a.is_del", "=", 0]])
  373. // ->where($where)
  374. // ->column("uid");
  375. // if ($post['islevel'] == 1) $post['uid'] = $uid;
  376. // else $post['nuid'] = $uid;
  377. //
  378. // unset($post["islevel"]);
  379. // }
  380. // var_dump($post);
  381. // $userinfo=GetUserList($post);
  382. $userCommon = CommonUser::getIns();
  383. $userinfo = $userCommon->GetList($post);
  384. $userinfo = json_decode($userinfo, true);
  385. if (empty($userinfo) || $userinfo['code'] != 0) {
  386. return app_show($userinfo['code'], $userinfo['msg']);
  387. }
  388. $data = $userinfo['data']['list'];
  389. $list = [];
  390. foreach ($data as $value) {
  391. $tmp_where=["uid" => $value['id'],"is_del" => 0];
  392. if ($value['level'] != 1) $tmp_where['a.companyNo'] = ['', $value['companyCode']];
  393. $roleArr = Db::name("user_role")
  394. ->alias("a")
  395. ->leftJoin("role b", "a.roleid=b.id")
  396. ->where($tmp_where)
  397. ->field("role_name,roleid,a.status")
  398. ->order(['a.companyNo'=>'desc'])
  399. ->findOrEmpty();
  400. $value['roleid'] = $roleArr['roleid'] ?? "";
  401. $value['status'] = $value['status'] == 1 ? ($roleArr['status'] ?? 0) : 0;
  402. $value["role_name"] = $roleArr['role_name'] ?? "";
  403. $value["role_level"] = $value['level'] ?? "";
  404. $list[] = $value;
  405. }
  406. return app_show(0, "获取成功", ["list" => $list, "count" => $userinfo['data']["count"]]);
  407. }
  408. //账号角色修改
  409. public function setRole()
  410. {
  411. //if ($this->level == 1) return json_show(1004, '超管账号不允许设置角色');
  412. $post = $this->request->only(["id", "roleid", 'companyNo'], "post", "trim");
  413. //待修改的账户详情
  414. $userCommon = \app\admin\common\User::getIns();
  415. $userinfo = $userCommon->handle('userinfo', ['id' => $post['id']]);
  416. if (!isset($userinfo['code']) || $userinfo['code'] != 0) return json_show($userinfo['code'], $userinfo['message'], $userinfo['data']);
  417. $post['level'] = $userinfo['data']['level'];
  418. if ($post['level'] == 1) return json_show(1004, '超管账号不允许设置角色');
  419. $valide = Validate::rule([
  420. "id|账户ID" => "require|number|gt:0",
  421. // "nickname|名称" => "require|max:255",
  422. // "mobile|手机号" => "require|mobile",
  423. // "relaComNo|业务公司" => "max:255",
  424. "roleid|角色ID" => "require|number|gt:0",
  425. "companyNo|业务公司" => "requireIf:level,2|max:255",
  426. ]);
  427. if ($valide->check($post) == false) return json_show(1004, $valide->getError());
  428. if ($post['roleid'] != 0) {
  429. $roleinfo = Db::name("role")
  430. ->field('id,status')
  431. ->where("id", $post['roleid'])
  432. ->find();
  433. if (empty($roleinfo)) return json_show(1002, "角色不存在");
  434. if ($roleinfo['status'] == 0) return json_show(1002, "角色已禁用");
  435. // if ($roleinfo['level'] == 1) return json_show(1002, "超管级别角色不允许设置");
  436. }
  437. // if ($this->level == 1) $companyNo = isset($post['companyNo']) && $post['companyNo'] !== '' ? trim($post['companyNo']) : "";
  438. // else $companyNo = isset($post['relaComNo']) && $post['relaComNo'] !== '' ? trim($post['relaComNo']) : "";
  439. // if ($companyNo == "") return json_show(1004, "业务公司编号不能为空");
  440. // $company = Db::name("supplier_info")->where(["code" => $companyNo])->find();
  441. // if ($company == false) return json_show(1004, "业务公司不存在");
  442. // $isSuper = Db::name("user_role")->alias("a")
  443. // ->leftJoin("role b", "a.roleid=b.id")
  444. // ->where(["a.uid" => $post['id'], "a.is_del" => 0, "b.level" => 1])
  445. // ->findOrEmpty();
  446. // if (!empty($isSuper)) return json_show(1002, "超管账户不允许设置其他角色");
  447. $where = [
  448. ['uid', '=', $post['id']],
  449. ['is_del', '=', 0],
  450. ];
  451. $is_main = 1;
  452. $company_type = 0;
  453. if ($post['companyNo'] != '') {
  454. $tmp = $userCommon->handle('userCompanyList', ['account_id' => $post['id'], 'companyCode' => $post['companyNo']]);
  455. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  456. if ($tmp['data']['count'] != 1) return json_show(1005, '尚未绑定该公司');
  457. $is_main = $tmp['data']['list'][0]['is_main'];
  458. $company_type = $tmp['data']['list'][0]['company_type'];
  459. $where[] = ['companyNo', '=', $post['companyNo']];
  460. }
  461. $userrole = Db::name("user_role")
  462. ->field('id')
  463. ->where($where)
  464. ->find();
  465. // $ismain = Db::name("user_role")->where(["uid" => $post['id'], "is_main" => 1, "is_del" => 0])->find();
  466. Db::startTrans();
  467. try {
  468. if (empty($userrole)) {
  469. $roledata = [
  470. "uid" => $post['id'],
  471. "roleid" => $post['roleid'],
  472. // "nickname" => $post['nickname'],
  473. "companyNo" => $post['companyNo'] != '' ? $post['companyNo'] : '',
  474. "company_type" => $company_type,
  475. "is_main" => $is_main,
  476. "status" => 1,
  477. "addtime" => date("Y-m-d H:i:s"),
  478. "updatetime" => date("Y-m-d H:i:s")
  479. ];
  480. // $companyarr = [
  481. // [
  482. // "companyCode" => $company["code"],
  483. // "companyName" => $company["name"],
  484. // "company_type" => $company["type"] == 3 ? 2 : 1,
  485. // "is_main" => $ismain ? 0 : 1
  486. // ]
  487. // ];
  488. // $companyinfo = ["id" => $post['id'], "company" => $companyarr, "token" => $post['token']];
  489. // $userCommon = \app\admin\common\User::getIns();
  490. // $companyset = $userCommon->handle('setcompany', $companyinfo);
  491. // if (!isset($companyset['code']) || $companyset['code'] != 0) return json_show(1004, $companyset['message']);
  492. $userrole['id'] = Db::name("user_role")->insertGetId($roledata);
  493. } else {
  494. // $userrole['roleid'] = $post['roleid'];
  495. // $userrole['nickname'] = $post['nickname'];
  496. // $userrole['companyNo'] = $companyNo;
  497. // $userrole['company_type'] = $company["type"] == 3 ? 2 : 1;
  498. // $userrole['updatetime'] = date("Y-m-d H:i:s");
  499. $up = Db::name("user_role")
  500. ->where('id', $userrole['id'])
  501. ->update([
  502. "company_type" => $company_type,
  503. "is_main" => $is_main,
  504. 'roleid' => $post['roleid'],
  505. 'updatetime' => date("Y-m-d H:i:s"),
  506. ]);
  507. }
  508. //设置默认角色
  509. if ($is_main == 1) {
  510. Db::name('user_role')
  511. ->where(["uid" => $post['id'], "is_del" => 0])
  512. ->where('id', '<>', $userrole['id'])
  513. ->update(['is_main' => 0]);
  514. }
  515. // $saveinfo = [
  516. // "id" => $post['id'],
  517. // "nickname" => $post['nickname'],
  518. // "mobile" => $post['mobile'],
  519. // "token" => $post['token']
  520. // ];
  521. // $saev = resetinfo($saveinfo);
  522. // $userCommon = \app\admin\common\User::getIns();
  523. // $saev = $userCommon->handle('usersave', $saveinfo);
  524. // if (!isset($saev['code']) || $saev['code'] != 0) {
  525. // return json_show(1004, $saev['message']);
  526. // }
  527. // if ($up == false) {
  528. // Db::rollback();
  529. // return json_show(1004, '用户信息更新失败');
  530. // }
  531. Db::commit();
  532. return json_show(0, '操作成功');
  533. } catch (\Exception $e) {
  534. Db::rollback();
  535. return json_show(1004, '操作失败,' . $e->getMessage());
  536. }
  537. }
  538. //修改
  539. public function UserSave()
  540. {
  541. /**
  542. * $post = $this->request->post();
  543. * $toke = isset($post['token']) ? trim($post['token']) : "";
  544. * if ($toke == "") {
  545. * return app_show(100, "token不能为空");
  546. * }
  547. * $verify = VerifyTokens($toke);
  548. * if ($verify['code'] != 0) {
  549. * return app_show($verify['code'], $verify['message']);
  550. * }
  551. * $userid = isset($post['id']) && $post['id'] !== "" ? intval($post['id']) : "";
  552. * if ($userid == "") {
  553. * return error_show(1004, "参数id 不能为空");
  554. * }
  555. *
  556. * $nickname = isset($post['nickname']) ? trim($post['nickname']) : "";
  557. * if ($nickname == "") {
  558. * return app_show(1001, "昵称不能为空");
  559. * }
  560. * $mobile = isset($post['mobile']) ? trim($post['mobile']) : "";
  561. * if ($mobile == "") {
  562. * return app_show(1002, "手机号不能为空");
  563. * }
  564. * if (checkMobile($mobile) == false) {
  565. * return app_show(1002, "手机号格式不正确");
  566. * }
  567. * $email = isset($post['email']) ? trim($post['email']) : "";
  568. * $role = isset($post['role']) ? intval($post['role']) : 0;
  569. * if ($role != 0) {
  570. * $roleinfo = Db::name("role")->where("id", "=", $role)->find();
  571. * if (empty($roleinfo)) {
  572. * return error_show(1002, "角色不存在");
  573. * }
  574. * if ($roleinfo['status'] == 0) {
  575. * return error_show(1002, "角色已禁用");
  576. * }
  577. * }
  578. * $itemid = isset($post['itemid']) && $post['itemid'] !== "" ? $post['itemid'] : "";
  579. * $item_arr = [];
  580. * if ($itemid !== "") {
  581. * foreach ($itemid as $value) {
  582. * $item_arr[] = end($value);
  583. * }
  584. * $iteminfo = Db::name("company_item")->where(["id" => $item_arr, 'is_del' => 0])->select()->toArray();
  585. * if (empty($iteminfo)) {
  586. * return error_show(1002, "未找到部门信息");
  587. * }
  588. * }
  589. * $companyArr = isset($post['companyArr']) && !empty($post['companyArr']) ? $post['companyArr'] : "";
  590. * if ($companyArr == "") {
  591. * return error_show(1004, "参数companyArr不能为空");
  592. * }
  593. * $compArr = [];
  594. * foreach ($companyArr as $value) {
  595. * $tmp = [];
  596. * if (!isset($value['businessid']) || $value['businessid'] == '') {
  597. * return error_show(1004, "参数业务公司businessid不能为空");
  598. * }
  599. * $tmp['userid'] = $userid;
  600. * $tmp['businessid'] = $value['businessid'];
  601. * $tmp['is_main'] = isset($value['is_main']) ? $value['is_main'] : 0;
  602. * $tmp['is_del'] = 0;
  603. * $tmp['addtime'] = date("Y-m-d H:i:s");
  604. * $tmp['updatetime'] = date("Y-m-d H:i:s");
  605. * $compArr[] = $tmp;
  606. * }
  607. * $platform = isset($post['platform']) && !empty($post['platform']) ? $post['platform'] : "";
  608. * if ($platform == "") {
  609. * return error_show(1004, "参数platform不能为空");
  610. * }
  611. * $plta = Db::name("platform")->where(["id" => $platform, "is_del" => 0])->select()->toArray();
  612. * if (empty($plta)) {
  613. * return error_show(1004, "未找到可用平台");
  614. * }
  615. * $userplat = Db::name("user_platform")->where(["uid" => $userid, "is_del" => 0])->find();
  616. *
  617. * $isall = isset($post['is_all']) && $post['is_all'] !== "" ? intval($post['is_all']) : "0";
  618. *
  619. * Db::startTrans();
  620. * try {
  621. * $sc = Db::name("user_business")->where(["userid" => $userid, "is_del" => 0])->select()->toArray();
  622. * if (!empty($sc)) {
  623. * $del = Db::name("user_business")->where(["userid" => $userid, "is_del" => 0])->save(["is_del" => 1,
  624. * "updatetime" => date("Y-m-d H:i:s")]);
  625. * if ($del == false) {
  626. * Db::rollback();
  627. * return error_show(1005, '业务公司添加失败');
  628. * }
  629. * }
  630. * if (!empty($compArr)) {
  631. * $in = Db::name("user_business")->insertAll($compArr);
  632. * if ($in == 0) {
  633. * Db::rollback();
  634. * return error_show(1005, '业务公司添加失败');
  635. * }
  636. * }
  637. *
  638. * if ($userplat == false) {
  639. * $userplat = [
  640. * "uid" => $userid,
  641. * "platform" => implode(",", $platform),
  642. * "is_del" => 0,
  643. * "addtime" => date("Y-m-d H:i:s"),
  644. * "updatetime" => date("Y-m-d H:i:s")
  645. * ];
  646. * } else {
  647. * $userplat['platform'] = implode(",", $platform);
  648. * $userplat['updatetime'] = date("Y-m-d H:i:s");
  649. * }
  650. * $patup = Db::name("user_platform")->save($userplat);
  651. * if ($patup == false) {
  652. * Db::rollback();
  653. * return error_show(1005, '业务平台添加失败');
  654. * }
  655. * $userrole = Db::name("user_role")->where(["uid" => $userid, "is_del" => 0])->find();
  656. * if (empty($userrole)) {
  657. * $roledata = [
  658. * "uid" => $userid,
  659. * "roleid" => $role,
  660. * "status" => 1,
  661. * "business_all" => $isall,
  662. * "addtime" => date("Y-m-d H:i:s"),
  663. * "updatetime" => date("Y-m-d H:i:s")
  664. * ];
  665. * $up = Db::name("user_role")->insert($roledata);
  666. * } else {
  667. * $userrole['roleid'] = $role;
  668. * $userrole['business_all'] = $isall;
  669. * $userrole['updatetime'] = date("Y-m-d H:i:s");
  670. * $up = Db::name("user_role")->save($userrole);
  671. * }
  672. * if ($up) {
  673. * Db::name("depart_user")->where(["uid" => $userid])->save(['is_del' => 1]);
  674. * if (isset($item_arr) && !empty($item_arr)) {
  675. * foreach ($item_arr as $v) {
  676. * $depin = [
  677. * "uid" => $userid,
  678. * "nickname" => $nickname,
  679. * "itemid" => $v,
  680. * "is_del" => 0,
  681. * "updatetime" => date("Y-m-d H:i:s")
  682. * ];
  683. * $ist = Db::name("depart_user")->where(["uid" => $userid, "itemid" => $v])->find();
  684. * if ($ist) {
  685. * $up = Db::name("depart_user")->where(["uid" => $userid, "itemid" => $v])->save($depin);
  686. * } else {
  687. * $depin['addtime'] = date("Y-m-d H:i:s");
  688. * $up = Db::name("depart_user")->insert($depin);
  689. * }
  690. * if ($up == false) {
  691. * Db::rollback();
  692. * return error_show(1005, '用户部门更新失败');
  693. * }
  694. * }
  695. * }
  696. * } else {
  697. * Db::rollback();
  698. * return error_show(1005, '角色更新失败');
  699. * }
  700. * $data = [
  701. * "id" => $userid,
  702. * "nickname" => $nickname,
  703. * "mobile" => $mobile,
  704. * "email" => $email
  705. * ];
  706. * $save = resetinfo($toke, $data);
  707. * if (!empty($save) && $save['code'] != 0) {
  708. * Db::rollback();
  709. * return error_show($save['code'], $save['message']);
  710. * }
  711. * Db::commit();
  712. * return app_show(0, "更新成功");
  713. * } catch (\Exception $e) {
  714. * Db::rollback();
  715. * return app_show(1002, $e->getMessage());
  716. * }
  717. *
  718. * **/
  719. $post = $this->request->only(["id", "nickname", "mobile", "email" => "", "portrait" => "", "sex" => ""], "post", 'trim');
  720. $validate = Validate::rule([
  721. 'id|主键ID' => 'require|number|gt:0',
  722. 'nickname|名称' => 'require|max:255',
  723. 'mobile|手机号' => 'require|mobile',
  724. 'email|名称' => 'email',
  725. 'sex|性别' => 'number|in:0,1,2',
  726. ]);
  727. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  728. $userCommon = CommonUser::getIns();
  729. $rs = json_decode($userCommon->userSave($post), true);
  730. return json_show($rs['code'], $rs['message'], $rs['data']);
  731. }
  732. //修改密码
  733. public function setPwd()
  734. {
  735. /**
  736. * $post = $this->request->post();
  737. * $token = isset($post['token']) ? trim($post['token']) : "";
  738. * if ($token == "") {
  739. * return error_show(101, 'token不能为空');
  740. * }
  741. * $effetc = VerifyTokens($token);
  742. * if (!empty($effetc) && $effetc['code'] != 0) {
  743. * return error_show($effetc['code'], $effetc['message']);
  744. * }
  745. * $userid = isset($post['id']) && $post['id'] !== "" ? intval($post['id']) : "";
  746. * if ($userid == "") {
  747. * return error_show(1004, "参数id 不能为空");
  748. * }
  749. * $newPwd = isset($post['password']) ? trim($post['password']) : "";
  750. * if ($newPwd == "") {
  751. * return error_show(1001, '密码不能为空');
  752. * }
  753. *
  754. * $data = resetpwd($token, ["id" => $userid, 'password' => $newPwd]);
  755. * if (empty($data) || $data['code'] != 0) {
  756. * return error_show($data['code'], $data['message']);
  757. * } else {
  758. * return app_show(0, "密码修改成功");
  759. * }
  760. * **/
  761. $post = $this->request->only(["id", "password"], "post", "trim");
  762. $validate = Validate::rule([
  763. 'id|账户ID' => 'require|number',
  764. 'password|密码' => 'require|min:6|max:200',
  765. ]);
  766. if (!$validate->check($post)) return json_show(1004, $validate->getError());
  767. $userCommon = CommonUser::getIns();
  768. $rs = json_decode($userCommon->resetPasswd($post), true);
  769. return json_show($rs['code'], $rs['message'], $rs['data']);
  770. }
  771. //启禁用
  772. public function userStatus()
  773. {
  774. /**
  775. * $post = $this->request->post();
  776. * $toke = isset($post['token']) ? trim($post['token']) : "";
  777. * if ($toke == "") {
  778. * return app_show(100, "token不能为空");
  779. * }
  780. * $verify = VerifyTokens($toke);
  781. * if ($verify['code'] != 0) {
  782. * return app_show($verify['code'], $verify['message']);
  783. * }
  784. * $userid = isset($post['id']) && $post['id'] !== "" ? intval($post['id']) : "";
  785. * if ($userid == "") {
  786. * return error_show(1004, "参数userid 不能为空");
  787. * }
  788. * $status = isset($post['status']) && $post['status'] != "" ? intval($post['status']) : "";
  789. *
  790. * if ($status === "") {
  791. * return error_show(1004, "参数status 不能为空");
  792. * }
  793. * if (!in_array($status, [0, 1])) {
  794. * return error_show(1004, "参数status无效");
  795. * }
  796. * $message = $status == 1 ? "启用" : "禁用";
  797. * $userinfo = Db::name("user_role")->where(["uid" => $userid, "is_del" => 0])->find();
  798. * if ($userinfo == false) {
  799. * $data = [
  800. * "uid" => $userid,
  801. * "roleid" => 0,
  802. * "status" => $status,
  803. * "is_del" => 0,
  804. * "addtime" => date("Y-m-d H:i:s"),
  805. * "updatetime" => date("Y-m-d H:i:s"),
  806. * ];
  807. * $sa = Db::name("user_role")->save($data);
  808. * } else {
  809. * $userinfo['status'] = $status;
  810. * $userinfo['updatetime'] = date("Y-m-d H:i:s");
  811. * $sa = Db::name("user_role")->save($userinfo);
  812. * }
  813. * if ($sa == false) {
  814. * return error_show(1005, "{$message}失败");
  815. * } else {
  816. *
  817. * return app_show(0, "{$message}成功");
  818. * }
  819. *
  820. * // $set = resetstatus($toke,["id"=>$userid,"status"=>$status]);
  821. * //
  822. * // if(empty($set)||$set['code']!=0){
  823. * // return error_show(1005,"{$message}失败");
  824. * // }else{
  825. * // D
  826. * // return app_show(0,"{$message}成功");
  827. * // }
  828. * **/
  829. $post = $this->request->only(["id", "status"], "post", 'trim');
  830. $validate = Validate::rule([
  831. 'id|主键ID' => 'require|number|gt:0',
  832. 'status|状态' => 'require|number|in:0,1',
  833. ]);
  834. if (!$validate->check($post)) return json_show(1004, $validate->getError());
  835. $userCommon = CommonUser::getIns();
  836. $rs = json_decode($userCommon->resetState($post), true);
  837. return json_show($rs['code'], $rs['message'], $rs['data']);
  838. }
  839. public function getinfo()
  840. {
  841. $post = $this->request->post();
  842. $token = isset($post['token']) ? trim($post['token']) : "";
  843. if ($token == "") {
  844. return app_show(101, 'token不能为空');
  845. }
  846. $effetc = VerifyTokens($token);
  847. if (!empty($effetc) && $effetc['code'] != 0) {
  848. return app_show($effetc['code'], $effetc['message']);
  849. }
  850. $userinfo = GetUserInfo($token);
  851. if (empty($userinfo) || $userinfo['code'] != 0) {
  852. return app_show(102, "员工信息不存在");
  853. }
  854. $data = $userinfo['data'];
  855. $role = Db::name("user_role")->alias('a')->leftJoin('role b', "a.roleid = b.id")->where("a.uid", "=", $data['id'])
  856. ->field("roleid,role_name,business_all,uid")->find();
  857. $data['role_name'] = isset($role['role_name']) ? $role['role_name'] : "";
  858. $data['roleid'] = isset($role['roleid']) ? $role['roleid'] : "";
  859. $st = Db::name('user_business')->where(['userid' => $role['uid'], "is_del" => 0])->field('businessid,is_main')->select()->toArray();
  860. $val = [];
  861. if (!empty($st)) {
  862. foreach ($st as $itom) {
  863. $tep = [];
  864. // $business = Db::name('business')->where(['id' => $itom['businessid'], "is_del" => 0])->field("company,companyNo")->find();
  865. //这个地方是根据业务企业id来匹配的,无法去sys_user查找
  866. $tep['company'] = '';//isset($business['company']) ? $business['company'] : "";
  867. $tep['companyNo'] = '';//isset($business['companyNo']) ? $business['companyNo'] : "";
  868. $tep['businessid'] = $itom['businessid'];
  869. $tep['is_main'] = $itom['is_main'];
  870. $val[] = $tep;
  871. }
  872. }
  873. $data['private_field'] = [];
  874. if ($data['roleid'] != "") {
  875. $action = Db::name("role_action")->where(["role_id" => $data['roleid'], "status" => 1])->find();
  876. if ($action != false) {
  877. $data['private_field'] = isset($action['private_field']) && $action['private_field'] != "" ? explode(",", $action['private_field']) : [];
  878. }
  879. }
  880. $data['business'] = $val;
  881. $info = [];
  882. $stn = Db::name('user_platform')->where(['uid' => $role['uid']])->find();
  883. if ($stn != false) {
  884. $plat = isset($stn['platform']) ? explode(",", $stn['platform']) : "";
  885. if ($plat != "") {
  886. $platform = Db::name('platform')->where(['id' => $plat, 'is_del' => 0])->column("id,platform_name,platform_code");
  887. $info = $platform;
  888. }
  889. }
  890. $depart = Db::name("depart_user")->where([["uid", "=", $data['id']], ["is_del", "=", 0]])->column("itemid");
  891. $data['depart'] = "";
  892. if (!empty($depart)) {
  893. $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")->select()->toArray();
  894. if (!empty($deparname)) {
  895. foreach ($deparname as $ky => $item) {
  896. $deparname[$ky]['depart_code'] = array_filter(explode("-", $item['depart_link']));
  897. }
  898. }
  899. $data['depart'] = $deparname;
  900. }
  901. $data['platform'] = $info;
  902. return app_show(0, "获取成功", $data);
  903. }
  904. //超管账号列表
  905. public function userSingleList()
  906. {
  907. $post = $this->request->only(["token" => "", "nickname" => "", "relaComNo" => "", "username" => "", "companyNo" => "", "status" => "", "page" => 1, "size" => 10], "post");
  908. if ($this->level == 1) {
  909. $companyNo = $post['companyNo'] ?? "";
  910. } else {
  911. $companyNo = $post['relaComNo'] ?? "";
  912. $post['companyNo'] = $post['relaComNo'] ?? "";
  913. }
  914. $where = [["a.is_del", "=", 0]];
  915. unset($post['relaComNo']);
  916. if ($post['companyNo'] != "") {
  917. $where[] = ["a.companyNo", "=", $companyNo];
  918. }
  919. // if ($post["islevel"] != "") {
  920. // $uid = Db::name("user_role")->alias("a")
  921. // ->leftJoin("role b", "a.roleid=b.id")->where([["b.level", "=", 1], ["a.is_del", "=", 0]])->where($where)->column
  922. // ("uid");
  923. // if ($post['islevel'] == 1) $post['uid'] = $uid;
  924. // else $post['nuid'] = $uid;
  925. //
  926. // unset($post["islevel"]);
  927. // }
  928. $userCommon = \app\admin\common\User::getIns();
  929. $userinfo = $userCommon->GetAccountall($post);
  930. $userinfo = json_decode($userinfo, true);
  931. if (empty($userinfo) || $userinfo['code'] != 0) return app_show($userinfo['code'], $userinfo['msg']);
  932. $data = $userinfo['data']['list'];
  933. $list = [];
  934. foreach ($data as $value) {
  935. if (isset($value['company_relaton']) && !empty($value['company_relaton'])) {
  936. foreach ($value['company_relaton'] as &$item) {
  937. $roleArr = Db::name("user_role")
  938. ->alias("a")
  939. ->leftJoin("role b", "a.roleid=b.id")
  940. ->where(["uid" => $value['id'], "a.companyNo" => $item['companyCode'], "is_del" => 0])
  941. ->field("role_name,roleid,a.status")
  942. ->findOrEmpty();
  943. $item['roleid'] = $roleArr['roleid'] ?? "";
  944. $item['status'] = $roleArr['status'] ?? 0;
  945. $item["role_name"] = $roleArr['role_name'] ?? "";
  946. $item["role_level"] = $value['level'];
  947. }
  948. }
  949. $value['roleid'] = $roleArr['roleid'] ?? "";
  950. $value['status'] = $value['status'] ?? 0;
  951. $value["role_name"] = $roleArr['role_name'] ?? "";
  952. $value["role_level"] = $value['level'] ?? "";
  953. $list[] = $value;
  954. }
  955. return app_show(0, "获取成功", ["list" => $list, "count" => $userinfo['data']["count"]]);
  956. }
  957. //修改密码
  958. public function passSetByPassword()
  959. {
  960. $param = $this->request->only(['old_pass', 'new_pass'], 'post', 'trim');
  961. $param['uid'] = $this->uid;
  962. $userCommon = \app\admin\common\User::getIns();
  963. $rs = $userCommon->handle('passSetByPassword', $param);
  964. return json_show($rs['code'], $rs['message'], $rs['data']);
  965. }
  966. }