Index.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. $message = '编辑成功';
  83. } else {
  84. // 新增
  85. $data['creator_name'] = $adminInfo['nickname']; // 可以从登录信息获取
  86. $data['create_time'] = date('Y-m-d H:i:s');
  87. $result = Db::name('company')
  88. ->insert($data);
  89. $id = Db::name('company')->getLastInsID();
  90. $message = '新增成功';
  91. }
  92. } catch (\Exception $e) {
  93. $this->error('操作失败: ' . $e->getMessage());
  94. }
  95. if ($result) {
  96. $this->success($message, ['id' => $id]);
  97. } else {
  98. $this->error(__('操作失败'));
  99. }
  100. }
  101. /**
  102. * 删除企业
  103. * @return void
  104. */
  105. public function delete():void
  106. {
  107. $id = $this->request->post('id');
  108. if (empty($id)) {
  109. $this->error(__('参数错误'));
  110. }
  111. try {
  112. $result = Db::name('company')
  113. ->where('id', $id)
  114. ->delete();
  115. } catch (\Exception $e) {
  116. $this->error('删除失败: ' . $e->getMessage());
  117. }
  118. if ($result) {
  119. $this->success('删除成功');
  120. } else {
  121. $this->error(__('删除失败'));
  122. }
  123. }
  124. /**
  125. * 禁用/启用企业
  126. * @return void
  127. */
  128. public function toggleStatus():void
  129. {
  130. $id = $this->request->post('id');
  131. if (empty($id)) {
  132. $this->error(__('参数错误'));
  133. }
  134. try {
  135. $companyInfo = Db::name('company')
  136. ->where('id', $id)
  137. ->find();
  138. if (!$companyInfo) {
  139. $this->error(__('企业不存在'));
  140. }
  141. $newStatus = $companyInfo['status'] == 1 ? 2 : 1;
  142. $statusText = $newStatus == 2 ? '禁用' : '启用';
  143. $result = Db::name('company')
  144. ->where('id', $id)
  145. ->update([
  146. 'status' => $newStatus,
  147. 'update_time' => date('Y-m-d H:i:s')
  148. ]);
  149. } catch (\Exception $e) {
  150. $this->error('操作失败: ' . $e->getMessage());
  151. }
  152. if ($result) {
  153. $this->success($statusText . '成功');
  154. } else {
  155. $this->error(__('操作失败'));
  156. }
  157. }
  158. /**
  159. * 获取企业详情
  160. * @return void
  161. */
  162. public function detail():void
  163. {
  164. $id = $this->request->get('id');
  165. if (empty($id)) {
  166. $this->error(__('参数错误'));
  167. }
  168. $companyInfo = Db::name('company')
  169. ->where('id', $id)
  170. ->find();
  171. if (!$companyInfo) {
  172. $this->error(__('企业不存在'));
  173. }
  174. $this->success('查询成功', $companyInfo);
  175. }
  176. /**
  177. * 获取企业状态选项
  178. * @return void
  179. */
  180. public function statusOptions():void
  181. {
  182. $options = [
  183. ['value' => 1, 'label' => '正常'],
  184. ['value' => 2, 'label' => '禁用']
  185. ];
  186. $this->success('获取成功', $options);
  187. }
  188. }