AdClass.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace bizMall\ad\classes;
  3. use common\components\imgUtil;
  4. use common\components\util;
  5. use Yii;
  6. use \biz\base\classes\BaseClass;
  7. class AdClass extends BaseClass
  8. {
  9. public static $baseFile = '\bizMall\ad\models\Ad';
  10. //取出有效的广告 ssh 2019.12.13
  11. public static function getValidAdList($sjId)
  12. {
  13. $time = time();
  14. $list = self::getAllByCondition([
  15. 'sjId' => $sjId,
  16. 'delStatus' => 0,
  17. 'type' => 1,
  18. 'status' => 1,
  19. 'startTime<' => $time,
  20. 'endTime>' => $time
  21. ], 'inTurn DESC', '*');
  22. return !empty($list) ? self::groupBaseInfo($list) : [];
  23. }
  24. //广告列表 ssh 2019.12.
  25. public static function getAdList($where)
  26. {
  27. $data = self::getList('*', $where, 'inTurn DESC,updateTime DESC');
  28. $list = $data['list'];
  29. if (!empty($list)) {
  30. $imgUrl = imgUtil::getPrefix();
  31. foreach ($list as $key => $val) {
  32. //图片提供完整地址给前端,同时原有addImg字段不占用,定义新字段adImgUrl
  33. $list[$key]['adImgUrl'] = $imgUrl . $val['adImg'];
  34. $style = $val['style'];
  35. $url = $val['url'];
  36. if ($style == 1) {
  37. //商品地址后面再补写
  38. $url = '';
  39. }
  40. $list[$key]['url'] = $url;
  41. }
  42. }
  43. $data['list'] = $list;
  44. return $data;
  45. }
  46. //添加广告
  47. public static function addAd($data)
  48. {
  49. //永久有效
  50. $time = time();
  51. $neverExpireTime = Yii::$app->params['neverExpireTime'];
  52. if (isset($data['endTime']) && empty($data['endTime'])) {
  53. $data['startTime'] = $time;
  54. $data['endTime'] = $neverExpireTime;
  55. } else {
  56. $data['startTime'] = strtotime($data['startTime']);
  57. $data['endTime'] = strtotime($data['endTime']);
  58. }
  59. return self::add($data);
  60. }
  61. //组装通用的信息
  62. public static function groupBaseInfo($list)
  63. {
  64. $imgUrl = imgUtil::getPrefix();
  65. foreach ($list as $key => $val) {
  66. $style = $val['style'];
  67. $url = $val['url'];
  68. if ($style == 1) {
  69. $url = '';
  70. }
  71. $list[$key]['url'] = $url;
  72. $list[$key]['adImgUrl'] = $imgUrl . $val['adImg'];
  73. }
  74. return $list;
  75. }
  76. //单个广告的信息
  77. public static function getDetail($id)
  78. {
  79. $info = self::getById($id);
  80. if (empty($info)) {
  81. util::fail('找不到广告');
  82. }
  83. $list = self::groupBaseInfo([$info]);
  84. return current($list);
  85. }
  86. //点击数+1
  87. public static function clickAd($id)
  88. {
  89. return self::counters(['viewTimes' => 1], ['id' => $id]);
  90. }
  91. //更新广告
  92. public static function updateAd($id, $data)
  93. {
  94. $time = time();
  95. //广告类型不允许修改
  96. unset($data['type']);
  97. $neverExpireTime = Yii::$app->params['neverExpireTime'];
  98. if (isset($data['endTime']) && empty($data['endTime'])) {
  99. $data['startTime'] = $time;
  100. $data['endTime'] = $neverExpireTime;
  101. } else {
  102. $data['startTime'] = strtotime($data['startTime']);
  103. $data['endTime'] = strtotime($data['endTime']);
  104. }
  105. return self::updateById($id, $data);
  106. }
  107. //开店广告初始化 ssh 2020.2.14
  108. public static function initAdd($sjId)
  109. {
  110. $time = time();
  111. $list = [
  112. ['adName' => '培训图', 'adImg' => '/example/mall/1.jpg', 'type' => 0, 'startTime' => 0, 'endTime' => 4102416000, 'addTime' => $time, 'sjId' => $sjId, 'status' => 1],
  113. ['adName' => '花礼', 'adImg' => '/example/mall/2.jpg', 'type' => 0, 'startTime' => 0, 'endTime' => 4102416000, 'addTime' => $time, 'sjId' => $sjId, 'status' => 1],
  114. ['adName' => '花艺', 'adImg' => '/example/mall/3.jpg', 'type' => 0, 'startTime' => 0, 'endTime' => 4102416000, 'addTime' => $time, 'sjId' => $sjId, 'status' => 1],
  115. ['adName' => '门店图片', 'adImg' => '/example/shop/1.jpg', 'type' => 1, 'startTime' => 0, 'endTime' => 4102416000, 'addTime' => $time, 'sjId' => $sjId, 'status' => 1],
  116. ];
  117. foreach ($list as $val) {
  118. self::add($val);
  119. }
  120. }
  121. //删除广告 ssh 2020.5.21
  122. public static function deleteAd($id)
  123. {
  124. self::updateById($id, ['delStatus' => 1]);
  125. }
  126. }