modelClass = new CooperationLeadModel(); $this->assign('statusList', $this->modelClass->getStatusList()); $this->assign('typeList', (new CooperationOptionModel())->getTypeList()); $this->assignLeadFilterOptions(); $action = strtolower($this->request->action()); if (in_array($action, ['options', 'addoption', 'editoption'], true)) { $config = $this->view->config ?: []; $config['jsname'] = 'backend/cooperation'; $this->view->config = $config; $this->assign('config', $config); } } public function index() { if ($this->request->isAjax()) { $page = (int)$this->request->param('page/d', 1); $limit = (int)$this->request->param('limit/d', 20); $sort = (string)$this->request->param('sort/s', 'id'); $order = (string)$this->request->param('order/s', 'desc'); $query = $this->buildLeadQuery(); $count = (clone $query)->count(); $data = $query->order($sort, $order)->page($page, $limit)->select(); return json(['code' => 0, 'count' => $count, 'data' => $data]); } return $this->fetch(); } public function del() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $ids = $this->request->post('ids/a', []); if (!$ids) { $this->error('请先选择留资记录'); } $count = $this->modelClass->where('id', 'in', $ids)->delete(); $this->success('已删除 ' . (int)$count . ' 条留资记录'); } public function changeStatus() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $id = (int)$this->request->post('id/d', 0); $status = (int)$this->request->post('status/d', 0); if ($id <= 0 || !array_key_exists($status, $this->modelClass->getStatusList())) { $this->error('参数错误'); } $row = $this->modelClass->find($id); if (!$row) { $this->error('留资记录不存在'); } $row->status = $status; $row->updatetime = time(); $row->save(); $this->success('状态更新成功'); } public function remark() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $id = (int)$this->request->post('id/d', 0); $remark = trim((string)$this->request->post('admin_remark/s', '')); $row = $this->modelClass->find($id); if (!$row) { $this->error('留资记录不存在'); } $row->admin_remark = mb_substr($remark, 0, 1000, 'UTF-8'); $row->updatetime = time(); $row->save(); $this->success('备注保存成功'); } public function options() { if ($this->request->isAjax()) { $page = (int)$this->request->param('page/d', 1); $limit = (int)$this->request->param('limit/d', 20); $sort = (string)$this->request->param('sort/s', 'listorder'); $order = (string)$this->request->param('order/s', 'asc'); $type = trim((string)$this->request->param('type/s', 'country')); $lang = trim((string)$this->request->param('lang/s', '')); $status = $this->request->param('status/s', ''); $typeList = (new CooperationOptionModel())->getTypeList(); if (!isset($typeList[$type])) { $type = 'country'; } $query = CooperationOptionModel::where('type', $type); if ($lang !== '') { $query = $query->where('lang', $lang); } if ($status !== '') { $query = $query->where('status', (int)$status); } $count = (clone $query)->count(); $data = $query->order($sort, $order)->order('id', 'asc')->page($page, $limit)->select(); return json(['code' => 0, 'count' => $count, 'data' => $data]); } $type = trim((string)$this->request->param('type/s', 'country')); $typeList = (new CooperationOptionModel())->getTypeList(); if (!isset($typeList[$type])) { $type = 'country'; } $this->assign('defaultType', $type); return $this->fetch(); } public function addOption() { if (!$this->request->isPost()) { $type = trim((string)$this->request->param('type/s', '')); if ($type !== '') { $this->assign('defaultType', $type); } return $this->fetch('option_form'); } $this->saveOption(); } public function editOption() { $id = (int)$this->request->param('id/d', 0); $row = CooperationOptionModel::find($id); if (!$row) { $this->error('选项不存在'); } if (!$this->request->isPost()) { $this->assign('row', $row); return $this->fetch('option_form'); } $this->saveOption($row); } public function delOption() { if (!$this->request->isPost()) { $this->error('请求方式错误'); } $ids = $this->request->post('ids/a', []); if (!$ids) { $this->error('请先选择选项'); } $count = CooperationOptionModel::where('id', 'in', $ids)->delete(); $this->success('已删除 ' . (int)$count . ' 个选项'); } protected function saveOption(?CooperationOptionModel $row = null): void { $type = trim((string)$this->request->post('type/s', '')); $lang = strtolower(trim((string)$this->request->post('lang/s', 'cn'))); $title = trim((string)$this->request->post('title/s', '')); $value = trim((string)$this->request->post('value/s', '')); $parentValue = trim((string)$this->request->post('parent_value/s', '')); $listorder = (int)$this->request->post('listorder/d', 100); $status = (int)$this->request->post('status/d', 1); $typeList = (new CooperationOptionModel())->getTypeList(); if (!isset($typeList[$type])) { $this->error('选项类型错误'); } if ($lang === '') { $this->error('语言不能为空'); } if ($title === '') { $this->error('显示名称不能为空'); } if ($value === '') { $value = $title; } if ($type !== 'country') { $parentValue = ''; } $now = time(); $data = [ 'type' => $type, 'lang' => $lang, 'title' => mb_substr($title, 0, 120, 'UTF-8'), 'value' => mb_substr($value, 0, 120, 'UTF-8'), 'parent_value' => mb_substr($parentValue, 0, 120, 'UTF-8'), 'listorder' => $listorder, 'status' => $status ? 1 : 0, 'updatetime' => $now, ]; if ($row) { $row->save($data); } else { $data['createtime'] = $now; CooperationOptionModel::create($data); } $this->success('保存成功'); } protected function assignLeadFilterOptions(): void { $langs = CooperationOptionModel::whereIn('type', ['country', 'industry', 'country_region']) ->where('status', 1) ->distinct(true) ->order('lang', 'asc') ->column('lang'); if (!in_array('cn', $langs, true)) { array_unshift($langs, 'cn'); } $this->assign('countryOptions', CooperationOptionModel::getOptions('country', 'cn')); $this->assign('industryOptions', CooperationOptionModel::getOptions('industry', 'cn')); $this->assign('langOptions', array_values(array_unique(array_filter($langs)))); } protected function buildLeadQuery() { $keyword = trim((string)$this->request->param('keyword/s', '')); $country = trim((string)$this->request->param('country/s', '')); $industry = trim((string)$this->request->param('industry/s', '')); $lang = trim((string)$this->request->param('lang/s', '')); $status = $this->request->param('status/s', ''); $createtimeStart = trim((string)$this->request->param('createtime_start/s', '')); $createtimeEnd = trim((string)$this->request->param('createtime_end/s', '')); $query = $this->modelClass; if ($keyword !== '') { $query = $query->where(function ($q) use ($keyword) { $q->whereOr('name', 'like', '%' . $keyword . '%') ->whereOr('phone', 'like', '%' . $keyword . '%') ->whereOr('email', 'like', '%' . $keyword . '%') ->whereOr('company', 'like', '%' . $keyword . '%'); }); } if ($country !== '') { $query = $query->where('country', $country); } if ($industry !== '') { $query = $query->where('industry', $industry); } if ($lang !== '') { $query = $query->where('lang', $lang); } if ($status !== '') { $query = $query->where('status', (int)$status); } if ($createtimeStart !== '') { $timestamp = strtotime($createtimeStart); if ($timestamp) { $query = $query->where('createtime', '>=', $timestamp); } } if ($createtimeEnd !== '') { $timestamp = strtotime($createtimeEnd); if ($timestamp) { $query = $query->where('createtime', '<=', $timestamp); } } return $query; } }