UserInfo.php 30 KB

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