feat: 新增多个页面及功能,优化用户体验

在项目中新增了多个页面,包括押金页面、设备详情页面、反馈页面和帮助页面。同时,更新了订单支付和归还成功页面的逻辑,确保用户在支付和归还设备时能够获得清晰的反馈。优化了扫码和订单状态处理逻辑,提升了整体用户体验。
This commit is contained in:
8vd8
2025-04-21 14:54:00 +08:00
parent 41b409c327
commit 8431cdf2d6
12 changed files with 337 additions and 111 deletions
Binary file not shown.
+76 -4
View File
@@ -89,12 +89,16 @@ export default {
statusCheckTimer: null,
maxStatusChecks: 30, // 最多检查30次
currentStatusChecks: 0,
statusCheckInterval: 5000 // 5秒检查一次
statusCheckInterval: 5000, // 5秒检查一次
isPageActive: false // 跟踪页面是否活跃
}
},
onLoad(options) {
console.log('Return page loaded with options:', JSON.stringify(options))
// 标记页面为活跃状态
this.isPageActive = true
// 获取传递的参数
this.orderInfo.orderId = options.orderId || ''
this.deviceId = options.deviceNo || options.deviceId || ''
@@ -118,7 +122,10 @@ export default {
// 如果订单ID有效,将订单添加到全局监控服务
try {
if (this.$orderMonitor) {
this.$orderMonitor.addOrder({orderId: this.orderInfo.orderId})
// 先确保移除之前的监控(防止重复添加)
this.$orderMonitor.removeOrder({orderId: this.orderInfo.orderId})
// 添加到监控队列,明确指定为归还页面
this.$orderMonitor.addOrder({orderId: this.orderInfo.orderId}, 'return')
console.log('订单已添加到监控队列:', this.orderInfo.orderId)
} else {
console.warn('$orderMonitor 未定义,无法添加订单到监控队列')
@@ -142,14 +149,46 @@ export default {
// 注册全局订单完成事件监听器
uni.$on('orderCompleted', this.handleOrderCompleted)
},
// 添加onHide生命周期,处理页面隐藏时的清理工作
onHide() {
console.log('归还页面隐藏,清理计时器资源和监控服务')
// 标记页面为非活跃状态
this.isPageActive = false
// 清理所有计时器
this.clearTimer()
this.clearStatusCheckTimer()
// 从全局订单监控服务中移除当前订单
this.removeFromOrderMonitor()
},
onUnload() {
console.log('归还页面卸载,清理所有资源')
// 标记页面为非活跃状态
this.isPageActive = false
// 页面卸载时清除定时器
this.clearTimer()
this.clearStatusCheckTimer()
// 从全局订单监控服务中移除当前订单
this.removeFromOrderMonitor()
// 注销全局事件监听
uni.$off('orderCompleted', this.handleOrderCompleted)
},
methods: {
// 从订单监控服务中移除当前订单
removeFromOrderMonitor() {
if (this.orderInfo.orderId && this.$orderMonitor) {
try {
this.$orderMonitor.removeOrder({orderId: this.orderInfo.orderId})
console.log('订单已从监控队列移除:', this.orderInfo.orderId)
} catch (error) {
console.error('从监控队列移除订单失败:', error)
}
}
},
// 处理订单完成事件(可由任何地方触发)
handleOrderCompleted(orderData) {
console.log('收到订单完成事件:', orderData)
@@ -206,8 +245,14 @@ export default {
// 获取订单详情
async getOrderDetails() {
// 如果页面不再活跃,不执行API请求
if (!this.isPageActive) {
console.log('页面已不活跃,跳过订单详情请求')
return
}
try {
uni.showLoading({ title: '加载中' })
// uni.showLoading({ title: '加载中' })
if (!this.orderInfo.orderId) {
throw new Error('订单ID不能为空')
@@ -320,10 +365,22 @@ export default {
// 更新使用时长的定时器
startTimer() {
// 清除现有计时器,确保不重复
this.clearTimer()
// 每分钟更新一次使用时长
this.timer = setInterval(() => {
// 只有当页面活跃时才执行更新
if (this.isPageActive) {
console.log('执行定时更新订单信息')
this.getOrderDetails()
} else {
console.log('页面已不活跃,停止计时器')
this.clearTimer()
}
}, 60000)
console.log('已启动使用时长更新计时器')
},
// 清除定时器
@@ -331,6 +388,7 @@ export default {
if (this.timer) {
clearInterval(this.timer)
this.timer = null
console.log('已清除使用时长更新计时器')
}
},
@@ -339,16 +397,21 @@ export default {
if (this.statusCheckTimer) {
clearInterval(this.statusCheckTimer)
this.statusCheckTimer = null
console.log('已清除归还状态检查计时器')
}
},
// 开始状态检查定时器
startStatusCheckTimer() {
this.currentStatusChecks = 0
// 确保之前的计时器被清除
this.clearStatusCheckTimer()
this.statusCheckTimer = setInterval(() => {
// 只有当页面活跃时才执行检查
if (this.isPageActive) {
this.currentStatusChecks++
console.log(`执行归还状态检查 (${this.currentStatusChecks}/${this.maxStatusChecks})`)
this.checkReturnStatus()
// 如果超过最大检查次数,停止定时器
@@ -362,13 +425,19 @@ export default {
duration: 3000
})
}
} else {
console.log('页面已不活跃,停止状态检查计时器')
this.clearStatusCheckTimer()
}
}, this.statusCheckInterval)
console.log('已启动归还状态检查计时器')
},
// 通过设备号查询使用中的订单
async getOrderByDevice() {
try {
uni.showLoading({ title: '加载中' })
// uni.showLoading({ title: '加载中' })
if (!this.deviceId) {
throw new Error('设备号不能为空')
@@ -432,7 +501,10 @@ export default {
// 检查归还状态
async checkReturnStatus() {
try {
// 只有页面活跃时才执行检查
if (this.isPageActive) {
await this.getOrderDetails()
}
} catch (error) {
console.error('检查归还状态失败:', error)
}
File diff suppressed because one or more lines are too long
@@ -1 +1 @@
{"version":3,"file":"payment.js","sources":["E:/迅雷下载/HBuilderX.4.57.2025032507/HBuilderX/plugins/uniapp-cli-vite/uniPage:/cGFnZXMvb3JkZXIvcGF5bWVudC52dWU"],"sourcesContent":["import MiniProgramPage from 'C:/Users/Administrator.DESKTOP-IRCM9I0/Desktop/locker-fans/locker-fans/uni-fans/pages/order/payment.vue'\nwx.createPage(MiniProgramPage)"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,GAAG,WAAW,eAAe;"}
{"version":3,"file":"payment.js","sources":["E:/迅雷下载/HBuilderX.4.57.2025032507/HBuilderX/plugins/uniapp-cli-vite/uniPage:/cGFnZXMvb3JkZXIvcGF5bWVudC52dWU"],"sourcesContent":["import MiniProgramPage from 'C:/Users/Administrator.DESKTOP-IRCM9I0/Desktop/locker-fans/locker-fans/uni-fans/pages/order/payment.vue'\nwx.createPage(MiniProgramPage)"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,GAAG,WAAW,eAAe;"}
@@ -1 +1 @@
{"version":3,"file":"index.js","sources":["E:/迅雷下载/HBuilderX.4.57.2025032507/HBuilderX/plugins/uniapp-cli-vite/uniPage:/cGFnZXMvcmV0dXJuL2luZGV4LnZ1ZQ"],"sourcesContent":["import MiniProgramPage from 'C:/Users/Administrator.DESKTOP-IRCM9I0/Desktop/locker-fans/locker-fans/uni-fans/pages/return/index.vue'\nwx.createPage(MiniProgramPage)"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,GAAG,WAAW,eAAe;"}
{"version":3,"file":"index.js","sources":["E:/迅雷下载/HBuilderX.4.57.2025032507/HBuilderX/plugins/uniapp-cli-vite/uniPage:/cGFnZXMvcmV0dXJuL2luZGV4LnZ1ZQ"],"sourcesContent":["import MiniProgramPage from 'C:/Users/Administrator.DESKTOP-IRCM9I0/Desktop/locker-fans/locker-fans/uni-fans/pages/return/index.vue'\nwx.createPage(MiniProgramPage)"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,GAAG,WAAW,eAAe;"}
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -6876,7 +6876,7 @@ function initOnError() {
function initRuntimeSocketService() {
const hosts = "10.8.0.5,192.168.1.15,127.0.0.1";
const port = "8090";
const id = "mp-weixin_V-Usyu";
const id = "mp-weixin_WlfuId";
const lazy = typeof swan !== "undefined";
let restoreError = lazy ? () => {
} : initOnError();
+11 -2
View File
@@ -47,6 +47,7 @@ const getDeviceInfo = (deviceNo) => {
});
};
const queryById = (id) => {
common_vendor.index.__f__("log", "at config/user.js:80", `查询订单详情, orderId: ${id}`);
return config_http.request({
url: `/app/order/${id}`,
method: "get",
@@ -61,20 +62,27 @@ const rentPowerBank = (deviceNo, phone) => {
});
};
const confirmPaymentAndRent = (orderId) => {
common_vendor.index.__f__("log", "at config/user.js:118", `确认支付并弹出充电宝, orderId: ${orderId}`);
common_vendor.index.__f__("log", "at config/user.js:119", `确认支付并弹出充电宝, orderId: ${orderId}`);
return config_http.request({
url: `/app/device/confirmPaymentAndRent?orderId=${orderId}`,
method: "post"
});
};
const updateOrderPackage = (data) => {
common_vendor.index.__f__("log", "at config/user.js:157", "更新订单套餐信息:", data);
common_vendor.index.__f__("log", "at config/user.js:158", "更新订单套餐信息:", data);
return config_http.request({
url: "/app/device/updateOrderPackage",
method: "post",
data
});
};
const updateUserBalance = (orderId) => {
return config_http.request({
url: `/app/user/updateBalance/${orderId}`,
method: "post",
hideLoading: true
});
};
exports.checkOrdersByStatus = checkOrdersByStatus;
exports.confirmPaymentAndRent = confirmPaymentAndRent;
exports.getDeviceInfo = getDeviceInfo;
@@ -84,5 +92,6 @@ exports.login = login;
exports.queryById = queryById;
exports.rentPowerBank = rentPowerBank;
exports.updateOrderPackage = updateOrderPackage;
exports.updateUserBalance = updateUserBalance;
exports.withdrawDeposit = withdrawDeposit;
//# sourceMappingURL=../../.sourcemap/mp-weixin/config/user.js.map
+13 -6
View File
@@ -76,6 +76,7 @@ const _sfc_main = {
formattedTime = this.formatTime(/* @__PURE__ */ new Date());
}
} catch (e) {
common_vendor.index.__f__("error", "at pages/order/payment.vue:158", "时间格式化错误:", e);
formattedTime = this.formatTime(/* @__PURE__ */ new Date());
}
this.orderInfo = {
@@ -84,12 +85,12 @@ const _sfc_main = {
createTime: formattedTime,
phone: orderData.phone,
deposit: this.passedDepositAmount || orderData.depositAmount || "99.00",
amount: orderData.packagePrice || this.packageInfo.price || "0.00"
// 优先使用传递的押金,然后是订单中的押金,最后默认99元
amount: orderData.amount || this.packageInfo.price || "0.00"
};
if (orderData.packageTime) {
this.packageInfo.time = `${orderData.packageTime / 60}小时`;
this.packageInfo.price = orderData.packagePrice || "0.00";
} else if (this.packageInfo.time) {
if (!orderData.packageTime && this.packageInfo.time) {
this.orderInfo.packageTime = this.packageInfo.time;
this.orderInfo.packagePrice = this.packageInfo.price;
}
} else {
throw new Error("获取订单信息失败");
@@ -126,6 +127,11 @@ const _sfc_main = {
title: "支付成功",
icon: "success"
});
try {
await config_user.updateUserBalance(this.orderId);
} catch (error) {
common_vendor.index.__f__("warn", "at pages/order/payment.vue:223", "更新用户余额失败:", error);
}
setTimeout(() => {
common_vendor.index.redirectTo({
url: `/pages/order/index?orderId=${this.orderId}`
@@ -133,6 +139,7 @@ const _sfc_main = {
}, 1500);
},
fail: (err) => {
common_vendor.index.__f__("error", "at pages/order/payment.vue:234", "支付失败:", err);
throw new Error("支付失败,请重试");
}
});
@@ -229,7 +236,7 @@ const _sfc_main = {
throw new Error("查询订单状态失败");
}
} catch (error) {
common_vendor.index.__f__("error", "at pages/order/payment.vue:332", "查询订单状态错误:", error);
common_vendor.index.__f__("error", "at pages/order/payment.vue:342", "查询订单状态错误:", error);
return null;
}
}
+74 -27
View File
@@ -21,15 +21,18 @@ const _sfc_main = {
maxStatusChecks: 30,
// 最多检查30次
currentStatusChecks: 0,
statusCheckInterval: 5e3
statusCheckInterval: 5e3,
// 5秒检查一次
isPageActive: false
// 跟踪页面是否活跃
};
},
onLoad(options) {
common_vendor.index.__f__("log", "at pages/return/index.vue:96", "Return page loaded with options:", JSON.stringify(options));
common_vendor.index.__f__("log", "at pages/return/index.vue:97", "Return page loaded with options:", JSON.stringify(options));
this.isPageActive = true;
this.orderInfo.orderId = options.orderId || "";
this.deviceId = options.deviceNo || options.deviceId || "";
common_vendor.index.__f__("log", "at pages/return/index.vue:102", `初始化参数: orderId=${this.orderInfo.orderId}, deviceId=${this.deviceId}`);
common_vendor.index.__f__("log", "at pages/return/index.vue:106", `初始化参数: orderId=${this.orderInfo.orderId}, deviceId=${this.deviceId}`);
if (!this.orderInfo.orderId && this.deviceId) {
this.getOrderByDevice();
} else if (this.orderInfo.orderId) {
@@ -39,13 +42,14 @@ const _sfc_main = {
common_vendor.index.setStorageSync("activeOrderId", this.orderInfo.orderId);
try {
if (this.$orderMonitor) {
this.$orderMonitor.addOrder({ orderId: this.orderInfo.orderId });
common_vendor.index.__f__("log", "at pages/return/index.vue:122", "订单已添加到监控队列:", this.orderInfo.orderId);
this.$orderMonitor.removeOrder({ orderId: this.orderInfo.orderId });
this.$orderMonitor.addOrder({ orderId: this.orderInfo.orderId }, "return");
common_vendor.index.__f__("log", "at pages/return/index.vue:129", "订单已添加到监控队列:", this.orderInfo.orderId);
} else {
common_vendor.index.__f__("warn", "at pages/return/index.vue:124", "$orderMonitor 未定义,无法添加订单到监控队列");
common_vendor.index.__f__("warn", "at pages/return/index.vue:131", "$orderMonitor 未定义,无法添加订单到监控队列");
}
} catch (error) {
common_vendor.index.__f__("error", "at pages/return/index.vue:127", "添加订单到监控队列失败:", error);
common_vendor.index.__f__("error", "at pages/return/index.vue:134", "添加订单到监控队列失败:", error);
}
} else {
common_vendor.index.showToast({
@@ -58,15 +62,37 @@ const _sfc_main = {
}
common_vendor.index.$on("orderCompleted", this.handleOrderCompleted);
},
onUnload() {
// 添加onHide生命周期,处理页面隐藏时的清理工作
onHide() {
common_vendor.index.__f__("log", "at pages/return/index.vue:154", "归还页面隐藏,清理计时器资源和监控服务");
this.isPageActive = false;
this.clearTimer();
this.clearStatusCheckTimer();
this.removeFromOrderMonitor();
},
onUnload() {
common_vendor.index.__f__("log", "at pages/return/index.vue:166", "归还页面卸载,清理所有资源");
this.isPageActive = false;
this.clearTimer();
this.clearStatusCheckTimer();
this.removeFromOrderMonitor();
common_vendor.index.$off("orderCompleted", this.handleOrderCompleted);
},
methods: {
// 从订单监控服务中移除当前订单
removeFromOrderMonitor() {
if (this.orderInfo.orderId && this.$orderMonitor) {
try {
this.$orderMonitor.removeOrder({ orderId: this.orderInfo.orderId });
common_vendor.index.__f__("log", "at pages/return/index.vue:186", "订单已从监控队列移除:", this.orderInfo.orderId);
} catch (error) {
common_vendor.index.__f__("error", "at pages/return/index.vue:188", "从监控队列移除订单失败:", error);
}
}
},
// 处理订单完成事件(可由任何地方触发)
handleOrderCompleted(orderData) {
common_vendor.index.__f__("log", "at pages/return/index.vue:155", "收到订单完成事件:", orderData);
common_vendor.index.__f__("log", "at pages/return/index.vue:194", "收到订单完成事件:", orderData);
if (orderData.orderId === this.orderInfo.orderId || orderData.orderNo === this.orderInfo.orderNo) {
this.showReturnSuccessModal(orderData);
}
@@ -108,18 +134,21 @@ const _sfc_main = {
},
// 获取订单详情
async getOrderDetails() {
if (!this.isPageActive) {
common_vendor.index.__f__("log", "at pages/return/index.vue:250", "页面已不活跃,跳过订单详情请求");
return;
}
try {
common_vendor.index.showLoading({ title: "加载中" });
if (!this.orderInfo.orderId) {
throw new Error("订单ID不能为空");
}
common_vendor.index.__f__("log", "at pages/return/index.vue:216", "请求订单详情, orderId:", this.orderInfo.orderId);
common_vendor.index.__f__("log", "at pages/return/index.vue:261", "请求订单详情, orderId:", this.orderInfo.orderId);
const result = await config_user.queryById(this.orderInfo.orderId);
common_vendor.index.__f__("log", "at pages/return/index.vue:218", "订单详情结果:", JSON.stringify(result));
common_vendor.index.__f__("log", "at pages/return/index.vue:263", "订单详情结果:", JSON.stringify(result));
if (result.code === 200 && result.data) {
const orderData = result.data;
common_vendor.index.__f__("log", "at pages/return/index.vue:222", "订单原始数据:", orderData);
common_vendor.index.__f__("log", "at pages/return/index.vue:223", "开始时间字段:", orderData.startTime, typeof orderData.startTime);
common_vendor.index.__f__("log", "at pages/return/index.vue:267", "订单原始数据:", orderData);
common_vendor.index.__f__("log", "at pages/return/index.vue:268", "开始时间字段:", orderData.startTime, typeof orderData.startTime);
if (orderData.orderStatus) {
this.orderInfo.orderStatus = orderData.orderStatus;
}
@@ -129,12 +158,12 @@ const _sfc_main = {
return;
}
this.updateOrderInfo(orderData);
common_vendor.index.__f__("log", "at pages/return/index.vue:245", "更新后的开始时间:", this.orderInfo.startTime);
common_vendor.index.__f__("log", "at pages/return/index.vue:290", "更新后的开始时间:", this.orderInfo.startTime);
} else {
throw new Error(result.msg || "获取订单详情失败");
}
} catch (error) {
common_vendor.index.__f__("error", "at pages/return/index.vue:250", "获取订单详情错误:", error);
common_vendor.index.__f__("error", "at pages/return/index.vue:295", "获取订单详情错误:", error);
common_vendor.index.showToast({
title: error.message || "获取订单信息失败",
icon: "none"
@@ -157,7 +186,7 @@ const _sfc_main = {
},
// 使用后端返回的使用时长和费用数据
updateOrderInfo(orderData) {
common_vendor.index.__f__("log", "at pages/return/index.vue:278", "更新订单信息:", JSON.stringify(orderData));
common_vendor.index.__f__("log", "at pages/return/index.vue:323", "更新订单信息:", JSON.stringify(orderData));
this.orderInfo.usedTime = orderData.usedTime || "0分钟";
this.orderInfo.currentFee = orderData.currentFee || orderData.actualDeviceAmount || orderData.payAmount || "0.00";
if (orderData.orderStatus) {
@@ -166,16 +195,16 @@ const _sfc_main = {
this.orderInfo._rawStartTime = orderData.startTime;
if (orderData.startTime) {
try {
common_vendor.index.__f__("log", "at pages/return/index.vue:297", "API返回的开始时间:", orderData.startTime);
common_vendor.index.__f__("log", "at pages/return/index.vue:342", "API返回的开始时间:", orderData.startTime);
this.orderInfo.startTime = orderData.startTime;
} catch (e) {
common_vendor.index.__f__("error", "at pages/return/index.vue:301", "更新开始时间错误:", e);
common_vendor.index.__f__("error", "at pages/return/index.vue:346", "更新开始时间错误:", e);
this.orderInfo.startTime = "未知";
}
} else {
common_vendor.index.__f__("warn", "at pages/return/index.vue:305", "API返回的订单数据中没有startTime字段");
common_vendor.index.__f__("warn", "at pages/return/index.vue:350", "API返回的订单数据中没有startTime字段");
if (orderData.createTime) {
common_vendor.index.__f__("log", "at pages/return/index.vue:308", "使用createTime作为备选:", orderData.createTime);
common_vendor.index.__f__("log", "at pages/return/index.vue:353", "使用createTime作为备选:", orderData.createTime);
this.orderInfo.startTime = orderData.createTime;
} else {
this.orderInfo.startTime = "未知";
@@ -187,15 +216,24 @@ const _sfc_main = {
},
// 更新使用时长的定时器
startTimer() {
this.clearTimer();
this.timer = setInterval(() => {
if (this.isPageActive) {
common_vendor.index.__f__("log", "at pages/return/index.vue:375", "执行定时更新订单信息");
this.getOrderDetails();
} else {
common_vendor.index.__f__("log", "at pages/return/index.vue:378", "页面已不活跃,停止计时器");
this.clearTimer();
}
}, 6e4);
common_vendor.index.__f__("log", "at pages/return/index.vue:383", "已启动使用时长更新计时器");
},
// 清除定时器
clearTimer() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
common_vendor.index.__f__("log", "at pages/return/index.vue:391", "已清除使用时长更新计时器");
}
},
// 清除状态检查定时器
@@ -203,6 +241,7 @@ const _sfc_main = {
if (this.statusCheckTimer) {
clearInterval(this.statusCheckTimer);
this.statusCheckTimer = null;
common_vendor.index.__f__("log", "at pages/return/index.vue:400", "已清除归还状态检查计时器");
}
},
// 开始状态检查定时器
@@ -210,7 +249,9 @@ const _sfc_main = {
this.currentStatusChecks = 0;
this.clearStatusCheckTimer();
this.statusCheckTimer = setInterval(() => {
if (this.isPageActive) {
this.currentStatusChecks++;
common_vendor.index.__f__("log", "at pages/return/index.vue:414", `执行归还状态检查 (${this.currentStatusChecks}/${this.maxStatusChecks})`);
this.checkReturnStatus();
if (this.currentStatusChecks >= this.maxStatusChecks) {
this.clearStatusCheckTimer();
@@ -220,12 +261,16 @@ const _sfc_main = {
duration: 3e3
});
}
} else {
common_vendor.index.__f__("log", "at pages/return/index.vue:429", "页面已不活跃,停止状态检查计时器");
this.clearStatusCheckTimer();
}
}, this.statusCheckInterval);
common_vendor.index.__f__("log", "at pages/return/index.vue:434", "已启动归还状态检查计时器");
},
// 通过设备号查询使用中的订单
async getOrderByDevice() {
try {
common_vendor.index.showLoading({ title: "加载中" });
if (!this.deviceId) {
throw new Error("设备号不能为空");
}
@@ -237,16 +282,16 @@ const _sfc_main = {
"Clientid": common_vendor.index.getStorageSync("client_id")
}
});
common_vendor.index.__f__("log", "at pages/return/index.vue:387", "通过设备号查询订单结果:", JSON.stringify(inUseRes));
common_vendor.index.__f__("log", "at pages/return/index.vue:456", "通过设备号查询订单结果:", JSON.stringify(inUseRes));
if (inUseRes.statusCode === 200 && inUseRes.data.code === 200 && inUseRes.data.data) {
const inUseOrder = inUseRes.data.data;
common_vendor.index.__f__("log", "at pages/return/index.vue:391", "使用中的订单:", inUseOrder);
common_vendor.index.__f__("log", "at pages/return/index.vue:460", "使用中的订单:", inUseOrder);
this.orderInfo.orderId = inUseOrder.orderId;
if (inUseOrder.orderStatus) {
this.orderInfo.orderStatus = inUseOrder.orderStatus;
}
if (inUseOrder.startTime) {
common_vendor.index.__f__("log", "at pages/return/index.vue:403", "inUse API返回的开始时间:", inUseOrder.startTime);
common_vendor.index.__f__("log", "at pages/return/index.vue:472", "inUse API返回的开始时间:", inUseOrder.startTime);
this.orderInfo.startTime = inUseOrder.startTime;
}
this.getOrderDetails();
@@ -256,7 +301,7 @@ const _sfc_main = {
throw new Error("未找到使用中的订单");
}
} catch (error) {
common_vendor.index.__f__("error", "at pages/return/index.vue:417", "通过设备号查询订单失败:", error);
common_vendor.index.__f__("error", "at pages/return/index.vue:486", "通过设备号查询订单失败:", error);
common_vendor.index.showToast({
title: error.message || "获取订单信息失败",
icon: "none"
@@ -271,9 +316,11 @@ const _sfc_main = {
// 检查归还状态
async checkReturnStatus() {
try {
if (this.isPageActive) {
await this.getOrderDetails();
}
} catch (error) {
common_vendor.index.__f__("error", "at pages/return/index.vue:437", "检查归还状态失败:", error);
common_vendor.index.__f__("error", "at pages/return/index.vue:509", "检查归还状态失败:", error);
}
},
// 返回首页
+59 -19
View File
@@ -7,34 +7,56 @@ class OrderMonitor {
this.timer = null;
this.checkInterval = 1e4;
this.isRunning = false;
this.currentPage = null;
}
/**
* 添加订单到监控队列
* @param {Object} orderData 订单数据对象必须包含orderId字段
* @param {String} pageName 关联的页面名称默认为'return'
*/
addOrder(orderData) {
addOrder(orderData, pageName = "return") {
if (!orderData || !orderData.orderId) {
common_vendor.index.__f__("error", "at utils/orderMonitor.js:21", "添加订单监控失败:无效的订单数据");
common_vendor.index.__f__("error", "at utils/orderMonitor.js:23", "添加订单监控失败:无效的订单数据");
return;
}
common_vendor.index.__f__("log", "at utils/orderMonitor.js:25", "添加订单到监控队列:", orderData.orderId);
this.activeOrders.set(orderData.orderId, orderData);
common_vendor.index.__f__("log", "at utils/orderMonitor.js:27", `添加订单到监控队列: ${orderData.orderId}, 页面: ${pageName}`);
this.activeOrders.set(orderData.orderId, {
...orderData,
pageName
});
if (!this.isRunning) {
this.start();
}
}
/**
* 移除订单的监控
* @param {String} orderId 订单ID
* @param {Object} params 包含orderId或pageName的对象
*/
removeOrder(orderId) {
if (this.activeOrders.has(orderId)) {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:40", "从监控队列移除订单:", orderId);
removeOrder(params) {
if (!params)
return;
if (params.orderId && this.activeOrders.has(params.orderId)) {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:48", "从监控队列移除订单:", params.orderId);
this.activeOrders.delete(params.orderId);
} else if (params.pageName) {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:53", "从监控队列移除页面相关订单:", params.pageName);
for (const [orderId, data] of this.activeOrders.entries()) {
if (data.pageName === params.pageName) {
this.activeOrders.delete(orderId);
}
}
}
if (this.activeOrders.size === 0) {
this.stop();
}
}
/**
* 设置当前活跃页面
* @param {String} pageName 页面名称
*/
setActivePage(pageName) {
this.currentPage = pageName;
common_vendor.index.__f__("log", "at utils/orderMonitor.js:73", "设置当前活跃页面:", pageName);
}
/**
* 启动监控服务
@@ -42,7 +64,7 @@ class OrderMonitor {
start() {
if (this.isRunning)
return;
common_vendor.index.__f__("log", "at utils/orderMonitor.js:56", "启动订单监控服务");
common_vendor.index.__f__("log", "at utils/orderMonitor.js:82", "启动订单监控服务");
this.isRunning = true;
this.checkOrders();
this.timer = setInterval(() => {
@@ -55,7 +77,7 @@ class OrderMonitor {
stop() {
if (!this.isRunning)
return;
common_vendor.index.__f__("log", "at utils/orderMonitor.js:71", "停止订单监控服务");
common_vendor.index.__f__("log", "at utils/orderMonitor.js:97", "停止订单监控服务");
this.isRunning = false;
if (this.timer) {
clearInterval(this.timer);
@@ -68,12 +90,18 @@ class OrderMonitor {
async checkOrders() {
if (this.activeOrders.size === 0)
return;
common_vendor.index.__f__("log", "at utils/orderMonitor.js:86", `检查 ${this.activeOrders.size} 个活跃订单状态`);
for (const [orderId, orderData] of this.activeOrders.entries()) {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:112", `检查 ${this.activeOrders.size} 个活跃订单状态, 当前页面: ${this.currentPage}`);
for (const [orderId, data] of this.activeOrders.entries()) {
try {
if (!data.pageName || data.pageName === "return") {
if (this.currentPage === "return" || this.currentPage === null) {
await this.checkOrderStatus(orderId);
} else {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:122", `跳过订单状态检查: ${orderId}, 当前不在归还页面`);
}
}
} catch (error) {
common_vendor.index.__f__("error", "at utils/orderMonitor.js:92", `检查订单状态失败: ${orderId}`, error);
common_vendor.index.__f__("error", "at utils/orderMonitor.js:126", `检查订单状态失败: ${orderId}`, error);
}
}
}
@@ -83,13 +111,18 @@ class OrderMonitor {
*/
async checkOrderStatus(orderId) {
try {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:103", `检查订单 ${orderId} 的状态`);
common_vendor.index.__f__("log", "at utils/orderMonitor.js:137", `检查订单 ${orderId} 的状态`);
const result = await config_user.queryById(orderId);
if (result.code === 200 && result.data) {
const orderData = result.data;
this.activeOrders.set(orderId, orderData);
const existingData = this.activeOrders.get(orderId);
const pageName = existingData ? existingData.pageName : null;
this.activeOrders.set(orderId, {
...orderData,
pageName
});
if (orderData.orderStatus === "used_done" || orderData.orderStatus === "used_down") {
common_vendor.index.__f__("log", "at utils/orderMonitor.js:114", `订单 ${orderId} 已完成!`);
common_vendor.index.__f__("log", "at utils/orderMonitor.js:153", `订单 ${orderId} 已完成!`);
common_vendor.index.$emit("orderCompleted", orderData);
common_vendor.index.showToast({
title: "充电宝归还成功",
@@ -99,7 +132,7 @@ class OrderMonitor {
const innerAudioContext = common_vendor.index.createInnerAudioContext();
innerAudioContext.src = "/static/audio/return_success.mp3";
innerAudioContext.play();
this.removeOrder(orderId);
this.removeOrder({ orderId });
setTimeout(() => {
common_vendor.index.showModal({
title: "归还成功",
@@ -117,16 +150,23 @@ class OrderMonitor {
}
}
} catch (error) {
common_vendor.index.__f__("error", "at utils/orderMonitor.js:153", `检查订单 ${orderId} 状态出错:`, error);
common_vendor.index.__f__("error", "at utils/orderMonitor.js:192", `检查订单 ${orderId} 状态出错:`, error);
}
}
}
const orderMonitor = new OrderMonitor();
common_vendor.index.onAppRoute((route) => {
const pagePath = route.path || "";
const pageSegments = pagePath.split("/");
const pageName = pageSegments[pageSegments.length - 1];
orderMonitor.setActivePage(pageName || null);
common_vendor.index.__f__("log", "at utils/orderMonitor.js:209", "页面切换:", pagePath, "当前活跃页面:", pageName);
});
const initOrderMonitor = () => {
const lastActiveOrderId = common_vendor.index.getStorageSync("activeOrderId");
if (lastActiveOrderId) {
const lastOrderData = { orderId: lastActiveOrderId };
orderMonitor.addOrder(lastOrderData);
orderMonitor.addOrder(lastOrderData, "return");
}
};
initOrderMonitor();
+65 -14
View File
@@ -6,24 +6,29 @@ import { queryById } from '@/config/user.js'
*/
class OrderMonitor {
constructor() {
this.activeOrders = new Map() // 存储活跃订单 Map<orderId, orderData>
this.activeOrders = new Map() // 存储活跃订单 Map<orderId, {orderData, pageName}>
this.timer = null
this.checkInterval = 10000 // 10秒检查一次
this.isRunning = false
this.currentPage = null // 当前活跃页面
}
/**
* 添加订单到监控队列
* @param {Object} orderData 订单数据对象必须包含orderId字段
* @param {String} pageName 关联的页面名称默认为'return'
*/
addOrder(orderData) {
addOrder(orderData, pageName = 'return') {
if (!orderData || !orderData.orderId) {
console.error('添加订单监控失败:无效的订单数据')
return
}
console.log('添加订单到监控队列:', orderData.orderId)
this.activeOrders.set(orderData.orderId, orderData)
console.log(`添加订单到监控队列: ${orderData.orderId}, 页面: ${pageName}`)
this.activeOrders.set(orderData.orderId, {
...orderData,
pageName
})
// 如果监控服务尚未启动,则启动
if (!this.isRunning) {
@@ -33,18 +38,39 @@ class OrderMonitor {
/**
* 移除订单的监控
* @param {String} orderId 订单ID
* @param {Object} params 包含orderId或pageName的对象
*/
removeOrder(orderId) {
if (this.activeOrders.has(orderId)) {
console.log('从监控队列移除订单:', orderId)
removeOrder(params) {
if (!params) return
// 如果提供了orderId,直接删除该订单
if (params.orderId && this.activeOrders.has(params.orderId)) {
console.log('从监控队列移除订单:', params.orderId)
this.activeOrders.delete(params.orderId)
}
// 如果提供了pageName,删除该页面关联的所有订单
else if (params.pageName) {
console.log('从监控队列移除页面相关订单:', params.pageName)
for (const [orderId, data] of this.activeOrders.entries()) {
if (data.pageName === params.pageName) {
this.activeOrders.delete(orderId)
}
}
}
// 如果没有订单需要监控了,停止服务
if (this.activeOrders.size === 0) {
this.stop()
}
}
/**
* 设置当前活跃页面
* @param {String} pageName 页面名称
*/
setActivePage(pageName) {
this.currentPage = pageName
console.log('设置当前活跃页面:', pageName)
}
/**
@@ -83,11 +109,19 @@ class OrderMonitor {
async checkOrders() {
if (this.activeOrders.size === 0) return
console.log(`检查 ${this.activeOrders.size} 个活跃订单状态`)
console.log(`检查 ${this.activeOrders.size} 个活跃订单状态, 当前页面: ${this.currentPage}`)
for (const [orderId, orderData] of this.activeOrders.entries()) {
// 只检查当前活跃页面关联的订单,或无页面关联的订单
for (const [orderId, data] of this.activeOrders.entries()) {
try {
// 只在归还页面(或页面未指定时)才执行轮询
if (!data.pageName || data.pageName === 'return') {
if (this.currentPage === 'return' || this.currentPage === null) {
await this.checkOrderStatus(orderId)
} else {
console.log(`跳过订单状态检查: ${orderId}, 当前不在归还页面`)
}
}
} catch (error) {
console.error(`检查订单状态失败: ${orderId}`, error)
}
@@ -105,9 +139,14 @@ class OrderMonitor {
if (result.code === 200 && result.data) {
const orderData = result.data
const existingData = this.activeOrders.get(orderId)
const pageName = existingData ? existingData.pageName : null
// 更新本地存储的订单数据
this.activeOrders.set(orderId, orderData)
// 更新本地存储的订单数据,保留页面关联信息
this.activeOrders.set(orderId, {
...orderData,
pageName
})
// 检查订单是否已完成
if (orderData.orderStatus === 'used_done' || orderData.orderStatus === 'used_down') {
@@ -129,7 +168,7 @@ class OrderMonitor {
innerAudioContext.play()
// 完成的订单不再需要监控
this.removeOrder(orderId)
this.removeOrder({orderId})
// 如果用户不在归还页面,则显示归还成功弹窗
setTimeout(() => {
@@ -158,12 +197,24 @@ class OrderMonitor {
// 导出单例实例
export const orderMonitor = new OrderMonitor()
// 监听页面切换事件
uni.onAppRoute((route) => {
const pagePath = route.path || ''
const pageSegments = pagePath.split('/')
const pageName = pageSegments[pageSegments.length - 1]
// 设置当前活跃页面
orderMonitor.setActivePage(pageName || null)
console.log('页面切换:', pagePath, '当前活跃页面:', pageName)
})
// 页面加载时自动恢复监控上次的活跃订单(如果有)
const initOrderMonitor = () => {
const lastActiveOrderId = uni.getStorageSync('activeOrderId')
if (lastActiveOrderId) {
const lastOrderData = { orderId: lastActiveOrderId }
orderMonitor.addOrder(lastOrderData)
orderMonitor.addOrder(lastOrderData, 'return')
}
}