App.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <script>
  2. import $mqtt from '@/utils/mqtt.min.js';
  3. import { config } from '@/api/request/config.js'
  4. export default {
  5. onLaunch: function() {
  6. console.log('App Launch')
  7. //获取openid
  8. uni.login({
  9. success(loginRes) {
  10. console.log('获取code成功');
  11. uni.setStorageSync('code', loginRes.code);
  12. }
  13. })
  14. },
  15. onShow: function() {
  16. // this.onMQTT();
  17. console.log('App Show')
  18. },
  19. onHide: function() {
  20. // this.offMQTT();
  21. console.log('App Hide')
  22. },
  23. data(){
  24. return{
  25. //MQTT数据
  26. client:{},
  27. //MQTT请求参数
  28. mtopicBigview:"lab/bigview",
  29. mtopicFunction:"lab/function/data",
  30. mtopicHardware:"lab/hardware/data",
  31. mtopicLine:"lab/exit/line",
  32. //监听数据
  33. watchObj:{},
  34. //当前通道数组
  35. mqttIdList:[],
  36. }
  37. },
  38. globalData:{
  39. mqttAlarmData:{},
  40. mqttSensorData:{},
  41. mqttDeviceData:{},
  42. warnData:{},
  43. lineData:{},
  44. mqttOutfireData:{},
  45. },
  46. methods: {
  47. //删除报警监听
  48. delWarnData(){
  49. delete this.watchObj.warnData;
  50. },
  51. //删除报警监听
  52. deleteAlarmWatch(){
  53. delete this.watchObj.mqttAlarmData;
  54. },
  55. //删除传感器监听
  56. deleteSensorWatch(){
  57. delete this.watchObj.mqttSensorData;
  58. },
  59. //删除设备监听
  60. deleteDeviceWatch(){
  61. delete this.watchObj.mqttDeviceData;
  62. },
  63. //删除应急监听
  64. deleteLineData(){
  65. delete this.watchObj.lineData;
  66. },
  67. //删除一键灭火
  68. deleteOutfireData(){
  69. delete this.watchObj.mqttOutfireData;
  70. },
  71. //监听对应的mqtt数据
  72. watch:function(method,istr){
  73. this.watchObj = this.globalData;
  74. Object.defineProperty(this.watchObj,istr,{
  75. configurable:true,
  76. enumerable:true,
  77. immediate: true,
  78. deep: true,
  79. set:function(value){
  80. this._consumerGoodsStatus=value
  81. method(value)
  82. },
  83. get:function(value){
  84. return this._consumerGoodsStatus
  85. }
  86. })
  87. },
  88. // 子页面调用
  89. appMqttOn(type,list){
  90. let self = this;
  91. if(this.mqttIdList){
  92. this.offMQTT();
  93. }
  94. uni.setStorageSync('mqttIdList',JSON.stringify(list));
  95. this.mqttIdList = list;
  96. setTimeout(function(){
  97. self.subscriptionMQTT();
  98. },100);
  99. },
  100. //MQTT订阅
  101. subscriptionMQTT(){
  102. let self = this;
  103. const mqttOptions = {
  104. keepalive: 30,
  105. clean: true,//保留会话
  106. connectTimeout: 5000, // 超时时间
  107. reconnectPeriod:5000, // 重连间隔
  108. clientId: uni.getStorageSync('token')+'lab/function/data',
  109. username: uni.getStorageSync('mqttUrl'),
  110. password: uni.getStorageSync('mqttUrl'),
  111. }
  112. const connectUrl = uni.getStorageSync('mqttUrl');
  113. // this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  114. this.client = $mqtt.connect('wxs://' + connectUrl, mqttOptions);
  115. this.client.on('connect', () => {
  116. // 这是为了给自己发条消息,其它无作用
  117. for(let i=0;i<self.mqttIdList.length;i++){
  118. if(self.mqttIdList[i].type == 'lab/function/data'){
  119. //传感器
  120. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  121. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], (err) => {
  122. if (!err) {
  123. console.log("传感器信息订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].idList[o])
  124. }
  125. });
  126. }
  127. }else if(self.mqttIdList[i].type == 'lab/hardware/data'){
  128. //传感器
  129. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  130. self.client.subscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], (err) => {
  131. if (!err) {
  132. console.log("硬件订阅成功",self.mqttIdList[i].type+self.mqttIdList[i].idList[o])
  133. }
  134. });
  135. }
  136. }else if(self.mqttIdList[i].type == 'lab/bigview'){
  137. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  138. if (!err) {
  139. console.log("报警信息订阅成功",self.mqttIdList[i].type)
  140. }
  141. });
  142. }else if(self.mqttIdList[i].type == 'lab/exit/line'){
  143. self.client.subscribe(self.mqttIdList[i].type, (err) => {
  144. if (!err) {
  145. console.log("应急疏散订阅成功",self.mqttIdList[i].type)
  146. }
  147. });
  148. }else if(self.mqttIdList[i].type == 'lab/fireDevice/Warn/'){
  149. self.client.subscribe(self.mqttIdList[i].type+'#', (err) => {
  150. if (!err) {
  151. console.log("一键灭火订阅成功",self.mqttIdList[i].type)
  152. }
  153. });
  154. }
  155. }
  156. });
  157. // 自动重连
  158. this.client.on('reconnect', (msg) => {
  159. console.log('自动重连-reconnect-1', msg)
  160. });
  161. // 错误
  162. this.client.on('error', () => {
  163. console.log('错误-error-1')
  164. });
  165. // 断开
  166. this.client.on('end', () => {
  167. console.log('断开-end-1')
  168. });
  169. // 掉线
  170. this.client.on('offline', (msg) => {
  171. console.log('掉线-offline-1',msg)
  172. });
  173. // 收到消息
  174. this.client.on('message', (topic, message) => {
  175. let data = JSON.parse(message)
  176. //console.log('消息message',data);
  177. if(data){
  178. if(topic == 'lab/exit/line'){
  179. // console.log("应急",data);
  180. this.globalData.lineData = data.data;
  181. }else if(topic.search("lab/fireDevice/Warn/") != -1){
  182. // console.log("一键灭火",data);
  183. this.globalData.mqttOutfireData = data.data;
  184. }else if(topic == 'lab/bigview'){
  185. // console.log("报警",data);
  186. if(data.data == 'SUB_RISKPLAN_GROUP'){
  187. this.globalData.mqttAlarmData = data.data;
  188. }
  189. }else {
  190. if(data.data.subId){
  191. // console.log('传感器',data);
  192. this.globalData.mqttSensorData = data.data;
  193. }else{
  194. // console.log("设备开关",data);
  195. this.globalData.mqttDeviceData = data.data;
  196. }
  197. }
  198. }
  199. })
  200. // 全局监听是否有关闭mqtt的消息的事件
  201. uni.$on('closeMqtt',() => {
  202. this.client.end(true); // 主动断开连接
  203. })
  204. },
  205. //取消订阅关闭MQTT连接
  206. offMQTT(){
  207. let self = this;
  208. if(self.mqttIdList[0]){
  209. uni.removeStorageSync('mqttIdList')
  210. for(let i=0;i<self.mqttIdList.length;i++){
  211. if(self.mqttIdList[i].type == 'lab/function/data'){
  212. //断开传感器
  213. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  214. self.client.unsubscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], error => {
  215. if (error) {
  216. console.log('Unsubscribe error mtopicBigview', error)
  217. }
  218. })
  219. }
  220. }else if(self.mqttIdList[i].type == 'lab/hardware/data'){
  221. //断开硬件
  222. for(let o=0;o<self.mqttIdList[i].idList.length;o++){
  223. self.client.unsubscribe(self.mqttIdList[i].type+self.mqttIdList[i].idList[o], error => {
  224. if (error) {
  225. console.log('Unsubscribe error mtopicBigview', error)
  226. }
  227. })
  228. }
  229. }else if(self.mqttIdList[i].type == 'lab/bigview'){
  230. //断开报警
  231. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  232. if (error) {
  233. console.log('Unsubscribe error mtopicBigview', error)
  234. }
  235. })
  236. }else if(self.mqttIdList[i].type == 'lab/exit/line'){
  237. //断开疏散
  238. self.client.unsubscribe(self.mqttIdList[i].type, error => {
  239. if (error) {
  240. console.log('Unsubscribe error mtopicBigview', error)
  241. }
  242. })
  243. }
  244. }
  245. this.client.end(true);
  246. this.client = null;
  247. this.mqttIdList = [];
  248. }
  249. },
  250. }
  251. }
  252. </script>
  253. <style lang="stylus">
  254. page{
  255. height:100%;
  256. width:100%;
  257. background:#f5f5f5;
  258. font-family: PingFang SC;
  259. font-weight: 400;
  260. font-size:30rpx;
  261. color:#333;
  262. }
  263. img{
  264. margin:0;
  265. padding:0;
  266. display:block;
  267. }
  268. .name-box-p{
  269. display block
  270. overflow:hidden;
  271. text-overflow:ellipsis;
  272. white-space:nowrap;
  273. }
  274. view{
  275. margin:0;
  276. padding:0;
  277. }
  278. /*每个页面公共css */
  279. @import './styles/index.styl'
  280. </style>