Advert.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\admin;
  10. use app\admin\controller\Admin;
  11. use app\common\builder\ZBuilder;
  12. use app\cms\model\Advert as AdvertModel;
  13. use app\cms\model\AdvertType as AdvertTypeModel;
  14. use think\facade\Validate;
  15. /**
  16. * 广告控制器
  17. * @package app\cms\admin
  18. */
  19. class Advert extends Admin
  20. {
  21. /**
  22. * 广告列表
  23. * @author 蔡伟明 <314013107@qq.com>
  24. * @return mixed
  25. * @throws \think\Exception
  26. * @throws \think\exception\DbException
  27. */
  28. public function index()
  29. {
  30. // 查询
  31. $map = $this->getMap();
  32. // 排序
  33. $order = $this->getOrder('update_time desc');
  34. // 数据列表
  35. $data_list = AdvertModel::where($map)->order($order)->paginate();
  36. $btnType = [
  37. 'class' => 'btn btn-info',
  38. 'title' => '广告分类',
  39. 'icon' => 'fa fa-fw fa-sitemap',
  40. 'href' => url('advert_type/index')
  41. ];
  42. $list_type = AdvertTypeModel::where('status', 1)->column('id,name');
  43. array_unshift($list_type, '默认分类');
  44. // 使用ZBuilder快速创建数据表格
  45. return ZBuilder::make('table')
  46. ->setSearch(['title' => '标题']) // 设置搜索框
  47. ->addColumns([ // 批量添加数据列
  48. ['id', 'ID'],
  49. ['name', '广告名称', 'text.edit'],
  50. ['typeid', '分类', 'select', $list_type],
  51. ['ad_type', '类型', 'text', '', ['代码', '文字', '图片', 'flash']],
  52. ['timeset', '时间限制', 'text', '', ['永不过期', '限时']],
  53. ['create_time', '创建时间', 'datetime'],
  54. ['update_time', '更新时间', 'datetime'],
  55. ['status', '状态', 'switch'],
  56. ['right_button', '操作', 'btn']
  57. ])
  58. ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
  59. ->addTopButton('custom', $btnType) // 添加顶部按钮
  60. ->addRightButtons(['edit', 'delete' => ['data-tips' => '删除后无法恢复。']]) // 批量添加右侧按钮
  61. ->addOrder('id,name,typeid,timeset,ad_type,create_time,update_time')
  62. ->setRowList($data_list) // 设置表格数据
  63. ->addValidate('Advert', 'name')
  64. ->fetch(); // 渲染模板
  65. }
  66. /**
  67. * 新增
  68. * @author 蔡伟明 <314013107@qq.com>
  69. * @return mixed
  70. * @throws \think\Exception
  71. */
  72. public function add()
  73. {
  74. // 保存数据
  75. if ($this->request->isPost()) {
  76. // 表单数据
  77. $data = $this->request->post();
  78. // 验证
  79. $result = $this->validate($data, 'Advert');
  80. if (true !== $result) $this->error($result);
  81. if ($data['ad_type'] != 0) {
  82. $data['link'] == '' && $this->error('链接不能为空');
  83. Validate::is($data['link'], 'url') === false && $this->error('链接不是有效的url地址'); // true
  84. }
  85. // 广告类型
  86. switch ($data['ad_type']) {
  87. case 0: // 代码
  88. $data['content'] = $data['code'];
  89. break;
  90. case 1: // 文字
  91. $data['content'] = '<a href="'.$data['link'].'" target="_blank" style="';
  92. if ($data['size'] != '') {
  93. $data['content'] .= 'font-size:'.$data['size'].'px;';
  94. }
  95. if ($data['color'] != '') {
  96. $data['content'] .= 'color:'.$data['color'];
  97. }
  98. $data['content'] .= '">'.$data['title'].'</a>';
  99. break;
  100. case 2: // 图片
  101. $data['content'] = '<a href="'.$data['link'].'" target="_blank"><img src="'.get_file_path($data['src']).'" style="';
  102. if ($data['width'] != '') {
  103. $data['content'] .= 'width:'.$data['width'].'px;';
  104. }
  105. if ($data['height'] != '') {
  106. $data['content'] .= 'height:'.$data['height'].'px;';
  107. }
  108. if ($data['alt'] != '') {
  109. $data['content'] .= '" alt="'.$data['alt'];
  110. }
  111. $data['content'] .= '" /></a>';
  112. break;
  113. case 3: // flash
  114. $data['content'] = '';
  115. $data['content'] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
  116. if ($data['width'] != '') {
  117. $data['content'] .= ' width="'.$data['width'].'"';
  118. }
  119. if ($data['height'] != '') {
  120. $data['content'] .= ' height="'.$data['height'].'"';
  121. }
  122. $data['content'] .= '><param name="quality" value="high" /><param name="movie" value="'.$data['link'].'" /><embed allowfullscreen="true"';
  123. if ($data['height'] != '') {
  124. $data['content'] .= ' height="'.$data['height'].'"';
  125. }
  126. $data['content'] .= ' pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="'.$data['link'].'" type="application/x-shockwave-flash"';
  127. if ($data['width'] != '') {
  128. $data['content'] .= ' width="'.$data['width'].'"';
  129. }
  130. $data['content'] .= '></embed></object>';
  131. break;
  132. }
  133. if ($advert = AdvertModel::create($data)) {
  134. // 记录行为
  135. action_log('advert_add', 'cms_advert', $advert['id'], UID, $data['name']);
  136. $this->success('新增成功', 'index');
  137. } else {
  138. $this->error('新增失败');
  139. }
  140. }
  141. $list_type = AdvertTypeModel::where('status', 1)->column('id,name');
  142. array_unshift($list_type, '默认分类');
  143. // 显示添加页面
  144. return ZBuilder::make('form')
  145. ->setPageTips('如果出现无法添加的情况,可能由于浏览器将本页面当成了广告,请尝试关闭浏览器的广告过滤功能再试。', 'warning')
  146. ->addFormItems([
  147. ['select', 'typeid', '广告分类', '', $list_type, 0],
  148. ['text', 'tagname', '广告位标识', '由小写字母、数字或下划线组成,不能以数字开头'],
  149. ['text', 'name', '广告位名称'],
  150. ['radio', 'timeset', '时间限制', '', ['永不过期', '在设内时间内有效'], 0],
  151. ['daterange', 'start_time,end_time', '开始时间-结束时间'],
  152. ['radio', 'ad_type', '广告类型', '', ['代码', '文字', '图片', 'flash'], 0],
  153. ['textarea', 'code', '代码', '<code>必填</code>,支持html代码'],
  154. ['image', 'src', '图片', '<code>必须</code>'],
  155. ['text', 'title', '文字内容', '<code>必填</code>'],
  156. ['text', 'link', '链接', '<code>必填</code>'],
  157. ['colorpicker', 'color', '文字颜色', '', '', 'rgb'],
  158. ['text', 'size', '文字大小', '只需填写数字,例如:12,表示12px', '', ['', 'px']],
  159. ['text', 'width', '宽度', '不用填写单位,只需填写具体数字'],
  160. ['text', 'height', '高度', '不用填写单位,只需填写具体数字'],
  161. ['text', 'alt', '图片描述', '即图片alt的值'],
  162. ['radio', 'status', '立即启用', '', ['否', '是'], 1]
  163. ])
  164. ->setTrigger('ad_type', '0', 'code')
  165. ->setTrigger('ad_type', '1', 'title,color,size')
  166. ->setTrigger('ad_type', '2', 'src,alt')
  167. ->setTrigger('ad_type', '2,3', 'width,height')
  168. ->setTrigger('ad_type', '1,2,3', 'link')
  169. ->setTrigger('timeset', '1', 'start_time')
  170. ->fetch();
  171. }
  172. /**
  173. * 编辑
  174. * @param null $id 广告id
  175. * @author 蔡伟明 <314013107@qq.com>
  176. * @return mixed
  177. * @throws \think\Exception
  178. */
  179. public function edit($id = null)
  180. {
  181. if ($id === null) $this->error('缺少参数');
  182. // 保存数据
  183. if ($this->request->isPost()) {
  184. // 表单数据
  185. $data = $this->request->post();
  186. // 验证
  187. $result = $this->validate($data, 'Advert');
  188. if (true !== $result) $this->error($result);
  189. if (AdvertModel::update($data)) {
  190. // 记录行为
  191. action_log('advert_edit', 'cms_advert', $id, UID, $data['name']);
  192. $this->success('编辑成功', 'index');
  193. } else {
  194. $this->error('编辑失败');
  195. }
  196. }
  197. $list_type = AdvertTypeModel::where('status', 1)->column('id,name');
  198. array_unshift($list_type, '默认分类');
  199. $info = AdvertModel::get($id);
  200. $info['ad_type'] = ['代码', '文字', '图片', 'flash'][$info['ad_type']];
  201. // 显示编辑页面
  202. return ZBuilder::make('form')
  203. ->setPageTips('如果出现无法添加的情况,可能由于浏览器将本页面当成了广告,请尝试关闭浏览器的广告过滤功能再试。', 'warning')
  204. ->addFormItems([
  205. ['hidden', 'id'],
  206. ['hidden', 'tagname'],
  207. ['static', 'tagname', '广告位标识'],
  208. ['static', 'ad_type', '广告类型'],
  209. ['text', 'name', '广告位名称'],
  210. ['select', 'typeid', '广告分类', '', $list_type],
  211. ['radio', 'timeset', '时间限制', '', ['永不过期', '在设内时间内有效']],
  212. ['daterange', 'start_time,end_time', '开始时间-结束时间'],
  213. ['textarea', 'content', '广告内容'],
  214. ['radio', 'status', '立即启用', '', ['否', '是']]
  215. ])
  216. ->setTrigger('timeset', '1', 'start_time')
  217. ->setFormData($info)
  218. ->fetch();
  219. }
  220. /**
  221. * 删除广告
  222. * @param array $record 行为日志
  223. * @author 蔡伟明 <314013107@qq.com>
  224. * @throws \think\Exception
  225. * @throws \think\exception\PDOException
  226. */
  227. public function delete($record = [])
  228. {
  229. return $this->setStatus('delete');
  230. }
  231. /**
  232. * 启用广告
  233. * @param array $record 行为日志
  234. * @author 蔡伟明 <314013107@qq.com>
  235. * @throws \think\Exception
  236. * @throws \think\exception\PDOException
  237. */
  238. public function enable($record = [])
  239. {
  240. return $this->setStatus('enable');
  241. }
  242. /**
  243. * 禁用广告
  244. * @param array $record 行为日志
  245. * @author 蔡伟明 <314013107@qq.com>
  246. * @throws \think\Exception
  247. * @throws \think\exception\PDOException
  248. */
  249. public function disable($record = [])
  250. {
  251. return $this->setStatus('disable');
  252. }
  253. /**
  254. * 设置广告状态:删除、禁用、启用
  255. * @param string $type 类型:delete/enable/disable
  256. * @param array $record
  257. * @author 蔡伟明 <314013107@qq.com>
  258. * @throws \think\Exception
  259. * @throws \think\exception\PDOException
  260. */
  261. public function setStatus($type = '', $record = [])
  262. {
  263. $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
  264. $advert_name = AdvertModel::where('id', 'in', $ids)->column('name');
  265. return parent::setStatus($type, ['advert_'.$type, 'cms_advert', 0, UID, implode('、', $advert_name)]);
  266. }
  267. /**
  268. * 快速编辑
  269. * @param array $record 行为日志
  270. * @author 蔡伟明 <314013107@qq.com>
  271. * @return mixed
  272. */
  273. public function quickEdit($record = [])
  274. {
  275. $id = input('post.pk', '');
  276. $field = input('post.name', '');
  277. $value = input('post.value', '');
  278. $advert = AdvertModel::where('id', $id)->value($field);
  279. $details = '字段(' . $field . '),原值(' . $advert . '),新值:(' . $value . ')';
  280. return parent::quickEdit(['advert_edit', 'cms_advert', $id, UID, $details]);
  281. }
  282. }