App.vue 8.4 KB

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