Jelajahi Sumber

updating: 基本完成挖矿客户端

yll 4 tahun lalu
induk
melakukan
c082f07efa

+ 1 - 17
view/xcx/app.js

@@ -143,20 +143,4 @@ App({
   * @return array
   */
   SplitArray: function (list, sp) { return util.SplitArray(list, sp)},
-
-  tsToString: function (ts) {
-    var date = new Date(ts * 1000);
-    var y = date.getFullYear();
-    var m = date.getMonth() + 1;
-    m = m < 10 ? ('0' + m) : m;
-    var d = date.getDate();
-    d = d < 10 ? ('0' + d) : d;
-    var h = date.getHours();
-    h=h < 10 ? ('0' + h) : h;
-    var minute = date.getMinutes();
-    minute = minute < 10 ? ('0' + minute) : minute;
-    var second=date.getSeconds();
-    second=second < 10 ? ('0' + second) : second;
-    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 
-  },
-})
+})

+ 1 - 1
view/xcx/components/authorize/authorize.js

@@ -26,7 +26,7 @@ Component({
     loading:false,
     errorSum:0,
     errorNum:3,
-	canIUseGetUserProfile: false // 判断是否为最新获取用户信息函数
+	  canIUseGetUserProfile: false // 判断是否为最新获取用户信息函数
   },
   attached() {
     this.get_logo_url();

+ 136 - 68
view/xcx/components/mine/index.js

@@ -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',
+    //   })
+    // },
   }
 })

+ 3 - 1
view/xcx/components/mine/index.json

@@ -1,4 +1,6 @@
 {
   "component": true,
-  "usingComponents": {}
+  "usingComponents": {
+    "authorize": "/components/authorize/authorize"
+  }
 }

+ 9 - 5
view/xcx/components/mine/index.wxml

@@ -2,12 +2,15 @@
     <!-- total -->
     <view class="head">
       <image src="{{icon}}"></image>
-      <text class="balance">{{totals}}</text> 
+      <text class="balance">{{ symbol }}:</text>
+      <text class="balance">{{total}}</text> 
     </view>
     <!-- digging -->
     <view class="main">
       <view class="left">
-        <button class="start" bindtap="onClickStart">{{btnTxt}}</button>
+        <button class="{{ progress > 0 ? 'started': 'stopped'}}" bindtap="onClickStart">
+        {{ progress > 0 ? '运行中': '启动'}}
+        </button>
       </view>
       <view class="right">
         <view class="current">
@@ -30,11 +33,12 @@
               <image src="{{item.icon}}"></image>
             </view>
             <text class="symbol">{{item.symbol}}</text>
-            <text class="direction">{{ item.out != 0 ? "获取" : "导出" }}</text>
-            <text class="amount">{{item.out != 0 ? item.amount : -item.amount}}</text>
+            <text class="direction">{{ item.out == 0 ? "获取" : "导出" }}</text>
+            <text class="amount">{{item.out == 0 ? item.amount : -item.amount}}</text>
             <text class="time">{{ item.ts }}</text>
           </view>
         </scroll-view>
       </view>
     </view>
-</view>
+</view>
+<authorize bind:onLoadFun='onLoadFun' iShidden="{{iShidden}}"></authorize>

+ 40 - 11
view/xcx/components/mine/index.wxss

@@ -13,11 +13,11 @@
   margin-top:20rpx;
 }
 .mine .head image {
-  height: 48rpx; 
-  width: 48rpx;
+  height: 64rpx; 
+  width: 64rpx;
   margin: 0 10rpx;
 }
