UserInfo.php 32 KB

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