UserInfo.php 33 KB

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