UserInfo.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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\facade\Db;
  9. use think\facade\Validate;
  10. class UserInfo extends BaseController
  11. {
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. }
  16. /**
  17. * @param string $nickname
  18. * @param string $username
  19. * @param int $status
  20. * @param array $uid
  21. * @param array $nuid
  22. * @param int $page
  23. * @param int $size
  24. * @param string $nickname
  25. * @return \think\response\Json
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function UserList()
  29. {
  30. $post = $this->request->only(["nickname" => "", "username" => "", "status" => "", "uid" => [], "nuid" => [], "page" => 1, "size" => 10], "post");
  31. $condition = [["a.is_del", "=", 0]];
  32. isset($post['nickname']) && $post['nickname'] != "" ? $condition[] = ["nickname", "like", "%{$post['nickname']}%"] : "";
  33. isset($post['username']) && $post['username'] != "" ? $condition[] = ["username", "like", "%{$post['username']}%"] : "";
  34. isset($post['status']) && $post['status'] !== "" ? $condition[] = ["a.status", "=", $post['status']] : "";
  35. isset($post['uid']) && $post['uid'] !== "" ? $condition[] = ["a.id", "in", $post['uid']] : "";
  36. isset($post['nuid']) && $post['nuid'] !== "" ? $condition[] = ["a.id", "not in", $post['nuid']] : "";
  37. $page = isset($post['page']) && $post['page'] !== "" ? intval($post['page']) : 1;
  38. $size = isset($post['size']) && $post['size'] !== "" ? intval($post['size']) : 10;
  39. $count = Db::name("account")->alias("a")
  40. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  41. ->where($condition)->count();
  42. $total = intval(ceil($count / $size));
  43. $page = $total >= $page ? $page : $total;
  44. $list = Db::name("account")->alias("a")
  45. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  46. ->where($condition)->page($page, $size)
  47. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime")
  48. ->cursor();
  49. $data = [];
  50. foreach ($list as $item) {
  51. $item['plat'] = Db::name("account_plat")->alias("a")
  52. ->leftJoin("platform b", "a.plat_code=b.plat_code and b.is_del=0 and b.status=1")
  53. ->where(["a.status" => 1, "a.is_del" => 0, "a.account_id" => $item['id']])->column("a.plat_code,plat_name");
  54. $item['company_relaton'] = Db::name("account_company")->where(["account_id" => $item['id'], "is_del" => 0, "status" => 1])
  55. ->column("companyCode,companyName,company_type,is_main,status");
  56. $data[] = $item;
  57. }
  58. return json_show(0, "获取成功", ["list" => $data, "count" => $count]);
  59. }
  60. /** @param int $id 账户id
  61. * @return \think\response\Json
  62. */
  63. public function info()
  64. {
  65. $post = $this->request->only(["id" => ""], "post", "intval");
  66. if ($post['id'] == "") {
  67. return json_show(1003, "参数 id 不能为空");
  68. }
  69. $list = Db::name("account")->alias("a")
  70. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  71. ->where(["a.id" => $post['id'], "a.is_del" => 0])
  72. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime")
  73. ->findOrEmpty();
  74. if (empty($list)) {
  75. return json_show(1004, "未找到用户信息");
  76. }
  77. $list['plat'] = Db::name("account_plat")->alias("a")
  78. ->leftJoin("platform b", "a.plat_code=b.plat_code and b.status=1")
  79. ->where(["a.status" => 1, "a.is_del" => 0, "a.account_id" => $list['id']])->column("a.plat_code,plat_name");
  80. $list['company_relaton'] = Db::name("account_company")->where(["account_id" => $list['id'], "is_del" => 0, "status" => 1])
  81. ->column("companyCode,companyName,company_type,is_main,status");
  82. return json_show(0, "获取成功", $list);
  83. }
  84. /**
  85. * @return \think\response\Json|void
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function PassSet()
  91. {
  92. $post = $this->request->only(["id" => "", "password" => ""], "post", "trim");
  93. $validate = Validate::rule([
  94. 'id|账户ID' => 'require|number',
  95. 'password|密码' => 'require|min:6|max:200',
  96. ]);
  97. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  98. $account = Db::name("account")->where("id", "=", $post['id'])->find();
  99. if (empty($account)) {
  100. return json_show(1003, "账户不存在");
  101. }
  102. $salt = makeSalt();
  103. $password = sha1($post['password'] . $salt);
  104. $account['password'] = $password;
  105. $account['salt'] = $salt;
  106. $account['is_pass'] = 1;
  107. $account['updatetime'] = date("Y-m-d H:i:s");
  108. $up = Db::name("account")->save($account);
  109. return $up ? json_show(0, "密码修改成功") : json_show(1005, "密码修改失败");
  110. }
  111. /**@param int $id
  112. * @param array $company
  113. * @return \think\response\Json
  114. */
  115. public function setCompany()
  116. {
  117. $post = $this->request->only(["id" => "", "company" => []], "post");
  118. $validate = Validate::rule([
  119. 'id|账户ID' => 'require|number|gt:0',
  120. 'company|业务公司' => 'require|array',
  121. ]);
  122. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  123. $company_insert = [];
  124. $acount = new AccountCompany();
  125. foreach ($post['company'] as $company) {
  126. $ist = $acount->where(["account_id" => $post['id'], "companyCode" => $company['companyCode']])->find();
  127. if ($ist != false) $company['id'] = $ist['id'];
  128. $company_insert[] = [
  129. "id" => $company['id'] ?? null,
  130. "account_id" => $post['id'],
  131. "companyCode" => $company['companyCode'],
  132. "companyName" => $company['companyName'],
  133. "company_type" => $company['company_type'],
  134. "is_main" => $company['is_main'],
  135. "status" => 1,
  136. "is_del" => $company['is_del'] ?? 0,
  137. "addtime" => date("Y-m-d H:i:s"),
  138. "updatetime" => date("Y-m-d H:i:s"),
  139. ];
  140. }
  141. $inser = $acount->saveAll($company_insert);
  142. return $inser ? json_show(0, "关联企业设置成功") : json_show(1005, "关联企业设置失败");
  143. }
  144. /**
  145. * @param int $id
  146. * @param int $status
  147. * @return \think\response\Json
  148. * @throws \think\exception\DbException
  149. */
  150. public function setCompanyStatus()
  151. {
  152. $post = $this->request->only(["id" => "", "status" => ""], "post", "intval");
  153. $validate = Validate::rule([
  154. 'id|主键ID' => 'require|number|gt:0',
  155. 'status|状态' => 'require|number|in:0,1',
  156. ]);
  157. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  158. $info = AccountCompany::where(["id" => $post['id']])->find();
  159. if ($info == false) {
  160. return json_show(1004, "未找到数据");
  161. }
  162. if ($info['status'] == $post['status']) {
  163. return json_show(1004, "数据已更新");
  164. }
  165. $info['status'] = $post['status'];
  166. $info['updatetime'] = date("Y-m-d H:i:s");
  167. $inser = AccountCompany::update($info->toArray());
  168. return $inser ? json_show(0, "关联企业状态设置成功") : json_show(1005, "关联企业状态设置失败");
  169. }
  170. /**
  171. * @param int $id
  172. * @param string $nickname
  173. * @param int $mobile
  174. * @param string $email
  175. * @param string $portrait
  176. * @param int $sex
  177. * @return \think\response\Json
  178. */
  179. public function UserSave()
  180. {
  181. $post = $this->request->only([
  182. "id" => "",
  183. "nickname" => "",
  184. "mobile" => "",
  185. "email" => "",
  186. "portrait" => "",
  187. "sex" => "",
  188. ], "post");
  189. $validate = Validate::rule([
  190. 'id|主键ID' => 'require|number|gt:0',
  191. 'nickname|名称' => 'require|max:255',
  192. 'mobile|手机号' => 'require|number|length:11|mobile',
  193. 'email|名称' => 'email',
  194. 'sex|性别' => 'number|in:0,1,2',
  195. ]);
  196. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  197. $account = Db::name("account")->where([["id", "=", $post['id']], ["is_del", "=", 0]])->findOrEmpty();
  198. if (empty($account)) {
  199. return json_show(1003, "账户不存在");
  200. }
  201. $accountinfo = Db::name("user")->where([["account_id", "=", $post['id']]])->findOrEmpty();
  202. if (empty($accountinfo)) {
  203. return json_show(1003, "账户信息不存在");
  204. }
  205. Db::startTrans();
  206. try {
  207. $userinfo = [
  208. "nickname" => $post['nickname'],
  209. "mobile" => $post['mobile'],
  210. "email" => $post['email'],
  211. "portrait" => $post['portrait'],
  212. "sex" => $post['sex'],
  213. "updatetime" => date("Y-m-d H:i:s")
  214. ];
  215. $dat = Db::name("user")->where($accountinfo)->update($userinfo);
  216. if ($dat == false) {
  217. Db::rollback();
  218. return json_show(1004, "信息修改失败");
  219. }
  220. $acc = [
  221. "id" => $post['id'],
  222. "mobile" => $post['mobile'],
  223. "username" => $post['mobile'],
  224. "updatetime" => date("Y-m-d H:i:s"),
  225. ];
  226. $nu = Db::name("account")->save($acc);
  227. if ($nu) {
  228. Db::commit();
  229. return json_show(0, "信息修改成功");
  230. } else {
  231. Db::rollback();
  232. return json_show(1004, "账户信息修改失败");
  233. }
  234. } catch (\Exception $e) {
  235. Db::rollback();
  236. return json_show(1005, $e->getMessage());
  237. }
  238. }
  239. /**
  240. * @param int $id
  241. * @param int $status
  242. * @return \think\response\Json
  243. * @throws \think\exception\DbException
  244. */
  245. public function UserStatus()
  246. {
  247. $post = $this->request->only(["id" => "", "status" => ""], "post", "trim");
  248. $validate = Validate::rule([
  249. 'id|主键ID' => 'require|number|gt:0',
  250. 'status|状态' => 'require|number|in:0,1',
  251. ]);
  252. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  253. $account = Account::where("id", $post['id'])->findOrEmpty();
  254. if (empty($account)) {
  255. return json_show(1003, "账户不存在");
  256. }
  257. if ($account['status'] == $post['status']) {
  258. return json_show(1004, "数据已更新");
  259. }
  260. $message = $post['status'] == 1 ? "启用" : "禁用";
  261. $result = Db::name("account")->where("id", "=", $post['id'])->save(['status' => $post['status'], "updatetime" => date("Y-m-d
  262. H:i:s")]);
  263. return $result ? json_show(0, "账户{$message}成功") : json_show(1005, "账户{$message}失败");
  264. }
  265. public function UserListByCompany()
  266. {
  267. $post = $this->request->only(["nickname" => "", "username" => "", "status" => "", "uid" => [], "nuid" => [], "companyNo" => "", "page" => 1, "size" => 10], "post");
  268. $condition = [["a.is_del", "=", 0]];
  269. $whereor = [];
  270. isset($post['nickname']) && $post['nickname'] != "" ? $condition[] = ["nickname", "like", "%{$post['nickname']}%"] : "";
  271. isset($post['username']) && $post['username'] != "" ? $condition[] = ["username", "like", "%{$post['username']}%"] : "";
  272. isset($post['status']) && $post['status'] !== "" ? $condition[] = ["a.status", "=", $post['status']] : "";
  273. isset($post['uid']) && $post['uid'] !== "" && !empty($post['uid']) ? $condition[] = ["a.id", "in", $post['uid']] : "";
  274. isset($post['nuid']) && $post['nuid'] !== "" && !empty($post['nuid']) ? $condition[] = ["a.id", "not in", $post['nuid']] : "";
  275. isset($post['companyNo']) && $post['companyNo'] !== "" ? $condition[] = ["c.companyCode", "=", $post['companyNo']]
  276. : $whereor[] = ["c.companyCode", "=", null];
  277. $page = isset($post['page']) && $post['page'] !== "" ? intval($post['page']) : 1;
  278. $size = isset($post['size']) && $post['size'] !== "" ? intval($post['size']) : 10;
  279. $count = Db::name("account")->alias("a")
  280. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  281. ->leftJoin("account_company c", "a.id=c.account_id and c.status=1 and c.is_del=0")
  282. ->where($condition)->whereOr($whereor)->count();
  283. $total = intval(ceil($count / $size));
  284. $page = $total >= $page ? $page : $total;
  285. $list = Db::name("account")->alias("a")
  286. ->leftJoin("user b", "a.id=b.account_id and b.status=1")
  287. ->leftJoin("account_company c", "a.id=c.account_id and c.is_del=0")
  288. ->where($condition)->whereOr($whereor)->page($page, $size)->order("c.addtime desc,a.addtime desc")
  289. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,c.companyCode,c.companyName,c.is_main,c.status as com_status")
  290. ->cursor();
  291. $data = [];
  292. foreach ($list as $item) {
  293. $item['plat'] = Db::name("account_plat")->alias("a")
  294. ->leftJoin("platform b", "a.plat_code=b.plat_code and b.is_del=0 and b.status=1")
  295. ->where(["a.status" => 1, "a.is_del" => 0, "a.account_id" => $item['id']])->column("a.plat_code,plat_name");
  296. $item['company_relaton'] = Db::name("account_company")->where(["account_id" => $item['id'], "is_del" => 0])
  297. ->column("companyCode,companyName,company_type,is_main,status");
  298. $data[] = $item;
  299. }
  300. return json_show(0, "获取成功", ["list" => $data, "count" => $count]);
  301. }
  302. /**
  303. * @return \think\response\Json
  304. * @throws \think\db\exception\DataNotFoundException
  305. * @throws \think\db\exception\DbException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. public function userAdd()
  309. {
  310. $post = $this->request->only(["nickname" => "", "mobile" => "", "email" => "", "companyArr" => []], "post", "trim");
  311. $validate = Validate::rule([
  312. 'nickname|真实姓名' => 'require|min:2|max:200',
  313. 'mobile|手机号' => 'require|number|length:11|mobile',
  314. 'email|邮箱' => 'email',
  315. 'companyArr|关联业务公司' => 'array',
  316. ]);
  317. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  318. Db::startTrans();
  319. $uiq = Db::table("sys_account")->where(["mobile" => $post['mobile']])->find();
  320. if ($uiq) {
  321. return json_show(1002, "手机号已注册!");
  322. }
  323. try {
  324. $salt = makeSalt();
  325. $password = sha1("dingding123" . $salt);
  326. $data = [
  327. 'username' => $post['mobile'],
  328. "password" => $password,
  329. "salt" => $salt,
  330. "mobile" => $post['mobile'],
  331. "source" => "paltadd",
  332. "status" => 1,
  333. "addtime" => date("Y-m-d H:i:s"),
  334. "updatetime" => date("Y-m-d H:i:s")
  335. ];
  336. $reuslt = Db::table('sys_account')->insert($data, true);
  337. if ($reuslt) {
  338. $data = [
  339. "nickname" => $post['nickname'],
  340. "mobile" => $post['mobile'],
  341. "email" => $post['email'],
  342. "portrait" => "",
  343. "sex" => 1,
  344. "post" => "",
  345. "department" => "",
  346. "account_id" => $reuslt,
  347. "status" => 1,
  348. "addtime" => date("Y-m-d H:i:s"),
  349. "updatetime" => date("Y-m-d H:i:s")
  350. ];
  351. $user = Db::table("sys_user")->insert($data);
  352. if ($user != false) {
  353. if (!empty($post['companyArr'])) {
  354. $company_insert = [];
  355. $acount = new AccountCompany();
  356. foreach ($post['companyArr'] as $company) {
  357. $company_insert[] = [
  358. "account_id" => $reuslt,
  359. "companyCode" => $company['companyCode'],
  360. "companyName" => $company['companyName'],
  361. "company_type" => $company['company_type'],
  362. "is_main" => $company['is_main'],
  363. "status" => 1,
  364. "is_del" => 0,
  365. "addtime" => date("Y-m-d H:i:s"),
  366. "updatetime" => date("Y-m-d H:i:s"),
  367. ];
  368. }
  369. $acount->saveAll($company_insert);
  370. }
  371. Db::commit();
  372. return json_show(0, "账户注册成功");
  373. }
  374. }
  375. Db::rollback();
  376. return json_show(1002, "账户注册失败");
  377. } catch (\Exception $e) {
  378. Db::rollback();
  379. return json_show(1002, "账户注册失败" . $e->getMessage());
  380. }
  381. }
  382. //删除
  383. public function userDelete()
  384. {
  385. $param = $this->request->only(['ids'], 'post', 'trim');
  386. $val = Validate::rule([
  387. 'ids' => 'require|array|max:100',
  388. ]);
  389. if (!$val->check($param)) return json_show(1004, $val->getError());
  390. $rs = Db::name('account')
  391. ->where(['is_del' => 0])
  392. ->whereIn('id', $param['ids'])
  393. ->update([
  394. 'is_del' => 1,
  395. 'updatetime' => date('Y-m-d H:i:s')
  396. ]);
  397. return $rs ? json_show(0, '删除成功') : json_show(1004, '删除失败');
  398. }
  399. }