UserInfo.php 31 KB

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