index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // components/mine/index.js
  2. import { getMineStatus, bootCoin, coinHistory } from "../../api/mine"
  3. import { strip, tsToStringDate } from "../../utils/util"
  4. var app = getApp()
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. history: [],
  16. symbol: "DOGE",
  17. icon: "/images/one.png",
  18. total: 0.00,
  19. progress: 0.00, // 0 表示已停止
  20. step: 0.00001, // 步长
  21. price: 0.0,
  22. displaying: false, // 当前是否在这个界面
  23. iShidden: false,
  24. stepTimer: null,
  25. syncTimer: null
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. methods: {
  31. show: function() {
  32. console.log('show:', app.globalData.token)
  33. if (app.globalData.token) {
  34. this.setData({ iShidden:true})
  35. this.getStatus()
  36. this.getHistory()
  37. }
  38. this.setData({
  39. displaying: true,
  40. })
  41. },
  42. hide: function() {
  43. if (this.data.stepTimer) {
  44. clearInterval(this.data.stepTimer);
  45. }
  46. if (this.data.syncTimer) {
  47. clearInterval(this.data.syncTimer);
  48. }
  49. this.setData({
  50. stepTimer: null,
  51. syncTimer: null,
  52. displaying: false,
  53. })
  54. },
  55. getStatus: function() {
  56. if (app.globalData.isLog) {
  57. var that = this;
  58. getMineStatus().then(res=>{
  59. that.setData({
  60. symbol: res.data.symbol,
  61. icon: res.data.icon,
  62. total: res.data.total,
  63. progress: res.data.progress,
  64. step: res.data.step,
  65. price: res.data.price,
  66. });
  67. if (that.data.progress > 0.0) {
  68. that.setupTimers();
  69. }
  70. }, err=>{
  71. console.log(err);
  72. that.setData({
  73. total: 0,
  74. progress: 0
  75. });
  76. });
  77. }
  78. },
  79. getHistory: function() {
  80. if (app.globalData.isLog) {
  81. var that = this
  82. coinHistory().then(res=>{
  83. if (res.data.length > 0) {
  84. res.data.forEach(function(e){
  85. e.ts = tsToStringDate(e.ts)
  86. })
  87. }
  88. that.setData({
  89. history: res.data,
  90. })
  91. })
  92. }
  93. },
  94. onLoadFun:function() {
  95. if (this.data.displaying && app.globalData.isLog){
  96. console.log("onLoadFun")
  97. this.getStatus()
  98. this.getHistory()
  99. }
  100. },
  101. // 客户端走进度,定时同步
  102. clientStep: function() {
  103. if (this.data.progress > 0 && this.data.step > 0) {
  104. var harfStep = this.data.step / 2
  105. var curStep = Math.floor(Math.random() * 10) * harfStep / 10 + harfStep
  106. var curProgress = strip(this.data.progress + curStep)
  107. this.setData({
  108. progress: curProgress,
  109. })
  110. }
  111. },
  112. setupTimers: function() {
  113. var that = this
  114. if (that.data.syncTimer == null) {
  115. that.setData({
  116. syncTimer: setInterval(() => {
  117. that.getStatus()
  118. }, 1000 * 60 * 2),
  119. })
  120. }
  121. if (that.data.stepTimer == null && that.data.step > 0.0) {
  122. that.setData({
  123. stepTimer: setInterval(() => {
  124. that.clientStep();
  125. }, 1000),
  126. })
  127. }
  128. },
  129. onClickStart: function() {
  130. if (this.data.progress > 0.0) {
  131. return
  132. }
  133. var that = this
  134. bootCoin().then(res=>{
  135. if (res.status === 200) {
  136. that.setData({
  137. symbol: res.data.symbol,
  138. coin: res.data.coin,
  139. price: res.data.price,
  140. step: res.data.step,
  141. progress: res.data.progress,
  142. total: res.data.total,
  143. })
  144. if (that.data.progress > 0.0) {
  145. that.setupTimers();
  146. }
  147. }
  148. }).catch(err=>{
  149. wx.showToast({
  150. title: err,
  151. icon: 'none',
  152. duration: 2000,
  153. })
  154. })
  155. },
  156. // goRule: function() {
  157. // wx.navigateTo({
  158. // url: '/pages/mine_rule/index',
  159. // })
  160. // },
  161. }
  162. })