template.html
5.15 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="format-detection" content="telephone=no" />
<meta name="format-detection" content="email=no" />
<meta name="format-detection" content="address=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="screen-orientation" content="portrait" />
<meta name="x5-orientation" content="portrait" />
<meta name="x5-page-mode" content="app" />
<meta name="x5-fullscreen" content="true" />
<meta name="full-screen" content="yes" />
<meta http-equiv="x-dns-prefetch-control" content="on" />
<style type="text/css">
* {
padding: 0;
font-weight: normal;
}
.content {
margin: 0 15px;
}
img {
width: 100%;
}
#title {
font-weight: bold;
font-size: 1.1rem;
text-align: center;
margin-top: 10px;
}
.common {
padding-top: 10px;
color: #999;
}
.bottom {
text-align: center;
margin-bottom: 1rem;
}
.btn {
display: inline-block;
border: 1px solid #999;
border-radius: 0.5rem;
text-align: center;
}
p {
margin-top: 15px;
}
span {
display: inline-block;
text-align: justify;
line-height: 1.5rem !important;
font-family: "黑体" !important;
font-size: 1rem !important;
}
.btn img {
width: 1rem;
padding: 0.1rem 1.25rem;
padding-top: 0.2rem;
position: relative;
}
#inner-html img {
margin: 15px 0;
}
</style>
</head>
<body>
<div class="content">
<div>
<div id="title"></div>
<div class="common">
<span id="person-num"></span>
<span id="date"></span>
</div>
</div>
<div id="inner-html"></div>
<div class="bottom">
<div onclick="switchClickFn()" class="btn">
<img id="disable" src="https://oss.tanleizhen.com/static/newnotgood.png" />
<img id="enable" src="https://oss.tanleizhen.com/static/newgood.png" style="display: none;" />
</div>
</div>
</div>
</body>
<script>
var clickStatus = false;
var pNum = 0;
// 原生ajax实现
function ajax(option) {
var xhr = new XMLHttpRequest();
if (option.method === 'GET') {
option.url += '?';
var strUrl = '';
for (var key in option.data) {
strUrl += key + '=' + option.data[key] + '&';
}
option.url += strUrl.substring(0, strUrl.length - 1);
xhr.open(option.method, option.url);
xhr.send()
}
if (option.method === 'POST') {
// xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.open(option.method, option.url);
xhr.send(option.data);
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
option.success(JSON.parse(xhr.responseText));
}
}
}
// 请求接口数据
ajax({
url: '../web/news/detail',
method: 'GET',
data: {
newsId: window.location.search.split('?')[1].split('&')[0],
clientType: '1',
version: '1.2.4'
},
success: function(resp) {
clickStatus = resp.data.hasClicked == '1' ? true : false;
var defaultImg = document.getElementById(clickStatus ? 'enable' : 'disable');
defaultImg.style.display = "inline-block";
// 设置title
var title = document.getElementById("title");
title.innerHTML = resp.data.newsTitle;
// 设置点赞人数
var personNum = document.getElementById("person-num");
pNum = parseInt(resp.data.randomNum);
personNum.innerHTML = resp.data.randomNum + '人赞过 ';
// 设置时间
var date = document.getElementById("date");
date.innerHTML = resp.data.releaseTime;
// 展示富文本字符串
var innerContent = document.getElementById("inner-html");
innerContent.innerHTML = resp.data.content;
}
})
// switch点击事件
function switchClickFn() {
ajax({
url: '../web/likeList/' + (clickStatus ? 'add' : 'delete'),
method: 'GET',
data: {
newsId: window.location.search.split('?')[1].split('&')[0],
token: window.location.search.split('?')[1].split('&')[1],
clientType: '1',
version: '1.2.4'
},
success: function(resp) {
var disable = document.getElementById('disable');
var enable = document.getElementById('enable');
var personNum = document.getElementById("person-num");
if (!clickStatus) {
clickStatus = true;
disable.style.display = "none";
enable.style.display = "inline-block";
pNum += 1;
personNum.innerHTML = pNum + '人赞过 ';
} else {
clickStatus = false;
enable.style.display = "none";
disable.style.display = "inline-block";
pNum -= 1;
personNum.innerHTML = pNum + '人赞过 ';
}
}
})
}
</script>
</html>