UserCompany.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $date = date('Y-m-d H:i:s');
  40. $tmp = Db::name('user_role')
  41. ->field('id')
  42. ->where(['uid' => $param['account_id'], 'companyNo' => $param['companyNo'], 'is_del' => 0])
  43. ->findOrEmpty();
  44. if (empty($tmp)) {
  45. $rs = Db::name('user_role')
  46. ->insert([
  47. 'uid' => $param['account_id'],
  48. 'roleid' => $param['roleid'],
  49. 'companyNo' => $param['companyNo'],
  50. 'addtime' => $date,
  51. 'updatetime' => $date,
  52. ]);
  53. } else {
  54. $rs = Db::name('user_role')
  55. ->where('id', $tmp['id'])
  56. ->update(['roleid' => $param['roleid'], 'updatetime' => $date]);
  57. }
  58. // $userCommon = UserCommon::getIns();
  59. // $rs = json_decode($userCommon->handle('userCompanyUpdate', $param), true);
  60. return $rs ? json_show(0, '修改成功') : json_show(1004, '修改失败');
  61. }
  62. //启禁用
  63. public function status()
  64. {
  65. $param = $this->request->only(['id', 'status'], 'post', 'trim');
  66. $val = Validate::rule([
  67. 'id' => 'require|number|gt:0',
  68. 'status' => 'require|number|in:0,1',
  69. ]);
  70. if ($val->check($param) == false) return json_show(1004, $val->getError());
  71. $userCommon = UserCommon::getIns();
  72. $rs = $userCommon->handle('userCompanyStatus', $param);
  73. if (!isset($rs['code']) || $rs['code'] != 0) return json_show($rs['code'], $rs['message'], $rs['data']);
  74. else {
  75. $res = Db::name('user_role')
  76. ->where('status', '<>', $param['status'])
  77. ->where(['uid' => $rs['data']['account_id'], 'companyNo' => $rs['data']['companyNo'], 'is_del' => 0])
  78. ->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  79. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  80. }
  81. }
  82. //详情
  83. public function info()
  84. {
  85. $param = $this->request->post(['id'], 'post', 'trim');
  86. $userCommon = UserCommon::getIns();
  87. $rs = $userCommon->handle('userCompanyInfo', $param);
  88. if (isset($rs['code']) && $rs['code'] == 0) {
  89. $tmp = Db::name('user_role')
  90. ->alias('a')
  91. ->field('a.roleid,b.role_name')
  92. ->leftJoin('role b', 'b.id=a.roleid')
  93. // ->leftJoin('company_item c', 'c.id=a.itemid and c.companyNo=a.companyNo')
  94. ->where(['a.uid' => $rs['data']['account_id'], 'is_del' => 0, 'a.companyNo' => $rs['data']['companyCode']])
  95. ->findOrEmpty();
  96. $rs['data']['roleid'] = $tmp['roleid'] ?? '';
  97. $rs['data']['role_name'] = $tmp['role_name'] ?? '';
  98. // $rs['data']['item_name'] = $tmp['item_name'] ?? '';
  99. }
  100. return json_show($rs['code'], $rs['message'], $rs['data']);
  101. }
  102. //获取当前账号绑定公司列表
  103. public function myCompanyList()
  104. {
  105. $param = $this->request->only(['status' => '', 'page' => 1, 'size' => 10, 'companyCode' => '', 'companyName' => ''], 'post', 'trim');
  106. $param['account_id'] = $this->uid;
  107. $userCommon = UserCommon::getIns();
  108. $rs = $userCommon->handle('userCompanyList', $param);
  109. if (isset($rs['code']) && $rs['code'] == 0) {
  110. //补充角色信息
  111. foreach ($rs['data']['list'] as &$item) {
  112. $tmp = Db::name('user_role')
  113. ->alias('a')
  114. ->field('a.roleid,b.role_name')
  115. ->leftJoin('role b', 'b.id=a.roleid')
  116. ->where(['a.uid' => $item['account_id'], 'a.is_del' => 0, 'a.companyNo' => [$item['companyCode'],'']])
  117. ->order(['a.companyNo'=>'desc'])
  118. ->findOrEmpty();
  119. $item['roleid'] = $tmp['roleid'] ?? '';
  120. $item['role_name'] = $tmp['role_name'] ?? '';
  121. }
  122. }
  123. return json_show($rs['code'], $rs['message'], $rs['data']);
  124. }
  125. //切换默认公司
  126. public function changeMain()
  127. {
  128. $param = $this->request->filter('trim')->post();
  129. $userCommon = UserCommon::getIns();
  130. $rs = $userCommon->handle('changeMain', $param);
  131. return json_show($rs['code'], $rs['message'], $rs['data']);
  132. }
  133. }