储能智慧云小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index copy.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. }
  101. });
  102. },
  103. // 图片旋转
  104. rotateImage() {
  105. const currentRotation = this.data.rotation + 180; // 每次增加180度
  106. const animation = wx.createAnimation({
  107. duration: 300, // 动画时长
  108. timingFunction: 'ease', // 动画效果
  109. });
  110. animation.rotate(currentRotation).step();
  111. this.setData({
  112. animationData: animation.export(),
  113. rotation: currentRotation
  114. });
  115. },
  116. // 电站列表
  117. bindPickerChange (e) {
  118. console.log('选中项改变,携带值为', e.detail.value);
  119. let _this = this
  120. const station = this.data.objectArray[e.detail.value];
  121. wx.setStorage({
  122. key: 'station', // 本地缓存中指定的 key
  123. data: station,
  124. success: function () {
  125. _this.setData({
  126. stationName:wx.getStorageSync('station').stationName
  127. })
  128. clearInterval(_this.timer1);
  129. clearInterval(_this.timer2);
  130. clearInterval(_this.timer3);
  131. _this.getlist()
  132. }
  133. });
  134. },
  135. getlist(){
  136. // 电站信息
  137. api.request(`/station/ectric/${wx.getStorageSync('station').id}`, 'POST')
  138. .then((res) => {
  139. this.setData({
  140. stationName:wx.getStorageSync('station').stationName,
  141. ectricdata:res.data
  142. })
  143. // console.log('请求成功:', res);
  144. // 先卸载旧图表
  145. this.beforeDestroy();
  146. // 重新初始化图表
  147. this.initCharts(res.data.quantity/100);
  148. // 在这里处理返回的数据
  149. })
  150. .catch((err) => {
  151. console.error('请求失败:', err);
  152. // 在这里处理请求失败的情况
  153. });
  154. // 电量信息
  155. api.request(`/stats/singleStation/${wx.getStorageSync('station').id}/between/time`, 'POST',{startDate:this.data.currentDate,endDate:this.data.currentDate})
  156. .then((res) => {
  157. if (res.data.length>0) {
  158. this.setData({
  159. between:res.data,
  160. })
  161. }else{
  162. this.setData({
  163. between:{
  164. list:[{chargetime: 0,
  165. dischargetime: 0,
  166. ym: "2024-07-08"},
  167. ],
  168. total:{accumulatedchargetime: "0",
  169. accumulateddischargetime: "0"} ,
  170. zonghe:{
  171. accumulatedchargetime: "0",
  172. accumulateddischargetime: "0"
  173. }
  174. },
  175. })
  176. }
  177. this.beforeDestroys();
  178. this.initChartright()
  179. })
  180. .catch((err) => {
  181. console.error('请求失败:', err);
  182. // 在这里处理请求失败的情况
  183. });
  184. //设备监测
  185. this.getfitness()
  186. this.timer1=setInterval(() => {
  187. this.getfitness()
  188. }, 10000)
  189. },
  190. // 获取首页数据
  191. getfitness(){
  192. api.request(`/station/fitness/${wx.getStorageSync('station').id}`, 'POST')
  193. .then((res) => {
  194. this.setData({
  195. fitnessdata:res.data,
  196. })
  197. // console.log('请求成功:', res);
  198. // 在这里处理返回的数据
  199. })
  200. .catch((err) => {
  201. console.error('请求失败:', err);
  202. // 在这里处理请求失败的情况
  203. });
  204. },
  205. // 卸载图表,清理资源
  206. unloadCharts: function () {
  207. if (this.charts) {
  208. this.charts.dispose(); // 销毁图表实例
  209. this.charts = null; // 置空图表实例
  210. }
  211. },
  212. // 电容量
  213. initChartright: function () {
  214. let _this = this;
  215. // 初始化图表
  216. this.ecline.init((canvas, width, height, dpr) => {
  217. const chart = echarts.init(canvas, null, {
  218. width: width,
  219. height: height,
  220. devicePixelRatio: dpr
  221. });
  222. // canvas.setChart(chart);
  223. // 初始数据
  224. let initialData = {
  225. accumulatedchargetime: _this.data.between.total.accumulatedchargetime,
  226. accumulateddischargetime: _this.data.between.total.accumulateddischargetime
  227. };
  228. // 设置初始选项
  229. let option = _this.getChartOption(initialData);
  230. chart.setOption(option);
  231. _this.chartInstance2 = chart;
  232. _this.startDataUpdates();
  233. return chart;
  234. });
  235. },
  236. // 定义更新数据函数
  237. updateChartData(newData) {
  238. let updatedOption = this.getChartOption(newData);
  239. this.chartInstance2.setOption(updatedOption);
  240. },
  241. // 更新图表数据函数
  242. startDataUpdates() {
  243. this.timer2 = setInterval(() => {
  244. api.request(`/stats/singleStation/${wx.getStorageSync('station').id}/between/time`, 'POST',{startDate:this.data.currentDate,endDate:this.data.currentDate})
  245. .then((res) => {
  246. if (res.data.length>0) {
  247. this.setData({
  248. between:res.data,
  249. })
  250. }else{
  251. this.setData({
  252. between:{
  253. list:[{chargetime: 0,
  254. dischargetime: 0,
  255. ym: "2024-07-08"},
  256. ],
  257. total:{accumulatedchargetime: "0",
  258. accumulateddischargetime: "0"} ,
  259. zonghe:{
  260. accumulatedchargetime: "0",
  261. accumulateddischargetime: "0"
  262. }
  263. },
  264. })
  265. }
  266. let initialData = {
  267. accumulatedchargetime: this.data.between.total.accumulatedchargetime,
  268. accumulateddischargetime: this.data.between.total.accumulateddischargetime
  269. };
  270. this.updateChartData(initialData)
  271. })
  272. .catch((err) => {
  273. console.error('请求失败:', err);
  274. // 在这里处理请求失败的情况
  275. });
  276. }, 10000);
  277. },
  278. // 辅助函数,用于生成图表选项
  279. getChartOption: function (data) {
  280. let _this = this;
  281. return {
  282. backgroundColor: "#ffffff",
  283. legend: [{
  284. left: '0',
  285. bottom: 20,
  286. itemHeight: 8,
  287. itemWidth: 8,
  288. icon: "circle",
  289. formatter: function (name) {
  290. return name + ` ${data.accumulatedchargetime} kWh`;
  291. },
  292. data: [{ name: '今日充电' }]
  293. }, {
  294. left: '0',
  295. bottom: 0,
  296. itemHeight: 8,
  297. itemWidth: 8,
  298. icon: "circle",
  299. formatter: function (name) {
  300. return name + ` ${data.accumulateddischargetime} kWh`;
  301. },
  302. data: [{ name: '今日放电' }]
  303. }],
  304. tooltip: {
  305. trigger: 'item'
  306. },
  307. series: [{
  308. type: 'pie',
  309. radius: ['40%', '60%'],
  310. center: ['50%', '40%'],
  311. avoidLabelOverlap: false,
  312. label: {
  313. show: false,
  314. position: 'center'
  315. },
  316. emphasis: {
  317. label: {
  318. show: true,
  319. fontSize: 0,
  320. fontWeight: 'bold'
  321. }
  322. },
  323. labelLine: {
  324. show: false
  325. },
  326. data: [{
  327. value: data.accumulatedchargetime,
  328. name: '今日充电',
  329. itemStyle: { color: '#4DF096' }
  330. }, {
  331. value: data.accumulateddischargetime,
  332. name: '今日放电',
  333. itemStyle: { color: '#3C9EFA' }
  334. }]
  335. }]
  336. };
  337. },
  338. // 设备详情
  339. onIconTap(){
  340. wx.navigateTo({
  341. url: '/pages/details/index',
  342. })
  343. },
  344. // 设备健康
  345. onhealthy(){
  346. wx.navigateTo({
  347. url: '/pages/healthy/index',
  348. })
  349. },
  350. // 设备信息
  351. ondetails(){
  352. wx.navigateTo({
  353. url: '/pages/monitor/index',
  354. })
  355. },
  356. // 水波图
  357. initCharts(value) {
  358. let _this = this;
  359. this.ecComponent.init((canvas, width, height, dpr) => {
  360. const charts = echarts.init(canvas, null, {
  361. width: width,
  362. height: height,
  363. devicePixelRatio: dpr
  364. });
  365. // 将初始的数值转换为数据
  366. let data = [value, value, value];
  367. // 初始化图表选项
  368. let option = _this.getChartOption1(value, data);
  369. charts.setOption(option);
  370. // 存储图表实例
  371. _this.chartInstance3 = charts;
  372. // 启动定时器更新数据
  373. _this.startDataUpdate();
  374. // 返回图表实例
  375. return charts;
  376. });
  377. },
  378. // 更新图表数据函数
  379. updateChartData1(value, data) {
  380. let updatedOption = this.getChartOption1(value, data);
  381. this.chartInstance3.setOption(updatedOption);
  382. },
  383. // 启动定时器更新数据
  384. startDataUpdate() {
  385. let _this =this
  386. _this.timer3 = setInterval(() => {
  387. // 获取新数据
  388. api.request(`/station/ectric/${wx.getStorageSync('station').id}`, 'POST')
  389. .then((res) => {
  390. // 更新数据
  391. let data = [res.data.quantity / 100, res.data.quantity / 100, res.data.quantity / 100];
  392. _this.updateChartData1(res.data.quantity / 100, data);
  393. // console.log('请求成功:', res);
  394. })
  395. .catch((err) => {
  396. console.error('请求失败:', err);
  397. // 处理请求失败情况
  398. });
  399. }, 10000);
  400. },
  401. // Vue.js 生命周期钩子
  402. beforeDestroy() {
  403. // 清除定时器
  404. if (this.timer3) {
  405. clearInterval(this.timer3);
  406. }
  407. // 销毁图表实例
  408. if (this.chartInstance3) {
  409. this.chartInstance3.dispose(); // 销毁图表
  410. this.chartInstance3 = null;
  411. }
  412. },
  413. beforeDestroys() {
  414. // 清除定时器
  415. if (this.timer2) {
  416. clearInterval(this.timer2);
  417. }
  418. // 销毁图表实例
  419. if (this.chartInstance2) {
  420. this.chartInstance2.dispose(); // 销毁图表
  421. this.chartInstance2 = null;
  422. }
  423. },
  424. // 辅助函数,用于生成图表选项
  425. getChartOption1: function (value, data) {
  426. let color = (value * 100).toFixed(0) < 70 ? '#004403' : '#fff';
  427. return {
  428. title: {
  429. text: (value * 100).toFixed(0) + '%',
  430. textStyle: {
  431. fontFamily: 'Microsoft Yahei',
  432. fontWeight: 'normal',
  433. color: color,
  434. rich: {
  435. a: {
  436. fontSize: 18,
  437. }
  438. }
  439. },
  440. x: 'center',
  441. y: '40%'
  442. },
  443. series: [
  444. {
  445. type: 'liquidFill',
  446. radius: '80%',
  447. center: ['50%', '50%'],
  448. // shape: 'roundRect',
  449. data: data,
  450. color: {
  451. type: 'linear',
  452. x: 0,
  453. y: 0,
  454. x2: 0,
  455. y2: 1,
  456. colorStops: [{
  457. offset: 1,
  458. color: 'rgba(0, 117, 68,1)'
  459. }, {
  460. offset: 0,
  461. color: 'rgba(0, 117, 68,0)'
  462. }],
  463. globalCoord: false
  464. },
  465. // 边框
  466. outline: {
  467. borderDistance: 1,
  468. itemStyle: {
  469. borderWidth: 2,
  470. borderColor:'rgba(0, 117, 68, .1)',
  471. }
  472. },
  473. backgroundStyle: {
  474. borderWidth: 1,
  475. color: '#fff',
  476. },
  477. label: {
  478. normal: {
  479. formatter: '',
  480. }
  481. }
  482. }]
  483. };
  484. },
  485. /**
  486. * 生命周期函数--监听页面卸载
  487. */
  488. onShow(){
  489. let _this =this
  490. // 电站列表stationindex
  491. api.request('/station/list/stationName', 'POST')
  492. .then((res) => {
  493. _this.setData({
  494. objectArray:res.data
  495. })
  496. if (wx.getStorageSync('station').id) {
  497. for (let index = 0; index < _this.data.objectArray.length; index++) {
  498. if (_this.data.objectArray[index].id==wx.getStorageSync('station').id) {
  499. _this.setData({
  500. stationindex:index
  501. })
  502. }
  503. }
  504. _this.getlist()
  505. }else{
  506. wx.setStorage({
  507. key: 'station', // 本地缓存中指定的 key
  508. data: res.data[0],
  509. success: function () {
  510. _this.getlist()
  511. }
  512. });
  513. }
  514. })
  515. .catch((err) => {
  516. console.error('请求失败:', err);
  517. // 在这里处理请求失败的情况
  518. });
  519. },
  520. onHide() {
  521. clearInterval(this.timer1);
  522. // clearInterval(_this.timer3);
  523. this.beforeDestroy()
  524. this.beforeDestroys()
  525. },
  526. });