|
|
@@ -1,6 +1,8 @@
|
|
|
// components/mine/index.js
|
|
|
-import { getMineStatus, bootCoin } from "../../api/mine"
|
|
|
-import { formatTime } from "../../utils/util"
|
|
|
+import { getMineStatus, bootCoin, coinHistory } from "../../api/mine"
|
|
|
+import { strip, tsToStringDate } from "../../utils/util"
|
|
|
+
|
|
|
+var app = getApp()
|
|
|
|
|
|
Component({
|
|
|
/**
|
|
|
@@ -13,96 +15,162 @@ Component({
|
|
|
* 组件的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
- btnTxt: "启动",
|
|
|
- history: [{
|
|
|
- "symbol": "DOGE",
|
|
|
- "icon": "/images/one.png",
|
|
|
- "amount": 12.034,
|
|
|
- "ts": "2021-05-10",
|
|
|
- "from": "abcde034345dd",
|
|
|
- "to": "aabde2343df34",
|
|
|
- "out": 0,
|
|
|
- },{
|
|
|
- "symbol": "DOGE",
|
|
|
- "icon": "/images/one.png",
|
|
|
- "amount": 12,
|
|
|
- "ts": "2021-05-10",
|
|
|
- "from": "abcde034345dd",
|
|
|
- "to": "aabde2343df34",
|
|
|
- "out": -1,
|
|
|
- }],
|
|
|
+ history: [],
|
|
|
symbol: "DOGE",
|
|
|
icon: "/images/one.png",
|
|
|
- totals: 9999.9999,
|
|
|
- progress: 99.999, // 0 表示已停止
|
|
|
-
|
|
|
- timer: null,
|
|
|
- coinTimer: null
|
|
|
+ total: 0.00,
|
|
|
+ progress: 0.00, // 0 表示已停止
|
|
|
+ step: 0.00001, // 步长
|
|
|
+ price: 0.0,
|
|
|
+
|
|
|
+ displaying: false, // 当前是否在这个界面
|
|
|
+ iShidden: false,
|
|
|
+ stepTimer: null,
|
|
|
+ syncTimer: null
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 组件的方法列表
|
|
|
*/
|
|
|
methods: {
|
|
|
- tabIn: function() {
|
|
|
- this.getStatus();
|
|
|
- this.starTimer();
|
|
|
+ show: function() {
|
|
|
+ if (app.globalData.token) {
|
|
|
+ this.setData({ iShidden:true})
|
|
|
+ this.getStatus()
|
|
|
+ this.getHistory()
|
|
|
+ }
|
|
|
|
|
|
- console.log(formatTime(new Date(1620633003000)));
|
|
|
- this.setData({history: []});
|
|
|
+ this.setData({
|
|
|
+ displaying: true,
|
|
|
+ })
|
|
|
},
|
|
|
-
|
|
|
- tabOut: function() {
|
|
|
- clearInterval(this.data.timer);
|
|
|
- clearTimeout(this.data.coinTimer);
|
|
|
+
|
|
|
+ hide: function() {
|
|
|
+ if (this.data.stepTimer) {
|
|
|
+ clearInterval(this.data.stepTimer);
|
|
|
+ }
|
|
|
+ if (this.data.syncTimer) {
|
|
|
+ clearInterval(this.data.syncTimer);
|
|
|
+ }
|
|
|
+ this.setData({
|
|
|
+ stepTimer: null,
|
|
|
+ syncTimer: null,
|
|
|
+ displaying: false,
|
|
|
+ })
|
|
|
},
|
|
|
|
|
|
getStatus: function() {
|
|
|
- var that = this;
|
|
|
- getMineStatus().then(res=>{
|
|
|
- console.log(res);
|
|
|
- that.setData({
|
|
|
+ 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
|
|
|
+ });
|
|
|
});
|
|
|
- }, err=>{
|
|
|
- console.log(err);
|
|
|
- that.setData({
|
|
|
- totals: 0,
|
|
|
- progress: 0
|
|
|
- });
|
|
|
- });
|
|
|
+ }
|
|
|
+ },
|
|
|
|
|
|
- that.data.coinTimer = setTimeout(()=>{
|
|
|
- that.getStatus();
|
|
|
- }, 30000);
|
|
|
+ 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,
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
- starTimer: function() {
|
|
|
- var that = this;
|
|
|
- that.data.timer = setInterval(function(){
|
|
|
- var current = parseFloat(that.data.progress);
|
|
|
- that.setData({
|
|
|
- progress: (current + parseFloat(Math.random() * 0.001)).toFixed(4)
|
|
|
- });
|
|
|
- }, 1000);
|
|
|
+ onLoadFun:function() {
|
|
|
+ if (this.data.displaying){
|
|
|
+ this.getStatus()
|
|
|
+ this.getHistory()
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
- onClickStart: function() {
|
|
|
- let that = this;
|
|
|
- that.setData({
|
|
|
- btnTxt: "挖矿中"
|
|
|
- });
|
|
|
+ // 客户端走进度,定时同步
|
|
|
+ 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 (that.data.syncTimer == null) {
|
|
|
+ that.setData({
|
|
|
+ syncTimer: setInterval(() => {
|
|
|
+ that.getStatus()
|
|
|
+ }, 1000 * 60 * 2),
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
- goRule: function() {
|
|
|
- wx.navigateTo({
|
|
|
- url: '/pages/mine_rule/index',
|
|
|
+ if (that.data.stepTimer == null && that.data.step > 0.0) {
|
|
|
+ that.setData({
|
|
|
+ stepTimer: setInterval(() => {
|
|
|
+ that.clientStep();
|
|
|
+ }, 1000),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ onClickStart: function() {
|
|
|
+ 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,
|
|
|
+ })
|
|
|
})
|
|
|
},
|
|
|
|
|
|
- dateTime: function(time) {
|
|
|
- console.log(time);
|
|
|
- return formatTime(new Date(parseInt(time)));
|
|
|
- }
|
|
|
+ // goRule: function() {
|
|
|
+ // wx.navigateTo({
|
|
|
+ // url: '/pages/mine_rule/index',
|
|
|
+ // })
|
|
|
+ // },
|
|
|
}
|
|
|
})
|