UserInfo.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use app\model\Account;
  6. use app\model\AccountCompany;
  7. use app\model\AccountPlat;use think\App;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\facade\Validate;
  11. class UserInfo extends BaseController
  12. {
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. }
  17. /**
  18. * @param string $nickname
  19. * @param string $username
  20. * @param int $status
  21. * @param array $uid
  22. * @param array $nuid
  23. * @param int $page
  24. * @param int $size
  25. * @param string $nickname
  26. * @return \think\response\Json
  27. * @throws \think\db\exception\DbException
  28. */
  29. public function UserList()
  30. {
  31. $post = $this->request->only(["nickname" => "", "username" => "", "status" => "", "companyNo" => "", "uid" => [], "nuid" => [], "page" => 1, "size" => 10, 'level' => ''], "post");
  32. $condition = [["a.is_del", "=", 0]];
  33. isset($post['nickname']) && $post['nickname'] != "" ? $condition[] = ["nickname", "like", "%{$post['nickname']}%"] : "";
  34. isset($post['username']) && $post['username'] != "" ? $condition[] = ["username", "like", "%{$post['username']}%"] : "";
  35. isset($post['status']) && $post['status'] !== "" ? $condition[] = ["a.status", "=", $post['status']] : "";
  36. isset($post['uid']) && !empty($post['uid']) ? $condition[] = ["a.id", "in", $post['uid']] : "";
  37. isset($post['nuid']) && !empty($post['nuid']) ? $condition[] = ["a.id", "not in", $post['nuid']] : "";
  38. isset($post['level']) && !empty($post['level']) ? $condition[] = ["a.level", '=', $post['level']] : "";
  39. if ($post['companyNo'] != "") {
  40. $uid = Db::name("account_company")->where(["companyCode" => $post['companyNo'], "is_del" => 0])->column("account_id");
  41. $condition[] = ["a.id", "in", $uid];
  42. }
  43. $page = isset($post['page']) && $post['page'] !== "" ? intval($post['page']) : 1;
  44. $size = isset($post['size']) && $post['size'] !== "" ? intval($post['size']) : 10;
  45. $count = Db::name("account")
  46. ->alias("a")
  47. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  48. ->where($condition)
  49. ->count('a.id');
  50. $total = intval(ceil($count / $size));
  51. $page = $total >= $page ? $page : $total;
  52. $list = Db::name("account")
  53. ->alias("a")
  54. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  55. ->append(['plat', 'company_relaton'])
  56. ->withAttr('company_relaton', function ($val, $da) {
  57. return Db::name("account_company")
  58. ->where(["account_id" => $da['id'], "is_del" => 0])
  59. ->field("companyCode,companyName,company_type,is_main,status")
  60. ->select()
  61. ->toArray();
  62. })
  63. ->where($condition)
  64. ->page($page, $size)
  65. ->order("a.id desc")
  66. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,a.level")
  67. ->select()
  68. ->toArray();
  69. return json_show(0, "获取成功", ["list" => $list, "count" => $count]);
  70. }
  71. /** @param int $id 账户id
  72. * @return \think\response\Json
  73. */
  74. public function info()
  75. {
  76. $post = $this->request->only(["id" => ""], "post", "intval");
  77. if ($post['id'] == "") {
  78. return json_show(1003, "参数 id 不能为空");
  79. }
  80. $list = Db::name("account")->alias("a")
  81. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  82. ->where(["a.id" => $post['id'], "a.is_del" => 0])
  83. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,a.level")
  84. ->findOrEmpty();
  85. if (empty($list)) {
  86. return json_show(1004, "未找到用户信息");
  87. }
  88. $list['system_version']=(new AccountPlat())->where(['account_id'=>$list['id']])->column('system_version','plat_code');
  89. $list['company_relaton'] = Db::name("account_company")->where(["account_id" => $list['id'], "is_del" => 0, "status" => 1])
  90. ->column("companyCode,companyName,company_type,is_main,status");
  91. return json_show(0, "获取成功", $list);
  92. }
  93. /**
  94. * @return \think\response\Json|void
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function PassSet()
  100. {
  101. $post = $this->request->only(["id" => "", "password" => ""], "post", "trim");
  102. $validate = Validate::rule([
  103. 'id|账户ID' => 'require|number',
  104. 'password|密码' => 'require|min:6|max:200',
  105. ]);
  106. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  107. $account = Db::name("account")->where([["id", "=", $post['id']], ["is_del", "=", "0"]])->find();
  108. if (empty($account)) {
  109. return json_show(1003, "账户不存在");
  110. }
  111. $salt = makeSalt();
  112. $password = sha1($post['password'] . $salt);
  113. $account['password'] = $password;
  114. $account['salt'] = $salt;
  115. $account['is_pass'] = 1;
  116. $account['updatetime'] = date("Y-m-d H:i:s");
  117. $up = Db::name("account")->save($account);
  118. return $up ? json_show(0, "密码修改成功") : json_show(1005, "密码修改失败");
  119. }
  120. /**@param int $id
  121. * @param array $company
  122. * @return \think\response\Json
  123. */
  124. public function setCompany()
  125. {
  126. $post = $this->request->only(["id" => "", "company" => []], "post");
  127. $validate = Validate::rule([
  128. 'id|账户ID' => 'require|number|gt:0',
  129. 'company|业务公司' => 'require|array',
  130. ]);
  131. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  132. $company_insert = [];
  133. $acount = new AccountCompany();
  134. foreach ($post['company'] as $company) {
  135. $ist = $acount->where(["account_id" => $post['id'], "companyCode" => $company['companyCode'], "is_del" => 0])->find();
  136. if ($ist != false) $company['id'] = $ist['id'];
  137. $company_insert[] = [
  138. "id" => $company['id'] ?? null,
  139. "account_id" => $post['id'],
  140. "companyCode" => $company['companyCode'],
  141. "companyName" => $company['companyName'],
  142. "company_type" => $company['company_type'],
  143. "is_main" => $company['is_main'],
  144. "status" => 1,
  145. "is_del" => $company['is_del'] ?? 0,
  146. "addtime" => date("Y-m-d H:i:s"),
  147. "updatetime" => date("Y-m-d H:i:s"),
  148. ];
  149. }
  150. $inser = $acount->saveAll($company_insert);
  151. return $inser ? json_show(0, "关联企业设置成功") : json_show(1005, "关联企业设置失败");
  152. }
  153. /**
  154. * @param int $id
  155. * @param int $status
  156. * @return \think\response\Json
  157. * @throws \think\exception\DbException
  158. */
  159. public function setCompanyStatus()
  160. {
  161. $post = $this->request->only(["account_id" => "", "companyCode" => '', "status" => ""], "post");
  162. $validate = Validate::rule([
  163. 'account_id|账户id' => 'require|number|gt:0',
  164. 'status|状态' => 'require|number|in:0,1',
  165. 'companyCode|公司编号' => 'require',
  166. ]);
  167. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  168. $account = Db::name("account")->where(["id" => $post['account_id'], "is_del" => 0])->findOrEmpty();
  169. if (empty($account)) return json_show(1004, "未找账户到数据");
  170. $acc = new AccountCompany();
  171. $info = $acc->where(["account_id" => $post['account_id'], "companyCode" => $post['companyCode'], "is_del" => 0])
  172. ->findOrEmpty();
  173. if ($info->isEmpty()) {
  174. return json_show(1004, "未找账户到数据");
  175. }
  176. $upda = ["status" => $post['status'], "updatetime" => date("Y-m-d H:i:s")];
  177. $inser = $acc->update($upda, ["account_id" => $post['account_id'], "companyCode" => $post['companyCode'], "is_del" => 0]);
  178. if ($inser == false) return json_show(1005, "关联企业状态设置失败");
  179. $count = $acc->where([["account_id", "=", $post['account_id']], ["status", "<>", $post['status']], ["is_del", "=", 0]])->count();
  180. if ($count == 0 && $account['status'] != $post['status']) Db::name("account")->where(["id" => $post['account_id'], "is_del" => 0])->update($upda);
  181. return json_show(0, "关联企业状态设置成功");
  182. }
  183. /**
  184. * @param int $id
  185. * @param string $nickname
  186. * @param int $mobile
  187. * @param string $email
  188. * @param string $portrait
  189. * @param int $sex
  190. * @return \think\response\Json
  191. */
  192. public function UserSave()
  193. {
  194. $post = $this->request->only([
  195. "id" => "",
  196. "nickname" => "",
  197. "mobile" => "",
  198. "email" => "",
  199. "portrait" => "",
  200. "sex" => "",
  201. ], "post");
  202. $validate = Validate::rule([
  203. 'id|主键ID' => 'require|number|gt:0',
  204. 'nickname|名称' => 'require|max:255',
  205. 'mobile|手机号' => 'require|number|length:11|mobile',
  206. 'email|名称' => 'email',
  207. 'sex|性别' => 'number|in:0,1,2',
  208. ]);
  209. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  210. $account = Db::name("account")->where([["id", "=", $post['id']], ["is_del", "=", 0]])->findOrEmpty();
  211. if (empty($account)) {
  212. return json_show(1003, "账户不存在");
  213. }
  214. $accountinfo = Db::name("user")->where([["account_id", "=", $post['id']]])->findOrEmpty();
  215. if (empty($accountinfo)) {
  216. return json_show(1003, "账户信息不存在");
  217. }
  218. $uiq = Db::table("sys_account")->where([["mobile", "=", $post['mobile']], ["id", "<>", $post['id']], ["is_del", "=", 0]])->find();
  219. if ($uiq) {
  220. return json_show(1002, "手机号已存在!");
  221. }
  222. Db::startTrans();
  223. try {
  224. $userinfo = [
  225. "nickname" => $post['nickname'],
  226. "mobile" => $post['mobile'],
  227. "email" => $post['email'],
  228. "portrait" => $post['portrait'],
  229. "sex" => $post['sex'],
  230. "updatetime" => date("Y-m-d H:i:s")
  231. ];
  232. $dat = Db::name("user")->where($accountinfo)->update($userinfo);
  233. if ($dat == false) {
  234. Db::rollback();
  235. return json_show(1004, "信息修改失败");
  236. }
  237. $acc = [
  238. "id" => $post['id'],
  239. "mobile" => $post['mobile'],
  240. "username" => $post['mobile'],
  241. "updatetime" => date("Y-m-d H:i:s"),
  242. ];
  243. $nu = Db::name("account")->save($acc);
  244. if ($nu) {
  245. Db::commit();
  246. return json_show(0, "信息修改成功");
  247. } else {
  248. Db::rollback();
  249. return json_show(1004, "账户信息修改失败");
  250. }
  251. } catch (\Exception $e) {
  252. Db::rollback();
  253. return json_show(1005, $e->getMessage());
  254. }
  255. }
  256. /**
  257. * @param int $id
  258. * @param int $status
  259. * @return \think\response\Json
  260. * @throws \think\exception\DbException
  261. */
  262. public function UserStatus()
  263. {
  264. $post = $this->request->only(["id" => "", "status" => ""], "post", "trim");
  265. $validate = Validate::rule([
  266. 'id|主键ID' => 'require|number|gt:0',
  267. 'status|状态' => 'require|number|in:0,1',
  268. ]);
  269. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  270. $account = Account::where("id", $post['id'])->findOrEmpty();
  271. if ($account->isEmpty()) {
  272. return json_show(1003, "账户不存在");
  273. }
  274. if ($account['status'] == $post['status']) {
  275. return json_show(1004, "数据已更新");
  276. }
  277. $message = $post['status'] == 1 ? "启用" : "禁用";
  278. Db::startTrans();
  279. try {
  280. $result = Db::name("account")->where("id", "=", $post['id'])->save(['status' => $post['status'], "updatetime" => date("Y-m-d H:i:s")]);
  281. if ($result) {
  282. $ip = AccountCompany::update(['status' => $post['status'], "updatetime" => date("Y-m-d H:i:s")], ["account_id" => $post['id'], "is_del" => 0]);
  283. if ($ip) {
  284. Db::commit();
  285. return json_show(0, "账户{$message}成功");
  286. }
  287. }
  288. Db::rollback();
  289. return json_show(1005, "账户{$message}失败");
  290. } catch (\Exception $e) {
  291. Db::rollback();
  292. return json_show(1004, $e->getMessage());
  293. }
  294. }
  295. //根据业务公司获取用户数据
  296. public function UserListByCompany()
  297. {
  298. $post = $this->request->only(["nickname" => "", "username" => "", "status" => "", "uid" => [], "nuid" => [], "companyNo" => "", "page" => 1, "size" => 10,'level' => ''], "post");
  299. $condition = [["a.is_del", "=", 0]];
  300. // $whereor = [];
  301. isset($post['nickname']) && $post['nickname'] != "" ? $condition[] = ["nickname", "like", "%{$post['nickname']}%"] : "";
  302. isset($post['username']) && $post['username'] != "" ? $condition[] = ["username", "like", "%{$post['username']}%"] : "";
  303. isset($post['status']) && $post['status'] !== "" ? $condition[] = ["a.status", "=", $post['status']] : "";
  304. isset($post['uid']) && !empty($post['uid']) && !empty($post['uid']) ? $condition[] = ["a.id", "in", $post['uid']] : "";
  305. isset($post['nuid']) && !empty($post['nuid']) && !empty($post['nuid']) ? $condition[] = ["a.id", "not in", $post['nuid']] : "";
  306. isset($post['companyNo']) && $post['companyNo'] !== "" ? $condition[] = ["c.companyCode", "=", $post['companyNo']] : '';//$whereor[] = ["c.companyCode", "=", null];
  307. if ($post['level'] !== '') $condition[] = ['a.level', '=', $post['level']];
  308. $page = isset($post['page']) && $post['page'] !== "" ? intval($post['page']) : 1;
  309. $size = isset($post['size']) && $post['size'] !== "" ? intval($post['size']) : 10;
  310. $count = Db::name("account")
  311. ->alias("a")
  312. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  313. ->leftJoin("account_company c", "a.id=c.account_id and c.is_del=0")
  314. ->where($condition)
  315. ->count();
  316. $total = intval(ceil($count / $size));
  317. $page = $total >= $page ? $page : $total;
  318. $list = Db::name("account")
  319. ->alias("a")
  320. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,companyCode,companyName,company_type,is_main,c.status as com_status,a.level")
  321. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  322. ->leftJoin("account_company c", "a.id=c.account_id and c.is_del=0")
  323. ->where($condition)
  324. // ->whereOr($whereor)
  325. ->page($page, $size)
  326. ->append(['plat', 'company_relaton'])
  327. ->withAttr('company_relaton', function ($val, $da) {
  328. return Db::name("account_company")
  329. ->where(["account_id" => $da['id'], "is_del" => 0])
  330. ->field("companyCode,companyName,company_type,is_main,status")
  331. ->select()
  332. ->toArray();
  333. })
  334. ->order("a.addtime desc")
  335. ->select()
  336. ->toArray();
  337. return json_show(0, "获取成功", ["list" => $list, "count" => $count]);
  338. }
  339. /**
  340. * @return \think\response\Json
  341. * @throws \think\db\exception\DataNotFoundException
  342. * @throws \think\db\exception\DbException
  343. * @throws \think\db\exception\ModelNotFoundException
  344. */
  345. public function userAdd()
  346. {
  347. $post = $this->request->only(["nickname" => "", "mobile" => "", "email" => "", "companyArr" => [],'level'], "post", "trim");
  348. $validate = Validate::rule([
  349. 'nickname|真实姓名' => 'require|min:2|max:200',
  350. 'mobile|手机号' => 'require|number|length:11|mobile',
  351. 'email|邮箱' => 'email',
  352. 'level|账号等级' => 'require|number|in:1,2,3',
  353. 'companyArr|关联公司' => 'requireIf:level,2|requireIf:level,3|array',
  354. ]);
  355. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  356. $uiq = Db::table("sys_account")->field('id')->where(["mobile" => $post['mobile']])->find();
  357. if ($uiq) return json_show(1002, "手机号已注册!");
  358. Db::startTrans();
  359. try {
  360. $date = date('Y-m-d H:i:s');
  361. $salt = makeSalt();
  362. $password = sha1("dingding123" . $salt);
  363. $da = [
  364. 'username' => $post['mobile'],
  365. "password" => $password,
  366. "salt" => $salt,
  367. "mobile" => $post['mobile'],
  368. "source" => "paltadd",
  369. "status" => 1,
  370. 'level'=>$post['level'],
  371. "addtime" => $date,
  372. "updatetime" => $date
  373. ];
  374. $reuslt = Db::table('sys_account')->insert($da, true);
  375. if ($reuslt) {
  376. $data = [
  377. "nickname" => $post['nickname'],
  378. "mobile" => $post['mobile'],
  379. "email" => $post['email'],
  380. "portrait" => "",
  381. "sex" => 1,
  382. "post" => "",
  383. "department" => "",
  384. "account_id" => $reuslt,
  385. "status" => 1,
  386. "addtime" => $date,
  387. "updatetime" => $date
  388. ];
  389. $user = Db::table("sys_user")->insert($data);
  390. if ($user != false) {
  391. $acount = new AccountCompany();
  392. if (!empty($post['companyArr'])) {
  393. // //判断关联条件是否都是供应商
  394. // $all_companyNo = array_column($post['companyArr'], 'companyCode');
  395. //
  396. // $tmp = Db::name('headquarters')
  397. // ->where(['is_del' => 0, 'status' => 1])
  398. // ->whereFindInSet('type', '1')
  399. // ->column('code');
  400. // $temp = array_diff($all_companyNo, $tmp);
  401. // if (!empty($temp)) throw new Exception(implode(',', $temp) . '不是业务公司');
  402. $company_insert = [];
  403. foreach ($post['companyArr'] as $company) {
  404. $company_insert[] = [
  405. "account_id" => $reuslt,
  406. "companyCode" => $company['companyCode'],
  407. "companyName" => $company['companyName'],
  408. "company_type" => $company['company_type'],
  409. "is_main" => $company['is_main'],
  410. "status" => 1,
  411. "is_del" => 0,
  412. "addtime" => $date,
  413. "updatetime" => $date,
  414. ];
  415. }
  416. $u = $acount->saveAll($company_insert);
  417. } else {
  418. $company_insert = [
  419. "account_id" => $reuslt,
  420. "companyCode" => '',
  421. "companyName" => '',
  422. "company_type" => '0',
  423. "is_main" => 1,
  424. "status" => 1,
  425. "is_del" => 0,
  426. "addtime" => date("Y-m-d H:i:s"),
  427. "updatetime" => date("Y-m-d H:i:s"),
  428. ];
  429. $u = $acount->save($company_insert);
  430. }
  431. if ($u == false) throw new Exception("账户新建失败");
  432. Db::commit();
  433. return json_show(0, "账户注册成功", ["userid" => $reuslt, "nickname" => $post['nickname']]);
  434. }
  435. }
  436. Db::rollback();
  437. return json_show(1002, "账户注册失败");
  438. } catch (\Exception $e) {
  439. Db::rollback();
  440. return json_show(1002, "账户注册失败," . $e->getMessage());
  441. }
  442. }
  443. //添加超管账号
  444. // public function addAdminAccount()
  445. // {
  446. //
  447. // $post = $this->request->filter('trim')->post();
  448. //
  449. // $tmp = Db::table("sys_account")
  450. // ->field('id')
  451. // ->where(["mobile" => $post['mobile'], 'is_del' => 0])
  452. // ->findOrEmpty();
  453. // if ($tmp) return json_show(1002, "手机号已注册");
  454. //
  455. // Db::startTrans();
  456. // try {
  457. // $date = date("Y-m-d H:i:s");
  458. // $salt = makeSalt();
  459. // $password = sha1("dingding123" . $salt);
  460. // $da = [
  461. // 'username' => $post['mobile'],
  462. // "password" => $password,
  463. // "salt" => $salt,
  464. // "mobile" => $post['mobile'],
  465. // "source" => "paltadd",
  466. // "status" => 1,
  467. // "level" => 1,
  468. // "addtime" => date("Y-m-d H:i:s"),
  469. // "updatetime" => date("Y-m-d H:i:s")
  470. // ];
  471. // $reuslt = Db::table('sys_account')->insert($da, true);
  472. // if ($reuslt) {
  473. // $data = [
  474. // "nickname" => $post['nickname'],
  475. // "mobile" => $post['mobile'],
  476. // "email" => $post['email'],
  477. // "portrait" => "",
  478. // "sex" => 1,
  479. // "post" => "",
  480. // "department" => "",
  481. // "account_id" => $reuslt,
  482. // "status" => 1,
  483. // "addtime" => $date,
  484. // "updatetime" => $date
  485. // ];
  486. // $user = Db::table("sys_user")->insert($data);
  487. // if ($user != false) {
  488. // $acount = new AccountCompany();
  489. // if (!empty($post['companyArr'])) {
  490. // $company_insert = [];
  491. // foreach ($post['companyArr'] as $company) {
  492. // $company_insert[] = [
  493. // "account_id" => $reuslt,
  494. // "companyCode" => $company['companyCode'],
  495. // "companyName" => $company['companyName'],
  496. // "company_type" => $company['company_type'],
  497. // "is_main" => $company['is_main'],
  498. // "status" => 1,
  499. // "is_del" => 0,
  500. // "addtime" => $date,
  501. // "updatetime" => $date,
  502. // ];
  503. // }
  504. // $u = $acount->saveAll($company_insert);
  505. // } else {
  506. // $company_insert = [
  507. // "account_id" => $reuslt,
  508. // "companyCode" => '',
  509. // "companyName" => '',
  510. // "company_type" => '0',
  511. // "is_main" => 1,
  512. // "status" => 1,
  513. // "is_del" => 0,
  514. // "addtime" => $date,
  515. // "updatetime" => $date,
  516. // ];
  517. // $u = $acount->save($company_insert);
  518. // }
  519. //
  520. // if ($u == false) throw new Exception("账户新建失败");
  521. // Db::commit();
  522. // return json_show(0, "账户注册成功", ["userid" => $reuslt, "nickname" => $post['nickname']]);
  523. // }
  524. // }
  525. // Db::rollback();
  526. // return json_show(1002, "账户注册失败");
  527. //
  528. // } catch (\Exception $e) {
  529. // Db::rollback();
  530. // return json_show(1002, "账户注册失败" . $e->getMessage());
  531. // }
  532. //
  533. // }
  534. //添加供应商账号
  535. // public function addSupplierAccount()
  536. // {
  537. //
  538. // $post = $this->request->filter('trim')->post();
  539. //
  540. // $tmp = Db::table("sys_account")
  541. // ->field('id')
  542. // ->where(["mobile" => $post['mobile'], 'is_del' => 0])
  543. // ->findOrEmpty();
  544. // if ($tmp) return json_show(1002, "手机号已注册");
  545. //
  546. // Db::startTrans();
  547. //
  548. // try {
  549. // $date = date("Y-m-d H:i:s");
  550. // $salt = makeSalt();
  551. // $password = sha1("dingding123" . $salt);
  552. // $da = [
  553. // 'username' => $post['mobile'],
  554. // "password" => $password,
  555. // "salt" => $salt,
  556. // "mobile" => $post['mobile'],
  557. // "source" => "paltadd",
  558. // "status" => 1,
  559. // "level" => 3,//供应商端账号
  560. // "addtime" => date("Y-m-d H:i:s"),
  561. // "updatetime" => date("Y-m-d H:i:s")
  562. // ];
  563. // $reuslt = Db::table('sys_account')->insert($da, true);
  564. // if ($reuslt) {
  565. // $data = [
  566. // "nickname" => $post['nickname'],
  567. // "mobile" => $post['mobile'],
  568. // "email" => $post['email'],
  569. // "portrait" => "",
  570. // "sex" => 1,
  571. // "post" => "",
  572. // "department" => "",
  573. // "account_id" => $reuslt,
  574. // "status" => 1,
  575. // "addtime" => $date,
  576. // "updatetime" => $date
  577. // ];
  578. // $user = Db::table("sys_user")->insert($data);
  579. // if ($user != false) {
  580. //
  581. // //判断关联条件是否都是供应商
  582. // $all_companyNo = array_column($post['companyArr'], 'companyCode');
  583. //
  584. // $tmp = Db::name('headquarters')
  585. // ->where(['is_del' => 0, 'status' => 1])
  586. // ->whereFindInSet('type', '3')
  587. // ->column('code');
  588. // $temp = array_diff($all_companyNo, $tmp);
  589. // if (!empty($temp)) throw new Exception(implode(',', $temp) . '不是供应商');
  590. //
  591. // $company_insert = [];
  592. // foreach ($post['companyArr'] as $company) {
  593. // $company_insert[] = [
  594. // "account_id" => $reuslt,
  595. // "companyCode" => $company['companyCode'],
  596. // "companyName" => $company['companyName'],
  597. // "company_type" => 1,
  598. // "is_main" => $company['is_main'],
  599. // "status" => 1,
  600. // "is_del" => 0,
  601. // "addtime" => $date,
  602. // "updatetime" => $date,
  603. // ];
  604. // }
  605. //
  606. // if ($company_insert) Db::name('account_company')->insertAll($company_insert);
  607. //
  608. // Db::commit();
  609. // return json_show(0, "添加供应商账号成功", ["userid" => $reuslt, "nickname" => $post['nickname']]);
  610. // }
  611. // }
  612. // Db::rollback();
  613. // return json_show(1002, "添加供应商账号失败");
  614. //
  615. // } catch (Exception $e) {
  616. // Db::rollback();
  617. // return json_show(1002, "添加供应商账号失败," . $e->getMessage());
  618. // }
  619. //
  620. // }
  621. //修改密码通过旧密码
  622. public function passSetByPassword()
  623. {
  624. $param = $this->request->only(['uid', 'old_pass', 'new_pass'], 'post', 'trim');
  625. $val = Validate::rule([
  626. 'uid|用户ID' => 'require|number|gt:0',
  627. 'old_pass|旧密码' => 'require|max:255',
  628. 'new_pass|新密码' => 'require|min:6|max:255',
  629. ]);
  630. if ($val->check($param) == false) return json_show(1004, $val->getError());
  631. $acc = Db::name("account")
  632. ->field('id,password,salt,status')
  633. ->where(['id' => $param['uid'], "is_del" => Account::$account_del])
  634. ->findOrEmpty();
  635. if (empty($acc)) return json_show(1003, '账户不存在');
  636. if ($acc['status'] == Account::$account_end) return json_show(1003, '账户已禁用');
  637. $sha1 = sha1($param['old_pass'] . $acc['salt']);
  638. if ($sha1 != $acc['password']) return json_show(1003, '密码错误');
  639. $salt = makeSalt();
  640. $password = sha1($param['new_pass'] . $salt);
  641. $rs = Db::name('account')
  642. ->where(['id' => $param['uid'], "is_del" => Account::$account_del])
  643. ->update([
  644. 'password' => $password,
  645. 'salt' => $salt,
  646. 'updatetime' => date('Y-m-d H:i:s')
  647. ]);
  648. return $rs ? json_show(0, '修改密码成功') : json_show(1004, '修改密码失败');
  649. }
  650. //获取用户所绑定的公司列表
  651. public function getCompanyList(){
  652. $post = $this->request->only(["uid" => 0, "companyCode" => "","companyName" => "", "page" => 1, "size" => 20], "post", "trim");
  653. $where=[['a.is_del','=',0],['a.account_id','=',$post['uid']]];
  654. if($post['companyCode']!='') $where[]=['a.companyCode','like','%'.$post['companyCode'].'%'];
  655. if($post['companyName']!='') $where[]=['a.companyName','like','%'.$post['companyName'].'%'];
  656. $count = Db::name('account_company')
  657. ->alias('a')
  658. ->where($where)
  659. ->count('a.id');
  660. $list = Db::name('account_company')
  661. ->alias('a')
  662. ->field('a.*,b.level')
  663. ->where($where)
  664. ->leftJoin('account b','b.id=a.account_id')
  665. ->order(['a.id' => 'asc'])
  666. ->page(intval($post['page']), intval($post['size']))
  667. ->select()
  668. ->toArray();
  669. return json_show(0, '获取用户所绑定的公司列表成功', ['count' => $count, 'list' => $list]);
  670. }
  671. //所有账号
  672. public function userAll()
  673. {
  674. $post = $this->request->filter('trim')->post();
  675. $condition = [["a.is_del", "=", 0]];
  676. isset($post['nickname']) && $post['nickname'] != "" ? $condition[] = ["nickname", "like", "%{$post['nickname']}%"] : "";
  677. isset($post['username']) && $post['username'] != "" ? $condition[] = ["username", "like", "%{$post['username']}%"] : "";
  678. // isset($post['status']) && $post['status'] !== "" ? $condition[] = ["a.status", "=", $post['status']] : "";
  679. // isset($post['uid']) && !empty($post['uid']) ? $condition[] = ["a.id", "in", $post['uid']] : "";
  680. // isset($post['nuid']) && !empty($post['nuid']) ? $condition[] = ["a.id", "not in", $post['nuid']] : "";
  681. isset($post['level']) && !empty($post['level']) ? $condition[] = ["a.level", '=', $post['level']] : "";
  682. $list = Db::name("account")
  683. ->field("a.id,a.username,a.mobile,a.status,b.nickname,a.level")
  684. ->alias("a")
  685. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  686. ->where($condition)
  687. ->order(['a.id' => 'desc'])
  688. ->select()
  689. ->toArray();
  690. return json_show(0, "获取成功", $list);
  691. }
  692. //检查供应商是否有开通账号
  693. public function checkHasAccountBySupplierNos()
  694. {
  695. $supplierNos = $this->request->post('supplierNo', '', 'trim');
  696. $where = [
  697. ['a.is_del', '=', 0],
  698. ['a.status', '=', 1],
  699. ['b.level', '=', 3],
  700. ['a.companyCode', 'like', 'QS%']
  701. ];
  702. if ($supplierNos !== '') $where[] = ['a.companyCode', 'in', $supplierNos];
  703. $list = Db::name('account_company')
  704. ->alias('a')
  705. ->leftJoin('account b', 'b.id=a.account_id')
  706. ->where($where)
  707. ->column('a.account_id', 'a.companyCode');
  708. return json_show(0, '获取供应商账号开通情况成功', $list);
  709. }
  710. }