UserCompanyBasic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\User as UserCommon;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. //【公司账号管理】
  8. class UserCompanyBasic extends Base
  9. {
  10. //列表
  11. public function getList()
  12. {
  13. $param = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'level' => '2,3', 'companyNo' => ''], 'post', 'trim');
  14. if ($param['level'] == 1) return json_show(1004, '这里不能查看超管账号');//排除超管账号
  15. $userCommon = UserCommon::getIns();
  16. $rs = $userCommon->handle('userCompanyBasicList', $param);
  17. return json_show($rs['code'], $rs['message'], $rs['data']);
  18. }
  19. //添加
  20. public function add()
  21. {
  22. $param = $this->request->only(['nickname', 'mobile', 'email' => '', 'level' => 2, 'companyArr' => []], 'post', 'trim');
  23. $val = Validate::rule([
  24. 'nickname|真实姓名' => 'require|min:2|max:200',
  25. 'mobile|手机号' => 'require|number|length:11|mobile',
  26. 'email|邮箱' => 'email',
  27. 'level|账号等级' => 'number|in:1,2',
  28. 'companyArr|关联业务公司' => 'array|max:100',
  29. ]);
  30. if (!$val->check($param)) return json_show(1004, $val->getError());
  31. $val_company = Validate::rule([
  32. 'companyCode|公司编码' => 'require|length:18',
  33. 'companyName|公司名称' => 'require',
  34. 'company_type|公司类型' => 'require|number',
  35. 'is_main|是否默认公司' => 'require|in:0,1',
  36. 'roleid|角色id' => 'require|number|gt:0',
  37. ]);
  38. foreach ($param['companyArr'] as $company) {
  39. if (!$val_company->check($company)) {
  40. return json_show(1004, $val_company->getError());
  41. break;
  42. }
  43. }
  44. $userCommon = UserCommon::getIns();
  45. $rs = $userCommon->handle('userCompanyBasicAdd', $param);
  46. if (!isset($rs['code']) || $rs['code'] != 0) return json_show($rs['code'], $rs['message'], $rs['data']);
  47. else {
  48. Db::startTrans();
  49. try {
  50. $date = date('Y-m-d H:i:s');
  51. $insert_data = [];
  52. foreach ($param['companyArr'] as $company) {
  53. $insert_data[] = [
  54. 'uid' => $rs['data']['uid'],
  55. 'nickname' => $param['nickname'],
  56. 'roleid' => $company['roleid'],
  57. 'companyNo' => $company['companyCode'],
  58. 'company_type' => $company['company_type'],
  59. 'status' => 1,
  60. 'is_main' => $company['is_main'],
  61. 'is_del' => 0,
  62. 'addtime' => $date,
  63. 'updatetime' => $date,
  64. ];
  65. }
  66. Db::name('user_role')->insertAll($insert_data);
  67. Db::commit();
  68. return json_show($rs['code'], $rs['message'], $rs['data']);
  69. } catch (Exception $exception) {
  70. Db::rollback();
  71. return json_show(1004, '账户注册失败,' . $exception->getMessage());
  72. }
  73. }
  74. }
  75. //删除
  76. public function delete()
  77. {
  78. $param = $this->request->only(['ids'], 'post', 'trim');
  79. $val = Validate::rule(['ids|账号ID' => 'require|array|max:100']);
  80. if (!$val->check($param)) return json_show(1004, $val->getError());
  81. $userCommon = UserCommon::getIns();
  82. $rs = $userCommon->handle('userCompanyBasicDelete', $param);
  83. return json_show($rs['code'], $rs['message'], $rs['data']);
  84. }
  85. //编辑
  86. public function update()
  87. {
  88. $param = $this->request->only(['id', 'itemid'], 'post');
  89. $userCommon = UserCommon::getIns();
  90. $rs = $userCommon->handle('userCompanyBasicUpdate', $param);
  91. return json_show($rs['code'], $rs['message'], $rs['data']);
  92. }
  93. //启禁用
  94. public function status()
  95. {
  96. $param = $this->request->only(['id', 'status'], 'post', 'trim');
  97. $userCommon = UserCommon::getIns();
  98. $rs = $userCommon->handle('userCompanyBasicStatus', $param);
  99. return json_show($rs['code'], $rs['message'], $rs['data']);
  100. }
  101. //修改密码
  102. public function password()
  103. {
  104. $param = $this->request->only(['id', 'password'], 'post', 'trim');
  105. $userCommon = UserCommon::getIns();
  106. $rs = $userCommon->handle('setpasswd', $param);
  107. return json_show($rs['code'], $rs['message'], $rs['data']);
  108. }
  109. //详情
  110. public function info()
  111. {
  112. $param = $this->request->only(['id'], 'post', 'trim');
  113. $userCommon = UserCommon::getIns();
  114. $rs = $userCommon->handle('userCompanyBasicInfo', $param);
  115. $user_platform = [];
  116. if (isset($rs['code']) && $rs['code'] == 0) {
  117. $user_platform_id = Db::name('user_platform')
  118. ->where(['uid' => $param['id'], 'is_del' => 0])
  119. ->value('platform','');
  120. if ($user_platform_id != '') {
  121. $user_platform = Db::name('platform')
  122. ->field('id,platform_code,platform_name')
  123. ->where('is_del = 0 AND id IN (' . $user_platform_id . ')')
  124. ->select()
  125. ->toArray();
  126. }
  127. }
  128. $rs['data']['platform'] = $user_platform;
  129. return json_show($rs['code'], $rs['message'], $rs['data']);
  130. }
  131. //设置账号关联的平台
  132. public function setUserPlatfrom()
  133. {
  134. $param = $this->request->only(['uid', 'platform_id'], 'post', 'trim');
  135. $val = Validate::rule([
  136. 'uid|用户ID' => 'require|number|gt:0',
  137. 'platform_id|平台id集合' => 'require|array|max:9999'
  138. ]);
  139. if ($val->check($param) == false) return json_show(1004, $val->getError());
  140. $platform_id = Db::name('platform')
  141. ->where(['is_del' => 0, 'id' => $param['platform_id']])
  142. ->column('id');
  143. if (empty($platform_id)) return json_show(1004, '平台id不正确');
  144. $tmp = Db::name('user_platform')
  145. ->field('id')
  146. ->where(['is_del' => 0, 'uid' => $param['uid']])
  147. ->findOrEmpty();
  148. $date = date('Y-m-d H:i:s');
  149. if (empty($tmp)) {
  150. $res = Db::name('user_platform')
  151. ->insert([
  152. 'uid' => $param['uid'],
  153. 'platform' => implode(',', $platform_id),
  154. 'is_del' => 0,
  155. 'addtime' => $date,
  156. 'updatetime' => $date,
  157. ]);
  158. } else {
  159. $res = Db::name('user_platform')
  160. ->where(['is_del' => 0, 'id' => $tmp['id']])
  161. ->update([
  162. 'platform' => implode(',', $platform_id),
  163. 'updatetime' => $date,
  164. ]);
  165. }
  166. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  167. }
  168. }