index.js 5.0 KB

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