Manage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace app\admin\controller\ad;
  3. use app\common\library\Redis;
  4. use app\common\model\AdManage;
  5. use app\common\controller\Backend;
  6. use app\common\model\object\BaseObject;
  7. use app\common\service\AdPlanService;
  8. use app\main\constants\AdConstants;
  9. use think\Controller;
  10. use think\Request;
  11. /**
  12. * 广告计划管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Manage extends Backend
  17. {
  18. /**
  19. * AdManage模型对象
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. /**
  26. * @var AdManage
  27. */
  28. $this->model = model('AdManage');
  29. $this->assign('bannerPositions', $this->model->getBannerPosition());
  30. $this->assign('screenPositions', $this->model->getScreenPosition());
  31. $this->assign('welfarePositions', $this->model->getWelfarePosition());
  32. $this->assign('adTypes', $this->model->getAdType());
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. /**
  40. * 查看
  41. */
  42. public function index()
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags']);
  46. if ($this->request->isAjax())
  47. {
  48. //如果发送的来源是Selectpage,则转发到Selectpage
  49. if ($this->request->request('pkey_name'))
  50. {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $total = $this->model
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->count();
  58. $list = $this->model
  59. ->join('ad_user_group', 'ad_manage.user_group_id = ad_user_group.id', 'left')
  60. ->where($where)
  61. ->field('ad_manage.*, ad_user_group.group_name, ad_user_group.group_type')
  62. ->order('ad_manage.id', $order)
  63. ->limit($offset, $limit)
  64. ->select();
  65. $result = array("total" => $total, "rows" => $list);
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 添加
  72. */
  73. public function add()
  74. {
  75. if ($this->request->isPost())
  76. {
  77. $params = $this->request->post("row/a");
  78. if ($params)
  79. {
  80. /*
  81. * 已经弃用,如果为了兼容老版可取消注释
  82. foreach ($params as $k => &$v)
  83. {
  84. $v = is_array($v) ? implode(',', $v) : $v;
  85. }
  86. */
  87. if ($this->dataLimit)
  88. {
  89. $params[$this->dataLimitField] = $this->auth->id;
  90. }
  91. try
  92. {
  93. //是否采用模型验证
  94. if ($this->modelValidate)
  95. {
  96. $name = basename(str_replace('\\', '/', get_class($this->model)));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  98. $this->model->validate($validate);
  99. }
  100. $this->_validateParams($params);
  101. $params['show_position'] = implode(',', $params['show_position']);
  102. $this->_welfareJsonEncode($params);
  103. $result = $this->model->allowField(true)->save($params);
  104. if ($result !== false)
  105. {
  106. $this->success();
  107. }
  108. else
  109. {
  110. $this->error($this->model->getError());
  111. }
  112. }
  113. catch (\think\exception\PDOException $e)
  114. {
  115. $this->error($e->getMessage());
  116. }
  117. }
  118. $this->error(__('Parameter %s can not be empty', ''));
  119. }
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 校验表单
  124. * @param $params
  125. * @return bool
  126. */
  127. private function _validateParams(&$params)
  128. {
  129. if ($params['ad_type'] == 2) {
  130. $params['show_type'] = 0;
  131. // 福利广告
  132. $tar_arr = $this->model->getWelfarePosition();
  133. $params['show_position'] = $roolPosition = array_column($tar_arr, 'id');
  134. $result = array_intersect($roolPosition, $params['show_position']);
  135. }elseif ($params['ad_type'] == 1) {
  136. $params['show_type'] = 0;
  137. // 插屏广告
  138. $tar_arr = $this->model->getScreenPosition();
  139. $roolPosition = array_column($tar_arr, 'id');
  140. $result = array_intersect($roolPosition, $params['show_position']);
  141. if (in_array(19, $params['show_position'])) {
  142. if ((integer)$params['chapter_start_num'] <= 0) {
  143. $this->error('请填写阅读页插屏(整章)开始章节数');
  144. }
  145. if ((integer)$params['chapter_step_num'] <= 0) {
  146. $this->error('阅读页插屏(整章)间隔章节数');
  147. }
  148. }
  149. } else {
  150. $params['chapter_num'] = 0;
  151. // Banner广告
  152. $tar_arr = $this->model->getBannerPosition();
  153. $roolPosition = array_column($tar_arr, 'id');
  154. $result = array_intersect($roolPosition, $params['show_position']);
  155. }
  156. if (count($result) == 0) {
  157. $this->error('请选择展示位置');
  158. return false;
  159. } else {
  160. $params['show_position'] = $result;
  161. }
  162. if (!in_array(AdConstants::AD_P_READ_SCREEN, $params['show_position'])) {
  163. $params['chapter_num'] = 0;
  164. }
  165. if(in_array(AdConstants::AD_P_READ_SCREEN, $params['show_position']) && $params['chapter_num'] == 0){
  166. $this->error('勾选阅读器插屏后,请填写间隔章节数');
  167. return false;
  168. }
  169. }
  170. /**
  171. * 将福利广告的参数压缩成JSON格式
  172. * @param $params
  173. */
  174. private function _welfareJsonEncode(&$params)
  175. {
  176. $welfare_params = ['float_pop_icon', 'float_pop_chapter_num', 'recharge_pop_icon', 'smart_title', 'smart_deputy_title', 'smart_icon', 'day_receive_num', 'give_kandian'];
  177. $welfare_arr = [];
  178. foreach ($welfare_params as $k => $item) {
  179. $welfare_arr[$item] = $params[$item] ?? '';
  180. unset($params[$item]);
  181. }
  182. $params['welfare_json'] = json_encode($welfare_arr);
  183. }
  184. /**
  185. * 将福利广告的JSON格式 解析为 数组
  186. * @param $row
  187. */
  188. private function _welfareJsonDecode(AdManage &$row)
  189. {
  190. if($row -> ad_type == 2){
  191. $welfare_arr = json_decode($row->welfare_json, true);
  192. foreach ($welfare_arr as $k=>$value){
  193. $row->$k = $value;
  194. }
  195. }
  196. }
  197. /**
  198. * 编辑
  199. */
  200. public function edit($ids = NULL)
  201. {
  202. $row = $this->model->get($ids);
  203. $this->_welfareJsonDecode($row); //
  204. if (!$row)
  205. $this->error(__('No Results were found'));
  206. $adminIds = $this->getDataLimitAdminIds();
  207. if (is_array($adminIds))
  208. {
  209. if (!in_array($row[$this->dataLimitField], $adminIds))
  210. {
  211. $this->error(__('You have no permission'));
  212. }
  213. }
  214. if ($this->request->isPost())
  215. {
  216. $params = $this->request->post("row/a");
  217. if ($params)
  218. {
  219. /*
  220. * 已经弃用,如果为了兼容老版可取消注释
  221. foreach ($params as $k => &$v)
  222. {
  223. $v = is_array($v) ? implode(',', $v) : $v;
  224. }
  225. */
  226. try
  227. {
  228. //是否采用模型验证
  229. if ($this->modelValidate)
  230. {
  231. $name = basename(str_replace('\\', '/', get_class($this->model)));
  232. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  233. $row->validate($validate);
  234. }
  235. $this->_validateParams($params);
  236. $params['show_position'] = implode(',', $params['show_position']);
  237. $this->_welfareJsonEncode($params);
  238. $result = $row->allowField(true)->save($params);
  239. if ($result !== false)
  240. {
  241. // 编辑时,删除缓存
  242. Redis::instance()->del(AdConstants::AD_PLAN . $ids);
  243. $this->success();
  244. }
  245. else
  246. {
  247. $this->error($row->getError());
  248. }
  249. }
  250. catch (\think\exception\PDOException $e)
  251. {
  252. $this->error($e->getMessage());
  253. }
  254. }
  255. $this->error(__('Parameter %s can not be empty', ''));
  256. }
  257. $this->view->assign("row", $row);
  258. $this->view->assign('show_position', explode(',', $row->show_position));
  259. return $this->view->fetch();
  260. }
  261. /**
  262. * 删除
  263. */
  264. public function del($ids = "")
  265. {
  266. if ($ids)
  267. {
  268. $pk = $this->model->getPk();
  269. $adminIds = $this->getDataLimitAdminIds();
  270. if (is_array($adminIds))
  271. {
  272. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  273. }
  274. $list = $this->model->where($pk, 'in', $ids)->select();
  275. $count = 0;
  276. foreach ($list as $k => $v)
  277. {
  278. $count += $v->delete();
  279. }
  280. if ($count)
  281. {
  282. $this->success();
  283. }
  284. else
  285. {
  286. $this->error(__('No rows were deleted'));
  287. }
  288. }
  289. $this->error(__('Parameter %s can not be empty', 'ids'));
  290. }
  291. /**
  292. * 广告计划关联用户组ID
  293. */
  294. public function ajaxSaveUserGroup()
  295. {
  296. $params = $this->request->param();
  297. $rst = $this->model->where('id', 'eq', $params['ad_id'])->update(['user_group_id' => $params['user_group_id'], 'flag'=> 1]);
  298. if ($rst) {
  299. $arr = [
  300. 'code' => 0,
  301. 'msg' =>'成功'
  302. ];
  303. echo json_encode($arr);
  304. }else{
  305. $arr = [
  306. 'code' => 1,
  307. 'msg' =>'关联用户组出错'
  308. ];
  309. echo json_encode($arr);
  310. }
  311. }
  312. /**
  313. * Ajax 更新所有的广告计划缓存
  314. */
  315. public function ajaxUpdateCache()
  316. {
  317. AdPlanService::instance()->refreshAllAd();
  318. $arr = [
  319. 'code' => 0,
  320. 'msg' => '成功'
  321. ];
  322. echo json_encode($arr);
  323. }
  324. /**
  325. * 批量生效 | 批量失效
  326. */
  327. public function batchValidate()
  328. {
  329. $params = $this->request->param();
  330. $ids = $params['ids'];
  331. $state = $params['state'];
  332. if (is_array($ids)) {
  333. foreach ($ids as $id) {
  334. $result = $this->model->update(['state' => $state], ['id' => $id]);
  335. }
  336. } else {
  337. $result = $this->model->update(['state' => $state], ['id' => $ids]);
  338. }
  339. if($result){
  340. $this->success('设置成功');
  341. }else{
  342. $this->error('设置失败');
  343. }
  344. }
  345. }