// components/mine/index.js import { getMineStatus, bootCoin, coinHistory } from '../../api/mine' import { strip, tsToStringDate } from '../../utils/util' import d from '../../utils/d.js' var app = getApp() Component({ /** * 组件的属性列表 */ properties: {}, /** * 组件的初始数据 */ data: { history: [], symbol: 'DOGE', icon: '/images/one.png', total: 0.0, progress: 0.0, // 0 表示已停止 step: 0.00001, // 步长 price: 0.0, displaying: false, // 当前是否在这个界面 // isHidden: false, // isGoIndex: false, stepTimer: null, syncTimer: null }, /** * 组件的方法列表 */ methods: { show: function () { d.debug('mine show:', app.globalData.token) if (app.globalData.isLog) { // this.setData({ // isHidden: true // }) this.getStatus() this.getHistory() } else { this.triggerEvent('onNeedLogin') } this.setData({ displaying: true }) }, hide: function () { d.debug('mine hidden') if (this.data.stepTimer) { clearInterval(this.data.stepTimer) } if (this.data.syncTimer) { clearInterval(this.data.syncTimer) } this.setData({ stepTimer: null, syncTimer: null, displaying: false }) }, // 获取数据 1 getStatus: function () { d.debug('isLog:', app.globalData.isLog) if (app.globalData.isLog) { var that = this getMineStatus().then( res => { that.setData({ symbol: res.data.symbol, icon: res.data.icon, total: res.data.total, progress: res.data.progress, step: res.data.step, price: res.data.price }) if (that.data.progress > 0.0) { that.setupTimers() } }, err => { console.log(err) that.setData({ total: 0, progress: 0 }) } ) } }, // 获取数据 2 getHistory: function () { if (app.globalData.isLog) { var that = this coinHistory().then(res => { if (res.data.length > 0) { res.data.forEach(function (e) { e.ts = tsToStringDate(e.ts) }) } that.setData({ history: res.data }) }) } }, onLoadFun: function () { if (this.data.displaying && app.globalData.isLog) { d.debug('mine onLoadFun') this.getStatus() this.getHistory() } }, // 客户端走进度,定时同步 clientStep: function () { if (this.data.progress > 0 && this.data.step > 0) { var harfStep = this.data.step / 2 var curStep = (Math.floor(Math.random() * 10) * harfStep) / 10 + harfStep var curProgress = strip(this.data.progress + curStep) this.setData({ progress: curProgress }) } }, setupTimers: function () { var that = this if (this.data.syncTimer == null) { this.setData({ syncTimer: setInterval( () => { that.getStatus() }, 1000 * 60 * 2 ) }) } if (this.data.stepTimer == null && this.data.step > 0.0) { this.setData({ stepTimer: setInterval(() => { that.clientStep() }, 1000) }) } }, onClickStart: function () { if (!app.globalData.isLog) { this.triggerEvent('onNeedLogin') return } if (this.data.progress > 0.0) { return } var that = this bootCoin() .then(res => { if (res.status === 200) { that.setData({ symbol: res.data.symbol, coin: res.data.coin, price: res.data.price, step: res.data.step, progress: res.data.progress, total: res.data.total }) if (that.data.progress > 0.0) { that.setupTimers() } } }) .catch(err => { wx.showToast({ title: err, icon: 'none', duration: 2000 }) }) } } })