| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- // 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
- })
- })
- }
- }
- })
|