UserCompanyBasic.php 12 KB

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