AdvertUtils.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\api\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,$sex=null) {
  26. $adver = (new AdvertModel())
  27. ->where('page_id',$page_id)
  28. ->where('is_show',1)
  29. ->order("sort","desc")
  30. ->when(!empty($pageCount),function ($query) use($pageCount){
  31. $query->limit(0,$pageCount);
  32. })
  33. ->when(!empty($sex),function ($query) use($sex){
  34. if($sex == 1) {
  35. $query->whereIn("sex","0,1");
  36. }
  37. if($sex == 2) {
  38. $query->whereIn("sex","0,2");
  39. }
  40. })
  41. ->select()
  42. ->toArray();
  43. $lData = [];
  44. foreach ($adver as $v) {
  45. $d['title'] = $v['title'];
  46. $d['text'] = $v['text'];
  47. $d['url'] = $v['url'];
  48. $d['img'] = $v['img'];
  49. $d['code'] = $v['code'];
  50. $tAr = [];
  51. $jsonData = empty($v['data']) ? [] : json_decode($v['data'],true);
  52. foreach($jsonData as $v2) {
  53. $tAr[$v2['code']] = [
  54. 'name' => $v2['name'],
  55. 'type' => $v2['type'],
  56. 'value' => $v2[$v2['code']]
  57. ];
  58. }
  59. // $d['data'] = $tAr;
  60. $lData[] = $d;
  61. }
  62. return $lData;
  63. }
  64. }