history.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <template>
  2. <view class="container">
  3. <!-- 建议放在外层 -->
  4. <!-- <u-navbar
  5. title="历史数据"
  6. @leftClick="devicemenuShow"
  7. :safeAreaInsetTop="true"
  8. >
  9. <view class="u-nav-slot" slot="left">
  10. <u-icon name="list" size="20"> </u-icon>
  11. </view>
  12. </u-navbar> -->
  13. <!-- 侧边栏 -->
  14. <view v-show="menushow" class="menupage">
  15. <DeviceMenu @menuClick="menuClick"></DeviceMenu>
  16. </view>
  17. <!-- 历史数据列表页 -->
  18. <view v-if="!menushow" class="main">
  19. <view class="u-page">
  20. <div class="flcard">
  21. <div class="btns">
  22. <uni-datetime-picker
  23. :show="dataShow"
  24. mode="datetime"
  25. @cancel="dataShow = false"
  26. @confirm="selectStartTime"
  27. v-model="StartTime"
  28. ></uni-datetime-picker>
  29. <uni-datetime-picker
  30. :show="dataShow1"
  31. mode="datetime"
  32. @cancel="dataShow1 = false"
  33. @confirm="selectEndTime"
  34. v-model="EndTime"
  35. ></uni-datetime-picker>
  36. </div>
  37. <div class="btns" style="margin-top: 10px">
  38. <u-button
  39. type="primary"
  40. :text="deviceName ? deviceName : '选择设备'"
  41. @click="deviceShow = true"
  42. ></u-button>
  43. <!-- <u-button
  44. v-show="TabCur == 'fanmain' || TabCur == 'fanlocal'"
  45. type="primary"
  46. :text="labelName ? labelName : '子设备'"
  47. @click="deviceShow1 = true"
  48. ></u-button> -->
  49. <u-button
  50. type="primary"
  51. :text="lable ? lable : '间隔时间'"
  52. @click="show = true"
  53. ></u-button>
  54. <u-button
  55. type="primary"
  56. text="查询"
  57. @click="checkHistory"
  58. ></u-button>
  59. </div>
  60. <u-picker
  61. :show="deviceShow"
  62. :columns="[devices]"
  63. @cancel="deviceShow = false"
  64. @confirm="selectDevice"
  65. keyName="strinstallpos"
  66. >
  67. </u-picker>
  68. <u-picker
  69. :show="deviceShow1"
  70. :columns="devices1"
  71. @cancel="deviceShow1 = false"
  72. @confirm="selectDevice1"
  73. keyName="label"
  74. ></u-picker>
  75. <u-picker
  76. :show="show"
  77. :columns="timeColumns"
  78. @cancel="show = false"
  79. keyName="label"
  80. @confirm="selectSkipTime"
  81. ></u-picker>
  82. </div>
  83. <u-list
  84. :class="{ emptyhistory: historyData.length === 0 }"
  85. class="historycontainer"
  86. >
  87. <template v-if="historyData.length > 0">
  88. <u-list-item
  89. class="itemback"
  90. v-for="(item, index) in historyData"
  91. :key="index"
  92. >
  93. <u-row gutter="5" customStyle="margin-bottom: 10px">
  94. <u-col span="24">
  95. <u--text :text="item.ginstallpos || item.devicePos"></u--text>
  96. </u-col>
  97. </u-row>
  98. <u-row gutter="5" customStyle="margin-bottom: 10px">
  99. <u-col span="2" style="margin-right: 15rpx">
  100. <div v-if="item.netStatus == 0" class="error-tag1">
  101. <image
  102. src="/static/model/connectFalse.svg"
  103. alt=""
  104. class="icon-style"
  105. />
  106. <span style="float: right">断开</span>
  107. </div>
  108. <div v-else class="success-tag">
  109. <image
  110. src="/static/model/connectTrue.svg"
  111. alt=""
  112. class="icon-style"
  113. />
  114. <span style="float: right">连接</span>
  115. </div>
  116. </u-col>
  117. <u-col span="2"> </u-col>
  118. <u-col span="3"> </u-col>
  119. <u-col span="5">
  120. <u--text
  121. class="timetext"
  122. :text="item.time || item.ttime"
  123. ></u--text>
  124. </u-col>
  125. </u-row>
  126. <view style="display: flex; flex-wrap: wrap">
  127. <view
  128. class="datacard"
  129. v-for="(showitem, index) in deviceShowList"
  130. :key="index"
  131. v-if="
  132. showitem.appShow == 1 &&
  133. showitem.monitorcode != 'strinstallpos' &&
  134. showitem.monitorcode != 'gdevicename' &&
  135. showitem.monitorcode != 'ginstallpos' &&
  136. showitem.monitorcode != 'netStatus' &&
  137. showitem.monitorcode != 'warnFlag' &&
  138. showitem.monitorcode != 'gcreatetime' &&
  139. showitem.monitorcode != 'ttime' &&
  140. showitem.monitorcode != 'time'
  141. "
  142. >
  143. <view
  144. :class="TabCur"
  145. style="
  146. padding-top: 10rpx;
  147. padding-bottom: 10rpx;
  148. display: flex;
  149. flex-direction: column;
  150. align-items: center;
  151. height: 100%;
  152. "
  153. >
  154. <view
  155. v-if="showitem.monitorcode == 'doorUse'"
  156. class="demo-layout bg-purple-light"
  157. style="
  158. margin-top: 10rpx;
  159. color: #3787fe;
  160. font-size: 30rpx;
  161. margin-top: 20rpx;
  162. "
  163. >
  164. {{
  165. item[showitem.monitorcode] == "2"
  166. ? "行人"
  167. : item[showitem.monitorcode] == "1"
  168. ? "行车"
  169. : "-"
  170. }}
  171. </view>
  172. <view
  173. v-else
  174. class="demo-layout bg-purple-light"
  175. style="
  176. color: #3787fe;
  177. font-size: 30rpx;
  178. margin-top: 20rpx;
  179. "
  180. >
  181. {{
  182. item[showitem.monitorcode] == null ||
  183. item[showitem.monitorcode] == ""
  184. ? "-"
  185. : item[showitem.monitorcode]
  186. }}
  187. </view>
  188. <view
  189. class="demo-layout bg-purple-light"
  190. style="
  191. margin-top: 20rpx;
  192. color: #677799;
  193. margin-top: 20rpx;
  194. "
  195. >
  196. {{ showitem.des }}
  197. </view>
  198. </view>
  199. </view>
  200. </view>
  201. </u-list-item>
  202. </template>
  203. <template v-else>
  204. <div class="empty-message">数据为空</div>
  205. </template>
  206. </u-list>
  207. </view>
  208. </view>
  209. </view>
  210. </template>
  211. <script>
  212. import api from "@/api/api";
  213. import dayjs from "dayjs";
  214. import DeviceMenu from "../../pages/device/devicemenu/devicemenu";
  215. import uniDatetimePicker from "../../uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue";
  216. export default {
  217. data() {
  218. return {
  219. menushow: false,
  220. TabCur: "gate",
  221. curlist: [],
  222. colums: [],
  223. deviceList: {},
  224. startDate: null,
  225. endDate: null,
  226. show: false,
  227. deviceShow: false,
  228. deviceShow1: false,
  229. dataShow: false,
  230. dataShow1: false,
  231. StartTime: "", //起始时间
  232. EndTime: "", //结束时间
  233. devices: [], //设备类型数组
  234. devices1: [
  235. [
  236. {
  237. label: "1#风机",
  238. value: "fan1",
  239. },
  240. {
  241. label: "2#风机",
  242. value: "fan2",
  243. },
  244. ],
  245. ], //子设备类型数组
  246. deviceName: "", //设备名称
  247. labelName: "", //子设备名称
  248. timeS: "", //间隔时间
  249. lable: "", //间隔时间
  250. timeColumns: [
  251. [
  252. {
  253. label: "1秒",
  254. value: "1",
  255. value1: "1s",
  256. },
  257. {
  258. label: "5秒",
  259. value: "2",
  260. value1: "5s",
  261. },
  262. {
  263. label: "10秒",
  264. value: "3",
  265. value1: "10s",
  266. },
  267. {
  268. label: "30秒",
  269. value: "4",
  270. value1: "30s",
  271. },
  272. {
  273. label: "1分钟",
  274. value: "5",
  275. value1: "1m",
  276. },
  277. {
  278. label: "10分钟",
  279. value: "6",
  280. value1: "10m",
  281. },
  282. {
  283. label: "30分钟",
  284. value: "7",
  285. value1: "30m",
  286. },
  287. {
  288. label: "1小时",
  289. value: "8",
  290. value1: "1h",
  291. },
  292. ],
  293. ],
  294. deviceID: "", //设备ID
  295. deviceType1: "", //设备类型
  296. skip: 8, //时间间隔
  297. interval: "1h", //时间间隔
  298. dataTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  299. historyData: [], //历史数据
  300. deviceShowList: [],
  301. loading: false, // 是否正在加载数据
  302. hasMoreData: true, // 是否还有更多数据可以加载
  303. pageNo: 1, // 当前页码(用于请求数据时分页)
  304. stationType: "plc1", // 站点类型(用于请求数据时的参数)
  305. strType: "", //设备小类
  306. sysId: "",
  307. };
  308. },
  309. components: {
  310. DeviceMenu,
  311. uniDatetimePicker,
  312. },
  313. props: ["showColum", "deviceType"],
  314. watch: {
  315. deviceType: {
  316. async handler(data) {
  317. if (data) {
  318. this.TabCur = data;
  319. console.log(this.TabCur, "设备");
  320. this.curlist = this.deviceList[this.TabCur];
  321. if (this.curlist == null) {
  322. this.curlist = [];
  323. }
  324. // 选择设备分类,重新获取数据
  325. await this.loadData(this.TabCur);
  326. this.$emit("setMenushow", {
  327. menushow: false,
  328. });
  329. }
  330. },
  331. immediate: true,
  332. },
  333. },
  334. created() {
  335. this.colums = this.showColum;
  336. const startTime = new Date().getTime() - 3600 * 1000 * 24 * 30;
  337. const endTime = new Date();
  338. this.StartTime = dayjs(startTime).format("YYYY-MM-DD HH:mm:ss");
  339. this.EndTime = dayjs(endTime).format("YYYY-MM-DD HH:mm:ss");
  340. },
  341. mounted() {
  342. // this.TabCur = deviceType;
  343. if (this.TabCur.startsWith("sys_")) {
  344. this.loadNewData(this.TabCur);
  345. } else {
  346. this.loadData(this.TabCur);
  347. }
  348. },
  349. computed: {},
  350. methods: {
  351. loadData(type) {
  352. new Promise((resolve, reject) => {
  353. api
  354. .getDeviceMonitor({
  355. devicetype: type,
  356. pagetype: "normal",
  357. filterParams: {},
  358. })
  359. .then((response) => {
  360. if (response.data.code == 200) {
  361. this.deviceList[type] = response.data.result.msgTxt[0].datalist;
  362. this.curlist = this.deviceList[type];
  363. this.setData(this.curlist);
  364. } else {
  365. resolve(response);
  366. }
  367. })
  368. .catch((error) => {
  369. reject(error);
  370. });
  371. });
  372. },
  373. loadNewData(type) {
  374. return new Promise((resolve, reject) => {
  375. api
  376. .getSysList({
  377. strtype: type,
  378. pagetype: "normal",
  379. })
  380. .then((response) => {
  381. if (response.data.code == 200) {
  382. this.sysId = response.data.result.records[0].id;
  383. this.loadSysData({
  384. devicetype: "sys",
  385. systemID: this.sysId,
  386. });
  387. } else {
  388. resolve(response);
  389. }
  390. })
  391. .catch((error) => {
  392. // console.log("catch===>response", response);
  393. reject(error);
  394. });
  395. });
  396. },
  397. //加载场景类数据
  398. loadSysData(data) {
  399. new Promise((resolve, reject) => {
  400. api
  401. .getDeviceMonitor(data)
  402. .then((response) => {
  403. if (response.data.code == 200) {
  404. this.deviceList[this.TabCur] =
  405. response.data.result.msgTxt[0].datalist;
  406. this.curlist = this.deviceList[this.TabCur];
  407. this.setData(this.curlist);
  408. } else {
  409. resolve(response);
  410. }
  411. })
  412. .catch((error) => {
  413. reject(error);
  414. });
  415. });
  416. },
  417. //选择设备选择器 保存对应的id
  418. setData(data) {
  419. this.devices = [];
  420. data.forEach((element) => {
  421. const deviceObj = {
  422. // id: element.deviceID,
  423. ID: element.deviceID,
  424. strinstallpos: element.strinstallpos,
  425. type: element.deviceType,
  426. stationtype: element.stationtype,
  427. strtype: element.strtype,
  428. };
  429. this.devices.push(deviceObj);
  430. });
  431. },
  432. //选择设备 点击确定按钮 保存id
  433. selectDevice(e) {
  434. // this.deviceId = e.value[0].id;
  435. this.deviceID = e.value[0].ID;
  436. this.stationType = e.value[0].stationtype;
  437. this.strType = e.value[0].type;
  438. this.deviceType1 = e.value[0].type;
  439. this.deviceName = e.value[0].strinstallpos;
  440. this.deviceShow = false;
  441. let original = this.TabCur;
  442. let newName = original.startsWith("sys_")
  443. ? original.substring(4)
  444. : original;
  445. const key = newName + "_history";
  446. const key1 = this.deviceType1 + "_history";
  447. console.log(key1, "key1");
  448. if (this.showColum?.[key1]?.length > 0) {
  449. this.deviceShowList = this.showColum[key1];
  450. } else {
  451. this.deviceShowList = this.showColum[key] ?? [];
  452. }
  453. },
  454. selectDevice1(e) {
  455. this.deviceNum = e.value[0].value;
  456. this.labelName = e.value[0].label;
  457. this.deviceShow1 = false;
  458. },
  459. //选择起始时间
  460. selectStartTime(e) {
  461. const startTime = e.value;
  462. const formattedTime = dayjs(startTime).format("YYYY-MM-DD HH:mm:ss");
  463. this.StartTime = formattedTime;
  464. this.dataShow = false;
  465. },
  466. //选择结束时间
  467. selectEndTime(e) {
  468. const endTime = e.value;
  469. const formattedTime = dayjs(endTime).format("YYYY-MM-DD HH:mm:ss");
  470. this.EndTime = formattedTime;
  471. this.dataShow1 = false;
  472. },
  473. //选择间隔时间
  474. selectSkipTime(e) {
  475. this.skip = e.value[0].value;
  476. this.interval = e.value[0].value1;
  477. this.lable = e.value[0].label;
  478. this.show = false;
  479. },
  480. //查询历史数据 getDeviceHistory
  481. checkHistory() {
  482. this.historyData = []; // 清空历史数据
  483. if (this.stationType !== "redis") {
  484. this.getHistoryData();
  485. } else {
  486. this.getHistoryData2();
  487. }
  488. },
  489. // 查询历史数据
  490. getHistoryData() {
  491. const params = {
  492. ttime_begin: this.StartTime,
  493. ttime_end: this.EndTime,
  494. gdeviceid: this.deviceID,
  495. skip: this.skip,
  496. pageNo: this.pageNo,
  497. pageSize: 10,
  498. strtype: this.strType,
  499. };
  500. new Promise((resolve, reject) => {
  501. api
  502. .getDeviceHistory(params)
  503. .then((response) => {
  504. if (response.data.code == 200) {
  505. const infolist = response.data.result;
  506. const result = infolist.datalist.records;
  507. result.forEach((item) => {
  508. const newData = { ...item, ...item.readData };
  509. this.historyData.push(newData);
  510. });
  511. this.pageNo = response.data.result.datalist.current;
  512. } else {
  513. resolve(response);
  514. }
  515. })
  516. .catch((error) => {
  517. reject(error);
  518. });
  519. });
  520. },
  521. getHistoryData2() {
  522. const params = {
  523. startTime: this.StartTime,
  524. endTime: this.EndTime,
  525. deviceId: this.deviceID,
  526. interval: this.interval,
  527. isEmployee: this.TabCur.startsWith("vehicle") ? false : true,
  528. pageNum: 1,
  529. pageSize: 10,
  530. strtype:
  531. (this.TabCur.startsWith("sys_")
  532. ? this.TabCur.substring(4)
  533. : this.TabCur) + "*",
  534. };
  535. if (this.TabCur.startsWith("sys")) {
  536. params.sysId = this.sysId;
  537. }
  538. new Promise((resolve, reject) => {
  539. api
  540. .getDeviceHistory2(params)
  541. .then((response) => {
  542. if (response.data.code === 200) {
  543. const result1 = response.data.result;
  544. if (result1.records.length != 0) {
  545. this.historyData = result1.records;
  546. }
  547. // this.pageNo = response1.data.result.datalist.current;
  548. } else {
  549. resolve(response);
  550. }
  551. })
  552. .catch((error) => {
  553. reject(error);
  554. });
  555. });
  556. },
  557. async loadMoreData() {
  558. if (this.loading || !this.hasMoreData) return;
  559. this.loading = true;
  560. try {
  561. // 如果新数据为空,则表示没有更多数据了
  562. if (this.historyData.length === 0) {
  563. this.hasMoreData = false;
  564. } else {
  565. // 将新数据追加到数据列表中
  566. this.historyData = [...this.historyData, ...newData];
  567. // 更新页码以便下次加载下一页数据
  568. this.pageNo += 1;
  569. }
  570. } catch (error) {
  571. // 处理请求错误
  572. console.error("加载数据失败:", error);
  573. } finally {
  574. // 无论请求成功还是失败,都要将加载状态设置为false
  575. this.loading = false;
  576. }
  577. },
  578. devicemenuShow(e) {
  579. this.menushow = !this.menushow;
  580. },
  581. menuClick(id) {
  582. this.TabCur = id;
  583. console.log(this.TabCur);
  584. // 显示该分类的数据
  585. this.curlist = this.deviceList[this.TabCur];
  586. if (this.curlist == null) {
  587. this.curlist = [];
  588. }
  589. // 选择设备分类,重新获取数据
  590. this.loadData(this.TabCur);
  591. this.menushow = false;
  592. },
  593. },
  594. destroyed() {
  595. // 停止定时器
  596. },
  597. onReachBottom() {
  598. // uni-app提供的页面滚动到底部钩子函数
  599. console.log("上拉加载更多数据");
  600. // this.loadMoreData();
  601. },
  602. };
  603. </script>
  604. <style lang="scss" scoped>
  605. .emptyhistory {
  606. background: url("/static/empty.png") no-repeat;
  607. background-size: 100% 50%;
  608. }
  609. .empty-message {
  610. display: flex;
  611. justify-content: center;
  612. align-items: center;
  613. font-size: 16px;
  614. line-height: 80vh;
  615. color: #333;
  616. }
  617. .content {
  618. width: 100%;
  619. display: flex;
  620. flex-direction: column;
  621. }
  622. .main {
  623. /* margin-top: 100rpx; */
  624. display: flex;
  625. flex-direction: column;
  626. }
  627. .menupage {
  628. position: absolute;
  629. z-index: 2;
  630. top: 40rpx;
  631. height: calc(100% - 40rpx);
  632. width: 100%;
  633. }
  634. .btns {
  635. display: flex;
  636. }
  637. .btns > * {
  638. margin-right: 10px; /* 根据需要调整间距大小 */
  639. }
  640. /* 去除最后一个按钮的右边距 */
  641. .btns > *:last-child {
  642. margin-right: 0;
  643. }
  644. .flcard {
  645. padding: 20rpx;
  646. background-color: #ffffff;
  647. margin-bottom: 5rpx;
  648. }
  649. .itemback {
  650. padding: 20rpx;
  651. background-color: #ffffff;
  652. margin-bottom: 5rpx;
  653. }
  654. .datacard {
  655. width: 30%;
  656. margin: 1%;
  657. float: left;
  658. height: 190rpx;
  659. text-align: center;
  660. background: linear-gradient(
  661. to right,
  662. rgba(55, 135, 254, 0.08),
  663. rgba(4, 184, 255, 0.08),
  664. rgba(60, 161, 237, 0.08)
  665. );
  666. }
  667. .error-tag {
  668. border-radius: 10%;
  669. display: inline-block;
  670. color: #e90000;
  671. line-height: 50rpx;
  672. font-size: 14px;
  673. text-align: center;
  674. width: 120rpx;
  675. height: 50rpx;
  676. background-color: rgba(233, 0, 0, 0.2);
  677. }
  678. .error-tag1 {
  679. border-radius: 10%;
  680. display: inline-block;
  681. color: #696969;
  682. line-height: 50rpx;
  683. font-size: 14px;
  684. text-align: center;
  685. width: 120rpx;
  686. height: 50rpx;
  687. background-color: rgba(105, 105, 105, 0.2);
  688. }
  689. .success-tag {
  690. border-radius: 10%;
  691. color: #42c000;
  692. line-height: 50rpx;
  693. font-size: 14px;
  694. width: 120rpx;
  695. height: 50rpx;
  696. display: inline-block;
  697. background-color: rgba(226, 250, 214, 0.5);
  698. }
  699. .icon-style {
  700. margin: 15rpx;
  701. width: 14px;
  702. height: 14px;
  703. }
  704. </style>