| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\admin\controller\userCenter;
- use app\common\controller\Backend;
- use think\facade\Db;
- class Index extends Backend
- {
- protected array $noNeedLogin = ['*'];
- /**
- * 企业列表查询
- * @return void
- */
- public function index():void
- {
- $where = [];
- // 企业名称搜索
- if (!empty($this->request->get('name'))) {
- $name = $this->request->get('name');
- $where[] = ['name', 'like', '%' . $name . '%'];
- }
- // 状态筛选
- if (!empty($this->request->get('status'))) {
- $status = $this->request->get('status');
- $where['status'] = $status;
- }
- // 排序
- $order = [];
- if (!empty($this->request->get('order'))) {
- $order_a = $this->request->get('order');
- $order_a = explode(',', $order_a);
- $order[$order_a[0]] = $order_a[1];
- } else {
- $order['id'] = 'desc';
- }
- // 分页
- $page = $this->request->get('page', 1);
- $limit = $this->request->get('limit', 20);
- $companyList = Db::name('company')
- ->where($where)
- ->order($order)
- ->paginate([
- 'list_rows' => $limit,
- 'page' => $page
- ]);
- $this->success('查询成功', [
- 'list' => $companyList->items(),
- 'total' => $companyList->total()
- ]);
- }
- /**
- * 新增/编辑企业
- * @return void
- */
- public function edit():void
- {
- $id = $this->request->post('id');
- $name = $this->request->post('name');
- $status = $this->request->post('status', 1);
- if (empty($name)) {
- $this->error(__('企业名称不能为空'));
- }
- // 验证企业名称是否已存在(编辑时排除自己)
- $checkName = Db::name('company')
- ->where('name', $name);
- if (!empty($id)) {
- $checkName->where('id', '<>', $id);
- }
- if ($checkName->find()) {
- $this->error(__('企业名称已存在'));
- }
- $data = [
- 'name' => $name,
- 'status' => $status,
- 'update_time' => date('Y-m-d H:i:s')
- ];
- $adminInfo = $this->auth->getInfo();
- try {
- if (!empty($id)) {
- // 编辑
- $result = Db::name('company')
- ->where('id', $id)
- ->update($data);
- Db::name('user_on')
- ->where('company_id', $id)
- ->update(['company'=>$name]);
- $message = '编辑成功';
- } else {
- // 新增
- $data['creator_name'] = $adminInfo['nickname']; // 可以从登录信息获取
- $data['create_time'] = date('Y-m-d H:i:s');
- $result = Db::name('company')
- ->insert($data);
- $id = Db::name('company')->getLastInsID();
- $message = '新增成功';
- }
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success($message, ['id' => $id]);
- } else {
- $this->error(__('操作失败'));
- }
- }
- /**
- * 删除企业
- * @return void
- */
- public function delete():void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- try {
- $result = Db::name('company')
- ->where('id', $id)
- ->delete();
- } catch (\Exception $e) {
- $this->error('删除失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success('删除成功');
- } else {
- $this->error(__('删除失败'));
- }
- }
- /**
- * 获取启用企业选项(用于下拉选择)
- * @return void
- */
- public function selectOptions():void
- {
- $packageList = Db::name('company')
- ->where('status', 1)
- ->field('id,name')
- ->select()
- ->toArray();
- $this->success('获取成功', $packageList);
- }
- /**
- * 禁用/启用企业
- * @return void
- */
- public function toggleStatus():void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- try {
- $companyInfo = Db::name('company')
- ->where('id', $id)
- ->find();
- if (!$companyInfo) {
- $this->error(__('企业不存在'));
- }
- $newStatus = $companyInfo['status'] == 1 ? 2 : 1;
- $statusText = $newStatus == 2 ? '禁用' : '启用';
- $result = Db::name('company')
- ->where('id', $id)
- ->update([
- 'status' => $newStatus,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success($statusText . '成功');
- } else {
- $this->error(__('操作失败'));
- }
- }
- /**
- * 获取企业详情
- * @return void
- */
- public function detail():void
- {
- $id = $this->request->get('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $companyInfo = Db::name('company')
- ->where('id', $id)
- ->find();
- if (!$companyInfo) {
- $this->error(__('企业不存在'));
- }
- $this->success('查询成功', $companyInfo);
- }
- /**
- * 获取企业状态选项
- * @return void
- */
- public function statusOptions():void
- {
- $options = [
- ['value' => 1, 'label' => '正常'],
- ['value' => 2, 'label' => '禁用']
- ];
- $this->success('获取成功', $options);
- }
- }
|