App.vue 8.4 KB

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