Browse Source

停留时间

dev_des
1054425342@qq.com 2 months ago
parent
commit
68677fa7e5
  1. 283
      App.vue

283
App.vue

@ -1,40 +1,232 @@
<script> <script>
export default { export default {
globalData: { globalData: {
mainSliderIndex: 0, mainSliderIndex: 0,
randomImages: [], randomImages: [],
bgMusic: null, bgMusic: null,
isMusicPlaying: false, isMusicPlaying: false,
musicSrc: 'https://static.ticket.sz-trip.com/epicSoul/EpicSouls.mp3' musicSrc: "https://static.ticket.sz-trip.com/epicSoul/EpicSouls.mp3",
// 使
userSessionId: null,
networkStartTime: null, //
networkEndTime: null, //
}, },
onLaunch: function() { onLaunch: function () {
console.warn('当前组件仅支持 uni_modules 目录结构 ,请升级 HBuilderX 到 3.1.0 版本以上!') // 使
console.log('App Launch') this.initUserUsageStats();
// 使
this.retryReportLocalStats();
// //
// this.initBackgroundMusic(); // this.initBackgroundMusic();
// //
this.Post({id: 10217},'/api/article/getArticleById').then(res => { this.Post({ id: 10217 }, "/api/article/getArticleById").then((res) => {
try { try {
let SHFlag = res.data.title let SHFlag = res.data.title;
// let SHFlag = res.data.subtitle // let SHFlag = res.data.subtitle
uni.setStorageSync('SHFlag', SHFlag) uni.setStorageSync("SHFlag", SHFlag);
} catch(e) {} } catch (e) {}
}); });
}, },
onShow: function() { onShow: function () {
console.log('App Show') //
this.recordAppShow();
}, },
onHide: function() { onHide: function () {
console.log('App Hide') // 退
this.recordAppHide();
}, },
methods: { methods: {
// 使
initUserUsageStats() {
// ID
this.globalData.userSessionId = this.generateSessionId();
},
// ID
generateSessionId() {
const timestamp = Date.now();
const random = Math.random().toString(36).substring(2, 15);
return `session_${timestamp}_${random}`;
},
// -
recordAppShow() {
//
this.getNetworkTime()
.then((networkTime) => {
this.globalData.networkStartTime = networkTime;
})
.catch((err) => {
//
});
},
// -
recordAppHide() {
//
this.getNetworkTime()
.then((networkTime) => {
this.globalData.networkEndTime = networkTime;
// 使1
this.reportUserUsageStats();
})
.catch((err) => {
//
});
},
getUserId() {
const userInfoFromStorage = uni.getStorageSync("userInfo");
if (userInfoFromStorage) {
const userInfo = JSON.parse(userInfoFromStorage);
if (userInfo.id) {
return userInfo.id;
}
}
return store.state.user.userInfo.id;
},
// 使
reportUserUsageStats() {
if (
!this.globalData.networkStartTime ||
!this.globalData.networkEndTime
) {
return;
}
let userId = this.getUserId();
const usageData = {
sessionId: this.globalData.userSessionId,
startTime: this.globalData.networkStartTime.toString(),
endTime: this.globalData.networkEndTime.toString(),
userId: userId,
method: "POST",
};
if (!userId) {
this.saveUsageStatsToLocal(usageData);
return;
}
//
this.Post(usageData, "/api/visit/end", "DES")
.then((res) => {
//
this.clearUsageStats();
})
.catch((err) => {
//
this.saveUsageStatsToLocal(usageData);
});
},
//
getPlatform() {
// #ifdef MP-WEIXIN
return "weixin";
// #endif
// #ifdef H5
return "h5";
// #endif
// #ifdef APP-PLUS
return "app";
// #endif
return "unknown";
},
//
getNetworkTime() {
return new Promise((resolve, reject) => {
//
this.Post({}, "/api/visit/currentTime", "DES")
.then((res) => {
if (res.code == 1 || res.code == 200) {
// serverTime
const networkTime = res.data;
resolve(networkTime);
} else {
reject(new Error(res.msg || "获取网络时间失败"));
}
})
.catch((err) => {
reject(err);
});
});
},
//
getDeviceInfo() {
try {
const systemInfo = uni.getSystemInfoSync();
return {
model: systemInfo.model || "",
system: systemInfo.system || "",
platform: systemInfo.platform || "",
version: systemInfo.version || "",
};
} catch (e) {
return {};
}
},
// 使
clearUsageStats() {
this.globalData.networkStartTime = null;
this.globalData.networkEndTime = null;
this.globalData.userSessionId = null;
},
// 使
saveUsageStatsToLocal(usageData) {
try {
const localStats = uni.getStorageSync("pendingUsageStats") || [];
localStats.push(usageData);
uni.setStorageSync("pendingUsageStats", localStats);
} catch (e) {
//
}
},
// 使
retryReportLocalStats() {
try {
const localStats = uni.getStorageSync("pendingUsageStats") || [];
if (localStats.length === 0) {
return;
}
let userId = this.getUserId();
if (!userId) {
return;
}
//
localStats.forEach((stats, index) => {
stats.userId = userId;
this.Post(stats, "/api/visit/end", "DES")
.then((res) => {
//
localStats.splice(index, 1);
uni.setStorageSync("pendingUsageStats", localStats);
})
.catch((err) => {
//
});
});
} catch (e) {
//
}
},
initBackgroundMusic() { initBackgroundMusic() {
try { try {
console.log('bgMusic',this.globalData.bgMusic) console.log("bgMusic", this.globalData.bgMusic);
// //
if (this.globalData.bgMusic) { if (this.globalData.bgMusic) {
this.globalData.bgMusic.stop(); this.globalData.bgMusic.stop();
this.globalData.bgMusic.destroy() this.globalData.bgMusic.destroy();
this.globalData.bgMusic = null; this.globalData.bgMusic = null;
} }
@ -61,7 +253,7 @@
// //
bgMusic.src = this.globalData.musicSrc; bgMusic.src = this.globalData.musicSrc;
console.log(bgMusic.src) console.log(bgMusic.src);
bgMusic.loop = true; // bgMusic.loop = true; //
// 使 // 使
@ -131,10 +323,10 @@
} }
return this.globalData.isMusicPlaying; return this.globalData.isMusicPlaying;
}, },
isPlaying: () => this.globalData.isMusicPlaying isPlaying: () => this.globalData.isMusicPlaying,
}; };
} catch (err) { } catch (err) {
console.error('初始化背景音乐失败:', err); console.error("初始化背景音乐失败:", err);
} }
}, },
updateMusicSrc(newSrc) { updateMusicSrc(newSrc) {
@ -142,40 +334,39 @@
if (this.globalData.bgMusic) { if (this.globalData.bgMusic) {
this.globalData.bgMusic.src = newSrc; this.globalData.bgMusic.src = newSrc;
} }
} },
} },
} };
</script> </script>
<style lang="scss"> <style lang="scss">
@font-face { @font-face {
font-family: 'Futura'; font-family: "Futura";
src: url(https://static.ticket.sz-trip.com/epicSoul/taozi/fonts/Futura.ttc); src: url(https://static.ticket.sz-trip.com/epicSoul/taozi/fonts/Futura.ttc);
} }
/*每个页面公共css */ /*每个页面公共css */
@import '@/uni_modules/uni-scss/index.scss'; @import "@/uni_modules/uni-scss/index.scss";
@import "@/static/css/base.css"; @import "@/static/css/base.css";
/* #ifndef APP-NVUE */ /* #ifndef APP-NVUE */
// //
page { page {
background-color: #f5f5f5; background-color: #f5f5f5;
} }
/* #endif */ /* #endif */
.example-info { .example-info {
font-size: 14px; font-size: 14px;
color: #333; color: #333;
padding: 10px; padding: 10px;
} }
/* 清除按钮默认样式 */ /* 清除按钮默认样式 */
button::after { button::after {
border: none; border: none;
} }
@keyframes bounce {
@keyframes bounce {
0%, 0%,
20%, 20%,
50%, 50%,
@ -191,10 +382,10 @@
60% { 60% {
transform: translateY(-10rpx); transform: translateY(-10rpx);
} }
} }
/* 音乐控制按钮动画 */ /* 音乐控制按钮动画 */
@keyframes rotate { @keyframes rotate {
from { from {
transform: rotate(0deg); transform: rotate(0deg);
} }
@ -202,10 +393,10 @@
to { to {
transform: rotate(360deg); transform: rotate(360deg);
} }
} }
/* 隐藏微信小程序默认音频组件 */ /* 隐藏微信小程序默认音频组件 */
#mp-audio { #mp-audio {
display: none; display: none;
} }
</style> </style>
Loading…
Cancel
Save