-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAds.php
133 lines (117 loc) · 4.27 KB
/
Ads.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
#
# Ads 广告位
# http://www.koalilab.top
#
# (c) ScientistPun
#
class Ads
{
# ~
const version = '1.0.0';
# ~
private $options = ['type' => 0, 'content' => '', 'width' => 100, 'height' => 100];
private $str = '';
private $error = '';
public function __construct($options = [])
{
$this->options = array_merge($this->options, is_array($options) ? $options : []);
}
/**
* 展示
*/
public function show()
{
if (!$this->parse()) return $this->error;
return $this->str;
}
/**
* 格式化输出
*/
protected function parse()
{
$content = $this->options['content'];
$w = $this->options['width'];
$h = $this->options['height'];
$updatetime = $this->options['updatetime'];
$str = '';
switch ($this->options['type']) {
case 0:
$content = trim($content);
$contentInfo = explode("\r\n", $content);
if (count($contentInfo) < 2) {
$this->error = '内容错误';
return false;
}
$str = "<div><a target='_blank' href='{$contentInfo[0]}' style='display:block;'>";
// 如果有注释内容
if (count($contentInfo) == 2) {
$contentInfo[2] = $contentInfo[0];
}
// 如果有宽高
$str .= "<img src='{$contentInfo[1]}?t={$updatetime}' alt='{$contentInfo[2]}' style='display: block; ";
$str .= $w ? "width: {$w}px;" : "width: 100%;";
$str .= $h ? "height: {$h}px;" : "height: 100%;";
$str .= "'/></a></div>";
break;
case 1:
$str = "<div style='";
$str .= $w ? "width: {$w}px;" : "";
$str .= $h ? "height: {$h}px;" : "";
$str .= "'>";
$str .= $content;
$str .= "</div>";
break;
case 2:
$content = json_decode($content, true);
if (!is_array($content)) {
$this->error = '广告位内容错误';
return false;
}
$options = Helper::options();
$pluginUrl = $options->pluginUrl . '/AdsPlugin';
$idname = 'carousel-' . mt_rand(1000, 9999);
$str .= "<div class='layui-carousel' id='{$idname}' style='";
$str .= $w ? "width: {$w}px;" : "width: 100%;";
$str .= $h ? "height: {$h}px;" : "height: 100%;";
$str .= "'><div carousel-item=''>";
foreach ($content as $con) {
if (!isset($con['alt'])) $con['alt'] = $con['href'];
$str .= "<div><a target='_blank' href='{$con['href']}'><img src='{$con['src']}?t={$updatetime}' alt='{$con['alt']}' style='";
$str .= $w ? "width: {$w}px;" : "width: 100%;";
$str .= $h ? "height: {$h}px;" : "height: 100%;";
$str .= "'></a></div>";
}
$str .= " </div> </div>";
$str .= "<script src='{$pluginUrl}/style/layui.all.js'></script>";
$width = $w ? "{$w}px;" : "100%;";
$height = $h ? "{$h}px;" : "100%;";
$str .= "<script>layui.use(['carousel'], function(){
var carousel = layui.carousel;
//图片轮播
carousel.render({
elem: '#{$idname}'
,width: '{$width}'
,height: '{$height}'
,interval: 8000
,indicator: 'none'
,arrow: 'hover' //始终显示箭头
});
//事件
carousel.on('change(id)', function(res){
console.log(res)
});
});</script>";
break;
default:
$this->error = '错误展示方式';
return false;
}
$this->str = $str;
return true;
}
private function getError()
{
return $this->error;
}
}