index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // isGoIndex: false,
  31. stepTimer: null,
  32. syncTimer: null
  33. },
  34. /**
  35. * 组件的方法列表
  36. */
  37. methods: {
  38. show: function () {
  39. // console.log('show:', app.globalData.token)
  40. if (app.globalData.isLog) {
  41. // this.setData({
  42. // iShidden: true
  43. // })
  44. this.getStatus()
  45. this.getHistory()
  46. } else {
  47. this.triggerEvent('onNeedLogin');
  48. }
  49. this.setData({
  50. displaying: true,
  51. })
  52. },
  53. hide: function () {
  54. if (this.data.stepTimer) {
  55. clearInterval(this.data.stepTimer);
  56. }
  57. if (this.data.syncTimer) {
  58. clearInterval(this.data.syncTimer);
  59. }
  60. this.setData({
  61. stepTimer: null,
  62. syncTimer: null,
  63. displaying: false,
  64. })
  65. },
  66. getStatus: function () {
  67. // console.log('isLog:', app.globalData.isLog)
  68. if (app.globalData.isLog) {
  69. var that = this;
  70. getMineStatus().then(res => {
  71. that.setData({
  72. symbol: res.data.symbol,
  73. icon: res.data.icon,
  74. total: res.data.total,
  75. progress: res.data.progress,
  76. step: res.data.step,
  77. price: res.data.price,
  78. });
  79. if (that.data.progress > 0.0) {
  80. that.setupTimers();
  81. }
  82. }, err => {
  83. console.log(err);
  84. that.setData({
  85. total: 0,
  86. progress: 0
  87. });
  88. });
  89. }
  90. },
  91. getHistory: function () {
  92. if (app.globalData.isLog) {
  93. var that = this
  94. coinHistory().then(res => {
  95. if (res.data.length > 0) {
  96. res.data.forEach(function (e) {
  97. e.ts = tsToStringDate(e.ts)
  98. })
  99. }
  100. that.setData({
  101. history: res.data,
  102. })
  103. })
  104. }
  105. },
  106. onLoadFun: function () {
  107. if (this.data.displaying && app.globalData.isLog) {
  108. // console.log("onLoadFun")
  109. this.getStatus()
  110. this.getHistory()
  111. }
  112. },
  113. // 客户端走进度,定时同步
  114. clientStep: function () {
  115. if (this.data.progress > 0 && this.data.step > 0) {
  116. var harfStep = this.data.step / 2
  117. var curStep = Math.floor(Math.random() * 10) * harfStep / 10 + harfStep
  118. var curProgress = strip(this.data.progress + curStep)
  119. this.setData({
  120. progress: curProgress,
  121. })
  122. }
  123. },
  124. setupTimers: function () {
  125. var that = this
  126. if (that.data.syncTimer == null) {
  127. that.setData({
  128. syncTimer: setInterval(() => {
  129. that.getStatus()
  130. }, 1000 * 60 * 2),
  131. })
  132. }
  133. if (that.data.stepTimer == null && that.data.step > 0.0) {
  134. that.setData({
  135. stepTimer: setInterval(() => {
  136. that.clientStep();
  137. }, 1000),
  138. })
  139. }
  140. },
  141. onClickStart: function () {
  142. if (!app.globalData.isLog) {
  143. this.triggerEvent('onNeedLogin')
  144. return
  145. }
  146. if (this.data.progress > 0.0) {
  147. return
  148. }
  149. var that = this
  150. bootCoin().then(res => {
  151. if (res.status === 200) {
  152. that.setData({
  153. symbol: res.data.symbol,
  154. coin: res.data.coin,
  155. price: res.data.price,
  156. step: res.data.step,
  157. progress: res.data.progress,
  158. total: res.data.total,
  159. })
  160. if (that.data.progress > 0.0) {
  161. that.setupTimers();
  162. }
  163. }
  164. }).catch(err => {
  165. wx.showToast({
  166. title: err,
  167. icon: 'none',
  168. duration: 2000,
  169. })
  170. })
  171. },
  172. }
  173. })