|
|
@ -1,4 +1,6 @@ |
|
|
|
|
|
|
|
<script> |
|
|
|
import store from './store' |
|
|
|
export default { |
|
|
|
globalData: { |
|
|
|
mainSliderIndex: 0, |
|
|
@ -11,38 +13,46 @@ export default { |
|
|
|
networkStartTime: null, // 网络时间开始时间 |
|
|
|
networkEndTime: null, // 网络时间结束时间 |
|
|
|
}, |
|
|
|
onLaunch: function () { |
|
|
|
// 初始化用户使用统计 |
|
|
|
this.initUserUsageStats(); |
|
|
|
|
|
|
|
// 重试上报本地存储的使用统计数据 |
|
|
|
this.retryReportLocalStats(); |
|
|
|
|
|
|
|
// 移除初始化背景音乐的调用 |
|
|
|
// this.initBackgroundMusic(); |
|
|
|
// 审核 |
|
|
|
this.Post({ id: 10217 }, "/api/article/getArticleById").then((res) => { |
|
|
|
try { |
|
|
|
let SHFlag = res.data.title; |
|
|
|
// let SHFlag = res.data.subtitle |
|
|
|
uni.setStorageSync("SHFlag", SHFlag); |
|
|
|
} catch (e) {} |
|
|
|
}); |
|
|
|
}, |
|
|
|
onShow: function () { |
|
|
|
// 记录应用显示时间(重新进入小程序) |
|
|
|
this.recordAppShow(); |
|
|
|
}, |
|
|
|
onHide: function () { |
|
|
|
// 记录应用隐藏时间(退出小程序) |
|
|
|
this.recordAppHide(); |
|
|
|
}, |
|
|
|
onLaunch: function () { |
|
|
|
// 初始化用户使用统计 |
|
|
|
this.initUserUsageStats(); |
|
|
|
|
|
|
|
// 重试上报本地存储的使用统计数据 |
|
|
|
this.retryReportLocalStats(); |
|
|
|
|
|
|
|
// 移除初始化背景音乐的调用 |
|
|
|
// this.initBackgroundMusic(); |
|
|
|
// 审核 |
|
|
|
this.Post({ id: 10217 }, "/api/article/getArticleById").then((res) => { |
|
|
|
try { |
|
|
|
let SHFlag = res.data.title; |
|
|
|
// let SHFlag = res.data.subtitle |
|
|
|
uni.setStorageSync("SHFlag", SHFlag); |
|
|
|
} catch (e) {} |
|
|
|
}); |
|
|
|
}, |
|
|
|
onShow: function () { |
|
|
|
// 记录应用显示时间(重新进入小程序) |
|
|
|
this.recordAppShow(); |
|
|
|
this.getUserInfo() |
|
|
|
}, |
|
|
|
onHide: function () { |
|
|
|
// 记录应用隐藏时间(退出小程序) |
|
|
|
this.recordAppHide(); |
|
|
|
}, |
|
|
|
methods: { |
|
|
|
// 初始化用户使用统计 |
|
|
|
initUserUsageStats() { |
|
|
|
// 生成会话ID |
|
|
|
this.globalData.userSessionId = this.generateSessionId(); |
|
|
|
}, |
|
|
|
getUserInfo(){ |
|
|
|
if(!this.getUserId())return |
|
|
|
this.Post({}, "/framework/user/getInfo", "DES") |
|
|
|
.then((res) => { |
|
|
|
uni.setStorageSync('userInfo', JSON.stringify(res.data)) |
|
|
|
}) |
|
|
|
}, |
|
|
|
// 初始化用户使用统计 |
|
|
|
initUserUsageStats() { |
|
|
|
// 生成会话ID |
|
|
|
this.globalData.userSessionId = this.generateSessionId(); |
|
|
|
}, |
|
|
|
|
|
|
|
// 生成会话ID |
|
|
|
generateSessionId() { |
|
|
@ -51,31 +61,31 @@ export default { |
|
|
|
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) => { |
|
|
|
// 获取网络时间失败,静默处理 |
|
|
|
}); |
|
|
|
}, |
|
|
|
// 记录应用显示时间 - 获取网络时间作为开始时间 |
|
|
|
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) { |
|
|
@ -87,38 +97,38 @@ export default { |
|
|
|
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); |
|
|
|
}); |
|
|
|
}, |
|
|
|
// 上报用户使用统计数据 |
|
|
|
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() { |
|
|
@ -137,40 +147,40 @@ export default { |
|
|
|
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 {}; |
|
|
|
} |
|
|
|
}, |
|
|
|
// 获取网络时间 |
|
|
|
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() { |
|
|
@ -179,46 +189,46 @@ export default { |
|
|
|
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) { |
|
|
|
// 重试失败,静默处理 |
|
|
|
} |
|
|
|
}, |
|
|
|
// 保存使用统计数据到本地 |
|
|
|
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() { |
|
|
|
try { |
|
|
|