UserCompanyBasic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\Account;
  5. use app\model\AccountCompany;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //【公司账号管理】
  10. class UserCompanyBasic extends BaseController
  11. {
  12. //列表
  13. public function getList()
  14. {
  15. $post = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'islevel' => 1], 'post');
  16. $condition = [['a.is_del', '=', 0]];
  17. if ($post['nickname'] != '') $condition[] = ['nickname', 'like', "%{$post['nickname']}%"];
  18. if ($post['username'] != '') $condition[] = ['username', 'like', "%{$post['username']}%"];
  19. if ($post['status'] != '') $condition[] = ['a.status', '=', $post['status']];
  20. if ($post['islevel'] == 1) $condition[] = ['a.level', '=', 1];
  21. else $condition[] = ['a.level', '<>', 1];
  22. $count = Db::name('account')
  23. ->alias('a')
  24. ->leftJoin('user b', 'a.id=b.account_id and b.status=1')
  25. ->where($condition)
  26. ->count('a.id');
  27. $list = Db::name('account')
  28. ->alias('a')
  29. ->field('a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime')
  30. ->leftJoin('user b', 'a.id=b.account_id and b.status=1')
  31. ->where($condition)
  32. ->page($post['page'], $post['size'])
  33. ->append(['plat', 'company_relaton'])
  34. // ->withAttr('plat', function ($val, $da) {
  35. // return Db::name('account_plat')
  36. // ->alias('a')
  37. // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.is_del=0 and b.status=1')
  38. // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $da['id']])
  39. // ->field('a.plat_code,plat_name')
  40. // ->select()
  41. // ->toArray();
  42. // })
  43. ->withAttr('company_relaton', function ($val, $da) {
  44. return Db::name('account_company')
  45. ->where(['account_id' => $da['id'], 'is_del' => 0])
  46. ->field('companyCode,companyName,company_type,is_main,status')
  47. ->select()
  48. ->toArray();
  49. })
  50. ->order('a.addtime desc')
  51. ->select()
  52. ->toArray();
  53. return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  54. }
  55. //注册
  56. public function add()
  57. {
  58. $post = $this->request->only(['nickname' => '', 'mobile' => '', 'email' => '', 'companyArr' => []], 'post', 'trim');
  59. $validate = Validate::rule([
  60. 'nickname|真实姓名' => 'require|min:2|max:200',
  61. 'mobile|手机号' => 'require|number|length:11|mobile',
  62. 'email|邮箱' => 'email',
  63. 'companyArr|关联业务公司' => 'require|array|max:100',
  64. ]);
  65. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  66. Db::startTrans();
  67. try {
  68. $uiq = Db::table('sys_account')
  69. ->field('id')
  70. ->where(['mobile' => $post['mobile']])
  71. ->findOrEmpty();
  72. if ($uiq) throw new Exception('手机号已注册!');
  73. $date = date('Y-m-d H:i:s');
  74. $salt = makeSalt();
  75. $password = sha1('dingding123' . $salt);
  76. $data = [
  77. 'username' => $post['mobile'],
  78. 'password' => $password,
  79. 'salt' => $salt,
  80. 'mobile' => $post['mobile'],
  81. 'source' => 'paltadd',
  82. 'status' => 1,
  83. 'addtime' => $date,
  84. 'updatetime' => $date
  85. ];
  86. $reuslt = Db::table('sys_account')->insertGetId($data);
  87. if ($reuslt) {
  88. $data = [
  89. 'nickname' => $post['nickname'],
  90. 'mobile' => $post['mobile'],
  91. 'email' => $post['email'],
  92. 'portrait' => '',
  93. 'sex' => 1,
  94. 'post' => '',
  95. 'department' => '',
  96. 'account_id' => $reuslt,
  97. 'status' => 1,
  98. 'addtime' => $date,
  99. 'updatetime' => $date
  100. ];
  101. $user = Db::table('sys_user')->insert($data);
  102. if ($user != false) {
  103. if (!empty($post['companyArr'])) {
  104. $company_insert = [];
  105. $acount = new AccountCompany();
  106. foreach ($post['companyArr'] as $company) {
  107. $company_insert[] = [
  108. 'account_id' => $reuslt,
  109. 'companyCode' => $company['companyCode'],
  110. 'companyName' => $company['companyName'],
  111. 'company_type' => $company['company_type'],
  112. 'is_main' => $company['is_main'],
  113. 'status' => 1,
  114. 'is_del' => 0,
  115. 'addtime' => $date,
  116. 'updatetime' => $date,
  117. ];
  118. }
  119. $acount->saveAll($company_insert);
  120. }
  121. Db::commit();
  122. return json_show(0, '账户注册成功', ['uid' => $reuslt]);
  123. }
  124. } else throw new Exception();
  125. } catch (Exception $e) {
  126. Db::rollback();
  127. return json_show(1002, '账户注册失败,' . $e->getMessage());
  128. }
  129. }
  130. //删除
  131. public function delete()
  132. {
  133. $param = $this->request->only(['ids'], 'post', 'trim');
  134. $val = Validate::rule([
  135. 'ids' => 'require|array|max:100',
  136. ]);
  137. if (!$val->check($param)) return json_show(1004, $val->getError());
  138. Db::startTrans();
  139. try {
  140. Db::name('account')
  141. ->where(['is_del' => 0])
  142. ->whereIn('id', $param['ids'])
  143. ->update([
  144. 'is_del' => 1,
  145. 'updatetime' => date('Y-m-d H:i:s')
  146. ]);
  147. Db::name('account_company')
  148. ->where(['is_del' => 0])
  149. ->whereIn('account_id', $param['ids'])
  150. ->update([
  151. 'is_del' => 1,
  152. 'updatetime' => date('Y-m-d H:i:s')
  153. ]);
  154. Db::commit();
  155. return json_show(0, '删除成功');
  156. } catch (Exception $exception) {
  157. Db::rollback();
  158. return json_show(1005, '删除失败,' . $exception->getMessage());
  159. }
  160. }
  161. //编辑
  162. public function update()
  163. {
  164. $post = $this->request->only(['id', 'itemid'], 'post');
  165. $validate = Validate::rule([
  166. 'id|主键ID' => 'require|number|gt:0',
  167. 'itemid|部门ID' => 'require|number|gt:0',
  168. ]);
  169. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  170. $account = Db::name('account')
  171. ->field('id')
  172. ->where([['id', '=', $post['id']], ['is_del', '=', 0]])
  173. ->findOrEmpty();
  174. if (empty($account)) return json_show(1003, '账户不存在');
  175. $tmp = Db::name('account_item')
  176. ->field('id')
  177. ->where(['account_id' => $post['id']])
  178. ->findOrEmpty();
  179. $date = date('Y-m-d H:i:s');
  180. if (empty($tmp)) $rs = Db::name('account_item')
  181. ->insert([
  182. 'account_id' => $post['id'],
  183. 'itemid' => $post['itemid'],
  184. 'addtime' => $date,
  185. 'updatetime' => $date,
  186. ]);
  187. else $rs = Db::name('account_item')
  188. ->where(['id' => $tmp['id']])
  189. ->update([
  190. 'itemid' => $post['itemid'],
  191. 'updatetime' => $date
  192. ]);
  193. return $rs ? json_show(0, '信息修改成功') : json_show(0, '信息修改失败');;
  194. // $accountinfo = Db::name('user')
  195. // ->field('id')
  196. // ->where([['account_id', '=', $post['id']]])
  197. // ->findOrEmpty();
  198. // if (empty($accountinfo)) return json_show(1003, '账户信息不存在');
  199. // Db::startTrans();
  200. // try {
  201. // Db::name('user')
  202. // ->where($accountinfo)
  203. // ->update([
  204. // 'nickname' => $post['nickname'],
  205. // 'mobile' => $post['mobile'],
  206. // 'email' => $post['email'],
  207. // 'portrait' => $post['portrait'],
  208. // 'sex' => $post['sex'],
  209. // 'updatetime' => date('Y-m-d H:i:s')
  210. // ]);
  211. //
  212. // Db::name('account')
  213. // ->where('id', $post['id'])
  214. // ->update([
  215. // 'id' => $post['id'],
  216. // 'mobile' => $post['mobile'],
  217. // 'username' => $post['mobile'],
  218. // 'updatetime' => date('Y-m-d H:i:s'),
  219. // ]);
  220. // Db::commit();
  221. // return json_show(0, '信息修改成功');
  222. // } catch (Exception $exception) {
  223. // Db::rollback();
  224. // return json_show(1005, '信息修改失败,' . $exception->getMessage());
  225. // }
  226. }
  227. //启禁用
  228. public function status()
  229. {
  230. $post = $this->request->only(['id', 'status'], 'post', 'trim');
  231. $validate = Validate::rule(['id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1']);
  232. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  233. Db::startTrans();
  234. try {
  235. $account = Account::where(['id' => $post['id'], 'is_del' => 0])
  236. ->field('id,status')
  237. ->findOrEmpty();
  238. if (empty($account)) throw new Exception('账户不存在');
  239. if ($account['status'] == $post['status']) throw new Exception('重复操作');
  240. $message = $post['status'] == 1 ? '启用' : '禁用';
  241. Db::name('account')
  242. ->where('status', '<>', $post['status'])
  243. ->where(['id' => $post['id'], 'is_del' => 0])
  244. ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  245. Db::name('account_company')
  246. ->where('status', '<>', $post['status'])
  247. ->where(['account_id' => $post['id'], 'is_del' => 0])
  248. ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  249. Db::commit();
  250. return json_show(0, "账户{$message}成功");
  251. } catch (Exception $exception) {
  252. Db::rollback();
  253. return json_show(1004, '操作失败,' . $exception->getMessage());
  254. }
  255. }
  256. //详情
  257. public function info()
  258. {
  259. $id = $this->request->post('id/d', 0, 'trim');
  260. if ($id == 0) return json_show(1003, "参数 id 不能为空");
  261. $list = Db::name('account')
  262. ->alias('a')
  263. ->field('a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,c.itemid,d.name item_name')
  264. ->leftJoin('user b', 'a.id=b.account_id')
  265. ->leftJoin('account_item c', 'c.account_id=a.id')
  266. ->leftJoin('company_item d', 'd.id=c.itemid')
  267. ->append(['plat', 'company_relaton'])
  268. // ->withAttr('plat', function ($val, $da) {
  269. // return Db::name('account_plat')
  270. // ->alias('a')
  271. // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.status=1')
  272. // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $da['id']])
  273. // ->column('a.plat_code,plat_name');
  274. // })
  275. ->withAttr('company_relaton', function ($val, $da) {
  276. return Db::name('account_company')
  277. ->where(['account_id' => $da['id'], 'is_del' => 0, 'status' => 1])
  278. ->column('companyCode,companyName,company_type,is_main,status');
  279. })
  280. ->where(['a.id' => $id, 'a.is_del' => 0])
  281. ->findOrEmpty();
  282. if (empty($list)) return json_show(1004, '未找到用户信息');
  283. // $list['plat'] = Db::name('account_plat')->alias('a')
  284. // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.status=1')
  285. // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $list['id']])->column('a.plat_code,plat_name');
  286. // $list['company_relaton'] = Db::name('account_company')->where(['account_id' => $list['id'], 'is_del' => 0, 'status' => 1])
  287. // ->column('companyCode,companyName,company_type,is_main,status');
  288. return json_show(0, '获取成功', $list);
  289. }
  290. }