Index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\admin\controller\userCenter;
  3. use app\common\controller\Backend;
  4. use think\facade\Db;
  5. class Index extends Backend
  6. {
  7. protected array $noNeedLogin = ['*'];
  8. /**
  9. * 企业列表查询
  10. * @return void
  11. */
  12. public function index():void
  13. {
  14. $where = [];
  15. // 企业名称搜索
  16. if (!empty($this->request->get('name'))) {
  17. $name = $this->request->get('name');
  18. $where[] = ['name', 'like', '%' . $name . '%'];
  19. }
  20. // 状态筛选
  21. if (!empty($this->request->get('status'))) {
  22. $status = $this->request->get('status');
  23. $where['status'] = $status;
  24. }
  25. // 排序
  26. $order = [];
  27. if (!empty($this->request->get('order'))) {
  28. $order_a = $this->request->get('order');
  29. $order_a = explode(',', $order_a);
  30. $order[$order_a[0]] = $order_a[1];
  31. } else {
  32. $order['id'] = 'desc';
  33. }
  34. // 分页
  35. $page = $this->request->get('page', 1);
  36. $limit = $this->request->get('limit', 20);
  37. $companyList = Db::name('company')
  38. ->where($where)
  39. ->order($order)
  40. ->paginate([
  41. 'list_rows' => $limit,
  42. 'page' => $page
  43. ]);
  44. $this->success('查询成功', [
  45. 'list' => $companyList->items(),
  46. 'total' => $companyList->total()
  47. ]);
  48. }
  49. /**
  50. * 新增/编辑企业
  51. * @return void
  52. */
  53. public function edit():void
  54. {
  55. $id = $this->request->post('id');
  56. $name = $this->request->post('name');
  57. $status = $this->request->post('status', 1);
  58. if (empty($name)) {
  59. $this->error(__('企业名称不能为空'));
  60. }
  61. // 验证企业名称是否已存在(编辑时排除自己)
  62. $checkName = Db::name('company')
  63. ->where('name', $name);
  64. if (!empty($id)) {
  65. $checkName->where('id', '<>', $id);
  66. }
  67. if ($checkName->find()) {
  68. $this->error(__('企业名称已存在'));
  69. }
  70. $data = [
  71. 'name' => $name,
  72. 'status' => $status,
  73. 'update_time' => date('Y-m-d H:i:s')
  74. ];
  75. $adminInfo = $this->auth->getInfo();
  76. try {
  77. if (!empty($id)) {
  78. // 编辑
  79. $result = Db::name('company')
  80. ->where('id', $id)
  81. ->update($data);
  82. Db::name('user_on')
  83. ->where('company_id', $id)
  84. ->update(['company'=>$name]);
  85. $message = '编辑成功';
  86. } else {
  87. // 新增
  88. $data['creator_name'] = $adminInfo['nickname']; // 可以从登录信息获取
  89. $data['create_time'] = date('Y-m-d H:i:s');
  90. $result = Db::name('company')
  91. ->insert($data);
  92. $id = Db::name('company')->getLastInsID();
  93. $message = '新增成功';
  94. }
  95. } catch (\Exception $e) {
  96. $this->error('操作失败: ' . $e->getMessage());
  97. }
  98. if ($result) {
  99. $this->success($message, ['id' => $id]);
  100. } else {
  101. $this->error(__('操作失败'));
  102. }
  103. }
  104. /**
  105. * 删除企业
  106. * @return void
  107. */
  108. public function delete():void
  109. {
  110. $id = $this->request->post('id');
  111. if (empty($id)) {
  112. $this->error(__('参数错误'));
  113. }
  114. try {
  115. $result = Db::name('company')
  116. ->where('id', $id)
  117. ->delete();
  118. } catch (\Exception $e) {
  119. $this->error('删除失败: ' . $e->getMessage());
  120. }
  121. if ($result) {
  122. $this->success('删除成功');
  123. } else {
  124. $this->error(__('删除失败'));
  125. }
  126. }
  127. /**
  128. * 获取启用企业选项(用于下拉选择)
  129. * @return void
  130. */
  131. public function selectOptions():void
  132. {
  133. $packageList = Db::name('company')
  134. ->where('status', 1)
  135. ->field('id,name')
  136. ->select()
  137. ->toArray();
  138. $this->success('获取成功', $packageList);
  139. }
  140. /**
  141. * 禁用/启用企业
  142. * @return void
  143. */
  144. public function toggleStatus():void
  145. {
  146. $id = $this->request->post('id');
  147. if (empty($id)) {
  148. $this->error(__('参数错误'));
  149. }
  150. try {
  151. $companyInfo = Db::name('company')
  152. ->where('id', $id)
  153. ->find();
  154. if (!$companyInfo) {
  155. $this->error(__('企业不存在'));
  156. }
  157. $newStatus = $companyInfo['status'] == 1 ? 2 : 1;
  158. $statusText = $newStatus == 2 ? '禁用' : '启用';
  159. $result = Db::name('company')
  160. ->where('id', $id)
  161. ->update([
  162. 'status' => $newStatus,
  163. 'update_time' => date('Y-m-d H:i:s')
  164. ]);
  165. } catch (\Exception $e) {
  166. $this->error('操作失败: ' . $e->getMessage());
  167. }
  168. if ($result) {
  169. $this->success($statusText . '成功');
  170. } else {
  171. $this->error(__('操作失败'));
  172. }
  173. }
  174. /**
  175. * 获取企业详情
  176. * @return void
  177. */
  178. public function detail():void
  179. {
  180. $id = $this->request->get('id');
  181. if (empty($id)) {
  182. $this->error(__('参数错误'));
  183. }
  184. $companyInfo = Db::name('company')
  185. ->where('id', $id)
  186. ->find();
  187. if (!$companyInfo) {
  188. $this->error(__('企业不存在'));
  189. }
  190. $this->success('查询成功', $companyInfo);
  191. }
  192. /**
  193. * 获取企业状态选项
  194. * @return void
  195. */
  196. public function statusOptions():void
  197. {
  198. $options = [
  199. ['value' => 1, 'label' => '正常'],
  200. ['value' => 2, 'label' => '禁用']
  201. ];
  202. $this->success('获取成功', $options);
  203. }
  204. }