User.php 38 KB

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