Cooperation.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\CooperationLead as CooperationLeadModel;
  5. use app\common\model\CooperationOption as CooperationOptionModel;
  6. use think\facade\Db;
  7. class Cooperation extends Backend
  8. {
  9. protected $modelClass = null;
  10. protected $modelValidate = false;
  11. protected $searchFields = 'id,name,phone,email,company,country,industry,lang,ip';
  12. protected function initialize()
  13. {
  14. parent::initialize();
  15. $this->modelClass = new CooperationLeadModel();
  16. $this->assign('statusList', $this->modelClass->getStatusList());
  17. $this->assign('typeList', (new CooperationOptionModel())->getTypeList());
  18. $this->assignLeadFilterOptions();
  19. $action = strtolower($this->request->action());
  20. if (in_array($action, ['options', 'addoption', 'editoption'], true)) {
  21. $config = $this->view->config ?: [];
  22. $config['jsname'] = 'backend/cooperation';
  23. $this->view->config = $config;
  24. $this->assign('config', $config);
  25. }
  26. }
  27. public function index()
  28. {
  29. if ($this->request->isAjax()) {
  30. $page = (int)$this->request->param('page/d', 1);
  31. $limit = (int)$this->request->param('limit/d', 20);
  32. $sort = (string)$this->request->param('sort/s', 'id');
  33. $order = (string)$this->request->param('order/s', 'desc');
  34. $query = $this->buildLeadQuery();
  35. $count = (clone $query)->count();
  36. $data = $query->order($sort, $order)->page($page, $limit)->select();
  37. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  38. }
  39. return $this->fetch();
  40. }
  41. public function del()
  42. {
  43. if (!$this->request->isPost()) {
  44. $this->error('请求方式错误');
  45. }
  46. $ids = $this->request->post('ids/a', []);
  47. if (!$ids) {
  48. $this->error('请先选择留资记录');
  49. }
  50. $count = $this->modelClass->where('id', 'in', $ids)->delete();
  51. $this->success('已删除 ' . (int)$count . ' 条留资记录');
  52. }
  53. public function changeStatus()
  54. {
  55. if (!$this->request->isPost()) {
  56. $this->error('请求方式错误');
  57. }
  58. $id = (int)$this->request->post('id/d', 0);
  59. $status = (int)$this->request->post('status/d', 0);
  60. if ($id <= 0 || !array_key_exists($status, $this->modelClass->getStatusList())) {
  61. $this->error('参数错误');
  62. }
  63. $row = $this->modelClass->find($id);
  64. if (!$row) {
  65. $this->error('留资记录不存在');
  66. }
  67. $row->status = $status;
  68. $row->updatetime = time();
  69. $row->save();
  70. $this->success('状态更新成功');
  71. }
  72. public function remark()
  73. {
  74. if (!$this->request->isPost()) {
  75. $this->error('请求方式错误');
  76. }
  77. $id = (int)$this->request->post('id/d', 0);
  78. $remark = trim((string)$this->request->post('admin_remark/s', ''));
  79. $row = $this->modelClass->find($id);
  80. if (!$row) {
  81. $this->error('留资记录不存在');
  82. }
  83. $row->admin_remark = mb_substr($remark, 0, 1000, 'UTF-8');
  84. $row->updatetime = time();
  85. $row->save();
  86. $this->success('备注保存成功');
  87. }
  88. public function options()
  89. {
  90. if ($this->request->isAjax()) {
  91. $page = (int)$this->request->param('page/d', 1);
  92. $limit = (int)$this->request->param('limit/d', 20);
  93. $sort = (string)$this->request->param('sort/s', 'listorder');
  94. $order = (string)$this->request->param('order/s', 'asc');
  95. $type = trim((string)$this->request->param('type/s', 'country'));
  96. $lang = trim((string)$this->request->param('lang/s', ''));
  97. $status = $this->request->param('status/s', '');
  98. $typeList = (new CooperationOptionModel())->getTypeList();
  99. if (!isset($typeList[$type])) {
  100. $type = 'country';
  101. }
  102. $query = CooperationOptionModel::where('type', $type);
  103. if ($lang !== '') {
  104. $query = $query->where('lang', $lang);
  105. }
  106. if ($status !== '') {
  107. $query = $query->where('status', (int)$status);
  108. }
  109. $count = (clone $query)->count();
  110. $data = $query->order($sort, $order)->order('id', 'asc')->page($page, $limit)->select();
  111. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  112. }
  113. $type = trim((string)$this->request->param('type/s', 'country'));
  114. $typeList = (new CooperationOptionModel())->getTypeList();
  115. if (!isset($typeList[$type])) {
  116. $type = 'country';
  117. }
  118. $this->assign('defaultType', $type);
  119. return $this->fetch();
  120. }
  121. public function addOption()
  122. {
  123. if (!$this->request->isPost()) {
  124. $type = trim((string)$this->request->param('type/s', ''));
  125. if ($type !== '') {
  126. $this->assign('defaultType', $type);
  127. }
  128. return $this->fetch('option_form');
  129. }
  130. $this->saveOption();
  131. }
  132. public function editOption()
  133. {
  134. $id = (int)$this->request->param('id/d', 0);
  135. $row = CooperationOptionModel::find($id);
  136. if (!$row) {
  137. $this->error('选项不存在');
  138. }
  139. if (!$this->request->isPost()) {
  140. $this->assign('row', $row);
  141. return $this->fetch('option_form');
  142. }
  143. $this->saveOption($row);
  144. }
  145. public function delOption()
  146. {
  147. if (!$this->request->isPost()) {
  148. $this->error('请求方式错误');
  149. }
  150. $ids = $this->request->post('ids/a', []);
  151. if (!$ids) {
  152. $this->error('请先选择选项');
  153. }
  154. $count = CooperationOptionModel::where('id', 'in', $ids)->delete();
  155. $this->success('已删除 ' . (int)$count . ' 个选项');
  156. }
  157. protected function saveOption(?CooperationOptionModel $row = null): void
  158. {
  159. $type = trim((string)$this->request->post('type/s', ''));
  160. $lang = strtolower(trim((string)$this->request->post('lang/s', 'cn')));
  161. $title = trim((string)$this->request->post('title/s', ''));
  162. $value = trim((string)$this->request->post('value/s', ''));
  163. $parentValue = trim((string)$this->request->post('parent_value/s', ''));
  164. $listorder = (int)$this->request->post('listorder/d', 100);
  165. $status = (int)$this->request->post('status/d', 1);
  166. $typeList = (new CooperationOptionModel())->getTypeList();
  167. if (!isset($typeList[$type])) {
  168. $this->error('选项类型错误');
  169. }
  170. if ($lang === '') {
  171. $this->error('语言不能为空');
  172. }
  173. if ($title === '') {
  174. $this->error('显示名称不能为空');
  175. }
  176. if ($value === '') {
  177. $value = $title;
  178. }
  179. if ($type !== 'country') {
  180. $parentValue = '';
  181. }
  182. $now = time();
  183. $data = [
  184. 'type' => $type,
  185. 'lang' => $lang,
  186. 'title' => mb_substr($title, 0, 120, 'UTF-8'),
  187. 'value' => mb_substr($value, 0, 120, 'UTF-8'),
  188. 'parent_value' => mb_substr($parentValue, 0, 120, 'UTF-8'),
  189. 'listorder' => $listorder,
  190. 'status' => $status ? 1 : 0,
  191. 'updatetime' => $now,
  192. ];
  193. if ($row) {
  194. $row->save($data);
  195. } else {
  196. $data['createtime'] = $now;
  197. CooperationOptionModel::create($data);
  198. }
  199. $this->success('保存成功');
  200. }
  201. protected function assignLeadFilterOptions(): void
  202. {
  203. $langs = CooperationOptionModel::whereIn('type', ['country', 'industry', 'country_region'])
  204. ->where('status', 1)
  205. ->distinct(true)
  206. ->order('lang', 'asc')
  207. ->column('lang');
  208. if (!in_array('cn', $langs, true)) {
  209. array_unshift($langs, 'cn');
  210. }
  211. $this->assign('countryOptions', CooperationOptionModel::getOptions('country', 'cn'));
  212. $this->assign('industryOptions', CooperationOptionModel::getOptions('industry', 'cn'));
  213. $this->assign('langOptions', array_values(array_unique(array_filter($langs))));
  214. }
  215. protected function buildLeadQuery()
  216. {
  217. $keyword = trim((string)$this->request->param('keyword/s', ''));
  218. $country = trim((string)$this->request->param('country/s', ''));
  219. $industry = trim((string)$this->request->param('industry/s', ''));
  220. $lang = trim((string)$this->request->param('lang/s', ''));
  221. $status = $this->request->param('status/s', '');
  222. $createtimeStart = trim((string)$this->request->param('createtime_start/s', ''));
  223. $createtimeEnd = trim((string)$this->request->param('createtime_end/s', ''));
  224. $query = $this->modelClass;
  225. if ($keyword !== '') {
  226. $query = $query->where(function ($q) use ($keyword) {
  227. $q->whereOr('name', 'like', '%' . $keyword . '%')
  228. ->whereOr('phone', 'like', '%' . $keyword . '%')
  229. ->whereOr('email', 'like', '%' . $keyword . '%')
  230. ->whereOr('company', 'like', '%' . $keyword . '%');
  231. });
  232. }
  233. if ($country !== '') {
  234. $query = $query->where('country', $country);
  235. }
  236. if ($industry !== '') {
  237. $query = $query->where('industry', $industry);
  238. }
  239. if ($lang !== '') {
  240. $query = $query->where('lang', $lang);
  241. }
  242. if ($status !== '') {
  243. $query = $query->where('status', (int)$status);
  244. }
  245. if ($createtimeStart !== '') {
  246. $timestamp = strtotime($createtimeStart);
  247. if ($timestamp) {
  248. $query = $query->where('createtime', '>=', $timestamp);
  249. }
  250. }
  251. if ($createtimeEnd !== '') {
  252. $timestamp = strtotime($createtimeEnd);
  253. if ($timestamp) {
  254. $query = $query->where('createtime', '<=', $timestamp);
  255. }
  256. }
  257. return $query;
  258. }
  259. }