天波h5前端应用
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

368 linhas
15KB

  1. <!--
  2. * @Date: 2023-10-10 15:29:50
  3. * @LastEditors: JinxChen
  4. * @LastEditTime: 2023-10-19 14:21:55
  5. * @FilePath: \TelpoH5FrontendWeb\src\views\gps-card-frontend\device-power\index.vue
  6. * @description:
  7. -->
  8. <template>
  9. <div class="device-power">
  10. <van-nav-bar :left-arrow="true" @click-left="onNavBack">
  11. <template #left>
  12. <van-icon name="arrow-left" size="24" style="padding: 0"/>
  13. <span>返回</span>
  14. </template>
  15. </van-nav-bar>
  16. <div class="top">
  17. <p>查看数据</p>
  18. </div>
  19. <div class="action">
  20. <div class="left">
  21. <van-dropdown-menu>
  22. <van-dropdown-item style="min-width: 150px" v-model="dayValue" :options="dayOption" @change="onChange" />
  23. </van-dropdown-menu>
  24. </div>
  25. <div class="right">
  26. <div class="btn-container">
  27. <van-button :class="['btn', {active: btnActive === item.index }]" @click="onClickBtn(item.index)" v-for="(item, index) in btnList" :key="index">{{ item.text }}</van-button>
  28. </div>
  29. </div>
  30. </div>
  31. <div class="details-container" v-if="btnActive === 0 && data.length > 0 || tableData.length > 0 && btnActive === 1">
  32. <p class="last-time" v-show="btnActive === 0 && params.title === 'Offline'">最近一次离线时间:{{ lastTime }}</p>
  33. <Echart :option="chartOption" v-show="btnActive === 0"/>
  34. <Table :columns="titleList" :data="tableData" v-show="btnActive === 1"/>
  35. </div>
  36. <div class="details-container no-data" v-else>
  37. <van-empty image="error" description="暂无数据" />
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import Echart from '@/components/echarts';
  43. import Table from '@/components/tables';
  44. import APIIot from '@/api/iot';
  45. import { DEVICE_POWER } from "@/config/models";
  46. import axios from 'axios';
  47. export default {
  48. name:'',
  49. components: { Echart,Table },
  50. data(){
  51. return {
  52. dayValue: 7,
  53. dayOption: [
  54. { text: '7天', value: 7 },
  55. { text: '30天', value: 30 },
  56. ],
  57. btnList: [
  58. { text: '图表', index: 0 },
  59. { text: '表格', index: 1 },
  60. ],
  61. btnActive: 0,
  62. chartOption: {},
  63. titleList: [
  64. { title: '时间', width: '40%', key: 'time'},
  65. { title: '原始值', width: '60%', key: 'value' },
  66. ],
  67. tableData: [],
  68. echarts: {
  69. title: '',
  70. type: '',
  71. },
  72. data: [],
  73. xAxisData: [],
  74. filterData: [],
  75. yAxisObject: {},
  76. lastTime: '',
  77. params: {}
  78. }
  79. },
  80. created() {
  81. this.loadParams();
  82. },
  83. mounted() {
  84. },
  85. methods: {
  86. loadParams() {
  87. let params = this.$route.query;
  88. this.params = {...params};
  89. if(params) {
  90. this.echarts.title = DEVICE_POWER[params.title].title;
  91. this.echarts.type = DEVICE_POWER[params.title].value;
  92. // 获取接口数据
  93. this.getData();
  94. } else {
  95. // todo跳转404页面
  96. }
  97. },
  98. getData() {
  99. this.$toast.loading();
  100. let reqBody = {
  101. days: String(this.dayValue),
  102. identifier: this.echarts.type,
  103. imei: /* '861281060086216' *//* '862838050029479' */this.$store.getters.imei
  104. };
  105. // 线上地址
  106. let baseUrl = process.env.VUE_APP_BASE_API;
  107. let reqUrl = `${baseUrl}iotservice/getdeviceinfo`;
  108. // 开启代理如下
  109. /* let reqUrl = `/api/id/getdeviceinfo`; */
  110. axios.post(`${reqUrl}`, reqBody).then(res => {
  111. let data = res.data;
  112. if(data.code === 200) {
  113. if(this.echarts.type === 'BatteryLevel' || this.echarts.type === 'status') {
  114. this.filterData = data[this.echarts.type].data/* .list.propertyInfo */;
  115. } else if(this.echarts.type === 'offline') {
  116. this.filterData = data.Offline.data;
  117. }
  118. if(this.echarts.type === 'status') {
  119. // 信号强度
  120. this.yAxisObject = {
  121. type: "value",
  122. /* max: 4,
  123. min: 0,
  124. interval: 1,
  125. splitNumber : 1,
  126. boundaryGap : [ '5%', '5%' ], */
  127. }
  128. this.titleList = [
  129. { title: '时间', width: '40%', key: 'time'},
  130. { title: '原始值', width: '60%', key: 'value' },
  131. ];
  132. let propertyInfo = this.filterData.map(item => {
  133. return item.list.propertyInfo;
  134. });
  135. let result = [];
  136. for (let i = 0; i < propertyInfo.length; i++) {
  137. // 循环对象中的数组
  138. result = [].concat(...propertyInfo)
  139. }
  140. // 表格则要显示全部数据
  141. this.tableData = result.map(item => {
  142. return {
  143. value: item.value,
  144. time: this.$dayjs(this.$dayjs(Number(item.time))).format("YYYY/MM/DD hh:mm")
  145. }
  146. }).reverse();
  147. // 图表筛选过滤只显示 rssi <= 2的数据
  148. this.data = result.map(item => {
  149. // 序列化json
  150. let json = JSON.parse(item.value);
  151. return {
  152. value: json.rssi,
  153. time: this.$dayjs(this.$dayjs(Number(item.time))).format("YYYY/MM/DD hh:mm"),
  154. type: 'status'
  155. }
  156. }).filter(f => {
  157. return f.value <=2;
  158. })
  159. } else if (this.echarts.type === 'offline') {
  160. // 设备离线次数
  161. this.titleList = [
  162. { title: '时间', width: '40%', key: 'time'},
  163. { title: '最近离线时间', width: '60%', key: 'lastTime' },
  164. { title: '离线次数', width: '40%', key: 'value' },
  165. ],
  166. this.yAxisObject = {
  167. type: "value",
  168. /* max: 10,
  169. min: 0,
  170. interval: 1,
  171. splitNumber : 1,
  172. boundaryGap : [ '5%', '5%' ], */
  173. };
  174. this.data = this.filterData.sort((date1, date2) => {
  175. // 日期升序排序
  176. return date2.time < date1.time ? 1 : -1
  177. }).map(item => {
  178. return {
  179. /* value: item.value, */
  180. time: item.createTime.slice(0,10),
  181. day: item.createTime.slice(0,10),
  182. type: 'offline',
  183. lastTime: item.createTime.replace('T', ' ')
  184. }
  185. }).reduce((accumulator, currentValue) => {
  186. if (accumulator.find(obj => obj.day === currentValue.day)) {
  187. accumulator.find(obj => obj.day === currentValue.day).value++;
  188. } else {
  189. accumulator.push({ day: currentValue.day, time: currentValue.time, value: 1, type: 'offline', lastTime: currentValue.lastTime });
  190. }
  191. return accumulator;
  192. }, []).sort(function(date1, date2) {
  193. // 日期升序排序
  194. return date2.time < date1.time ? 1 : -1
  195. });
  196. console.log("data", this.data);
  197. console.log(this.data);
  198. this.lastTime = this.data && this.data[this.data.length - 1].lastTime || '';
  199. this.tableData = this.filterData/* .sort((date1, date2) => {
  200. // 日期升序排序
  201. return date2.time > date1.time ? 1 : -1
  202. }). */.map(item => {
  203. return {
  204. /* value: item.value, */
  205. time: item.createTime.slice(0,10),
  206. day: item.createTime.slice(0,10),
  207. lastTime: item.createTime.replace('T', ' ')
  208. }
  209. }).reduce((accumulator, currentValue) => {
  210. // 筛选遍历数据,获取相同日期的数据,计算出现的次数后重新累加组合到一个新的数组里面
  211. if (accumulator.find(obj => obj.day === currentValue.day)) {
  212. accumulator.find(obj => obj.day === currentValue.day).value++;
  213. } else {
  214. accumulator.push({ day: currentValue.day, time: currentValue.time, value: 1, lastTime: currentValue.lastTime });
  215. }
  216. return accumulator;
  217. }, []).sort(function(date1, date2) {
  218. // 日期升序排序
  219. return date2.time < date1.time ? 1 : -1
  220. }).reverse();
  221. } else {
  222. this.titleList = [
  223. { title: '时间', width: '40%', key: 'time'},
  224. { title: '电量', width: '60%', key: 'value' },
  225. ],
  226. this.yAxisObject = {
  227. type: "value",
  228. max: 100,
  229. min: 0,
  230. interval: 20,
  231. splitNumber : 1,
  232. boundaryGap : [ '5%', '5%' ],
  233. }
  234. let propertyInfo = this.filterData.map(item => {
  235. return item.list.propertyInfo;
  236. });
  237. let result = [];
  238. for (let i = 0; i < propertyInfo.length; i++) {
  239. // 循环对象中的数组
  240. result = [].concat(...propertyInfo)
  241. }
  242. this.data = result.map(item => {
  243. return {
  244. value: item.value,
  245. time: this.$dayjs(this.$dayjs(Number(item.time))).format("YYYY/MM/DD hh:mm"),
  246. }
  247. }).sort((date1, date2) => {
  248. // 日期升序排序
  249. return date2.time < date1.time ? 1 : -1
  250. });
  251. this.tableData = result.map(item => {
  252. return {
  253. value: item.value,
  254. time: this.$dayjs(this.$dayjs(Number(item.time))).format("YYYY/MM/DD hh:mm")
  255. }
  256. }).sort((date1, date2) => {
  257. // 日期升序排序
  258. return date2.time < date1.time ? 1 : -1
  259. }).reverse();
  260. }
  261. this.xAxisData = this.data.map(item => {
  262. return this.$dayjs(item.time).format("MM/DD");
  263. })
  264. this.chartOption = {
  265. title: {
  266. text: this.echarts.title,
  267. textStyle: {
  268. fontSize: 16
  269. },
  270. },
  271. xAxis: {
  272. type: "category",
  273. axisLine: {
  274. show: false
  275. },
  276. textStyle: {
  277. fontSize: 10
  278. },
  279. axisTick: {
  280. show: false
  281. },
  282. splitLine: {
  283. show: false,
  284. lineStyle: {
  285. color: "#ddd",
  286. width: 2
  287. }
  288. },
  289. nameLocation: 'center',
  290. axisLabel: {
  291. show: true,
  292. fontSize: 12,
  293. showMinLabel: true, //显示最小值
  294. showMaxLabel: true, //显示最大值
  295. },
  296. data: this.xAxisData
  297. },
  298. yAxis: this.yAxisObject,
  299. series: [
  300. {
  301. type: "line",
  302. data: this.data,
  303. /* areaStyle: {}, */
  304. },
  305. ],
  306. dataZoom: [
  307. {
  308. start: 0,
  309. end: 100,
  310. textStyle: {
  311. color: "#FFF",
  312. fontSize: 14
  313. },
  314. show: true,
  315. height: 20,
  316. bottom: 5,
  317. handleStyle: {
  318. borderWidth: 1,
  319. borderCap: "square"
  320. }
  321. }
  322. ],
  323. tooltip: {
  324. trigger: "axis",
  325. textStyle: {
  326. fontSize: 14,
  327. align: "center"
  328. },
  329. formatter: function(params) {
  330. return `${params[0].marker}${params[0].data.time}</br>
  331. ${ params[0].data.type === 'offline' ? '最近离线时间:' + params[0].data.lastTime : ''}</br>
  332. ${ params[0].data.type === 'offline' ? '离线次数:' + params[0].data.value + '次' :
  333. params[0].data.type === 'status' ? 'rssi值:' + params[0].data.value :
  334. '电量:' + params[0].data.value}`
  335. }
  336. },
  337. }
  338. }
  339. }).catch(() => {
  340. }).finally(() => {
  341. this.$toast.clear();
  342. })
  343. },
  344. onClickBtn(value) {
  345. this.btnActive = value;
  346. this.getData();
  347. },
  348. onChange(value) {
  349. this.dayValue = value;
  350. this.getData();
  351. },
  352. onNavBack() {
  353. this.$router.push({
  354. name: 'deviceSetting'
  355. })
  356. }
  357. }
  358. }
  359. </script>
  360. <style scoped lang="scss">
  361. @import "./index.scss";
  362. </style>