Ad.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\model;
  4. /**
  5. * 广告管理
  6. * Class Ad
  7. * @package app\common\model
  8. */
  9. class Ad extends BaseModel
  10. {
  11. /*
  12. 前端说广告位置他们控制并且跟小程序端同步,后台不用改
  13. <a-select-option value="1">首页上部轮播图</a-select-option>
  14. <a-select-option value="2">首页中间小banner广告1</a-select-option>
  15. <a-select-option value="3">首页中间button广告</a-select-option>
  16. <a-select-option value="4">首页中间小banner广告2</a-select-option>
  17. */
  18. // 广告位置
  19. const AD_TYPE = [1 => '首页上部轮播图', 2 => '首页中间小banner广告1', 3 => '首页中间button广告',4=>'首页中间小banner广告2',
  20. 5=>'砍价活动banner',6=>'”惠“存-礼品卡上方配图',7=>'”惠“存-电子E卡上方配图',8=>'首页弹窗广告'];
  21. // 链接分类id
  22. // const JUMP_TYPE = [1 => '商品详情页', 2 => '品牌文化页', 3 => '购买米卡', 4 => '首页的全部商品',
  23. // 5 => '我的优惠券(可使用)', 6 => '领券中心', 7 => '裂变券礼包'];
  24. const JUMP_TYPE = [1=>'商品详情页',2=>'探秘',3=>'体验',4=>'购买米卡(惠存)',5=>'领券中心(惠买)',6=>'我的优惠券(可使用)',7=>'裂变券礼包'];
  25. // <a-select-option value="1">商品详情页</a-select-option>
  26. // <a-select-option value="2">探秘</a-select-option>
  27. // <a-select-option value="3">体验</a-select-option>
  28. // <a-select-option value="4">购买米卡(惠存)</a-select-option>
  29. // <a-select-option value="5">领券中心(惠买)</a-select-option>
  30. // <a-select-option value="6">我的优惠券(可使用)</a-select-option>
  31. // <a-select-option value="7">裂变券礼包</a-select-option>
  32. protected $name = 'ad';
  33. protected $append = ['ad_type_text'];
  34. /**
  35. * 关联广告封面图
  36. * @return \think\model\relation\HasOne
  37. */
  38. public function image()
  39. {
  40. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  41. }
  42. public function getAdTypeTextAttr($value, $data)
  43. {
  44. return self::AD_TYPE[$data['ad_type']] ?? '';
  45. }
  46. public function getJumpTypeTextAttr($value, $data)
  47. {
  48. return self::JUMP_TYPE[$data['jump_type']] ?? '';
  49. }
  50. /**
  51. * 获取列表
  52. * @param array $param
  53. * @return \think\Paginator
  54. * @throws \think\db\exception\DbException
  55. */
  56. public function getList($param = [])
  57. {
  58. // 检索查询调价你
  59. $filter = $this->getFilter($param);
  60. // 排序条件
  61. $sort = $this->setQuerySort($param);
  62. // 查询列表数据
  63. return $this->where($filter)->with(['image'])->order($sort)->paginate(15);
  64. }
  65. public function setQuerySort($param = [])
  66. {
  67. $params = $this->setQueryDefaultValue($param, [
  68. 'sortField' => 'create_time', // 排序字段 create_time-创建时间 start_time-开始时间 end_time-结束时间 sort-排序 click_user_nums-浏览人数 click_nums-点击次数
  69. 'sortOrder' => 'descend' // 排序方式 descend-倒序 ascend-顺序
  70. ]);
  71. $sort = [];
  72. if (in_array($params['sortOrder'], ['descend','ascend']) && in_array($params['sortField'], ['create_time','start_time','end_time','sort','click_user_nums','click_nums'])) {
  73. $sort = [$params['sortField'] => str_replace(['descend','ascend'],['desc','asc'], $params['sortOrder'])];
  74. }
  75. return array_merge($sort, [$this->getPk() => 'desc']);
  76. }
  77. /**
  78. * 检索查询条件
  79. * @param array $param
  80. * @return array
  81. */
  82. private function getFilter($param = [])
  83. {
  84. // 默认查询条件
  85. $params = $this->setQueryDefaultValue($param, ['title' => '','ad_type' => 0]);
  86. // 检索查询条件
  87. $filter = [];
  88. !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
  89. !empty($params['ad_type']) && $filter[] = ['ad_type', '=', $params['ad_type']];
  90. return $filter;
  91. }
  92. /**
  93. * 详情
  94. * @param int $id
  95. * @return null|static
  96. */
  97. public static function detail(int $id)
  98. {
  99. return self::get($id,['image']);
  100. }
  101. /**
  102. * 添加
  103. * @param $data
  104. * @return false|int
  105. */
  106. public function add($data)
  107. {
  108. isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  109. isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  110. return $this->save($data);
  111. }
  112. /**
  113. * 编辑
  114. * @param $data
  115. * @return bool|int
  116. */
  117. public function edit($data)
  118. {
  119. isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  120. isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  121. // 是否删除图片
  122. !isset($data['image_id']) && $data['image_id'] = 0;
  123. return $this->save($data) !== false;
  124. }
  125. /**
  126. * 删除
  127. * @param $id
  128. * @return bool|int
  129. */
  130. public function remove()
  131. {
  132. return $this->delete();
  133. }
  134. }