AdRepository.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Ad;
  4. use App\Models\AdCategory;
  5. use Prettus\Repository\Eloquent\BaseRepository;
  6. /**
  7. * Class AdRepositoryEloquent.
  8. *
  9. * @package namespace App\Repositories;
  10. */
  11. class AdRepository extends BaseRepository
  12. {
  13. protected $ads;
  14. protected $adCategory;
  15. /**
  16. * AdRepository constructor.
  17. * @param $ads
  18. */
  19. public function __construct(Ad $ads, AdCategory $adCategory)
  20. {
  21. $this->ads = $ads;
  22. $this->adCategory = $adCategory;
  23. }
  24. public function model()
  25. {
  26. return Ad::class;
  27. }
  28. public function getFilterAds($where, $whereIn = array())
  29. {
  30. $today_time = strtotime(date('Y-m-d', strtotime("+1 day")))-1;
  31. /*
  32. if ($whereIn) {
  33. $res = $this->ads->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height','c.alias')
  34. ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
  35. ->where($where)
  36. ->where(function ($query) {
  37. $today = strtotime(date('Y-m-d', time()));
  38. $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
  39. })
  40. ->where('started_at', '<=', $today_time)
  41. ->whereIn('c.alias', $whereIn)
  42. ->orderBy('list_order', 'asc')
  43. ->orderBy('id', 'desc')
  44. ->get();
  45. } else {
  46. $res = $this->ads->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height','c.alias')
  47. ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
  48. ->where($where)
  49. ->where(function ($query) {
  50. $today = strtotime(date('Y-m-d', time()));
  51. $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
  52. })
  53. ->where('started_at', '<=', $today_time)
  54. ->orderBy('list_order', 'asc')
  55. ->orderBy('id', 'desc')
  56. ->get();
  57. }
  58. */
  59. $res = $this->ads
  60. ->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height','c.alias','ads.target')
  61. ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
  62. ->where($where)
  63. ->where(function ($query) {
  64. $today = strtotime(date('Y-m-d', time()));
  65. $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
  66. })
  67. ->where('started_at', '<=', $today_time);
  68. if ($whereIn) {
  69. $res->whereIn('c.alias', $whereIn);
  70. }
  71. /*$res->when(get_subsite_id()>0, function ($query) {
  72. $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
  73. });*/
  74. $res->whereHas('subsites', function ($query) {
  75. $query->where('subsite_id', get_subsite_id());
  76. });
  77. $rst = $res->orderBy('list_order', 'desc')->orderBy('ads.created_at', 'desc')->get();
  78. $res_new = $this->get_html($rst);
  79. return $res_new;
  80. }
  81. public function getAds($where, $limit)
  82. {
  83. $today_time = strtotime(date('Y-m-d', strtotime("+1 day")))-1;
  84. $res = $this->ads->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height')
  85. ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
  86. ->where($where)
  87. ->where(function ($query) {
  88. $today = strtotime(date('Y-m-d', time()));
  89. $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
  90. })
  91. ->where('started_at', '<=', $today_time)
  92. ->when(get_subsite_id()>0, function ($query) {
  93. $query->whereHas('subsites', function ($query) {
  94. $query->where('subsite_id', get_subsite_id());
  95. });
  96. })
  97. ->orderBy('list_order', 'asc')
  98. ->orderBy('ads.created_at', 'desc')
  99. ->limit($limit)
  100. ->get();
  101. $res_new = $this->get_html($res);
  102. return $res_new;
  103. }
  104. /*
  105. * 处理广告数据
  106. * _get_html方法用于根据不同的广告类型生成对应的模板文件代码
  107. * 广告类型(1:文字,2:图片,3:代码,4:flash,5:视频)
  108. */
  109. private function get_html($ads)
  110. {
  111. if ($ads->ToArray()) {
  112. foreach ($ads as $k => $v) {
  113. if ($v->alias == 'AIX_index_five_recommend' && $v->uid ==0) {
  114. unset($ads[$k]);
  115. continue;
  116. }
  117. $html = '';
  118. $size_html = '';
  119. $href = '';
  120. $v->width && $size_html .= 'width="'.$v->width.'"';
  121. $v->height && $size_html .= ' height="'.$v->height.'"';
  122. if ($v->url) {
  123. $href = $v->url;
  124. } elseif ($v->uid) {
  125. //企业信息查看页面
  126. $href = route(url_rewrite('AIX_companyshow'), array('id'=>$v->uid));
  127. }
  128. !$href && $href = 'javascript:;';
  129. switch ($v->type_id) {
  130. case 1:
  131. $v->text_color && $style="color:{{$v->text_color}};";
  132. $v->width && $style.="width:{{$v->width}};";
  133. $v->height && $style.="height:{{$v->height}}";
  134. $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank" style="'.$style.'">'.$v->content.'</a>';
  135. break;
  136. case 2://生成广告为图片类型的代码
  137. $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">';
  138. $html .= '<img alt="'.$v->title.'" src="'.upload_asset($v->content).'" '.$size_html.'>';
  139. $html .= '</a>';
  140. break;
  141. case 3:
  142. $html = $v->content;
  143. break;
  144. case 4://生成广告为flash类型的代码
  145. $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">';
  146. $html .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '.$size_html.' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">';
  147. $html .= '<param name="movie" value="{{upload_asset($v->content)}}" />';
  148. $html .= '<param name="quality" value="autohigh" />';
  149. $html .= '<param name="wmode" value="opaque" />';
  150. $html .= '<embed src="{{upload_asset($v->content)}}" quality="autohigh" wmode="opaque" name="flashad" swliveconnect="TRUE" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" '.$size_html.'></embed>';
  151. $html .= '</object>';
  152. $html .= '</a>';
  153. break;
  154. case 5://生成视频广告
  155. if (preg_match("/^.+\.(flv|f4v)$/i", $v->content)) {
  156. $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">';
  157. $html .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '.$size_html.'>';
  158. $html .= '<param name="movie" value="__ROOT__/static/flvplayer.swf"/>';
  159. $html .= '<param name="quality" value="high"/>';
  160. $html .= '<param name="allowFullScreen" value="true" />';
  161. $html .= '<param name="FlashVars" value="vcastr_file='.upload_asset($v->content).'&IsAutoPlay=1&IsContinue=1" />';
  162. $html .= '<embed src="__ROOT__/static/flvplayer.swf" autostart=true loop=true allowFullScreen="true" FlashVars="vcastr_file='.upload_asset($v->content).'&IsAutoPlay=1&IsContinue=1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" '.$size_html.'></embed>';
  163. $html .= '</object>';
  164. $html .= '</a>';
  165. } else {
  166. //通用代码
  167. $v->content = preg_replace('/((width)[=]?[:]?[\"]?[0-9]+[\"]?)/i', 'width="'.$v->width.'"', $v->content);//宽
  168. $v->content = preg_replace('/((height)[=]?[:]?[\'"]?[0-9]+[\'"]?)/i', 'height="'.$v->height.'"', $v->content);//高
  169. $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">'.$v->content.'</a>';
  170. }
  171. break;
  172. }
  173. $v->html = $html;
  174. $v->href = $href;
  175. }
  176. }
  177. return $ads;
  178. }
  179. }