heyujie
2021-06-07 8f8155aa4c83f7c2577ac123add550766b6a7ce3
pages/index/index.vue
@@ -48,8 +48,7 @@
               </view> -->
            <view class="list-item" v-for="(x,i) in wList" @tap="toDetail(x)">
               <view class="left-img">
                  <!-- <image :src="'/httpImage/' + x.imgUrl" mode=""></image> -->
                  <image :src="'http://192.168.20.10:7009'+'/httpImage/'+ x.imgUrl" mode="">
                  <image :src="'http://'+ getResourceUrl() +'/httpImage/'+ x.imgUrl" mode="">
                  </image>
               </view>
               <view class="right-desc">
@@ -72,7 +71,7 @@
                        来自:{{x.pos}}
                     </view>
                     <view class="time">
                        {{x.time}}
                        {{x.date}} {{x.time}}
                     </view>
                  </view>
               </view>
@@ -106,21 +105,30 @@
               wsport: '1883',
               path: '/ws',
            },
            onTopic: 'MIDDOL-TEST', // this.$mqtt.subscribe('MIDDOL-TEST')
            onTopic: 'MIDDOL-TEST',
            Qos: 0,
            sendMassage: 'Hello EMQ-X!',
            time: 0,
            client: null,
            activeChannel: "全部设备",
            //MQTT连接的配置
            // clientId: 'WebClient-' + parseInt(Math.random() * 100000),
            // username: 'admin',
            // password: 'admin123',
            // useSSL: false,
            // mqttVersion: 4,
            // timeout: 3
            options: {
               wsOptions: {},
               protocolVersion: 4, //MQTT连接协议版本
               clientId: 'WebClient-' + parseInt(Math.random() * 100000),
               username: 'admin',
               password: 'admin123',
               keepalive: 60,
               reconnectPeriod: 100,
               // reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
               // connectTimeout: 60 * 1000, //1000毫秒,两次重新连接之间的间隔
               connectTimeout: 60 * 1000, //1000毫秒,两次重新连接之间的间隔
               resubscribe: true //如果连接断开并重新连接,则会再次自动订阅已订阅的主题
            },
            showActionSheet: {
@@ -138,56 +146,9 @@
         bottomDrawer
      },
      mounted() {
         // const connectUrl = 'ws://test.mosquitto.org:8080'
         // const connectUrl = 'wx://test.mosquitto.org:8080'
         // const client = mqtt.connect(connectUrl, options)
         // client.on('connect', function(mes) {
         //     // 订阅
         //    client.subscribe("testtopic");
         // })
         // client.on('reconnect', (error) => {
         //    console.log('正在重连:', error)
         // })
         // client.on('error', (error) => {
         //    console.log('连接失败:', error)
         // })
         // client.on('message', (topic, message) => {
         //    console.log('收到消息:', topic, message.toString())
         // })
         let _this = this
         // var hosts = 'ws://' + this.serve.host + ':' + this.serve.wsport + this.serve.path
         // var hosts = 'wx://192.168.20.189:15675/ws'
         var hosts = 'ws://192.168.20.189:15675/ws'
         const c = mqtt.connect(
            hosts,
            _this.options
         );
         c.on('connect', function() {
            // console.log('连接成功');
            c.subscribe(_this.onTopic, {
               qos: _this.Qos
            }, function(error) {
               if (!error) {
                  // console.log('订阅成功');
               }
            });
         });
         c.on('reconnect', function(error) {
            console.log("走这里111")
         });
         c.on('error', function(error) {
            uni.hideLoading();
         });
         c.on('message', function(topic, message) {
            const data = message.toString()
            // console.log('收到来自' + topic + '的消息' + JSON.parse(data).id);
            _this.handleMsg(data)
         });
         this.connectAndSubscribe('ws://' + getApp().globalData.mqUrl + '/ws')
         // _this.$mqtt.subscribe('MIDDOL-TEST')
         this.listenIPChange()
      },
      // mqtt: {
      //    'MIDDOL-TEST'(data) {
@@ -238,6 +199,16 @@
         this.pullData()
      },
      methods: {
         listenIPChange() {
            uni.$on('changeClientIP', (data) => {
               this.disconnect()
               this.connectAndSubscribe(`ws://${data.host}/ws`)
               getApp().globalData.mqUrl = data.host
               uni.$emit("connectStatus", {
                  status: this.client.connected
               })
            })
         },
         pullData(l, v) {
            DB.selectTableData('warning_list', l, v).then((res) => {
               this.wList = res
@@ -245,28 +216,75 @@
               console.log(6677, e)
            })
         },
         connectAndSubscribe(hosts) {
            let _this = this
            console.log(hosts)
            this.client = mqtt.connect(
               hosts,
               _this.options
            );
            this.client.on('connect', function() {
               console.log('连接成功');
               _this.client.subscribe(_this.onTopic, {
                  qos: _this.Qos
               }, function(error) {
                  if (!error) {
                     console.log('订阅成功');
                  }
               });
            });
            this.client.on('reconnect', function(error) {
               console.log("走这里111")
            });
            this.client.on('error', function(error) {
               console.log("连接失败")
            });
            this.client.on('message', function(topic, message) {
               console.log('新增一条数据')
               const data = message.toString()
               _this.handleMsg(data)
            });
         },
         getResourceUrl() {
            return getApp().globalData.resourseUrl
         },
         disconnect() {
            if (!this.client || !this.client.connected) {
               console.log('客户端未连接')
               return;
            }
            this.client.end();
            this.client = null
            console.log('已断开连接');
         },
         handleMsg(data) {
            const obj = JSON.parse(data)
            const [date, time] = obj.updateTime.split(" ")
            const t = new Date(obj.updateTime)
            let [date, time] = obj.picDate.split(" ")
            time = time.substring(0, time.length - 4);
            const t = new Date(obj.picDate)
            DB.insertTableData('warning_list',
               `'${obj.id}', '${time}', '${date}', '${obj.cameraAddr}', '${obj.taskName}', '${obj.alarmRules[0].alarmLevel}', '${obj.picMaxUrl[0]}','${t.getTime()}', 0`
            ).then(() => {
               console.log('成功插入一条数据')
               if (this.activeChannel == '全部设备' || this.activeChannel == obj.cameraAddr) {
                  if (this.wList.find(function(item) {
                        return item.id == obj.id
                     }) == undefined) {
                     this.wList.unshift({
                        id: obj.id,
                        time,
                        date,
                        pos: obj.cameraAddr,
                        name: obj.taskName,
                        warningLevel: obj.alarmRules[0].alarmLevel,
                        imgUrl: obj.picMaxUrl[0]
                     })
                  }
               }
            }, () => {
               console.log('插入一条数据失败')
            })
            if (this.activeChannel == '全部设备' || this.activeChannel == obj.cameraAddr) {
               this.wList.unshift({
                  id: obj.id,
                  time,
                  date,
                  pos: obj.cameraAddr,
                  name: obj.taskName,
                  warningLevel: obj.alarmRules[0].alarmLevel,
                  imgUrl: obj.picMaxUrl[0]
               })
            }
         },
         goFrontPage() {
            uni.navigateTo({
@@ -288,13 +306,13 @@
               })
            })
            plus.sqlite.selectSql({
               name: 'warns',
               sql: `select id from warning_list`,
               name: "warns",
               sql: 'select id from warning_list',
               success(e) {
                  console.log('所有id::', e)
                  console.log('所有id', e);
               },
               fail(e) {
                  console.log('出错了', e)
                  console.log('失败所有id', e);
               }
            })
         },