-.mine .head .balance {color: #e93323;}
+.mine .head .balance {color: #301310; font-size: 36rpx;}
 
 .mine .main {
   display: flex;
@@ -31,13 +31,13 @@
   flex-direction: column;
   justify-content: center;
   align-items: center;
-  flex-grow: 2;
+  width: 40%;
 }
 
-.main .left .start {
+.main .left .stopped {
   width: 200rpx; 
   height: 200rpx;
-  background-color: #e93323!important; 
+  background: #838383!important; 
   color: #fff; 
   border-radius: 50%;
   display: flex;
@@ -46,11 +46,35 @@
   align-items: center;
 }
 
+.main .left .started {
+  width: 200rpx; 
+  height: 200rpx;
+  background-color: rgba(214, 31, 31, 0.9)!important; 
+  color: #fff; 
+  border-radius: 50%;
+  display: flex;
+  flex-direction: column;
+  justify-content: center;
+  align-items: center;
+
+  transform: scale(1);
+  animation: heartbeat 3s;
+  animation-iteration-count: infinite;
+}
+
+@keyframes heartbeat{
+  60%{
+    transform: scale(1.1);
+  }
+  100%{
+    transform: scale(1);
+  }
+}
+
 .mine .main .right { 
   display: flex;
   flex-direction: column;
   justify-content: center;
-  flex-grow: 3;
 }
 
 .mine .main .right .current {
@@ -62,7 +86,7 @@
 
 .mine .main .right .current .progress {
   font-size: 48rpx;
-  color: green;
+  color: #e93323;
 }
 
 .mine .his {
@@ -99,17 +123,22 @@
   display: flex;
   flex-direction: row;
   justify-content: flex-start;
+  align-items: center;
   height: 54rpx;
   margin: 10rpx;
 }
 .mine .his .records .color {
-  color: forestgreen;
+  color: #e93323;
 }
-.mine .his .records .icon {
+.mine .his .records .item .icon {
+  display: flex;
+  flex-direction: row;
+  justify-content: flex-start;
+  align-items: center;
   width: 10%;
 }
 
-.mine .his .records .icon image {
+.mine .his .records .item .icon image {
   width: 48rpx;
   height: 48rpx;
 }

+ 10 - 3
view/xcx/pages/index/index.js

@@ -80,7 +80,8 @@ Page({
     seckillCont:true, //@deprecated 
     interval:null,  
     timerNotification: null,
-    newGoodsBananr: '', //@deprecated 
+    newGoodsBananr: '', //@deprecated
+    cpnMine: null,      // 挖礦組件
     liveList: [],
     liveInfo: {}
   },
@@ -138,7 +139,9 @@ Page({
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {
-
+    this.setData({
+      cpnMine: this.selectComponent('#mine'),
+    })
   },
 
   /** 轮播监听 */
@@ -272,7 +275,10 @@ Page({
     })
     // 黑洞
     if (this.data.categoryOne.children[activeTabIndex - 1].id === 199) {
+      this.data.cpnMine.show()
       return
+    } else {
+      this.data.cpnMine.hide()
     }
 
     that.setData({
@@ -445,7 +451,6 @@ Page({
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {
-    console.log('onhide()');
     this.setData({ window:false});
     this.data.interval !== null && clearInterval(this.data.interval);
     if (this.data.timerNotification != null) {
@@ -454,6 +459,8 @@ Page({
         timerNotification: null,
       })
     }
+
+    this.data.cpnMine.hide()
   },
 
   /**

+ 2 - 2
view/xcx/pages/index/index.wxml

@@ -45,7 +45,7 @@
                 <swiper-item catchtouchmove='catchTouchMove'>
                   <navigator wx:if="{{item.show == '1'}}" url='{{item.url}}'  open-type='switchTab' class='acea-row row-between-wrapper' hover-class='none'>
                     <view class='text acea-row row-between-wrapper'>
-                      <view class='label border2' wx:if="{{item.id%2 == 0}}">最新</view>
+                      <view class='label border2' wx:if="{{item.id < 5 }}">最新</view>
                       <view class='newsTitle line1'>{{item.info}}</view>
                     </view>
                     <!-- <view class='iconfont icon-xiangyou'></view> -->
@@ -119,7 +119,7 @@
         </view>
         -->
 
-        <!-- 直播列表,后台暂不支持 -->
+        <!-- 直播列表 -->
         <view class="liveBroadcast" wx:if='{{liveList.length || liveInfo.roomid}}'>
           <view class="publicTitle acea-row row-center-wrapper">
             <image src="/images/tv.png"></image>

+ 3 - 1
view/xcx/pages/message/index.js

@@ -1,6 +1,8 @@
 // pages/message/index.js
 import { getProductHot } from '../../api/store.js';
 import { getMessages, readMessages } from "../../api/message"
+import { tsToString } from "../../utils/util"
+
 var base64 = require("../../utils/base64")
 
 const app = getApp();
@@ -83,7 +85,7 @@ Page({
     getMessages(that.data.page).then(res=>{
       var unread = 0;
       res.data.forEach(function(row){
-        row.ts = app.tsToString(row.ts)
+        row.ts = tsToString(row.ts)
         row.read = row.read == null ? false : true
         if (!row.read){
           unread += 1

+ 33 - 1
view/xcx/utils/util.js

@@ -7,7 +7,33 @@ const formatTime = date => {
   const minute = date.getMinutes()
   const second = date.getSeconds()
 
-  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
+  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
+}
+
+const tsToString = (ts) => {
+  var date = new Date(ts * 1000);
+  var y = date.getFullYear();
+  var m = date.getMonth() + 1;
+  m = m < 10 ? ('0' + m) : m;
+  var d = date.getDate();
+  d = d < 10 ? ('0' + d) : d;
+  var h = date.getHours();
+  h=h < 10 ? ('0' + h) : h;
+  var minute = date.getMinutes();
+  minute = minute < 10 ? ('0' + minute) : minute;
+  var second=date.getSeconds();
+  second=second < 10 ? ('0' + second) : second;
+  return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 
+}
+
+const tsToStringDate = (ts) => {
+  var date = new Date(ts * 1000);
+  var y = date.getFullYear();
+  var m = date.getMonth() + 1;
+  m = m < 10 ? ('0' + m) : m;
+  var d = date.getDate();
+  d = d < 10 ? ('0' + d) : d;
+  return y + '-' + m + '-' + d; 
 }
 
 const $h={
@@ -526,9 +552,15 @@ const AnimationNumber = (BaseNumber,ChangeNumber,that,name) => {
   }
 }
 
+const strip = (num, precision = 12) => {
+  return +parseFloat(num.toPrecision(precision));
+}
 
 module.exports = {
   formatTime: formatTime,
+  tsToString: tsToString,
+  tsToStringDate: tsToStringDate,
+  strip: strip,
   $h:$h,
   Tips: Tips,
   uploadImageOne: uploadImageOne,