|
@@ -1,9 +1,9 @@
|
|
// app.ts
|
|
// app.ts
|
|
import mqtt from './utils/mqtt';
|
|
import mqtt from './utils/mqtt';
|
|
//连接的服务器域名
|
|
//连接的服务器域名
|
|
-// const host = 'wxs://mqtt.test.radio1964.com';
|
|
|
|
|
|
+const host = 'wxs://mqtt.test.radio1964.com';
|
|
// const host = 'wxs://mqtt.test.radio1964.com:8884';
|
|
// const host = 'wxs://mqtt.test.radio1964.com:8884';
|
|
-const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
|
|
|
|
|
|
+// const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
|
|
App({
|
|
App({
|
|
globalData: {
|
|
globalData: {
|
|
statusBarHeight: 0,
|
|
statusBarHeight: 0,
|
|
@@ -15,10 +15,12 @@ App({
|
|
pwdData: "",
|
|
pwdData: "",
|
|
userData: null,
|
|
userData: null,
|
|
userInfo: null,
|
|
userInfo: null,
|
|
- newDeviceId: null,
|
|
|
|
|
|
+ newDeviceId: null,
|
|
is_debug: 2, // 1 测试环境 // 2正式环境
|
|
is_debug: 2, // 1 测试环境 // 2正式环境
|
|
client: null,
|
|
client: null,
|
|
oneInitBluetooth: true,
|
|
oneInitBluetooth: true,
|
|
|
|
+ // 设备列表,跟app同步字段
|
|
|
|
+ classifyProducts: [],
|
|
//MQTT连接的配置
|
|
//MQTT连接的配置
|
|
options: {
|
|
options: {
|
|
clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10),
|
|
clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10),
|
|
@@ -35,51 +37,51 @@ App({
|
|
connect() {
|
|
connect() {
|
|
this.globalData.client = mqtt.connect(host, this.globalData.options);
|
|
this.globalData.client = mqtt.connect(host, this.globalData.options);
|
|
// 连接成功
|
|
// 连接成功
|
|
- this.globalData.client.on('connect', ()=> {
|
|
|
|
|
|
+ this.globalData.client.on('connect', () => {
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
- if(pageinfo.mqttCallback) {
|
|
|
|
|
|
+ if (pageinfo.mqttCallback) {
|
|
pageinfo.mqttCallback("connect")
|
|
pageinfo.mqttCallback("connect")
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+
|
|
});
|
|
});
|
|
// 接收消息
|
|
// 接收消息
|
|
- this.globalData.client.on("message", function(topic, payload) {
|
|
|
|
|
|
+ this.globalData.client.on("message", function (topic, payload) {
|
|
wx.hideLoading();
|
|
wx.hideLoading();
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
const thisPageIsIndex = getCurrentPages().length === 1;
|
|
const thisPageIsIndex = getCurrentPages().length === 1;
|
|
- if(pageinfo.mqttCallback) {
|
|
|
|
- if(topic.indexOf("status/onoffline") !== -1) {
|
|
|
|
- pageinfo.mqttCallback("message_onoffline", {topic, payload});
|
|
|
|
- if(!thisPageIsIndex) {
|
|
|
|
- getCurrentPages()[0].mqttCallback("message_onoffline", {topic, payload})
|
|
|
|
|
|
+ if (pageinfo.mqttCallback) {
|
|
|
|
+ if (topic.indexOf("status/onoffline") !== -1) {
|
|
|
|
+ pageinfo.mqttCallback("message_onoffline", { topic, payload });
|
|
|
|
+ if (!thisPageIsIndex) {
|
|
|
|
+ getCurrentPages()[0].mqttCallback("message_onoffline", { topic, payload })
|
|
}
|
|
}
|
|
- } else if(topic.indexOf("user/pub_response") !== -1) {
|
|
|
|
- pageinfo.mqttCallback("message", {topic, payload});
|
|
|
|
|
|
+ } else if (topic.indexOf("user/pub_response") !== -1) {
|
|
|
|
+ pageinfo.mqttCallback("message", { topic, payload });
|
|
}
|
|
}
|
|
};
|
|
};
|
|
});
|
|
});
|
|
//服务器连接异常的回调
|
|
//服务器连接异常的回调
|
|
- this.globalData.client.on("error", function(error) {
|
|
|
|
|
|
+ this.globalData.client.on("error", function (error) {
|
|
console.log(" 服务器 error 的回调" + error)
|
|
console.log(" 服务器 error 的回调" + error)
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
- if(pageinfo.mqttCallback) {
|
|
|
|
|
|
+ if (pageinfo.mqttCallback) {
|
|
pageinfo.mqttCallback("error")
|
|
pageinfo.mqttCallback("error")
|
|
};
|
|
};
|
|
})
|
|
})
|
|
|
|
|
|
// 服务器重连
|
|
// 服务器重连
|
|
- this.globalData.client.on("reconnect", function(errr) {
|
|
|
|
|
|
+ this.globalData.client.on("reconnect", function (errr) {
|
|
console.log(" 服务器 reconnect的回调", errr);
|
|
console.log(" 服务器 reconnect的回调", errr);
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
- if(pageinfo.mqttCallback) {
|
|
|
|
|
|
+ if (pageinfo.mqttCallback) {
|
|
pageinfo.mqttCallback("reconnect")
|
|
pageinfo.mqttCallback("reconnect")
|
|
};
|
|
};
|
|
})
|
|
})
|
|
//服务器断开连接
|
|
//服务器断开连接
|
|
- this.globalData.client.on("offline", function(errr) {
|
|
|
|
|
|
+ this.globalData.client.on("offline", function (errr) {
|
|
console.log(" 服务器 offline的回调", errr)
|
|
console.log(" 服务器 offline的回调", errr)
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
|
|
- if(pageinfo.mqttCallback) {
|
|
|
|
|
|
+ if (pageinfo.mqttCallback) {
|
|
pageinfo.mqttCallback("offline")
|
|
pageinfo.mqttCallback("offline")
|
|
};
|
|
};
|
|
})
|
|
})
|
|
@@ -88,13 +90,13 @@ App({
|
|
|
|
|
|
|
|
|
|
// 订阅主题
|
|
// 订阅主题
|
|
- subscribe: function(topic, callback) {
|
|
|
|
|
|
+ subscribe: function (topic, callback) {
|
|
|
|
|
|
if (this.globalData.client && this.globalData.client.connected) {
|
|
if (this.globalData.client && this.globalData.client.connected) {
|
|
//订阅主题
|
|
//订阅主题
|
|
- this.globalData.client.subscribe(topic, function(err, granted) {
|
|
|
|
|
|
+ this.globalData.client.subscribe(topic, function (err, granted) {
|
|
if (!err) {
|
|
if (!err) {
|
|
- if(callback) {
|
|
|
|
|
|
+ if (callback) {
|
|
callback();
|
|
callback();
|
|
}
|
|
}
|
|
console.log("订阅成功");
|
|
console.log("订阅成功");
|
|
@@ -112,7 +114,7 @@ App({
|
|
}
|
|
}
|
|
},
|
|
},
|
|
// 取消订阅
|
|
// 取消订阅
|
|
- unsubscribe: function(Topic) {
|
|
|
|
|
|
+ unsubscribe: function (Topic) {
|
|
if (this.globalData.client && this.globalData.client.connected) {
|
|
if (this.globalData.client && this.globalData.client.connected) {
|
|
this.globalData.client.unsubscribe(Topic);
|
|
this.globalData.client.unsubscribe(Topic);
|
|
} else {
|
|
} else {
|
|
@@ -139,11 +141,11 @@ App({
|
|
wx.showLoading({
|
|
wx.showLoading({
|
|
title: '请稍后',
|
|
title: '请稍后',
|
|
});
|
|
});
|
|
- const data = {"DstDeviceName": option.DstDeviceName,"SrcDeviceName": `ALY_${this.globalData.userInfo.userId}`,"type": option.type};
|
|
|
|
- if(option.other) {
|
|
|
|
|
|
+ const data = { "DstDeviceName": option.DstDeviceName, "SrcDeviceName": `ALY_${this.globalData.userInfo.userId}`, "type": option.type };
|
|
|
|
+ if (option.other) {
|
|
data.other = option.other;
|
|
data.other = option.other;
|
|
};
|
|
};
|
|
- this.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`,(err) => {
|
|
|
|
|
|
+ this.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`, (err) => {
|
|
if (err) {
|
|
if (err) {
|
|
console.log("发布消息失败");
|
|
console.log("发布消息失败");
|
|
}
|
|
}
|
|
@@ -159,18 +161,18 @@ App({
|
|
},
|
|
},
|
|
onLaunch() {
|
|
onLaunch() {
|
|
var _this = this;
|
|
var _this = this;
|
|
- wx.onAppHide(()=> {
|
|
|
|
- if(_this.globalData.client && _this.globalData.client.connected){
|
|
|
|
|
|
+ wx.onAppHide(() => {
|
|
|
|
+ if (_this.globalData.client && _this.globalData.client.connected) {
|
|
_this.globalData.client.end(true);
|
|
_this.globalData.client.end(true);
|
|
_this.globalData.client.end(true);
|
|
_this.globalData.client.end(true);
|
|
console.log("断开");
|
|
console.log("断开");
|
|
};
|
|
};
|
|
});
|
|
});
|
|
|
|
|
|
- wx.onAppShow(()=> {
|
|
|
|
|
|
+ wx.onAppShow(() => {
|
|
console.log("加载")
|
|
console.log("加载")
|
|
- if(_this.globalData.userInfo !== null){
|
|
|
|
- if(_this.globalData.client) {
|
|
|
|
|
|
+ if (_this.globalData.userInfo !== null) {
|
|
|
|
+ if (_this.globalData.client) {
|
|
_this.globalData.client.end(true);
|
|
_this.globalData.client.end(true);
|
|
_this.globalData.client.end(true);
|
|
_this.globalData.client.end(true);
|
|
}
|
|
}
|
|
@@ -183,27 +185,27 @@ App({
|
|
wx.getSystemInfo({
|
|
wx.getSystemInfo({
|
|
success: (res) => {
|
|
success: (res) => {
|
|
let custom = wx.getMenuButtonBoundingClientRect();
|
|
let custom = wx.getMenuButtonBoundingClientRect();
|
|
-
|
|
|
|
|
|
+
|
|
_this.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
|
|
_this.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
|
|
_this.globalData.MenuButtonheight = custom.height;
|
|
_this.globalData.MenuButtonheight = custom.height;
|
|
_this.globalData.MenuButtonTop = custom.top;
|
|
_this.globalData.MenuButtonTop = custom.top;
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
})
|
|
})
|
|
},
|
|
},
|
|
// 获取蓝牙权限
|
|
// 获取蓝牙权限
|
|
- getBluetoothStatus(){
|
|
|
|
|
|
+ getBluetoothStatus() {
|
|
const _this = this;
|
|
const _this = this;
|
|
wx.getSetting({
|
|
wx.getSetting({
|
|
- success (res) {
|
|
|
|
- if(res.authSetting["scope.bluetooth"]) {
|
|
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.authSetting["scope.bluetooth"]) {
|
|
_this.globalData.scopeBluetooth = true;
|
|
_this.globalData.scopeBluetooth = true;
|
|
- } else if(res.authSetting["scope.bluetooth"] === undefined) {
|
|
|
|
|
|
+ } else if (res.authSetting["scope.bluetooth"] === undefined) {
|
|
_this.globalData.scopeBluetooth = false;
|
|
_this.globalData.scopeBluetooth = false;
|
|
- wx.authorize({
|
|
|
|
|
|
+ wx.authorize({
|
|
scope: "scope.bluetooth",
|
|
scope: "scope.bluetooth",
|
|
- complete () {
|
|
|
|
|
|
+ complete() {
|
|
_this.getBluetoothStatus();
|
|
_this.getBluetoothStatus();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -212,11 +214,11 @@ App({
|
|
wx.showModal({
|
|
wx.showModal({
|
|
title: '请打开系统蓝牙进行配网',
|
|
title: '请打开系统蓝牙进行配网',
|
|
content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
|
|
content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
|
|
- success (res) {
|
|
|
|
|
|
+ success(res) {
|
|
if (res.confirm) {
|
|
if (res.confirm) {
|
|
console.log('用户点击确定')
|
|
console.log('用户点击确定')
|
|
wx.openSetting({
|
|
wx.openSetting({
|
|
- complete () {
|
|
|
|
|
|
+ complete() {
|
|
// _this.getBluetoothStatus();
|
|
// _this.getBluetoothStatus();
|
|
}
|
|
}
|
|
})
|
|
})
|
|
@@ -226,7 +228,7 @@ App({
|
|
}
|
|
}
|
|
})
|
|
})
|
|
};
|
|
};
|
|
- if(getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
|
|
|
|
|
|
+ if (getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
|
|
getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(_this.globalData.scopeBluetooth);
|
|
getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(_this.globalData.scopeBluetooth);
|
|
}
|
|
}
|
|
// res.authSetting = {
|
|
// res.authSetting = {
|