尝试解决主分支冲突
This commit is contained in:
@ -1,5 +1,19 @@
|
||||
import receivTool from "@/utils/BleReceive.js"
|
||||
var recei = null;
|
||||
const serviceDic = [ //合作供应商的蓝牙主服务
|
||||
{
|
||||
"serviceId": "0000FFE0-0000-1000-8000-00805F9B34FB",
|
||||
"writeId": "0000FFE1-0000-1000-8000-00805F9B34FB",
|
||||
"notifyId": "0000FFE2-0000-1000-8000-00805F9B34FB"
|
||||
},
|
||||
{
|
||||
"serviceId": "0000AE30-0000-1000-8000-00805F9B34FB",
|
||||
"writeId": "0000AE03-0000-1000-8000-00805F9B34FB",
|
||||
"notifyId": "0000AE02-0000-1000-8000-00805F9B34FB"
|
||||
}
|
||||
|
||||
];
|
||||
|
||||
class BleHelper {
|
||||
constructor() {
|
||||
this.StorageKey = "linkedDevices";
|
||||
@ -26,7 +40,10 @@ class BleHelper {
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
this.linkAllDevices();
|
||||
this.OpenBlue().then(()=>{
|
||||
this.linkAllDevices();
|
||||
});
|
||||
|
||||
}, 10);
|
||||
this.data = {
|
||||
isOpenBlue: false, //蓝牙模块是否开启
|
||||
@ -40,9 +57,9 @@ class BleHelper {
|
||||
onDeviceFound: [], //发现新设备的事件
|
||||
receivDataCallback: [] //接收到数据的事件
|
||||
}
|
||||
this.addReceiveCallback((a,b,c)=>{
|
||||
recei.ReceiveData(a,b,c);
|
||||
}, "BleReceiveData");
|
||||
// this.addReceiveCallback((a, b, c) => {
|
||||
// recei.ReceiveData(a, b, c);
|
||||
// }, "BleReceiveData");
|
||||
this.dic = {
|
||||
errRemarks: [{
|
||||
key: '10000',
|
||||
@ -104,18 +121,51 @@ class BleHelper {
|
||||
}
|
||||
}
|
||||
|
||||
DropDevice(bleId, deviceId) {
|
||||
let flag = false;
|
||||
for (var i = 0; i < this.data.LinkedList.length; i++) {
|
||||
let item = this.data.LinkedList[i];
|
||||
if (bleId) {
|
||||
if (item.deviceId == bleId) {
|
||||
this.data.LinkedList.splice(i, 1);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (item.device && item.device.id) {
|
||||
this.data.LinkedList.splice(i, 1);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag) {
|
||||
this.updateCache();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
updateCache() {
|
||||
// console.log("this.StorageKey=", this.StorageKey)
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
}
|
||||
|
||||
linkAllDevices() {
|
||||
console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
|
||||
// console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
|
||||
|
||||
if (this.data.LinkedList && this.data.LinkedList.length > 0) {
|
||||
for (var i = 0; i < this.data.LinkedList.length; i++) {
|
||||
let device = this.data.LinkedList[i];
|
||||
console.log("自动连接:" + device.deviceId);
|
||||
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
|
||||
.notifyCharactId);
|
||||
// console.log("自动连接绑定过的设备:" + device.deviceId);
|
||||
if (device.macAddress && device.device && device.device.id) {
|
||||
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
|
||||
.notifyCharactId);
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
console.log("无设备连接");
|
||||
// console.log("无设备连接");
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +178,7 @@ class BleHelper {
|
||||
}
|
||||
|
||||
const currentPage = pages[pages.length - 1];
|
||||
console.log("currentPage=", currentPage.route);
|
||||
// console.log("currentPage=", currentPage.route);
|
||||
return currentPage.route;
|
||||
}
|
||||
|
||||
@ -139,7 +189,7 @@ class BleHelper {
|
||||
key = new Date().getTime();
|
||||
}
|
||||
if (key) {
|
||||
console.log("key=" + key);
|
||||
// console.log("key=" + key);
|
||||
let f = this.cfg.onDeviceFound.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -159,7 +209,7 @@ class BleHelper {
|
||||
let key = currKey ? currKey : this.getCurrentPagePath();
|
||||
|
||||
if (key) {
|
||||
console.log("key=" + key);
|
||||
// console.log("key=" + key);
|
||||
let f = this.cfg.onDeviceFound.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -181,7 +231,7 @@ class BleHelper {
|
||||
key = new Date().getTime();
|
||||
}
|
||||
if (key) {
|
||||
console.log("订阅消息回调,key=" + key);
|
||||
// console.log("订阅消息回调,key=" + key);
|
||||
let f = this.cfg.receivDataCallback.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -202,7 +252,7 @@ class BleHelper {
|
||||
removeReceiveCallback(currKey) {
|
||||
let key = currKey ? currKey : this.getCurrentPagePath();
|
||||
if (key) {
|
||||
console.log("移除消息回调:" + key);
|
||||
// console.log("移除消息回调:" + key);
|
||||
let f = this.cfg.receivDataCallback.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -320,6 +370,7 @@ class BleHelper {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.data.available) {
|
||||
console.log("蓝牙模块是可用状态");
|
||||
resolve({
|
||||
available: this.data.available,
|
||||
discovering: this.data.discovering
|
||||
@ -327,6 +378,7 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
if (!this.data.isOpenBlue) {
|
||||
console.log("蓝牙模块未打开");
|
||||
resolve({
|
||||
available: false,
|
||||
discovering: false
|
||||
@ -335,12 +387,13 @@ class BleHelper {
|
||||
}
|
||||
uni.getBluetoothAdapterState({
|
||||
success: (info) => {
|
||||
this.data.available=info.available;
|
||||
this.data.discovering=info.discovering;
|
||||
console.log("蓝牙状态获取成功,",info)
|
||||
this.data.available = info.available;
|
||||
this.data.discovering = info.discovering;
|
||||
resolve(info);
|
||||
},
|
||||
fail: (ex1) => {
|
||||
////console.log("ex1", ex1);
|
||||
console.log("蓝牙状态获取失败", ex1);
|
||||
let res1 = {
|
||||
available: false,
|
||||
discovering: false
|
||||
@ -360,18 +413,18 @@ class BleHelper {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
uni.openBluetoothAdapter({
|
||||
success: (args) => {
|
||||
console.log("蓝牙初始化成功:" + JSON.stringify(args));
|
||||
// console.log("蓝牙初始化成功:" + JSON.stringify(args));
|
||||
this.data.isOpenBlue = true;
|
||||
this.data.available=true;
|
||||
this.data.available = true;
|
||||
resolve(true);
|
||||
|
||||
if (this.data.isSubscribe) { //整个App生命周期,只订阅一次
|
||||
return;
|
||||
}
|
||||
console.log("开始订阅各类变化消息");
|
||||
// console.log("开始订阅各类变化消息");
|
||||
this.data.isSubscribe = true;
|
||||
|
||||
|
||||
@ -381,23 +434,26 @@ class BleHelper {
|
||||
}
|
||||
|
||||
uni.onBluetoothAdapterStateChange((state) => {
|
||||
console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
this.data.available = state.available;
|
||||
this.data.discovering = state.discovering;
|
||||
if (this.data.available && this.data
|
||||
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
|
||||
|
||||
// console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
if (this.data.available !== state.available) {
|
||||
this.data.available = state.available;
|
||||
this.data.discovering = state.discovering;
|
||||
if (this.data.available && this.data
|
||||
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
|
||||
this.linkAllDevices();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接
|
||||
this.data.LinkedList.filter((v) => {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
return true;
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
.LinkedList);
|
||||
|
||||
this.updateCache();
|
||||
}
|
||||
});
|
||||
|
||||
@ -406,7 +462,7 @@ class BleHelper {
|
||||
if (!res.connected) {
|
||||
|
||||
console.log("蓝牙连接已断开", res);
|
||||
this.data.LinkedList.find((v) => {
|
||||
let f=this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == res.deviceId) {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
@ -414,15 +470,16 @@ class BleHelper {
|
||||
}
|
||||
return false;
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
.LinkedList);
|
||||
this.updateCache();
|
||||
console.log("尝试5次恢复连接");
|
||||
this.LinkBlue(res.deviceId,f.writeServiceId,f.wirteCharactId,f.notifyCharactId,5)
|
||||
} else {
|
||||
console.log("蓝牙连接已恢复,", res);
|
||||
console.log("蓝牙连接已恢复,", res);
|
||||
}
|
||||
});
|
||||
|
||||
uni.onBluetoothDeviceFound((devices) => {
|
||||
// ////console.log("发现新设备:" + JSON.stringify(devices));
|
||||
console.log("发现新设备:" + JSON.stringify(devices));
|
||||
this.data.searchList = this.data.searchList.concat(
|
||||
devices);
|
||||
if (this.cfg.onDeviceFound) {
|
||||
@ -437,9 +494,9 @@ class BleHelper {
|
||||
|
||||
uni.onBLECharacteristicValueChange((receive) => {
|
||||
//订阅消息
|
||||
console.log("收到订阅消息",receive);
|
||||
let f=this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == receive.deviceId;
|
||||
console.log("收到订阅消息", receive);
|
||||
let f = this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == receive.deviceId;
|
||||
})
|
||||
let dataView = new DataView(receive.value);
|
||||
|
||||
@ -458,24 +515,49 @@ class BleHelper {
|
||||
// 将每个字节转换为对应的字符
|
||||
str += String.fromCharCode(uint8Array[i]);
|
||||
}
|
||||
|
||||
let header = "mac address:";
|
||||
if (str.indexOf(header) == 0) { //650以文本传输mac
|
||||
|
||||
console.log("str=", str);
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.macAddress = str.replace(
|
||||
let macStr = str.replace(
|
||||
header, "");
|
||||
// console.log("收到mac地址:", str)
|
||||
if (macStr.includes(':')) {
|
||||
v.macAddress = macStr;
|
||||
} else {
|
||||
v.macAddress = macStr
|
||||
.replace(
|
||||
/(.{2})/g, '$1:')
|
||||
.slice(0, -1)
|
||||
}
|
||||
str = header + v.macAddress;
|
||||
console.log("收到mac地址:", v
|
||||
.macAddress)
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
.LinkedList);
|
||||
this.updateCache();
|
||||
}
|
||||
|
||||
if (str.indexOf('imei:') == 0) {
|
||||
let imei = str.split(':')[1];
|
||||
|
||||
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.imei=imei;
|
||||
}
|
||||
});
|
||||
this.updateCache();
|
||||
|
||||
}
|
||||
|
||||
if (bytes[0] == 0xFC) { //6155以0xFC开头代表mac地址
|
||||
// console.log("收到mac地址:", bytes)
|
||||
console.log("收到mac地址:", bytes)
|
||||
if (bytes.length >= 7) {
|
||||
console.log("hexs=", hexs);
|
||||
let mac = hexs.slice(1, 7).join(":")
|
||||
.toUpperCase();
|
||||
this.data.LinkedList.find((v) => {
|
||||
@ -485,12 +567,12 @@ class BleHelper {
|
||||
// console.log("收到mac地址:", str)
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this
|
||||
.data.LinkedList);
|
||||
this.updateCache();
|
||||
}
|
||||
}
|
||||
console.log("str=", str);
|
||||
} catch (ex) {
|
||||
////console.log("将数据转文本失败", ex);
|
||||
console.log("将数据转文本失败", ex);
|
||||
}
|
||||
let recData = {
|
||||
deviceId: receive.deviceId,
|
||||
@ -500,23 +582,40 @@ class BleHelper {
|
||||
str: str,
|
||||
hexs: hexs
|
||||
};
|
||||
console.log("监听到特征值:" + JSON.stringify(recData));
|
||||
if (this.cfg.receivDataCallback) {
|
||||
// console.log("监听到特征值:" + JSON.stringify(recData));
|
||||
try {
|
||||
if (this.cfg.receivDataCallback) {
|
||||
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
|
||||
let path = this.getCurrentPagePath();
|
||||
|
||||
this.cfg.receivDataCallback.forEach((rec) => {
|
||||
let path = this.getCurrentPagePath();
|
||||
console.log("有人订阅消息")
|
||||
this.cfg.receivDataCallback.forEach((
|
||||
rec) => {
|
||||
console.log("有人订阅消息111", )
|
||||
if (rec.callback) {
|
||||
try {
|
||||
rec.callback(recData, f,
|
||||
path, this.cfg
|
||||
.receivDataCallback
|
||||
);
|
||||
} catch (err) {
|
||||
console.log("订阅消息出现异常",
|
||||
err);
|
||||
}
|
||||
|
||||
if (rec.callback) {
|
||||
rec.callback(recData, f, path);
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
console.log("无人订阅消息");
|
||||
}
|
||||
} else {
|
||||
console.log("无人订阅receivDataCallback,不处理数据");
|
||||
}
|
||||
} else {
|
||||
console.log("无人订阅receivDataCallback,不处理数据");
|
||||
} catch (ex) {
|
||||
console.log("ex=", ex);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -582,22 +681,43 @@ class BleHelper {
|
||||
var these = this;
|
||||
//开始搜索
|
||||
var Search = () => {
|
||||
console.log("Search........");
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services: [],
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
//console.log('开始搜索蓝牙设备成功');
|
||||
resolve(res);
|
||||
//只搜索合作供应商的服务id
|
||||
let serviceIds = serviceDic.map(v => {
|
||||
return v.serviceId
|
||||
});
|
||||
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log(`搜索蓝牙设备失败:`, err);
|
||||
//搜索一个服务id的设备,循环调用
|
||||
let RunSearch = (serviceId) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services:serviceId? [serviceId]:[],
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
//console.log('开始搜索蓝牙设备成功');
|
||||
resolve(res);
|
||||
|
||||
reject(this.getError(err));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log(`搜索蓝牙设备失败:`, err);
|
||||
|
||||
reject(this.getError(err));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let promises = [];
|
||||
// for (let i = 0; i < serviceIds.length; i++) {
|
||||
|
||||
// promises.push(RunSearch(serviceIds[i]));
|
||||
|
||||
// }
|
||||
promises.push(RunSearch());
|
||||
Promise.all(promises).then(resolve).catch(reject);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -606,13 +726,15 @@ class BleHelper {
|
||||
|
||||
|
||||
return this.OpenBlue().then((res) => {
|
||||
console.log("蓝牙适配器状态", res)
|
||||
// console.log("蓝牙适配器状态", res)
|
||||
|
||||
return Search();
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//停止搜索
|
||||
StopSearch() {
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -656,21 +778,26 @@ class BleHelper {
|
||||
|
||||
//订阅消息
|
||||
subScribe(deviceId, state) {
|
||||
console.log("开始订阅消息");
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
|
||||
let c = this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == deviceId;
|
||||
});
|
||||
|
||||
if (state) {
|
||||
if (c.notifyState) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log("c=", c);
|
||||
let startSubScribe = (id, serviceId, characteristicId) => {
|
||||
|
||||
return new Promise((succ, err) => {
|
||||
let startSubScribe = (id, serviceId, characteristicId) => {
|
||||
console.log("serviceId=", serviceId);
|
||||
console.log("characteristicId=", characteristicId);
|
||||
let p1 = new Promise((succ, err) => {
|
||||
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
deviceId: id,
|
||||
serviceId: serviceId,
|
||||
@ -688,7 +815,10 @@ class BleHelper {
|
||||
v.notifyState = state;
|
||||
}
|
||||
});
|
||||
succ();
|
||||
// console.log("success SubScribe");
|
||||
succ(
|
||||
res
|
||||
); //见了鬼了,有时候执行了succ但promise永远pending了
|
||||
},
|
||||
fail: (ex) => {
|
||||
|
||||
@ -696,40 +826,70 @@ class BleHelper {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let p2 = new Promise((succ, err) => {
|
||||
setTimeout(() => {
|
||||
err({
|
||||
code: -1
|
||||
});
|
||||
}, 50);
|
||||
});
|
||||
return new Promise((succ, err) => {
|
||||
Promise.race([p1, p2]).then(succ).catch(ex => {
|
||||
if (ex.code == -1) {
|
||||
succ();
|
||||
return;
|
||||
}
|
||||
err(ex);
|
||||
})
|
||||
});
|
||||
|
||||
}
|
||||
let promies = new Array();
|
||||
for (var i = 0; i < c.Characteristics.length; i++) {
|
||||
let item = c.Characteristics[i];
|
||||
let serviceId = item.serviceId;
|
||||
let characteristicId = item.uuid;
|
||||
if (c.Characteristics && c.Characteristics.length) {
|
||||
for (var i = 0; i < c.Characteristics.length; i++) {
|
||||
let item = c.Characteristics[i];
|
||||
let serviceId = item.serviceId;
|
||||
let characteristicId = item.uuid;
|
||||
|
||||
if (item.properties.notify) {
|
||||
promies.push(startSubScribe(deviceId, serviceId, characteristicId));
|
||||
if (item.properties.notify) {
|
||||
promies.push(startSubScribe(deviceId, serviceId, characteristicId));
|
||||
|
||||
}
|
||||
}
|
||||
}else{
|
||||
promies.push(startSubScribe(c.deviceId, c.notifyServiceid, c.notifyCharactId));
|
||||
}
|
||||
if (promies.length > 0) {
|
||||
Promise.allSettled(promies).then((results) => {
|
||||
|
||||
if (promies.length > 0) {
|
||||
|
||||
|
||||
Promise.allSettled(promies).then((results) => {
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.status === "fulfilled") {
|
||||
//console.log(`操作${index + 1}成功:`, result.value);
|
||||
// console.log(`操作${index + 1}成功:`, result.value);
|
||||
} else {
|
||||
// console.log(`操作${index + 1}失败:`, result.reason
|
||||
// .message);
|
||||
// console.log(`操作${index + 1}失败:`, result.reason
|
||||
// .message);
|
||||
}
|
||||
});
|
||||
|
||||
// console.log("订阅消息成功");
|
||||
resolve();
|
||||
}).catch((ex) => {
|
||||
console.log("ex=",ex);
|
||||
reject(ex);
|
||||
}).finally(() => {
|
||||
// console.log("finally")
|
||||
});
|
||||
} else {
|
||||
console.log("没有特征需要订阅");
|
||||
resolve();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}, 20);
|
||||
}, 800);
|
||||
|
||||
});
|
||||
}
|
||||
@ -755,17 +915,44 @@ class BleHelper {
|
||||
v.services = res.services;
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey,
|
||||
this.data.LinkedList);
|
||||
//this.updateCache();
|
||||
|
||||
var promises = [];
|
||||
let s = null;
|
||||
let se = res.services.find((v) => {
|
||||
return v.uuid.indexOf(targetServiceId) > -1;
|
||||
|
||||
s = serviceDic.find(k => {
|
||||
|
||||
return k.serviceId == v.uuid;
|
||||
})
|
||||
if (s) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
// return v.uuid.indexOf(targetServiceId) > -1;
|
||||
});
|
||||
|
||||
if (se) {
|
||||
promises.push(this.getFeatrus(id, se.uuid, writeCharId,
|
||||
notifyCharId));
|
||||
console.log("合作供应商的",s)
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == id) {
|
||||
v.writeServiceId = s.serviceId;
|
||||
v.wirteCharactId = s.writeId;
|
||||
v.notifyServiceid = s.serviceId;
|
||||
v.notifyCharactId = s.notifyId;
|
||||
}
|
||||
});
|
||||
this.updateCache();
|
||||
|
||||
//合作供应商的设备,直接订阅消息,不走发现特征了
|
||||
promises.push(this.subScribe(id, true));
|
||||
|
||||
// promises.push(this.getFeatrus(id, se.uuid, writeCharId,
|
||||
// notifyCharId));
|
||||
|
||||
|
||||
} else {
|
||||
console.log("预设的蓝牙服务和特征中找不到");
|
||||
for (var i = 0; i < res.services.length; i++) {
|
||||
let service = res.services[i];
|
||||
promises.push(this.getFeatrus(id, service.uuid,
|
||||
@ -784,13 +971,19 @@ class BleHelper {
|
||||
|
||||
Promise.all(promises)
|
||||
.then(results => {
|
||||
if (!s) {
|
||||
//非指定供应商的设备,走订阅消息
|
||||
return this.subScribe(id, true);
|
||||
} else {
|
||||
//指定供应商的设备已经订阅过了
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return this.subScribe(id, true);
|
||||
})
|
||||
.then((res) => {
|
||||
console.log('设备连接成功,初始化完成', res);
|
||||
console.log("LinkedList=", this.data
|
||||
console.log('设备连接成功,初始化完成', this.data
|
||||
.LinkedList);
|
||||
|
||||
resolve();
|
||||
})
|
||||
.catch(error => {
|
||||
@ -836,28 +1029,47 @@ class BleHelper {
|
||||
deviceId: id,
|
||||
serviceId: serviceId,
|
||||
success: (res) => {
|
||||
|
||||
console.log("获取到特征:" + JSON.stringify(res));
|
||||
res.characteristics.forEach((v) => {
|
||||
v.serviceId = serviceId;
|
||||
});
|
||||
// res.characteristics.forEach((v) => {
|
||||
// v.serviceId = serviceId;
|
||||
// });
|
||||
|
||||
//写特征
|
||||
let writeChar = res.characteristics.find(char => {
|
||||
return char.uuid.indexOf(writeCharId) > -1
|
||||
let fe = serviceDic.find(v => {
|
||||
return v.serviceId == serviceId && v.writeId == char
|
||||
.uuid;
|
||||
})
|
||||
// return char.uuid.indexOf(writeCharId) > -1
|
||||
if (fe) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (!writeChar) {
|
||||
writeChar = res.characteristics.find(char => {
|
||||
return char.properties.write;
|
||||
});
|
||||
}
|
||||
// if (!writeChar) {
|
||||
// writeChar = res.characteristics.find(char => {
|
||||
// return char.properties.write;
|
||||
// });
|
||||
// }
|
||||
//通知特征
|
||||
let notiChar = res.characteristics.find(char => {
|
||||
return char.uuid.indexOf(notifyCharId) > -1;
|
||||
let fe = serviceDic.find(v => {
|
||||
return v.serviceId == serviceId && v.notifyId ==
|
||||
char.uuid;
|
||||
})
|
||||
// return char.uuid.indexOf(writeCharId) > -1
|
||||
if (fe) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
// return char.uuid.indexOf(notifyCharId) > -1;
|
||||
});
|
||||
if (!notiChar) {
|
||||
notiChar = res.characteristics.find(char => {
|
||||
return char.properties.notify;
|
||||
});
|
||||
}
|
||||
// if (!notiChar) {
|
||||
// notiChar = res.characteristics.find(char => {
|
||||
// return char.properties.notify;
|
||||
// });
|
||||
// }
|
||||
this.data.LinkedList.find(function(v) {
|
||||
if (v.deviceId == id) {
|
||||
if (!v.Characteristics) {
|
||||
@ -896,7 +1108,7 @@ class BleHelper {
|
||||
|
||||
|
||||
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
this.updateCache();
|
||||
resolve(res);
|
||||
},
|
||||
fail: (ex) => {
|
||||
@ -909,9 +1121,11 @@ class BleHelper {
|
||||
}
|
||||
|
||||
//连接某个设备
|
||||
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId) {
|
||||
|
||||
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId, maxRetries) {
|
||||
|
||||
if (maxRetries === undefined) {
|
||||
maxRetries = 0; // 最大重试次数
|
||||
}
|
||||
if (!writeCharId) {
|
||||
writeCharId = "xxxx"; // "FFE1";
|
||||
}
|
||||
@ -936,74 +1150,95 @@ class BleHelper {
|
||||
|
||||
//连接设备
|
||||
var linkDevice = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if (fIndex > -1 && f?.Linked) {
|
||||
console.log("当前已连接,跳过其他步骤")
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
if(!this.data.available){
|
||||
reject(this.getError({code:10001}));
|
||||
return;
|
||||
}
|
||||
console.log("正在连接" + deviceId);
|
||||
uni.createBLEConnection({
|
||||
deviceId: deviceId,
|
||||
timeout: 3000,
|
||||
success: (info) => {
|
||||
|
||||
console.log("新连接成功", this.data.LinkedList);
|
||||
this.getLinkBlue().then((arr) => {
|
||||
let cr = arr.devices.find(c => {
|
||||
if (c.deviceId == deviceId) {
|
||||
c.Linked = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
});
|
||||
if (fIndex > -1) {
|
||||
this.data.LinkedList[fIndex].Linked = true;
|
||||
} else {
|
||||
this.data.LinkedList.push(cr);
|
||||
}
|
||||
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
.LinkedList);
|
||||
|
||||
let os = plus.os.name;
|
||||
if (os == 'android') {
|
||||
uni.setBLEMTU({
|
||||
deviceId: deviceId,
|
||||
mtu: 512,
|
||||
success: (mtu) => {
|
||||
|
||||
////console.log("mtu设置成功");
|
||||
},
|
||||
fail: () => {
|
||||
////console.log("mtu设置失败")
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
resolve(true);
|
||||
|
||||
}).catch((ex) => {
|
||||
|
||||
reject(this.getError(ex));
|
||||
});
|
||||
// 添加重试次数限制
|
||||
let retryCount = 0;
|
||||
|
||||
|
||||
},
|
||||
fail: (ex) => {
|
||||
ex = this.getError(ex)
|
||||
console.log("蓝牙连接失败" + JSON.stringify(ex));
|
||||
reject(ex);
|
||||
const connect = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (fIndex > -1 && f?.Linked) {
|
||||
// console.log("当前已连接,跳过其他步骤");
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.data.available) {
|
||||
reject(this.getError({
|
||||
code: 10001
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("正在连接" + deviceId);
|
||||
uni.createBLEConnection({
|
||||
deviceId: deviceId,
|
||||
timeout: 30000,
|
||||
success: (info) => {
|
||||
console.log("新连接成功", this.data.LinkedList);
|
||||
this.getLinkBlue().then((arr) => {
|
||||
let cr = arr.devices.find(c => {
|
||||
if (c.deviceId == deviceId) {
|
||||
c.Linked = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (fIndex > -1) {
|
||||
this.data.LinkedList[fIndex].Linked = true;
|
||||
} else {
|
||||
this.data.LinkedList.push(cr);
|
||||
}
|
||||
|
||||
this.updateCache();
|
||||
console.log("LinkedList=", this.data
|
||||
.LinkedList);
|
||||
|
||||
// 处理 MTU 设置
|
||||
if (plus.os.name === 'Android') {
|
||||
uni.setBLEMTU({
|
||||
deviceId: deviceId,
|
||||
mtu: 512,
|
||||
success: (mtu) => {
|
||||
|
||||
resolve(true);
|
||||
},
|
||||
fail: () => {
|
||||
console.log("mtu设置失败");
|
||||
resolve(
|
||||
true
|
||||
); // MTU设置失败不影响连接成功
|
||||
}
|
||||
});
|
||||
} else {
|
||||
resolve(true);
|
||||
}
|
||||
}).catch((ex) => {
|
||||
reject(this.getError(ex));
|
||||
});
|
||||
},
|
||||
fail: (ex) => {
|
||||
ex = this.getError(ex);
|
||||
console.log("蓝牙" + deviceId + "连接失败" + JSON.stringify(ex));
|
||||
|
||||
// 连接超时后自动重试
|
||||
if (ex.code === 10012 && retryCount < maxRetries) {
|
||||
retryCount++;
|
||||
console.log(`重试连接 (${retryCount}/${maxRetries})`);
|
||||
// 使用 setTimeout 避免递归调用栈溢出
|
||||
setTimeout(() => {
|
||||
connect().then(resolve).catch(reject);
|
||||
}, 1000); // 延迟1秒后重试
|
||||
} else {
|
||||
reject(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return connect();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -1013,19 +1248,32 @@ class BleHelper {
|
||||
////console.log("2222222");
|
||||
return linkDevice(deviceId);
|
||||
}).then((res) => {
|
||||
////console.log("11111111");
|
||||
|
||||
if (res) { //新连接
|
||||
// console.log("11111111");
|
||||
if (fIndex == -1) {
|
||||
console.log("开始获取服务", targetServiceId)
|
||||
// console.log("开始获取服务", targetServiceId)
|
||||
return this.getService(deviceId, targetServiceId, writeCharId,
|
||||
notifyCharId); //获取服务
|
||||
notifyCharId); //获取服务
|
||||
} else {
|
||||
if (!f.notifyState) {
|
||||
this.subScribe(deviceId, true);
|
||||
if (f.wirteCharactId && f.notifyCharactId) {
|
||||
if (!f.notifyState) {
|
||||
// console.log("开始订阅特征");
|
||||
this.subScribe(deviceId, true);
|
||||
} else {
|
||||
console.log("不订阅消息");
|
||||
}
|
||||
return Promise.resolve(true);
|
||||
} else {
|
||||
console.log("开始获取服务", targetServiceId)
|
||||
return this.getService(deviceId, targetServiceId, writeCharId,
|
||||
notifyCharId);
|
||||
}
|
||||
return Promise.resolve(true);
|
||||
|
||||
|
||||
}
|
||||
} else { //已连接过,直接订阅消息
|
||||
} else { //已连接过,直接订阅消息
|
||||
// console.log("11111111");
|
||||
if (fIndex > -1 && f && !f.notifyState) {
|
||||
|
||||
this.subScribe(deviceId, true);
|
||||
@ -1034,13 +1282,13 @@ class BleHelper {
|
||||
}
|
||||
|
||||
}).then(() => {
|
||||
|
||||
// console.log("then.....");
|
||||
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, 500);
|
||||
}).catch((ex) => {
|
||||
////console.log("出现异常", ex);
|
||||
console.log("出现异常", ex);
|
||||
reject(ex);
|
||||
});
|
||||
});
|
||||
@ -1156,7 +1404,7 @@ class BleHelper {
|
||||
characteristicId: device.wirteCharactId,
|
||||
value: buffer,
|
||||
success: () => {
|
||||
console.log("发送数据成功");
|
||||
// console.log("发送数据成功");
|
||||
succ();
|
||||
},
|
||||
fail: (ex) => {
|
||||
@ -1181,7 +1429,7 @@ class BleHelper {
|
||||
}
|
||||
|
||||
Promise.race([timeOut(ms), promise]).then(resolve).catch((ex) => {
|
||||
console.log("ex=", ex);
|
||||
// console.log("ex=", ex);
|
||||
if (ex.code == -1) {
|
||||
resolve(ex);
|
||||
} else {
|
||||
@ -1244,7 +1492,7 @@ class BleHelper {
|
||||
let a = pixels[i + 3];
|
||||
|
||||
if (type == 'bgr') {
|
||||
result[index++] = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
|
||||
result[index++] = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
|
||||
} else {
|
||||
result[index++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
|
||||
}
|
||||
@ -1254,9 +1502,7 @@ class BleHelper {
|
||||
return result;
|
||||
}
|
||||
|
||||
setBleData() {
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let instance = null;
|
||||
|
||||
@ -44,30 +44,33 @@ class BleReceive {
|
||||
}
|
||||
|
||||
|
||||
ReceiveData(receive,f,path) {
|
||||
if(f && f.macAddress && f.id){
|
||||
ReceiveData(receive,f,path,recArr) {
|
||||
if(f && f.macAddress && f.device && f.device.id){
|
||||
let data={};
|
||||
if(f.detailPageUrl=='/pages/6155/deviceDetail'){
|
||||
console.log("该设备是6155");
|
||||
data= this.Receive_6155(receive,f,path);
|
||||
if(f.device.detailPageUrl=='/pages/6155/deviceDetail'){
|
||||
// console.log("该设备是6155");
|
||||
data= this.Receive_6155(receive,f,path,recArr);
|
||||
}
|
||||
|
||||
if(f.detailPageUrl=='/pages/650/HBY650'){
|
||||
console.log("该设备是650");
|
||||
data= this.Receive_650(receive,f,path);
|
||||
if(f.device.detailPageUrl=='/pages/650/HBY650'){
|
||||
// console.log("该设备是650");
|
||||
data= this.Receive_650(receive,f,path,recArr);
|
||||
}
|
||||
if(f.detailPageUrl=='/pages/670/HBY670'){
|
||||
console.log("该设备是670");
|
||||
data= this.Receive_670(receive,f,path);
|
||||
if(f.device.detailPageUrl=='/pages/670/HBY670'){
|
||||
// console.log("该设备是670");
|
||||
data= this.Receive_670(receive,f,path,recArr);
|
||||
}
|
||||
console.log("收到数据并处理完毕,",data);
|
||||
// console.log("收到数据并处理完毕,",data);
|
||||
return data;
|
||||
}else{
|
||||
console.log("已收到该消息,但无法处理",receive);
|
||||
}
|
||||
console.log("已收到该消息,但无法处理",f);
|
||||
|
||||
return receive;
|
||||
|
||||
}
|
||||
|
||||
Receive_650(receive,f,path) {
|
||||
Receive_650(receive,f,path,recArr) {
|
||||
console.log("通用程序正在处理650的数据",receive);
|
||||
|
||||
var parseData = () => {
|
||||
@ -93,7 +96,7 @@ class BleReceive {
|
||||
staticLevelText = '低档';
|
||||
modeCurr = "low";
|
||||
break;
|
||||
case 0x68:
|
||||
case 0x64:
|
||||
staticLevelText = '关闭';
|
||||
modeCurr = "close";
|
||||
break;
|
||||
@ -250,14 +253,14 @@ class BleReceive {
|
||||
|
||||
}
|
||||
|
||||
Receive_670(receive,f,path){
|
||||
|
||||
Receive_670(receive,f,path,recArr){
|
||||
console.log("pagh=",path);
|
||||
var todo = (bytes) =>{
|
||||
console.log("todo");
|
||||
// console.log("todo",receive);
|
||||
let receiveData = {};
|
||||
if (bytes[0] == 0x55) {
|
||||
try {
|
||||
console.log("todo");
|
||||
// console.log("todo");
|
||||
// 跳过帧头(第一个字节),从第二个字节开始解析
|
||||
let staticLevelByte = bytes[1];
|
||||
let staticLevelText = '';
|
||||
@ -271,11 +274,11 @@ class BleReceive {
|
||||
case 0x67:
|
||||
staticLevelText = 'low';
|
||||
break
|
||||
case 0x68:
|
||||
case 0x64:
|
||||
staticLevelText = 'close';
|
||||
break
|
||||
}
|
||||
console.log("todo");
|
||||
// console.log("todo");
|
||||
// 解析照明档位
|
||||
let lightingLevelByte = bytes[2];
|
||||
let lightingLevelText = lightingLevelByte === 0x6d ? 'hight': lightingLevelByte === 0x6e ? 'low': 'close';
|
||||
@ -287,7 +290,7 @@ class BleReceive {
|
||||
let batteryLevelByte = bytes[5];
|
||||
|
||||
let batteryLevel = Math.max(0, Math.min(100, batteryLevelByte));
|
||||
console.log("todo");
|
||||
// console.log("todo");
|
||||
let warn = bytes[6];
|
||||
if (warn == 0x00) {
|
||||
warn = 'none';
|
||||
@ -304,7 +307,7 @@ class BleReceive {
|
||||
let staticWarn = bytes[7] == 0x01;//静止报警
|
||||
let fourGStrenth = bytes[8]; //4g信号强度
|
||||
let sosTxt = bytes[9] == 0x00 ? 'close' : bytes[9] == 0x01 ? 'sg' : 'rb';
|
||||
console.log("todo");
|
||||
// console.log("todo");
|
||||
receiveData.modeCurr = staticLevelText;
|
||||
receiveData.lightCurr = lightingLevelText;
|
||||
receiveData.xuhang = lightingTime ;
|
||||
@ -313,10 +316,25 @@ class BleReceive {
|
||||
receiveData.staticWarn = staticWarn;
|
||||
receiveData.fourGStrenth = fourGStrenth;
|
||||
receiveData.SOS=sosTxt;
|
||||
|
||||
this.setBleFormData(receiveData,f);
|
||||
console.log("recArr=",recArr);
|
||||
let recCnt=recArr.find(v=>{
|
||||
return v.key == f.device.detailPageUrl;
|
||||
});
|
||||
if(!recCnt){
|
||||
if (this.formData.staticWarn) { //有静止报警
|
||||
uni.showModal({
|
||||
title:"警告",
|
||||
content:"设备静止报警中",
|
||||
showCancel:false
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch(error) {
|
||||
console.log('数据解析错误:', error);
|
||||
}
|
||||
console.log("todo");
|
||||
// console.log("todo");
|
||||
} else {
|
||||
try {
|
||||
let uint8Array = new Uint8Array(receive.value);
|
||||
@ -340,6 +358,7 @@ class BleReceive {
|
||||
console.log('收到纬度:', +this.receiveData.macAddress);
|
||||
} else {
|
||||
try {
|
||||
// console.log("str=",str);
|
||||
let json=JSON.parse(str);
|
||||
if("staBlue_picture" in json){
|
||||
//重发图片
|
||||
@ -372,7 +391,7 @@ class BleReceive {
|
||||
|
||||
} catch (error) {
|
||||
receiveData={};
|
||||
console.log("文本解析失败")
|
||||
// console.log("文本解析失败",error)
|
||||
}
|
||||
}
|
||||
} catch(ex) {
|
||||
@ -380,7 +399,7 @@ class BleReceive {
|
||||
console.log('将数据转文本失败', ex);
|
||||
}
|
||||
}
|
||||
console.log("todo",receiveData);
|
||||
// console.log("todo",receiveData);
|
||||
this.setBleFormData(receiveData,f);
|
||||
return receiveData;
|
||||
}
|
||||
@ -390,8 +409,81 @@ class BleReceive {
|
||||
|
||||
return data;
|
||||
}
|
||||
Receive_6155() {
|
||||
console.log("通用程序正在处理6155的数据");
|
||||
Receive_6155(receive,f,path,recArr) {
|
||||
let bytes=receive.bytes;
|
||||
if (bytes[0] == 0xFB && bytes[1] == 0x64 && bytes.length >= 8) {
|
||||
try {
|
||||
|
||||
let staticLevelByte = bytes[2];
|
||||
let getName = function(type) {
|
||||
let name = "";
|
||||
switch (type) {
|
||||
case 0x02:
|
||||
name = '弱光';
|
||||
break;
|
||||
case 0x04:
|
||||
name = '工作光';
|
||||
break;
|
||||
case 0x01:
|
||||
name = '强光';
|
||||
break;
|
||||
case 0x03:
|
||||
name = '爆闪';
|
||||
break;
|
||||
case 0x00:
|
||||
name = '关闭';
|
||||
break;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
let staticLevelText = getName(staticLevelByte);
|
||||
|
||||
// 解析照明档位
|
||||
let lightingLevelByte = bytes[3];
|
||||
let lightingLevelText = getName(lightingLevelByte);
|
||||
|
||||
|
||||
// 解析剩余电量
|
||||
let batteryLevelByte = bytes[4];
|
||||
// 电量百分比范围检查
|
||||
let batteryLevel = Math.max(0, Math.min(100, batteryLevelByte));
|
||||
|
||||
|
||||
|
||||
|
||||
//充电状态
|
||||
let warn = bytes[5];
|
||||
if (warn == 0x00) {
|
||||
warn = '未充电';
|
||||
} else if (warn == 0x01) {
|
||||
warn = '充电中';
|
||||
}
|
||||
|
||||
// 解析剩余照明时间(第三和第四字节,小端序)
|
||||
let lightingTime = "";
|
||||
let HH = Math.max(0, Math.min(100, bytes[6]));
|
||||
let mm = Math.max(0, Math.min(100, bytes[7]));
|
||||
lightingTime = HH + "小时" + mm + "分钟";
|
||||
let formData={};
|
||||
formData.mode = staticLevelText;
|
||||
formData.fuMode = lightingLevelText;
|
||||
formData.battary = batteryLevel;
|
||||
formData.statu = warn;
|
||||
formData.xuhang = lightingTime;
|
||||
|
||||
if (batteryLevel <= 20) {
|
||||
uni.showModal({
|
||||
content:"设备电量低",
|
||||
title:"提示"
|
||||
});
|
||||
|
||||
}
|
||||
this.setBleFormData(formData,f);
|
||||
return formData;
|
||||
} catch (error) {
|
||||
console.log('数据解析错误:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
440
utils/Common.js
440
utils/Common.js
@ -1,218 +1,284 @@
|
||||
var cfg={
|
||||
Version:'Uat',//Dev:开发环境,Uat:Uat环境,Relese正式环境
|
||||
DevApi:'http://192.168.110.54:8000/',//开发环境
|
||||
UatApi:'http://114.55.111.217/',//UAT环境
|
||||
ReleseApi:'http://relese:3169/api/'//Relese环境
|
||||
}
|
||||
import request from "@/utils/request.js";
|
||||
export default {
|
||||
baseURL : cfg.Version=='Dev'?cfg.DevApi:(cfg.Version=='Uat'?cfg.UatApi:cfg.ReleseApi),
|
||||
guid:function generateUUID() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
},
|
||||
alert: function(title, content, callback) {
|
||||
if(!title){
|
||||
title='提示'
|
||||
}
|
||||
if(!content){
|
||||
content=title;
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: title,
|
||||
content: content,
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
console.log('用户点击确定');
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(res);
|
||||
}
|
||||
guid: function generateUUID() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
},
|
||||
alert: function(title, content, callback) {
|
||||
if (!title) {
|
||||
title = '提示'
|
||||
}
|
||||
if (!content) {
|
||||
content = title;
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: title,
|
||||
content: content,
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
console.log('用户点击确定');
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
showLoading:function(title,mask){
|
||||
uni.showLoading({
|
||||
title:title,
|
||||
mask:mask,
|
||||
})
|
||||
},
|
||||
hideLoading:function(){
|
||||
uni.hideLoading();
|
||||
},
|
||||
showToast:function(title,mask,duration,callback){
|
||||
if(!duration){
|
||||
duration=1500;
|
||||
}
|
||||
if(mask==undefined){
|
||||
mask=false;
|
||||
}
|
||||
uni.showToast({
|
||||
title:title,
|
||||
mask:mask,
|
||||
duration:duration,
|
||||
callback:callback,
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
GetData:function(url,data,method,contentType,succ,err,complete){
|
||||
var these=this;
|
||||
if(!url){
|
||||
console.error("url为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if(url.toLowerCase().indexOf('http://')==-1 || url.toLowerCase().indexOf('https://')==-1){
|
||||
if(url.indexOf('/')==0){
|
||||
url=url.substr(1,url.length-1);
|
||||
|
||||
if (callback) {
|
||||
callback(res);
|
||||
}
|
||||
let ServerPath=these.DevApi;
|
||||
if(these.Version==='Dev'){
|
||||
ServerPath=these.DevApi;
|
||||
}
|
||||
else if(these.Version==='Uat'){
|
||||
ServerPath=these.UatApi;
|
||||
}
|
||||
else if(these.Version==='Relese'){
|
||||
ServerPath=these.ReleseApi;
|
||||
}else{
|
||||
these.DevApi
|
||||
}
|
||||
url=ServerPath+url;
|
||||
}
|
||||
|
||||
var these=this;
|
||||
if(!method){
|
||||
method='POST';
|
||||
});
|
||||
},
|
||||
|
||||
showLoading: function(title, mask) {
|
||||
uni.showLoading({
|
||||
title: title,
|
||||
mask: mask,
|
||||
})
|
||||
},
|
||||
hideLoading: function() {
|
||||
uni.hideLoading();
|
||||
},
|
||||
showToast: function(title, mask, duration, callback) {
|
||||
if (!duration) {
|
||||
duration = 1500;
|
||||
}
|
||||
if (mask == undefined) {
|
||||
mask = false;
|
||||
}
|
||||
uni.showToast({
|
||||
title: title,
|
||||
mask: mask,
|
||||
duration: duration,
|
||||
callback: callback,
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
GetData: function(url, data, method, contentType, succ, err, complete) {
|
||||
var these = this;
|
||||
if (!url) {
|
||||
console.error("url为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.toLowerCase().indexOf('http://') == -1 || url.toLowerCase().indexOf('https://') == -1) {
|
||||
if (url.indexOf('/') == 0) {
|
||||
url = url.substr(1, url.length - 1);
|
||||
}
|
||||
method=method.toUpperCase();
|
||||
|
||||
if(!contentType){
|
||||
contentType='application/json;charset=UTF-8';
|
||||
let ServerPath = these.DevApi;
|
||||
if (these.Version === 'Dev') {
|
||||
ServerPath = these.DevApi;
|
||||
} else if (these.Version === 'Uat') {
|
||||
ServerPath = these.UatApi;
|
||||
} else if (these.Version === 'Relese') {
|
||||
ServerPath = these.ReleseApi;
|
||||
} else {
|
||||
these.DevApi
|
||||
}
|
||||
|
||||
these.checkLAN(
|
||||
|
||||
function(){
|
||||
these.showLoading('请稍候..',true);
|
||||
setTimeout(function(){
|
||||
url = ServerPath + url;
|
||||
}
|
||||
|
||||
var these = this;
|
||||
if (!method) {
|
||||
method = 'POST';
|
||||
}
|
||||
method = method.toUpperCase();
|
||||
|
||||
if (!contentType) {
|
||||
contentType = 'application/json;charset=UTF-8';
|
||||
}
|
||||
|
||||
these.checkLAN(
|
||||
|
||||
function() {
|
||||
these.showLoading('请稍候..', true);
|
||||
setTimeout(function() {
|
||||
uni.request({
|
||||
url:url,
|
||||
data:data,
|
||||
header:{
|
||||
"Content-Type":contentType
|
||||
url: url,
|
||||
data: data,
|
||||
header: {
|
||||
"Content-Type": contentType
|
||||
},
|
||||
method:method,
|
||||
timeout:60000,
|
||||
dataType:'json',
|
||||
success:function(json){
|
||||
|
||||
if(succ){
|
||||
method: method,
|
||||
timeout: 60000,
|
||||
dataType: 'json',
|
||||
success: function(json) {
|
||||
|
||||
if (succ) {
|
||||
succ(json);
|
||||
}
|
||||
},
|
||||
fail:function(ex){
|
||||
|
||||
if(err){
|
||||
fail: function(ex) {
|
||||
|
||||
if (err) {
|
||||
err(ex);
|
||||
}
|
||||
},
|
||||
complete:function(){
|
||||
|
||||
if(complete){
|
||||
complete: function() {
|
||||
|
||||
if (complete) {
|
||||
complete();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
},0);
|
||||
|
||||
|
||||
}, 0);
|
||||
|
||||
}
|
||||
|
||||
,function(){
|
||||
|
||||
,
|
||||
function() {
|
||||
these.showToast('无网络连接');
|
||||
});
|
||||
},
|
||||
checkLAN:function(succ,error){
|
||||
uni.getNetworkType({
|
||||
success: (res) => {
|
||||
const networkType = res.networkType;
|
||||
|
||||
|
||||
// 判断网络是否连接
|
||||
if (networkType === 'none') {
|
||||
},
|
||||
checkLAN: function(succ, error) {
|
||||
uni.getNetworkType({
|
||||
success: (res) => {
|
||||
const networkType = res.networkType;
|
||||
|
||||
|
||||
// 判断网络是否连接
|
||||
if (networkType === 'none') {
|
||||
console.error('无网络连接')
|
||||
if(error){
|
||||
error();
|
||||
}
|
||||
}else{
|
||||
if(succ){
|
||||
if (error) {
|
||||
error();
|
||||
}
|
||||
} else {
|
||||
if (succ) {
|
||||
succ();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('获取网络状态失败:', err);
|
||||
if(error){
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('获取网络状态失败:', err);
|
||||
if (error) {
|
||||
error();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
DateFormat: function(date, format) {
|
||||
if(!date){
|
||||
date=new Date();
|
||||
}
|
||||
if(!format){
|
||||
format='yyyy-MM-dd HH:mm:ss';
|
||||
}
|
||||
// 处理参数默认值
|
||||
if (typeof date === 'string' || typeof date === 'number') {
|
||||
date = new Date(date);
|
||||
}
|
||||
date = date instanceof Date ? date : new Date();
|
||||
format = format || 'yyyy-MM-dd';
|
||||
|
||||
// 检查日期是否有效
|
||||
if (isNaN(date.getTime())) {
|
||||
return 'Invalid Date';
|
||||
}
|
||||
|
||||
// 定义格式化映射
|
||||
const formatMap = {
|
||||
'yyyy': date.getFullYear(),
|
||||
'MM': String(date.getMonth() + 1).padStart(2, '0'),
|
||||
'dd': String(date.getDate()).padStart(2, '0'),
|
||||
'HH': String(date.getHours()).padStart(2, '0'),
|
||||
'mm': String(date.getMinutes()).padStart(2, '0'),
|
||||
'ss': String(date.getSeconds()).padStart(2, '0'),
|
||||
'SSS': String(date.getMilliseconds()).padStart(3, '0'),
|
||||
'M': date.getMonth() + 1,
|
||||
'd': date.getDate(),
|
||||
'H': date.getHours(),
|
||||
'm': date.getMinutes(),
|
||||
's': date.getSeconds(),
|
||||
'S': date.getMilliseconds()
|
||||
};
|
||||
|
||||
// 替换格式字符串中的占位符
|
||||
return format.replace(/(yyyy|MM|dd|HH|mm|ss|SSS|M|d|H|m|s|S)/g, (match) => {
|
||||
return formatMap[match];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
},
|
||||
DateFormat: function(date, format) {
|
||||
if (!date) {
|
||||
date = new Date();
|
||||
}
|
||||
if (!format) {
|
||||
format = 'yyyy-MM-dd HH:mm:ss';
|
||||
}
|
||||
// 处理参数默认值
|
||||
if (typeof date === 'string' || typeof date === 'number') {
|
||||
date = new Date(date);
|
||||
}
|
||||
date = date instanceof Date ? date : new Date();
|
||||
format = format || 'yyyy-MM-dd';
|
||||
|
||||
// 检查日期是否有效
|
||||
if (isNaN(date.getTime())) {
|
||||
return 'Invalid Date';
|
||||
}
|
||||
|
||||
// 定义格式化映射
|
||||
const formatMap = {
|
||||
'yyyy': date.getFullYear(),
|
||||
'MM': String(date.getMonth() + 1).padStart(2, '0'),
|
||||
'dd': String(date.getDate()).padStart(2, '0'),
|
||||
'HH': String(date.getHours()).padStart(2, '0'),
|
||||
'mm': String(date.getMinutes()).padStart(2, '0'),
|
||||
'ss': String(date.getSeconds()).padStart(2, '0'),
|
||||
'SSS': String(date.getMilliseconds()).padStart(3, '0'),
|
||||
'M': date.getMonth() + 1,
|
||||
'd': date.getDate(),
|
||||
'H': date.getHours(),
|
||||
'm': date.getMinutes(),
|
||||
's': date.getSeconds(),
|
||||
'S': date.getMilliseconds()
|
||||
};
|
||||
|
||||
// 替换格式字符串中的占位符
|
||||
return format.replace(/(yyyy|MM|dd|HH|mm|ss|SSS|M|d|H|m|s|S)/g, (match) => {
|
||||
return formatMap[match];
|
||||
});
|
||||
},
|
||||
getdeviceShareId(id) {//获取设备分享信息
|
||||
return request({
|
||||
url: `/app/deviceShare/${id}`,
|
||||
method: 'get',
|
||||
})
|
||||
},
|
||||
getPermissions(type) {
|
||||
if (!type) {
|
||||
type='6170';
|
||||
}
|
||||
let array = [{
|
||||
value: "1",
|
||||
label: "灯光模式",
|
||||
checked: false,
|
||||
type: ['210', '6170', '670']
|
||||
},
|
||||
{
|
||||
value: "2",
|
||||
label: "激光模式",
|
||||
checked: false,
|
||||
type: ['210', '6170']
|
||||
},
|
||||
{
|
||||
value: "3",
|
||||
label: "开机画面",
|
||||
checked: false,
|
||||
type: ['210', '6170', '670']
|
||||
},
|
||||
{
|
||||
value: "4",
|
||||
label: "人员信息登记",
|
||||
checked: false,
|
||||
type: ['210', '6170', '670']
|
||||
},
|
||||
{
|
||||
value: "5",
|
||||
label: "发送信息",
|
||||
checked: false,
|
||||
type: ['210', '6170', '670']
|
||||
},
|
||||
{
|
||||
value: "6",
|
||||
label: "产品信息",
|
||||
checked: false,
|
||||
type: ['210', '6170', '670']
|
||||
}, {
|
||||
value: "41",
|
||||
label: "静电探测",
|
||||
checked: false,
|
||||
type: ['670']
|
||||
}, {
|
||||
value: "42",
|
||||
label: "SOS",
|
||||
checked: false,
|
||||
type: ['670']
|
||||
}
|
||||
]
|
||||
|
||||
let arr=[];
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
let item = array[i];
|
||||
if(!item){
|
||||
continue;
|
||||
}
|
||||
if(!item.type){
|
||||
continue;
|
||||
}
|
||||
let typeContais=item.type.find(v=>{
|
||||
return v.includes(type);
|
||||
});
|
||||
if(typeContais){
|
||||
let json={};
|
||||
Object.assign(json,item);
|
||||
arr.push(json);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
@ -1,44 +1,42 @@
|
||||
// utils/loading.js
|
||||
|
||||
// 显示loading
|
||||
// utils/loading.js
|
||||
|
||||
// 显示loading
|
||||
export const showLoading = (ev,options) => {
|
||||
if(!ev){
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
let defaultTxt="请稍候...";
|
||||
if(!options){
|
||||
options={a:1};
|
||||
options={text:defaultTxt};
|
||||
}
|
||||
if(!options.text && options.title){
|
||||
options.text=options.title;
|
||||
}
|
||||
}
|
||||
if(!options.text){
|
||||
options.text="请稍候...";
|
||||
options.text=defaultTxt;
|
||||
}
|
||||
ev.$refs.loading.show(options);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 隐藏loading
|
||||
}
|
||||
|
||||
// 隐藏loading
|
||||
export const hideLoading = (ev) => {
|
||||
|
||||
if(!ev){
|
||||
return;
|
||||
}
|
||||
ev.$refs.loading.hide();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 更新loading配置
|
||||
}
|
||||
ev.$refs.loading.hide();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 更新loading配置
|
||||
export const updateLoading = (ev,options) => {
|
||||
if(!ev){
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(!options){
|
||||
options={a:1};
|
||||
}
|
||||
ev.$refs.loading.update(options)
|
||||
|
||||
}
|
||||
ev.$refs.loading.update(options)
|
||||
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
// 兼容性补丁:为 Paho MQTT 提供 uni-app 环境下的 WebSocket 实现
|
||||
(function(root) {
|
||||
// 如果未能找到全局对象,则无法应用补丁。
|
||||
console.log("root=",root);
|
||||
if (!root) {
|
||||
console.error("MQTT Polyfill: 未能找到全局对象 (global/window/self 均未定义)。");
|
||||
return;
|
||||
@ -118,20 +117,20 @@
|
||||
*/
|
||||
import Paho from 'paho-mqtt';
|
||||
import allConfigs from '../config/index.js';
|
||||
|
||||
// 根据环境选择正确的配置
|
||||
const env = 'production'; //production //开发of线上 改这里就行
|
||||
const config = allConfigs[env];
|
||||
|
||||
const env = 'production'; //production development
|
||||
const envConfig = allConfigs[env];
|
||||
const mqttProtocol = env === 'production' ? 'wss' : 'ws';
|
||||
const useSSL = env === 'production';
|
||||
class MqttClient {
|
||||
constructor() {
|
||||
this.client = null;
|
||||
this.options = {
|
||||
host: config.MQTT_HOST,
|
||||
port: config.MQTT_PORT,
|
||||
host: envConfig.MQTT_HOST,
|
||||
port: envConfig.MQTT_PORT,
|
||||
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
|
||||
username: config.MQTT_USERNAME,
|
||||
password: config.MQTT_PASSWORD,
|
||||
username: envConfig.MQTT_USERNAME,
|
||||
password: envConfig.MQTT_PASSWORD,
|
||||
};
|
||||
this.onConnectCallback = null;
|
||||
this.messageCallbacks = new Map();
|
||||
@ -166,18 +165,22 @@ class MqttClient {
|
||||
|
||||
this.client.onMessageArrived = (message) => {
|
||||
const topic = message.destinationName;
|
||||
const payload = message.payloadString;
|
||||
// console.log(`收到消息, 主题: ${topic}, 内容: ${payload}`);
|
||||
const payload = message.payloadString;
|
||||
console.log(`收到消息, 主题: ${topic}, 内容: ${payload}`);
|
||||
const potentialJsons = payload.replace(/}\s*{/g, '}|{').split('|');
|
||||
potentialJsons.forEach(jsonString => {
|
||||
if (jsonString.trim() === '') return;
|
||||
if (this.messageCallbacks.has(topic)) {
|
||||
this.messageCallbacks.get(topic)(jsonString,message);
|
||||
this.messageCallbacks.get(topic)(jsonString, message);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
isConnected() {
|
||||
return this.client && this.client.isConnected();
|
||||
}
|
||||
|
||||
connect(onConnectCallback) {
|
||||
if (this.client && this.client.isConnected()) {
|
||||
console.log('MQTT客户端已连接。');
|
||||
@ -187,16 +190,18 @@ class MqttClient {
|
||||
|
||||
this.manualDisconnect = false;
|
||||
this.onConnectCallback = onConnectCallback;
|
||||
|
||||
console.log(`正在连接MQTT: ${this.options.host}:${this.options.port}/mqtt`);
|
||||
|
||||
console.log(`环境识别,http https:${env}`, {
|
||||
connectUrl: `${mqttProtocol}://${this.options.host}:${this.options.port}/mqtt`,
|
||||
useSSL: useSSL,
|
||||
clientId: this.options.clientId
|
||||
});
|
||||
try {
|
||||
const connectOptions = {
|
||||
timeout: 10, // 增加连接超时时间,应对网络波动
|
||||
keepAliveInterval: 30, // 明确设置心跳间隔为30秒
|
||||
userName: this.options.username,
|
||||
password: this.options.password,
|
||||
useSSL: false,
|
||||
useSSL: useSSL, //http ws https wss开启
|
||||
cleanSession: true,
|
||||
onSuccess: () => {
|
||||
console.log('MQTT连接成功');
|
||||
|
||||
@ -1,17 +1,11 @@
|
||||
import config from '../config/index.js';
|
||||
const env = 'production'; //production development //开发of线上 改这里就行
|
||||
const env = 'development'; //production development //开发of线上 改这里就行
|
||||
const BASE = config[env];
|
||||
const request = (options) => {
|
||||
console.log("options"+JSON.stringify(options),BASE.BASE_URL)
|
||||
return new Promise((resolve, reject) => {
|
||||
// 处理GET请求参数
|
||||
let url =options.url;
|
||||
if(url.indexOf('http://')===0 || url.indexOf('https://')===0){
|
||||
url=url;
|
||||
}else{
|
||||
url=BASE.BASE_URL + url;
|
||||
}
|
||||
|
||||
let url = BASE.BASE_URL + options.url;
|
||||
console.log("url"+url)
|
||||
if (options.method === 'GET' && options.data) {
|
||||
// 使用qs序列化参数
|
||||
@ -28,11 +22,11 @@ const request = (options) => {
|
||||
header: options.header || {},
|
||||
timeout: 30000,
|
||||
success: (res) => {
|
||||
console.log("请求成功",res);
|
||||
// console.log("res=",res);
|
||||
resolve(res.data);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log("请求失败",err);
|
||||
console.log("ex=",err);
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
|
||||
179
utils/update.js
179
utils/update.js
@ -1,65 +1,156 @@
|
||||
import request, {
|
||||
baseURL
|
||||
} from '@/utils/request.js'
|
||||
|
||||
/**
|
||||
* 检查并执行wgt热更新
|
||||
* @param {String} updateUrl - 检查更新的接口地址
|
||||
*/
|
||||
function checkAndUpdateWgt(updateUrl) {
|
||||
if(!plus){
|
||||
if (!plus) {
|
||||
return;
|
||||
}
|
||||
// 显示加载提示
|
||||
|
||||
let urls = ['http://114.55.111.217/app/CheckUpdate', 'https://t3v3e2xsjv.by.takin.cc/app/CheckUpdate'];
|
||||
|
||||
|
||||
|
||||
|
||||
// 1. 获取当前应用版本信息
|
||||
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
|
||||
const currentVersion = widgetInfo.version;
|
||||
console.log("当前版本:" + currentVersion);
|
||||
// 2. 调用后端接口检查是否有更新
|
||||
uni.request({
|
||||
url: updateUrl,
|
||||
method: 'GET',
|
||||
data: {
|
||||
currentVersion: currentVersion,
|
||||
platform: uni.getSystemInfoSync().platform
|
||||
},
|
||||
success: (res) => {
|
||||
uni.hideLoading();
|
||||
console.log("res=", res)
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
const updateInfo = res.data.data;
|
||||
if (!updateInfo.hasUpdate) {
|
||||
return;
|
||||
}
|
||||
// 3. 显示更新提示
|
||||
uni.showModal({
|
||||
title: '检测到更新',
|
||||
content: updateInfo.description || '有新版本可用,是否立即更新?',
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后更新',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
downloadAndInstallWgt(updateInfo.downloadUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '当前已是最新版本',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
|
||||
|
||||
let callbck = (res) => {
|
||||
uni.hideLoading();
|
||||
// console.log("检查更新成功=", res)
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
const updateInfo = res.data.data;
|
||||
if (!updateInfo.hasUpdate) {
|
||||
return;
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
// 3. 显示更新提示
|
||||
uni.showModal({
|
||||
title: '检测到更新',
|
||||
content: updateInfo.description || '有新版本可用,是否立即更新?',
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后更新',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
downloadAndInstallWgt(updateInfo.downloadUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '检查更新失败',
|
||||
title: '当前已是最新版本',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
console.error('检查更新失败:', err);
|
||||
}
|
||||
};
|
||||
|
||||
let Update = (url) => {
|
||||
console.log("url=",url);
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: url,
|
||||
method: 'GET',
|
||||
data: {
|
||||
currentVersion: currentVersion,
|
||||
platform: uni.getSystemInfoSync().platform,
|
||||
appId:"xhyc"
|
||||
},
|
||||
success: (res) => {
|
||||
// console.log("res=>",res)
|
||||
res.type='1';
|
||||
resolve(res);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log("err=",err);
|
||||
reject(err);
|
||||
},
|
||||
complete:()=>{
|
||||
console.log("complete");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
let Callback1 = (res) => {
|
||||
console.log("检查版本更新:", res);
|
||||
let os = plus.os.name.toLowerCase();
|
||||
if (res.code != 200) {
|
||||
return;
|
||||
}
|
||||
let f = res.data.find(v => {
|
||||
if (v.dictLabel.toLowerCase() == os) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (f) {
|
||||
if (f.dictValue == currentVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: '检测到更新',
|
||||
content: '当前版本“' + currentVersion + '”,发现新版本“' + f.dictValue + '”,是否立即更新?',
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后更新',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
downloadAndInstallWgt(f.remark);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let Update1 = () => {
|
||||
return request({
|
||||
url: '/app/auth/version',
|
||||
method: 'get'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
let promises = [];
|
||||
for (var i = 0; i < urls.length; i++) {
|
||||
promises.push(Update(urls[i]));
|
||||
}
|
||||
promises.push(Update1());
|
||||
|
||||
Promise.allSettled(promises).then(results => {
|
||||
let length = results.length;
|
||||
let flag=false;
|
||||
for (var i = 0; i < length; i++) {
|
||||
console.log('results['+i+']=',results[i]);
|
||||
if (results[i].status == 'fulfilled' && results[i].value.type==='1' && !flag ) {
|
||||
flag=true;
|
||||
callbck(results[i].value)
|
||||
|
||||
// break;
|
||||
}
|
||||
|
||||
if(results[i].status == 'fulfilled' && results[i].value.type!=='1' && !flag){
|
||||
flag=true;
|
||||
Callback1(results[i].value);
|
||||
// break;
|
||||
}
|
||||
}
|
||||
if(!flag){
|
||||
console.log("检查更新失败了");
|
||||
}else{
|
||||
console.log("检查更新成功");
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -69,8 +160,8 @@ function checkAndUpdateWgt(updateUrl) {
|
||||
*/
|
||||
function downloadAndInstallWgt(wgtUrl) {
|
||||
// 显示下载进度
|
||||
var wating=plus.nativeUI.showWaiting({
|
||||
title:"下载中0%"
|
||||
var wating = plus.nativeUI.showWaiting({
|
||||
title: "下载中0%"
|
||||
});
|
||||
// uni.showLoading({
|
||||
// title: '更新下载中...',
|
||||
@ -143,7 +234,7 @@ function downloadAndInstallWgt(wgtUrl) {
|
||||
// 监听下载进度
|
||||
downloadTask.onProgressUpdate((progress) => {
|
||||
console.log('下载进度: ' + progress.progress + '%');
|
||||
wating.setTitle("下载中"+ progress.progress + '%');
|
||||
wating.setTitle("下载中" + progress.progress + '%');
|
||||
// 可以在这里更新自定义进度条
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user