Skip to content

Commit 42a07ee

Browse files
committed
1、优化访问量统计逻辑,12个小时内同一个浏览器的连续访问都只算一个访问量;2、util新增创建/获取cookie方法
1 parent d03f484 commit 42a07ee

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

src/components/MainPage/BlogIndex.vue

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,30 @@
312312
},
313313
mounted: function () {
314314
let that = this,
315-
dateString = this.getSQTime();
315+
dateString = this.getSQTime(),
316+
cookie = '';
316317
317318
this.InitArticleTag(this);
318319
Store.commit("ChangeActive", 0); // 切换Topbar高亮
319320
// 统计访问量
320-
this.GetLocation(function (LocationCityName,ip) {
321-
that.SQFrontAjax({
322-
Url: '/api/visitCreate/foreend',
323-
UploadData: {
324-
location:LocationCityName,
325-
ip:ip,
326-
time:dateString
327-
},
328-
Success: function () {
329-
console.log('访问统计成功');
330-
}
321+
cookie = this.getSQCookie('sunqBlog');
322+
if(cookie){
323+
console.log('cookie还在',cookie)
324+
}else {
325+
this.GetLocation(function (LocationCityName,ip) {
326+
that.SQFrontAjax({
327+
Url: '/api/visitCreate/foreend',
328+
UploadData: {
329+
location:LocationCityName,
330+
ip:ip,
331+
time:dateString
332+
},
333+
Success: function () {
334+
that.setSQCookie('sunqBlog','统计访问量',12); // 12个小时内同一个浏览器算一个访问量
335+
}
336+
});
331337
});
332-
});
338+
}
333339
}
334340
}
335341
</script>

src/util/util.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,15 @@ CommonFunction.install = function (Vue) {
142142
},
143143
}).then(function (resp) {
144144
func(resp.data.city,data.IpAdress);
145-
console.log('留言城市',resp.data.city);
146-
console.log('留言ip',data.IpAdress);
147145
}).catch();
148146
}
149147
});
150148
};
151149

152-
// 获取IP
153-
Vue.prototype.GetIp = function (f){
154-
this.SQFrontAjax({
155-
Url: '/api/GetUserIp',
156-
Success: function (data) {
157-
f(data.IpAdress);
158-
}
159-
});
160-
};
161-
162-
// 获取当前时间
150+
/**
151+
* 获取当前时间
152+
* @returns {string:2021/11/18 16:45:39}
153+
*/
163154
Vue.prototype.getSQTime = function (){
164155
let dateObject = new Date(),
165156
year = dateObject.getFullYear(),
@@ -176,6 +167,36 @@ CommonFunction.install = function (Vue) {
176167
result = ''+ year +'/'+month+'/'+ day +' '+ hour +':'+ min +':' + second;
177168
return result;
178169
};
170+
171+
/**
172+
* 种cookie
173+
* @param name cookie名称
174+
* @param value cookie值
175+
* @param exHour 过期时间,单位小时
176+
*/
177+
Vue.prototype.setSQCookie = function (name,value,exHour){
178+
var d = new Date();
179+
d.setTime(d.getTime() + exHour*60*60*1000);
180+
var expires = 'expires=' + d.toGMTString(); // cookie的语法要求是这个标志,和这个时间格式
181+
document.cookie = name + '=' + value + '; ' + expires;
182+
console.log('种下cookie',name + '=' + value + '; ' + expires);
183+
};
184+
185+
/**
186+
* 获取cookie
187+
* @param name cookie的名称
188+
*/
189+
Vue.prototype.getSQCookie = function (cookName){
190+
let name = cookName + '=',
191+
cookies = document.cookie.split(';');
192+
for(let i=0;i<cookies.length;i++){
193+
let cleanItem = cookies[i].trim();
194+
if(cleanItem.indexOf(name) == 0){
195+
return cleanItem.substring(name.length,cookies[i].length);
196+
}
197+
}
198+
return '';
199+
};
179200
};
180201

181202
export default CommonFunction;

0 commit comments

Comments
 (0)