Ver Fonte

feat: api test

Casper Dai há 2 anos atrás
pai
commit
dcafc4410c

+ 5 - 5
src/router/index.js

@@ -441,11 +441,6 @@ export const asyncRoutes = [
         component: () => import('@/views/platform/debug/index'),
         meta: { title: 'MQTT' }
       },
-      {
-        path: 'simulator',
-        component: () => import('@/views/platform/simulator/index'),
-        meta: { title: '模拟器' }
-      },
       {
         path: 'remote/log',
         component: () => import('@/views/platform/remote-log/index'),
@@ -455,6 +450,11 @@ export const asyncRoutes = [
         path: 'cooperation',
         component: () => import('@/views/platform/cooperation/index'),
         meta: { title: '合作意向' }
+      },
+      {
+        path: 'api',
+        component: () => import('@/views/test-api'),
+        meta: { title: '接口测试' }
       }
     ]
   },

+ 5 - 4
src/views/platform/debug/debug.js

@@ -42,19 +42,20 @@ export function send (topic, message, encode) {
 
 function onMessage (topic, message) {
   const result = /^\d+\/(\d+)(.+)$/.exec(topic)
+  const now = Date.now()
   if (result) {
     const id = result[1]
     getDevice(id)
     deviceMessage[deviceMap[id].index].list.unshift({
-      id: Math.random().toString(16).slice(2),
-      time: parseTime(Date.now()),
+      id: `${now}_${Math.random().toString(16).slice(2)}`,
+      time: parseTime(now),
       topic: result[2],
       desc: message
     })
   } else {
     deviceMessage[0].list.unshift({
-      id: Math.random().toString(16).slice(2),
-      time: parseTime(Date.now()),
+      id: `${now}_${Math.random().toString(16).slice(2)}`,
+      time: parseTime(now),
       topic,
       desc: message
     })

+ 0 - 140
src/views/platform/simulator/index.vue

@@ -1,140 +0,0 @@
-<template>
-  <wrapper
-    fill
-    margin
-    padding
-    background
-  >
-    <div class="l-flex__none l-flex--row c-sibling-item--v">
-      <span class="l-flex__none c-sibling-item">sn</span>
-      <el-input
-        v-model.trim="sn"
-        class="c-sibling-item"
-      />
-      <button
-        class="l-flex__none c-sibling-item far o-button"
-        :disabled="loading"
-        @click="createProxy"
-      >
-        <i
-          v-if="loading"
-          class="el-icon-loading"
-        />
-        <template v-else>
-          代理
-        </template>
-      </button>
-    </div>
-    <template v-if="valid">
-      <div class="c-sibling-item--v far">
-        <button
-          class="l-flex__none c-sibling-item far o-button"
-          @click="publish('/oss')"
-        >
-          OSS
-        </button>
-        <button
-          class="l-flex__none c-sibling-item far o-button"
-          @click="publish('/online')"
-        >
-          Online
-        </button>
-        <button
-          class="l-flex__none c-sibling-item far o-button"
-          @click="publish('/offline')"
-        >
-          Offline
-        </button>
-      </div>
-      <div class="l-flex__auto c-sibling-item--v far c-simulator u-overflow-y--auto">
-        <div
-          v-for="message in messages"
-          :key="message.id"
-        >
-          <div>{{ message.topic }}</div>
-          <div>{{ message.payload }}</div>
-        </div>
-      </div>
-    </template>
-  </wrapper>
-</template>
-
-<script>
-import { createProxy } from './simulate'
-
-export default {
-  name: 'Simulate',
-  data () {
-    return {
-      sn: window.sessionStorage.getItem('isoc_simulator_device') || '',
-      valid: false,
-      loading: false,
-      messages: []
-    }
-  },
-  beforeDestroy () {
-    this.onClose(true)
-  },
-  methods: {
-    createProxy () {
-      if (!this.sn) {
-        return
-      }
-      this.onClose(true)
-      this.loading = true
-      this.$proxyPromise = createProxy(this.sn, this.onMessage, this.onClose)
-      this.$proxyPromise.then(
-        val => {
-          window.sessionStorage.setItem('isoc_simulator_device', this.sn)
-          this.$proxyPromise = null
-          this.$proxy = val
-          this.valid = true
-          this.$message({
-            type: 'success',
-            message: '代理成功'
-          })
-        },
-        () => {
-          this.$message({
-            type: 'error',
-            message: '代理失败'
-          })
-        }
-      ).finally(() => {
-        this.loading = false
-      })
-    },
-    publish (topic, message, encode, useRoot) {
-      return this.$proxy.publish(topic, message, encode, useRoot)
-    },
-    onClose (force) {
-      this.$proxyPromise?.reject()
-      this.$proxy?.stop()
-      this.valid = false
-      this.messages = []
-      if (!force) {
-        this.$message({
-          type: 'warning',
-          message: '代理已断开'
-        })
-      }
-    },
-    onMessage (topic, payload) {
-      this.messages.unshift({
-        id: Date.now(),
-        topic,
-        payload
-      })
-    }
-  }
-}
-</script>
-
-<style lang="scss" scoped>
-.c-simulator {
-  color: $black;
-  font-size: 14px;
-  line-height: 24px;
-  word-break: break-word;
-}
-</style>

+ 0 - 128
src/views/platform/simulator/simulate.js

@@ -1,128 +0,0 @@
-import sha256 from 'crypto-js/sha256'
-import {
-  subscribe,
-  unsubscribe,
-  publish,
-  createClient,
-  decodePayload,
-  sm4
-} from '@/utils/mqtt'
-
-export function createProxy (sn, onMessage, onClose) {
-  const snHash = sha256(sn).toString().toUpperCase()
-
-  let timer = -1
-  let fn
-
-  const targetTopic = `smsb/${snHash}/init/reply`
-
-  const promise = new Promise((resolve, reject) => {
-    fn = (topic, payload) => {
-      if (topic === targetTopic) {
-        unsubscribe(targetTopic, fn)
-        try {
-          onMessage(topic, payload)
-          createProxyClient(JSON.parse(payload), { onMessage, onClose, resolve, reject })
-        } catch (e) {
-          reject()
-        }
-      }
-    }
-
-    timer = setTimeout(() => {
-      publish(`smsb/${snHash}/init`, getPayload(), true).catch(reject)
-    }, 500)
-
-    subscribe(targetTopic, fn)
-  })
-
-  promise.reject = () => {
-    clearTimeout(timer)
-    unsubscribe(targetTopic, fn)
-  }
-
-  return promise
-}
-
-function createProxyClient ({ productId, deviceId, username, password }, { onMessage, onClose, resolve, reject }) {
-  console.log('Simulate Connecting mqtt client...')
-
-  let client = createClient({
-    username,
-    password,
-    clientId: deviceId,
-    reconnectPeriod: 0
-  })
-
-  let onEnd = reject
-  let force = false
-
-  client.on('error', e => {
-    console.log('Simulate MQTT error: ', e)
-  })
-
-  client.on('connect', () => {
-    console.log('Simulate MQTT connected')
-    client.subscribe([
-      `${productId}/${deviceId}/oss/reply`,
-      `${productId}/${deviceId}/calendar/update`
-    ])
-    resolve({
-      productId,
-      deviceId,
-      stop () {
-        if (!client) {
-          return
-        }
-        force = true
-        client.unsubscribe([
-          `${productId}/${deviceId}/oss/reply`,
-          `${productId}/${deviceId}/calendar/update`
-        ])
-        client.end(true)
-      },
-      publish (topic, message, encode = true, useRoot = false) {
-        if (!client) {
-          return
-        }
-        topic = `${productId}/${deviceId}${topic}`
-        const payload = getPayload(message, deviceId)
-        onMessage(topic, payload)
-        if (useRoot) {
-          publish(topic, payload, encode).catch(console.log)
-        } else {
-          client.publish(topic, encode ? sm4.encrypt(payload) : payload)
-        }
-      }
-    })
-    onEnd = onClose
-  })
-
-  client.on('offline', () => {
-    console.log('Simulate MQTT offline')
-  })
-
-  client.on('close', () => {
-    console.log('Simulate MQTT closed')
-    !force && client.end(true)
-  })
-
-  client.on('end', () => {
-    console.log('Simulate MQTT end')
-    client = null
-    !force && onEnd()
-  })
-
-  client.on('message', (topic, payload) => {
-    console.log('Simulate MQTT topic', topic)
-    onMessage(topic, decodePayload(topic, payload))
-  })
-}
-
-function getPayload (payload, deviceId = 'init') {
-  return JSON.stringify({
-    messageId: `simulate_${deviceId}_${Math.random().toString(16).slice(2)}`,
-    timestamp: `${Date.now()}`,
-    ...payload
-  })
-}

+ 198 - 0
src/views/test-api.vue

@@ -0,0 +1,198 @@
+<template>
+  <wrapper
+    fill
+    margin
+    padding
+    background
+  >
+    <div class="c-sibling-item--v">
+      <div
+        class="l-flex--row inline u-color--blue u-font-size--sm u-bold has-active"
+        @click="onChoose"
+      >
+        {{ tip }}
+      </div>
+    </div>
+    <div class="l-flex c-sibling-item--v">
+      <div class="l-flex__fill l-flex--col c-sibling-item">
+        <span class="c-sibling-item--v far u-font-size--sm u-bold">在线时长统计</span>
+        <div class="c-sibling-item--v l-grid--info mini">
+          <button
+            class="o-button"
+            @click="onTriggerOnlineDurationSnap"
+          >
+            触发快照
+          </button>
+          <button
+            class="o-button"
+            @click="onGetOnlineDurationByDevice"
+          >
+            设备总时长
+          </button>
+          <button
+            class="o-button"
+            @click="onGetOnlineDurationReport"
+          >
+            设备日报
+          </button>
+        </div>
+        <span class="c-sibling-item--v far u-font-size--sm u-bold">自助广告统计</span>
+        <div class="c-sibling-item--v l-grid--info mini">
+          <button
+            class="o-button"
+            @click="onTriggerAdSnap"
+          >
+            触发快照
+          </button>
+          <button
+            class="o-button"
+            @click="onGetAdReportForPlatfrom"
+          >
+            平台日报
+          </button>
+          <button
+            class="o-button"
+            @click="onGetAdReportForTenant"
+          >
+            租户日报
+          </button>
+          <button
+            class="o-button"
+            @click="onGetAdReportForDevice"
+          >
+            设备日报
+          </button>
+          <button
+            class="o-button"
+            @click="onGetAdCollectForPlatfrom"
+          >
+            平台汇总
+          </button>
+          <button
+            class="o-button"
+            @click="onGetAdCollectForTenant"
+          >
+            租户汇总
+          </button>
+          <button
+            class="o-button"
+            @click="onGetAdCollectForDevice"
+          >
+            设备汇总
+          </button>
+        </div>
+      </div>
+      <div class="l-flex__none c-sibling-item far u-width--lg u-font-size--sm u-overflow-y--auto">
+        <pre style="white-space: break-spaces;"><code>{{ responseData }}</code></pre>
+      </div>
+    </div>
+    <single-device-dialog
+      ref="deviceDialog"
+      @confirm="onConfirm"
+    />
+  </wrapper>
+</template>
+
+<script>
+import { parseTime } from '@/utils'
+import {
+  triggetOnlineDurationSnap,
+  getOnlineDurationByDevice,
+  getOnlineDurationReport,
+  triggerAdSnap,
+  getAdReport,
+  getAdCollect
+} from '@/api/statistics'
+
+export default {
+  name: 'TestApi',
+  data () {
+    return {
+      device: null,
+      responseData: null
+    }
+  },
+  computed: {
+    tip () {
+      return this.device?.name || '点击选择设备'
+    },
+    deviceId () {
+      return this.device?.id
+    }
+  },
+  methods: {
+    onChoose () {
+      this.$refs.deviceDialog.show(this.device)
+    },
+    onConfirm ({ value, done }) {
+      this.device = value
+      done()
+    },
+    onData (data) {
+      this.responseData = data
+    },
+    checkDevice () {
+      if (this.device) {
+        return true
+      }
+      this.$message({
+        type: 'warning',
+        message: '请先选择设备'
+      })
+      return false
+    },
+    getMonth () {
+      const endDate = new Date()
+      endDate.setDate(endDate.getDate() + 1)
+      const startDate = new Date(endDate.getTime())
+      startDate.setMonth(startDate.getMonth() - 1)
+      return {
+        startDate: parseTime(startDate, '{y}-{m}-{d}'),
+        endDate: parseTime(endDate, '{y}-{m}-{d}')
+      }
+    },
+    onTriggerOnlineDurationSnap () {
+      triggetOnlineDurationSnap()
+    },
+    onGetOnlineDurationByDevice () {
+      this.checkDevice() && getOnlineDurationByDevice(this.deviceId).then(this.onData)
+    },
+    onGetOnlineDurationReport () {
+      const { startDate, endDate } = this.getMonth()
+      this.checkDevice() && getOnlineDurationReport({
+        deviceId: this.deviceId,
+        type: 1,
+        sumDateFrom: startDate,
+        sumDateTo: endDate
+      }).then(this.onData)
+    },
+    onTriggerAdSnap () {
+      triggerAdSnap(parseTime(new Date(), '{y}-{m}-{d}'))
+    },
+    onGetAdReportForPlatfrom () {
+      getAdReport(this.getMonth()).then(this.onData)
+    },
+    onGetAdReportForTenant () {
+      getAdReport({
+        tenant: this.$store.getters.tenant,
+        ...this.getMonth()
+      }).then(this.onData)
+    },
+    onGetAdReportForDevice () {
+      this.checkDevice() && getAdReport({
+        deviceId: this.deviceId,
+        ...this.getMonth()
+      }).then(this.onData)
+    },
+    onGetAdCollectForPlatfrom () {
+      getAdCollect().then(this.onData)
+    },
+    onGetAdCollectForTenant () {
+      getAdCollect({ tenant: this.$store.getters.tenant }).then(this.onData)
+    },
+    onGetAdCollectForDevice () {
+      this.checkDevice() && getAdCollect({ deviceId: this.deviceId }).then(this.onData)
+    }
+  }
+}
+</script>