流程改版
This commit is contained in:
@@ -6,14 +6,14 @@ App({
|
||||
globalData: {
|
||||
// wx.request 请求的服务地址
|
||||
wxRequestUrl: "https://device.shuziweidao.com/gateway/",
|
||||
// wxRequestUrl: "http://192.168.10.173:8889/",
|
||||
// wxRequestUrl: "http://192.168.1.12:8889/",
|
||||
|
||||
// 秤的所有信息
|
||||
ppScale: {
|
||||
plugin: null,
|
||||
activeProtocol: null,
|
||||
domain: "http://device.shuziweidao.com:80/gateway",
|
||||
// domain: "http://192.168.10.173:8889",
|
||||
// domain: "http://192.168.1.12:8889",
|
||||
wifi: {
|
||||
ssid: "",
|
||||
password: ""
|
||||
@@ -129,19 +129,24 @@ App({
|
||||
"uhStatus": 1,
|
||||
"updateBy": null
|
||||
}], // 即将要搜索周边秤的配置
|
||||
list: [], // 根据配置搜索到秤的列表
|
||||
list: [], // 根据配置搜索到秤的列表
|
||||
name: "",
|
||||
mac: null, // 选中的设备mac地址/SN码
|
||||
name: "", // 设备名称
|
||||
connection: null, // 默认选中连接的设备
|
||||
connectState: null, // 当前连接设备的状态
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_callbacks: {} // 用于存储所有 globalData 属性的监听回调
|
||||
},
|
||||
|
||||
initPpScale() {
|
||||
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
|
||||
|
||||
// 蓝牙连接状态监听
|
||||
this.globalData.ppScale.plugin.bus.subscribe("connectState", (res) => {
|
||||
console.log("app.js ===> connectState", res);
|
||||
console.log("app.js ===> connectState", res);
|
||||
this.setGlobalData('ppScale.device.connectState', res);
|
||||
|
||||
// 连接失败则重新连接
|
||||
if (res == this.globalData.ppScale.plugin.BLUE_STATE.CONNECTFAILED) {
|
||||
@@ -150,6 +155,7 @@ App({
|
||||
wx.hideLoading();
|
||||
});
|
||||
|
||||
// 设备自动断开监听
|
||||
this.globalData.ppScale.plugin.bus.subscribe("deviceWillDisconnect", (res) => {
|
||||
console.log("app.js ===> deviceWillDisconnect", res);
|
||||
|
||||
@@ -165,5 +171,63 @@ App({
|
||||
this.reconnectDevice();
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 注册 globalData 某个属性的监听
|
||||
* @param {string} keyPath 要监听的 globalData 属性路径,例如 'ppScale.device.connectState'
|
||||
* @param {function} callback 属性变化时执行的回调函数 (newValue, oldValue) => {}
|
||||
*/
|
||||
watch(keyPath, callback) {
|
||||
if (!this.globalData._callbacks[keyPath]) {
|
||||
this.globalData._callbacks[keyPath] = [];
|
||||
}
|
||||
this.globalData._callbacks[keyPath].push(callback);
|
||||
},
|
||||
|
||||
/**
|
||||
* 取消 globalData 某个属性的监听
|
||||
* @param {string} keyPath 要取消监听的 globalData 属性路径
|
||||
* @param {function} callback 之前注册的回调函数
|
||||
*/
|
||||
unwatch(keyPath, callback) {
|
||||
if (this.globalData._callbacks[keyPath]) {
|
||||
this.globalData._callbacks[keyPath] = this.globalData._callbacks[keyPath].filter(cb => cb !== callback);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 设置 globalData 的值并触发所有相关监听器
|
||||
* 支持点表示法设置嵌套属性,例如 'ppScale.device.connectState'
|
||||
* @param {string} keyPath 要设置的 globalData 属性路径
|
||||
* @param {*} value 要设置的新值
|
||||
*/
|
||||
setGlobalData(keyPath, value) {
|
||||
const keys = keyPath.split('.');
|
||||
let current = this.globalData;
|
||||
let oldValue = undefined;
|
||||
|
||||
// 遍历到目标属性的父级
|
||||
for (let i = 0; i < keys.length - 1; i++) {
|
||||
if (!current[keys[i]]) {
|
||||
current[keys[i]] = {}; // 如果路径不存在,创建空对象
|
||||
}
|
||||
current = current[keys[i]];
|
||||
}
|
||||
|
||||
// 获取旧值
|
||||
oldValue = current[keys[keys.length - 1]];
|
||||
|
||||
// 只有当值发生变化时才更新并通知
|
||||
if (oldValue !== value) {
|
||||
current[keys[keys.length - 1]] = value; // 设置新值
|
||||
|
||||
// 触发监听器
|
||||
if (this.globalData._callbacks[keyPath]) {
|
||||
this.globalData._callbacks[keyPath].forEach(callback => {
|
||||
callback(value, oldValue);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user