User.php 39 KB

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