newhistoryfile.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <template>
  2. <view style="background-color: #f7f7f7; height: 100vh;padding-top: 88rpx;">
  3. <!-- <view class="status_bar"></view> -->
  4. <view>
  5. <uni-nav-bar @clickLeft="clickLeft" left-icon="back" title="历史数据" backgroundColor="#f7f7f7"></uni-nav-bar>
  6. </view>
  7. <view style="padding: 0 48rpx; background-color: #f7f7f7;">
  8. <u-calendar
  9. v-model="dataShow"
  10. :mode="mode"
  11. @change="tiemchange"
  12. :maxDate="maxDate"
  13. range-color="#999"
  14. btn-type="success"
  15. active-bg-color="#0BBC58"
  16. range-bg-color="rgba(11,188,88,0.13)"
  17. ></u-calendar>
  18. <view class="date" @click="dataShow = true">
  19. <span>选择日期 </span>
  20. <span style="color: #cccccc;">
  21. {{start_time}} <span style="margin: 0 10rpx;"> /</span> {{end_time}}
  22. <u-icon style="margin-left: 20rpx;" name="arrow-right" size="28"></u-icon>
  23. </span>
  24. </view>
  25. <view class="title">温湿度数据</view>
  26. <view class="shuju_one">
  27. <view class="canvastishi" v-if="!canvastishiTF && !dataloadingtf">
  28. 暂无数据
  29. </view>
  30. <view class="canvastishi" v-if="dataloadingtf">
  31. <p class="dataloading">加载中</p>
  32. </view>
  33. <canvas v-if="canvastishiTF" canvas-id="canvasColumnA" id="canvasColumnA" class="charts"
  34. @touchstart="touchLineA($event)" @touchmove="moveLineA($event)" @touchend="touchEndLineA($event)"
  35. disable-scroll=true
  36. :style="{'width':cWidth*pixelRatio+'px','height':cHeight*pixelRatio+'px', 'transform': 'scale('+(1/pixelRatio)+')','margin-left':-cWidth*(pixelRatio-1)/2+'px','margin-top':-cHeight*(pixelRatio-1)/2+'px'}"></canvas>
  37. </view>
  38. <!-- <pest-line
  39. :device_type_id="device_type"
  40. :d_id="d_id"
  41. :start_time="start_time"
  42. :end_time="end_time"
  43. :notify="notify"
  44. ></pest-line> -->
  45. <view class="title">监测数据
  46. <u-button size="mini" :loading='refreshLoading' class="refresh" throttle-time="500" @click="refresh"
  47. :customStyle="customStyle">刷 新</u-button>
  48. </view>
  49. <view class="condition">
  50. <scroll-view scroll-top="0" scroll-x="true" class="scroll-X">
  51. <table class="table" :style="tableStyle">
  52. <tr class="tr">
  53. <th style="background-color: #E6E8EB;" class="th" v-for="(value,key) in thdata" :key="key">
  54. {{value}}</th>
  55. </tr>
  56. <tr class="tr" v-for="(items,indexs) in historylistdata" :key="'b'+indexs" v-if="!forbidden">
  57. <td class="td" v-for="(value,key) in thdata">{{items[key]}}</td>
  58. </tr>
  59. <tr class="tr" v-if="forbidden">
  60. <td class="td" v-for="item in Object.keys(thdata)">暂无数据</td>
  61. </tr>
  62. </table>
  63. </scroll-view>
  64. <view class="pagenumber">
  65. <button @click="prev">上一页</button>
  66. <view class="pagenumber_page">
  67. 第 {{page}} 页
  68. </view>
  69. <view class="pagenumber_page">
  70. 共 {{pagesum}} 页
  71. </view>
  72. <button @click="next" :disabled="forbidden">下一页</button>
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import uCharts from '../../../../components/js_sdk/u-charts/u-charts/u-charts.js';
  81. import pestLine from './pestLine';
  82. var canvaColumnA = null;
  83. export default {
  84. data() {
  85. return {
  86. maxDate: '2050-12-31',
  87. styles: {
  88. // width: "650rpx",
  89. height: "400rpx"
  90. },
  91. d_id: '',
  92. start_time: "",
  93. end_time: "",
  94. historydatas: [],
  95. titletext: ["24小时", "近一个月", "近半年", "近一年"],
  96. titleidnex: 0,
  97. device_id: '',
  98. page: 1,
  99. historylistdata: [],
  100. thdata: {},
  101. forbidden: false,
  102. cWidth: '400',
  103. cHeight: '400',
  104. pixelRatio: 1,
  105. canvastishiTF: false, //暂无数据提示
  106. dataloadingtf: true, //加载中提示
  107. pagesum: 1,
  108. device_type: "",
  109. dataShow: false,
  110. mode: 'range',
  111. refreshLoading: false,
  112. notify: 0
  113. }
  114. },
  115. components: {
  116. pestLine
  117. },
  118. computed: {
  119. customStyle() {
  120. return {
  121. background: '#018B3F',
  122. fontSize: '12px',
  123. border: 'none',
  124. color: '#fff'
  125. }
  126. },
  127. tableStyle() {
  128. return {
  129. width: Object.keys(this.thdata).length * 150 + 'px'
  130. }
  131. }
  132. },
  133. methods: {
  134. tiemchange(e) {
  135. console.log(e)
  136. this.start_time = e.startDate
  137. this.end_time = e.endDate
  138. this.history()
  139. this.historylist()
  140. this.notify++
  141. },
  142. async history() { //历史数据列表折线图
  143. this.dataloadingtf = true
  144. const res = await this.$myRequest({
  145. url: '/api/api_gateway?method=new_gateway.data_info.temp_hum',
  146. data: {
  147. device_type_id: this.device_type,
  148. id: this.d_id,
  149. start: +new Date(this.start_time + ' 00:00:00') / 1000,
  150. end: +new Date(this.end_time + ' 23:59:59') / 1000
  151. }
  152. })
  153. this.dataloadingtf = false
  154. let {
  155. data,
  156. title
  157. } = res
  158. if (!data) {
  159. this.canvastishiTF = false
  160. } else {
  161. this.canvastishiTF = true
  162. if (title.addtime) delete title.addtime
  163. let xtitle = []
  164. let chartData = []
  165. if (data.length > 0) {
  166. data.forEach(item => {
  167. // 处理时间戳
  168. xtitle.push(item.addtime.split(' ')[0])
  169. })
  170. Object.keys(title).forEach(key => {
  171. let obj = {
  172. name: title[key],
  173. index: key === 'lux' ? 1 : 0,
  174. data: []
  175. }
  176. for (var i = 0; i < data.length; i++) {
  177. if (key in data[i]) {
  178. // 判断data中是否值
  179. obj.data.push(data[i][key])
  180. }
  181. }
  182. chartData.push(obj)
  183. })
  184. console.log('温湿度数据:', chartData)
  185. }
  186. let yAxis = {
  187. data: [{
  188. position: 'left'
  189. }, {
  190. position: 'right'
  191. }]
  192. }
  193. // console.log('----温度:',obj)
  194. this.showColumn("canvasColumnA", xtitle, chartData, yAxis)
  195. }
  196. },
  197. //历史数据列表
  198. async historylist() {
  199. const res = await this.$myRequest({
  200. url: '/api/api_gateway?method=new_gateway.data_info.history_data',
  201. data: {
  202. device_type_id: this.device_type,
  203. id: this.d_id,
  204. start: +new Date(this.start_time + ' 00:00:00') / 1000,
  205. end: +new Date(this.end_time + ' 23:59:59') / 1000,
  206. page: this.page
  207. }
  208. })
  209. this.refreshLoading = false
  210. // this.thdata = Object.values(res.title)
  211. Object.keys(res.title).forEach(item => {
  212. this.$set(this.thdata, item, res.title[item])
  213. })
  214. this.historylistdata = res.data
  215. this.pagesum = Math.ceil(res.total / 10) || 1
  216. if (res.data.length == 0) {
  217. this.forbidden = true
  218. } else {
  219. this.forbidden = false
  220. }
  221. console.log(this.historylistdata, 'historylistdata')
  222. },
  223. // forecast.send_control.get_device_config 获取当前时间的数据
  224. async newdata() {
  225. const res = await this.$myRequest({
  226. url: '/api/api_gateway?method=forecast.send_control.get_device_config',
  227. data: {
  228. device_type_id: 3,
  229. d_id: this.d_id,
  230. control_type: "data"
  231. }
  232. })
  233. if (res) {
  234. uni.showToast({
  235. title: '刷新成功',
  236. duration: 2000,
  237. icon: "none"
  238. });
  239. this.historylist()
  240. } else {
  241. uni.showToast({
  242. title: '刷新失败',
  243. duration: 2000,
  244. icon: "none"
  245. });
  246. }
  247. },
  248. refresh() { //获取当前时间的数据
  249. this.refreshLoading = true
  250. this.historylist()
  251. },
  252. prev() { //上一页
  253. if (this.page > 1) {
  254. this.page--
  255. this.historylist()
  256. }
  257. },
  258. next() { //下一页
  259. if (this.page < this.pagesum) {
  260. this.page++
  261. this.historylist()
  262. }
  263. },
  264. clickLeft() {
  265. uni.navigateBack({
  266. delta: 1
  267. })
  268. },
  269. showColumn(id, xtitle, xinfo, yAxis) {
  270. let _self = this
  271. const ctx = uni.createCanvasContext(id, this);
  272. canvaColumnA = new uCharts({
  273. context: ctx,
  274. type: 'line',
  275. legend: {
  276. position: "top"
  277. },
  278. fontSize: 11,
  279. background: '#FFFFFF',
  280. pixelRatio: 1,
  281. animation: true,
  282. dataLabel: false,
  283. categories: xtitle,
  284. series: xinfo,
  285. enableScroll: true, //开启图表拖拽功能
  286. xAxis: {
  287. disableGrid: true,
  288. type: 'grid',
  289. gridType: 'dash',
  290. itemCount: 4, //x轴单屏显示数据的数量,默认为5个
  291. scrollShow: true, //新增是否显示滚动条,默认false
  292. // scrollAlign: 'left', //滚动条初始位置
  293. scrollBackgroundColor: '#F7F7FF', //默认为 #EFEBEF
  294. scrollColor: '#DEE7F7', //默认为 #A6A6A6
  295. },
  296. yAxis: yAxis || {},
  297. width: _self.cWidth * 1,
  298. height: _self.cHeight * 1,
  299. extra: {
  300. line: {
  301. type: 'curve'
  302. }
  303. }
  304. });
  305. },
  306. touchLineA(e) {
  307. console.log(e)
  308. canvaColumnA.scrollStart(e);
  309. },
  310. moveLineA(e) {
  311. canvaColumnA.scroll(e);
  312. },
  313. touchEndLineA(e) {
  314. canvaColumnA.scrollEnd(e);
  315. //下面是toolTip事件,如果滚动后不需要显示,可不填写
  316. canvaColumnA.showToolTip(e, {
  317. format: function(item, category) {
  318. return category + ' ' + item.name + ':' + item.data
  319. }
  320. });
  321. },
  322. },
  323. onLoad(option) {
  324. this.d_id = option.d_id
  325. this.device_id = option.device_id
  326. this.device_type = option.device_type
  327. this.cWidth = uni.upx2px(650);
  328. this.cHeight = uni.upx2px(500);
  329. let endTime = +new Date() / 1000
  330. let startTime = endTime - 60 * 60 * 24
  331. this.end_time = this.formatTime(endTime * 1000, 'yyyy-MM-dd')
  332. console.log(this.end_time)
  333. this.start_time = this.formatTime(startTime * 1000, 'yyyy-MM-dd')
  334. setTimeout(() => {
  335. this.history()
  336. this.historylist()
  337. }, 1000)
  338. },
  339. onShow() {
  340. // this.end_time = +new Date()/1000
  341. // this.start_time = this.end_time - 24 * 60 * 60
  342. // // this.formatTime(date, 'yyyy-MM-dd')
  343. // setTimeout(()=>{
  344. // this.history()
  345. // this.historylist()
  346. // },1000)
  347. },
  348. onPullDownRefresh() {
  349. this.history()
  350. this.historylist()
  351. setTimeout(() => {
  352. uni.stopPullDownRefresh()
  353. }, 1000)
  354. },
  355. }
  356. </script>
  357. <style lang="scss" scoped>
  358. /deep/.u-calendar__action {
  359. display: flex;
  360. justify-content: space-around;
  361. .u-calendar__action__text {
  362. line-height: 25px;
  363. }
  364. }
  365. page {
  366. background: #f7f7f7;
  367. }
  368. .title {
  369. color: #999999;
  370. margin: 32rpx 0;
  371. }
  372. .date {
  373. height: 92rpx;
  374. line-height: 92rpx;
  375. border-radius: 92rpx;
  376. background: #fff;
  377. display: flex;
  378. justify-content: space-between;
  379. margin-top: 16rpx;
  380. padding: 0 48rpx;
  381. position: relative;
  382. }
  383. .shuju_one,
  384. .shuju_two {
  385. // position: absolute;
  386. // top: 54px;
  387. width: 100%;
  388. // left: 5%;
  389. // box-shadow: 0 0 10rpx #bcb9ca;
  390. padding-top: 20rpx;
  391. height: 550rpx;
  392. background-color: #fff;
  393. border-radius: 24rpx;
  394. .canvastishi {
  395. font-size: 32rpx;
  396. position: absolute;
  397. top: 50%;
  398. left: 50%;
  399. margin-left: -64rpx;
  400. margin-top: -21rpx;
  401. .dataloading:after {
  402. overflow: hidden;
  403. display: inline-block;
  404. vertical-align: bottom;
  405. animation: ellipsis 2s infinite;
  406. content: "\2026";
  407. }
  408. @keyframes ellipsis {
  409. from {
  410. width: 2px;
  411. }
  412. to {
  413. width: 15px;
  414. }
  415. }
  416. }
  417. }
  418. .refresh {
  419. // position: absolute;
  420. // top: 700rpx;
  421. // left: 5%;
  422. font-size: 12px;
  423. float: right;
  424. // padding: 0 10rpx;
  425. // height: 40rpx;
  426. // border-radius: 8rpx;
  427. // background-color: #018B3F;
  428. // color: #FFFFFF;
  429. // line-height: 40rpx;
  430. // text-align: center;
  431. }
  432. .condition {
  433. // position: absolute;
  434. // top: 770rpx;
  435. display: flex;
  436. flex-wrap: wrap;
  437. width: 100%;
  438. // left: 5%;
  439. // box-shadow: 0 0 10rpx #bcb9ca;
  440. margin-bottom: 30rpx;
  441. .scroll-X {
  442. width: 100%;
  443. margin: 20rpx auto;
  444. .table {
  445. width: 1620px;
  446. }
  447. .tr {
  448. display: flex;
  449. overflow: hidden;
  450. .th,
  451. .td {
  452. display: inline-block;
  453. padding: 4rpx;
  454. width: 286rpx;
  455. text-align: center;
  456. height: 52rpx;
  457. line-height: 52rpx;
  458. border: 2rpx solid #F1F1F1;
  459. }
  460. .th:first-child,
  461. .td:first-child {
  462. width: 300rpx;
  463. }
  464. }
  465. .tr:nth-child(2n-1) {
  466. // background-color: #f5fff8;
  467. }
  468. .tr:first-child {
  469. // background-color: #E6E8EB;
  470. color: #31373D;
  471. }
  472. }
  473. .pagenumber {
  474. display: flex;
  475. margin: 20rpx auto;
  476. button {
  477. width: 150rpx;
  478. height: 50rpx;
  479. line-height: 50rpx;
  480. font-size: 26rpx;
  481. text-align: center;
  482. background-color: #17BB89;
  483. color: #FFFFFF;
  484. }
  485. .pagenumber_page {
  486. width: 100rpx;
  487. height: 50rpx;
  488. line-height: 50rpx;
  489. font-size: 26rpx;
  490. text-align: center;
  491. }
  492. }
  493. }
  494. </style>