| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\model\CooperationLead as CooperationLeadModel;
- use app\common\model\CooperationOption as CooperationOptionModel;
- use think\facade\Db;
- class Cooperation extends Backend
- {
- protected $modelClass = null;
- protected $modelValidate = false;
- protected $searchFields = 'id,name,phone,email,company,country,industry,lang,ip';
- protected function initialize()
- {
- parent::initialize();
- $this->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;
- }
- }
|