电速宝
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. import * as echarts from '../../ec-canvas/echarts';
  2. import * as liquidFill from '../../ec-canvas/echarts-liquidfill.min';
  3. const util = require('../../utils/util.js')
  4. const app = getApp()
  5. const api = require('../../api/index.js');
  6. Page({
  7. data: {
  8. // 状态栏高度
  9. statusBarHeight: wx.getStorageSync('statusBarHeight'),
  10. // 导航栏高度
  11. navBarHeight: wx.getStorageSync('navBarHeight'),
  12. // 导航栏和状态栏高度
  13. navStatusBarHeight: wx.getStorageSync('navStatusBarHeight'),
  14. // 胶囊
  15. menu:wx.getStorageSync('menu'),
  16. mode: '',
  17. ec_dispace_charts: {
  18. lazyLoad: true
  19. },
  20. ec:{
  21. lazyLoad: true
  22. },
  23. img_path:'',
  24. ectricdata:[],
  25. between:{},
  26. currentDate:'',
  27. fitnessdata:[],
  28. rotation:0,
  29. objectArray: [],
  30. stationindex:0,
  31. stationName:wx.getStorageSync('station').stationName,
  32. timer1:'',
  33. timer2:'',
  34. timer3:'',
  35. chartInstance1:'',
  36. chartInstance2:'',
  37. chartInstance3:''
  38. },
  39. onLoad: function () {
  40. let _this =this
  41. const updateManager = wx.getUpdateManager();
  42. // 获取手机系统信息
  43. updateManager.onCheckForUpdate(function (res) {
  44. if (res.hasUpdate) {
  45. // 提示用户更新
  46. wx.showModal({
  47. title: '更新提示',
  48. content: '发现新版本,是否下载新版本并重启应用?',
  49. success: function (res) {
  50. if (res.confirm) {
  51. // 用户确认下载新版本
  52. downloadAndUpdate();
  53. } else if (res.cancel) {
  54. // 用户取消,可以选择后续操作
  55. }
  56. }
  57. });
  58. }
  59. });
  60. const now = new Date();
  61. const year = now.getFullYear();
  62. const month = now.getMonth() + 1;
  63. const date = now.getDate();
  64. const dateString = `${year}-${month}-${date}`;
  65. this.setData({
  66. currentDate: dateString // 假设在小程序页面中使用了data属性绑定了currentDate
  67. });
  68. // 获取组件
  69. this.ecComponent = this.selectComponent('#dispace_charts');
  70. this.ecline = this.selectComponent('#mychart_line');
  71. // this.initChart()
  72. // let base64imgages =util.Background_base64('https://esos-iot.bjdexn.cn/wx_images/bj.png')
  73. this.setData({
  74. img_path:'https://esos-iot.bjdexn.cn/wx_images/bj.png'
  75. })
  76. // this.rotateImage()
  77. },
  78. downloadAndUpdate() {
  79. wx.downloadFile({
  80. url: 'https://example.com/path/to/new/version.zip', // 新版本小程序包的下载链接
  81. success: function (res) {
  82. if (res.statusCode === 200) {
  83. // 下载成功,保存临时文件路径
  84. const tempFilePath = res.tempFilePath;
  85. // 应用新版本
  86. applyUpdate(tempFilePath);
  87. } else {
  88. // 下载失败,处理异常
  89. }
  90. }
  91. });
  92. },
  93. applyUpdate(tempFilePath) {
  94. let _this = this
  95. wx.applyUpdate({
  96. filePath: tempFilePath,
  97. success: function () {
  98. // 新版本应用成功
  99. // _this.getlist()
  100. wx.navigateTo({
  101. url: '/pages/login/index',
  102. })
  103. }
  104. });
  105. },
  106. // 图片旋转
  107. rotateImage() {
  108. const currentRotation = this.data.rotation + 180; // 每次增加180度
  109. const animation = wx.createAnimation({
  110. duration: 300, // 动画时长
  111. timingFunction: 'ease', // 动画效果
  112. });
  113. animation.rotate(currentRotation).step();
  114. this.setData({
  115. animationData: animation.export(),
  116. rotation: currentRotation
  117. });
  118. },
  119. // 电站列表
  120. bindPickerChange (e) {
  121. console.log('选中项改变,携带值为', e.detail.value);
  122. let _this = this
  123. const station = this.data.objectArray[e.detail.value];
  124. wx.setStorage({
  125. key: 'station', // 本地缓存中指定的 key
  126. data: station,
  127. success: function () {
  128. _this.setData({
  129. stationName:wx.getStorageSync('station').stationName
  130. })
  131. clearInterval(_this.timer1);
  132. clearInterval(_this.timer2);
  133. clearInterval(_this.timer3);
  134. _this.getlist()
  135. }
  136. });
  137. },
  138. getlist(){
  139. // 电站信息
  140. api.request(`/station/ectric/${wx.getStorageSync('station').id}`, 'POST')
  141. .then((res) => {
  142. this.setData({
  143. stationName:wx.getStorageSync('station').stationName,
  144. ectricdata:res.data
  145. })
  146. // console.log('请求成功:', res);
  147. // 先卸载旧图表
  148. this.beforeDestroy();
  149. // 重新初始化图表
  150. this.initCharts(res.data.quantity/100);
  151. // 在这里处理返回的数据
  152. })
  153. .catch((err) => {
  154. console.error('请求失败:', err);
  155. // 在这里处理请求失败的情况
  156. });
  157. // 电量信息
  158. api.request(`/stats/singleStation/${wx.getStorageSync('station').id}/between/time`, 'POST',{startDate:this.data.currentDate,endDate:this.data.currentDate})
  159. .then((res) => {
  160. if (res.data.length>0) {
  161. this.setData({
  162. between:res.data,
  163. })
  164. }else{
  165. this.setData({
  166. between:{
  167. list:[{chargetime: 0,
  168. dischargetime: 0,
  169. ym: "2024-07-08"},
  170. ],
  171. total:{accumulatedchargetime: "0",
  172. accumulateddischargetime: "0"} ,
  173. zonghe:{
  174. accumulatedchargetime: "0",
  175. accumulateddischargetime: "0"
  176. }
  177. },
  178. })
  179. }
  180. this.beforeDestroys();
  181. this.initChartright()
  182. })
  183. .catch((err) => {
  184. console.error('请求失败:', err);
  185. // 在这里处理请求失败的情况
  186. });
  187. //设备监测
  188. this.getfitness()
  189. this.timer1=setInterval(() => {
  190. this.getfitness()
  191. }, 10000)
  192. },
  193. // 获取首页数据
  194. getfitness(){
  195. api.request(`/station/fitness/${wx.getStorageSync('station').id}`, 'POST')
  196. .then((res) => {
  197. this.setData({
  198. fitnessdata:res.data,
  199. })
  200. // console.log('请求成功:', res);
  201. // 在这里处理返回的数据
  202. })
  203. .catch((err) => {
  204. console.error('请求失败:', err);
  205. // 在这里处理请求失败的情况
  206. });
  207. },
  208. // 卸载图表,清理资源
  209. unloadCharts: function () {
  210. if (this.charts) {
  211. this.charts.dispose(); // 销毁图表实例
  212. this.charts = null; // 置空图表实例
  213. }
  214. },
  215. // 电容量
  216. initChartright: function () {
  217. let _this = this;
  218. // 初始化图表
  219. this.ecline.init((canvas, width, height, dpr) => {
  220. const chart = echarts.init(canvas, null, {
  221. width: width,
  222. height: height,
  223. devicePixelRatio: dpr
  224. });
  225. // canvas.setChart(chart);
  226. // 初始数据
  227. let initialData = {
  228. accumulatedchargetime: _this.data.between.total.accumulatedchargetime,
  229. accumulateddischargetime: _this.data.between.total.accumulateddischargetime
  230. };
  231. // 设置初始选项
  232. let option = _this.getChartOption(initialData);
  233. chart.setOption(option);
  234. _this.chartInstance2 = chart;
  235. _this.startDataUpdates();
  236. return chart;
  237. });
  238. },
  239. // 定义更新数据函数
  240. updateChartData(newData) {
  241. let updatedOption = this.getChartOption(newData);
  242. this.chartInstance2.setOption(updatedOption);
  243. },
  244. // 更新图表数据函数
  245. startDataUpdates() {
  246. this.timer2 = setInterval(() => {
  247. api.request(`/stats/singleStation/${wx.getStorageSync('station').id}/between/time`, 'POST',{startDate:this.data.currentDate,endDate:this.data.currentDate})
  248. .then((res) => {
  249. if (res.data.length>0) {
  250. this.setData({
  251. between:res.data,
  252. })
  253. }else{
  254. this.setData({
  255. between:{
  256. list:[{chargetime: 0,
  257. dischargetime: 0,
  258. ym: "2024-07-08"},
  259. ],
  260. total:{accumulatedchargetime: "0",
  261. accumulateddischargetime: "0"} ,
  262. zonghe:{
  263. accumulatedchargetime: "0",
  264. accumulateddischargetime: "0"
  265. }
  266. },
  267. })
  268. }
  269. let initialData = {
  270. accumulatedchargetime: this.data.between.total.accumulatedchargetime,
  271. accumulateddischargetime: this.data.between.total.accumulateddischargetime
  272. };
  273. this.updateChartData(initialData)
  274. })
  275. .catch((err) => {
  276. console.error('请求失败:', err);
  277. // 在这里处理请求失败的情况
  278. });
  279. }, 10000);
  280. },
  281. // 辅助函数,用于生成图表选项
  282. getChartOption: function (data) {
  283. let _this = this;
  284. return {
  285. backgroundColor: "#ffffff",
  286. legend: [{
  287. left: '0',
  288. bottom: 20,
  289. itemHeight: 8,
  290. itemWidth: 8,
  291. icon: "circle",
  292. formatter: function (name) {
  293. return name + ` ${data.accumulatedchargetime} kwh`;
  294. },
  295. data: [{ name: '今日充电' }]
  296. }, {
  297. left: '0',
  298. bottom: 0,
  299. itemHeight: 8,
  300. itemWidth: 8,
  301. icon: "circle",
  302. formatter: function (name) {
  303. return name + ` ${data.accumulateddischargetime} kwh`;
  304. },
  305. data: [{ name: '今日放电' }]
  306. }],
  307. tooltip: {
  308. trigger: 'item'
  309. },
  310. series: [{
  311. type: 'pie',
  312. radius: ['40%', '60%'],
  313. center: ['50%', '40%'],
  314. avoidLabelOverlap: false,
  315. label: {
  316. show: false,
  317. position: 'center'
  318. },
  319. emphasis: {
  320. label: {
  321. show: true,
  322. fontSize: 0,
  323. fontWeight: 'bold'
  324. }
  325. },
  326. labelLine: {
  327. show: false
  328. },
  329. data: [{
  330. value: data.accumulatedchargetime,
  331. name: '今日充电',
  332. itemStyle: { color: '#4DF096' }
  333. }, {
  334. value: data.accumulateddischargetime,
  335. name: '今日放电',
  336. itemStyle: { color: '#3C9EFA' }
  337. }]
  338. }]
  339. };
  340. },
  341. // 设备详情
  342. onIconTap(){
  343. wx.navigateTo({
  344. url: '/pages/details/index',
  345. })
  346. },
  347. // 设备健康
  348. onhealthy(){
  349. wx.navigateTo({
  350. url: '/pages/healthy/index',
  351. })
  352. },
  353. // 设备信息
  354. ondetails(){
  355. wx.navigateTo({
  356. url: '/pages/monitor/index',
  357. })
  358. },
  359. // 水波图
  360. initCharts(value) {
  361. let _this = this;
  362. this.ecComponent.init((canvas, width, height, dpr) => {
  363. const charts = echarts.init(canvas, null, {
  364. width: width,
  365. height: height,
  366. devicePixelRatio: dpr
  367. });
  368. // 将初始的数值转换为数据
  369. let data = [value, value, value];
  370. // 初始化图表选项
  371. let option = _this.getChartOption1(value, data);
  372. charts.setOption(option);
  373. // 存储图表实例
  374. _this.chartInstance3 = charts;
  375. // 启动定时器更新数据
  376. _this.startDataUpdate();
  377. // 返回图表实例
  378. return charts;
  379. });
  380. },
  381. // 更新图表数据函数
  382. updateChartData1(value, data) {
  383. let updatedOption = this.getChartOption1(value, data);
  384. this.chartInstance3.setOption(updatedOption);
  385. },
  386. // 启动定时器更新数据
  387. startDataUpdate() {
  388. let _this =this
  389. _this.timer3 = setInterval(() => {
  390. // 获取新数据
  391. api.request(`/station/ectric/${wx.getStorageSync('station').id}`, 'POST')
  392. .then((res) => {
  393. // 更新数据
  394. let data = [res.data.quantity / 100, res.data.quantity / 100, res.data.quantity / 100];
  395. _this.updateChartData1(res.data.quantity / 100, data);
  396. // console.log('请求成功:', res);
  397. })
  398. .catch((err) => {
  399. console.error('请求失败:', err);
  400. // 处理请求失败情况
  401. });
  402. }, 10000);
  403. },
  404. // Vue.js 生命周期钩子
  405. beforeDestroy() {
  406. // 清除定时器
  407. if (this.timer3) {
  408. clearInterval(this.timer3);
  409. }
  410. // 销毁图表实例
  411. if (this.chartInstance3) {
  412. this.chartInstance3.dispose(); // 销毁图表
  413. this.chartInstance3 = null;
  414. }
  415. },
  416. beforeDestroys() {
  417. // 清除定时器
  418. if (this.timer2) {
  419. clearInterval(this.timer2);
  420. }
  421. // 销毁图表实例
  422. if (this.chartInstance2) {
  423. this.chartInstance2.dispose(); // 销毁图表
  424. this.chartInstance2 = null;
  425. }
  426. },
  427. // 辅助函数,用于生成图表选项
  428. getChartOption1: function (value, data) {
  429. let color = (value * 100).toFixed(0) < 70 ? '#004403' : '#fff';
  430. return {
  431. title: {
  432. text: (value * 100).toFixed(0) + '%',
  433. textStyle: {
  434. fontFamily: 'Microsoft Yahei',
  435. fontWeight: 'normal',
  436. color: color,
  437. rich: {
  438. a: {
  439. fontSize: 18,
  440. }
  441. }
  442. },
  443. x: 'center',
  444. y: '40%'
  445. },
  446. series: [
  447. {
  448. type: 'liquidFill',
  449. radius: '80%',
  450. center: ['50%', '50%'],
  451. // shape: 'roundRect',
  452. data: data,
  453. color: {
  454. type: 'linear',
  455. x: 0,
  456. y: 0,
  457. x2: 0,
  458. y2: 1,
  459. colorStops: [{
  460. offset: 0,
  461. color: 'rgba(0, 117, 68,0)'
  462. },{
  463. offset: 0.5,
  464. color: 'rgba(0, 117, 68, .5)'
  465. }, {
  466. offset: 1,
  467. color: 'rgba(0, 117, 68,1)'
  468. }],
  469. globalCoord: false
  470. },
  471. // 边框
  472. outline: {
  473. borderDistance: 2,
  474. itemStyle: {
  475. borderWidth: 5,
  476. borderColor: {
  477. type: 'linear',
  478. x: 0,
  479. y: 0,
  480. x2: 0,
  481. y2: 1,
  482. colorStops: [{
  483. offset: 0,
  484. color: 'rgba(0, 117, 68, .25)'
  485. },{
  486. offset: 0.5,
  487. color: 'rgba(0, 117, 68, .25)'
  488. }, {
  489. offset: 1,
  490. color: 'rgba(0, 117, 68, .25)'
  491. }],
  492. globalCoord: false
  493. },
  494. shadowBlur: 0,
  495. shadowColor: '#ffffff',
  496. }
  497. },
  498. backgroundStyle: {
  499. borderWidth: 0,
  500. color: {
  501. type: 'linear',
  502. x: 0,
  503. y: 0,
  504. x2: 0,
  505. y2: 1,
  506. colorStops: [{
  507. offset: 1,
  508. color: 'rgba(255, 255, 255, 1)'
  509. }, {
  510. offset: 0.5,
  511. color: 'rgba(255, 255, 255, .25)'
  512. }, {
  513. offset: 0,
  514. color: 'rgba(255, 255, 255, 1)'
  515. }],
  516. globalCoord: false
  517. },
  518. },
  519. label: {
  520. normal: {
  521. formatter: '',
  522. }
  523. }
  524. }]
  525. };
  526. },
  527. /**
  528. * 生命周期函数--监听页面卸载
  529. */
  530. onShow(){
  531. let _this =this
  532. // 电站列表stationindex
  533. api.request('/station/list/stationName', 'POST')
  534. .then((res) => {
  535. _this.setData({
  536. objectArray:res.data
  537. })
  538. if (wx.getStorageSync('station').id) {
  539. for (let index = 0; index < _this.data.objectArray.length; index++) {
  540. if (_this.data.objectArray[index].id==wx.getStorageSync('station').id) {
  541. _this.setData({
  542. stationindex:index
  543. })
  544. }
  545. }
  546. _this.getlist()
  547. }else{
  548. wx.setStorage({
  549. key: 'station', // 本地缓存中指定的 key
  550. data: res.data[0],
  551. success: function () {
  552. _this.getlist()
  553. }
  554. });
  555. }
  556. })
  557. .catch((err) => {
  558. console.error('请求失败:', err);
  559. // 在这里处理请求失败的情况
  560. });
  561. },
  562. onHide() {
  563. clearInterval(this.timer1);
  564. // clearInterval(_this.timer3);
  565. this.beforeDestroy()
  566. this.beforeDestroys()
  567. },
  568. });