AdvertUtils.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare (strict_types = 1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ 广告位 ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-29 20:41
  12. // +----------------------------------------------------------------------
  13. use \app\model\system\Advert as AdvertModel;
  14. class AdvertUtils
  15. {
  16. private $sassid;
  17. /**
  18. * Advert constructor.
  19. * @param int $sassid 子站sassid 无默认为0
  20. */
  21. public function __construct($sassid = 0)
  22. {
  23. $this->sassid = $sassid;
  24. }
  25. public function getData($page_id,$pageCount = 0) {
  26. $adver = (new AdvertModel())
  27. ->where('page_id',$page_id)
  28. ->where('is_show',1)
  29. ->where('sassid', $this->sassid)
  30. ->order("sort","desc")
  31. ->when(!empty($pageCount),function ($query) use($pageCount){
  32. $query->limit(0,$pageCount);
  33. })
  34. ->select()
  35. ->toArray();
  36. if(empty($adver)) {
  37. $adver = (new AdvertModel())
  38. ->where('page_id',$page_id)
  39. ->where('is_show',1)
  40. ->where('sassid', 0)
  41. ->order("sort","desc")
  42. ->when(!empty($pageCount),function ($query) use($pageCount){
  43. $query->limit(0,$pageCount);
  44. })
  45. ->select()
  46. ->toArray();
  47. }
  48. $lData = [];
  49. foreach ($adver as $v) {
  50. $d['title'] = $v['title'];
  51. $d['text'] = $v['text'];
  52. $d['url'] = $v['url'];
  53. $d['img'] = $v['img'];
  54. $tAr = [];
  55. $jsonData = empty($v['data']) ? [] : json_decode($v['data'],true);
  56. foreach($jsonData as $v2) {
  57. $tAr[$v2['code']] = [
  58. 'name' => $v2['name'],
  59. 'type' => $v2['type'],
  60. 'value' => $v2[$v2['code']]
  61. ];
  62. }
  63. $d['data'] = $tAr;
  64. $lData[] = $d;
  65. }
  66. return $lData;
  67. }
  68. }