UserCompany.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\User as UserCommon;
  4. use think\facade\Db;
  5. use think\facade\Validate;
  6. //【公司账号查询】
  7. class UserCompany extends Base
  8. {
  9. //列表
  10. public function getList()
  11. {
  12. $param = $this->request->only(['account_id' => '', 'status' => '', 'page' => 1, 'size' => 10], 'post', 'trim');
  13. $userCommon = UserCommon::getIns();
  14. $rs = $userCommon->handle('userCompanyList', $param);
  15. if (isset($rs['code']) && $rs['code'] == 0) {
  16. //补充角色信息
  17. foreach ($rs['data']['list'] as &$item) {
  18. $tmp = Db::name('user_role')
  19. ->alias('a')
  20. ->field('b.role_name')
  21. ->leftJoin('role b', 'b.id=a.roleid')
  22. ->where(['a.uid' => $item['account_id'], 'a.is_del' => 0, 'a.companyNo' => $item['companyCode']])
  23. ->findOrEmpty();
  24. $item['role_name'] = $tmp['role_name'] ?? '';
  25. }
  26. }
  27. return json_show($rs['code'], $rs['message'], $rs['data']);
  28. }
  29. //编辑
  30. public function update()
  31. {
  32. $param = $this->request->only(['account_id', 'companyNo', 'roleid'], 'post');
  33. $val = Validate::rule([
  34. 'account_id|账户id' => 'require|number|gt:0',
  35. 'companyNo|公司编码' => 'require|length:18',
  36. 'roleid|角色' => 'require|number|gt:0',
  37. ]);
  38. if (!$val->check($param)) return json_show(1004, $val->getError());
  39. // $role = Db::name('role')
  40. // ->field('id')
  41. // ->where('id', $param['roleid'])
  42. // ->where('level', '<>', 1)
  43. // ->findOrEmpty();
  44. //
  45. // if (empty($role)) return json_show(1004, '角色不存在或等级不符合要求');
  46. // $item = Db::name('company_item')
  47. // ->field('id')
  48. // ->where(['id' => $param['itemid'], 'companyNo' => $param['companyNo']])
  49. // ->findOrEmpty();
  50. // if (empty($item)) return json_show(1004, '该公司部门不存在');
  51. // $tmp = Db::name('user_role')
  52. // ->field('id')
  53. // ->where(['uid' => $param['account_id'], 'companyNo' => $param['companyNo'], 'is_del' => 0])
  54. // ->findOrEmpty();
  55. $date = date('Y-m-d H:i:s');
  56. $tmp = Db::name('user_role')
  57. ->field('id')
  58. ->where(['uid' => $param['account_id'], 'companyNo' => $param['companyNo'], 'is_del' => 0])
  59. ->findOrEmpty();
  60. if (empty($tmp)) {
  61. $rs = Db::name('user_role')
  62. ->insert([
  63. 'uid' => $param['account_id'],
  64. 'roleid' => $param['roleid'],
  65. 'companyNo' => $param['companyNo'],
  66. 'addtime' => $date,
  67. 'updatetime' => $date,
  68. ]);
  69. } else {
  70. $rs = Db::name('user_role')
  71. ->where('id', $tmp['id'])
  72. ->update(['roleid' => $param['roleid'], 'updatetime' => $date]);
  73. }
  74. // $userCommon = UserCommon::getIns();
  75. // $rs = json_decode($userCommon->handle('userCompanyUpdate', $param), true);
  76. return $rs ? json_show(0, '修改成功') : json_show(1004, '修改失败');
  77. }
  78. //启禁用
  79. public function status()
  80. {
  81. $param = $this->request->only(['id', 'status'], 'post', 'trim');
  82. $val = Validate::rule([
  83. 'id' => 'require|number|gt:0',
  84. 'status' => 'require|number|in:0,1',
  85. ]);
  86. if ($val->check($param) == false) return json_show(1004, $val->getError());
  87. $userCommon = UserCommon::getIns();
  88. $rs = $userCommon->handle('userCompanyStatus', $param);
  89. if (!isset($rs['code']) || $rs['code'] != 0) return json_show($rs['code'], $rs['message'], $rs['data']);
  90. else {
  91. $res = Db::name('user_role')
  92. ->where('status', '<>', $param['status'])
  93. ->where(['uid' => $rs['data']['account_id'], 'companyNo' => $rs['data']['companyNo'], 'is_del' => 0])
  94. ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  95. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  96. }
  97. }
  98. //详情
  99. public function info()
  100. {
  101. $param = $this->request->post(['id'], 'post', 'trim');
  102. $userCommon = UserCommon::getIns();
  103. $rs = $userCommon->handle('userCompanyInfo', $param);
  104. if (isset($rs['code']) && $rs['code'] == 0) {
  105. $tmp = Db::name('user_role')
  106. ->alias('a')
  107. ->field('a.roleid,b.role_name')
  108. ->leftJoin('role b', 'b.id=a.roleid')
  109. // ->leftJoin('company_item c', 'c.id=a.itemid and c.companyNo=a.companyNo')
  110. ->where(['a.uid' => $rs['data']['account_id'], 'is_del' => 0, 'a.companyNo' => $rs['data']['companyCode']])
  111. ->findOrEmpty();
  112. $rs['data']['roleid'] = $tmp['roleid'] ?? '';
  113. $rs['data']['role_name'] = $tmp['role_name'] ?? '';
  114. // $rs['data']['item_name'] = $tmp['item_name'] ?? '';
  115. }
  116. return json_show($rs['code'], $rs['message'], $rs['data']);
  117. }
  118. //获取当前账号绑定公司列表
  119. public function myCompanyList()
  120. {
  121. $param = $this->request->only(['status' => '', 'page' => 1, 'size' => 10, 'companyCode' => '', 'companyName' => ''], 'post', 'trim');
  122. $param['account_id'] = $this->uid;
  123. $userCommon = UserCommon::getIns();
  124. $rs = $userCommon->handle('userCompanyList', $param);
  125. if (isset($rs['code']) && $rs['code'] == 0) {
  126. //补充角色信息
  127. foreach ($rs['data']['list'] as &$item) {
  128. $tmp = Db::name('user_role')
  129. ->alias('a')
  130. ->field('a.roleid,b.role_name')
  131. ->leftJoin('role b', 'b.id=a.roleid')
  132. ->where(['a.uid' => $item['account_id'], 'a.is_del' => 0, 'a.companyNo' => [$item['companyCode'],'']])
  133. ->order(['a.companyNo'=>'desc'])
  134. ->findOrEmpty();
  135. $item['roleid'] = $tmp['roleid'] ?? '';
  136. $item['role_name'] = $tmp['role_name'] ?? '';
  137. }
  138. }
  139. return json_show($rs['code'], $rs['message'], $rs['data']);
  140. }
  141. //切换默认公司
  142. public function changeMain()
  143. {
  144. $param = $this->request->filter('trim')->post();
  145. $userCommon = UserCommon::getIns();
  146. $rs = $userCommon->handle('changeMain', $param);
  147. return json_show($rs['code'], $rs['message'], $rs['data']);
  148. }
  149. }