index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // components/mine/index.js
  2. import { getMineStatus, bootCoin, coinHistory } from '../../api/mine'
  3. import { strip, tsToStringDate } from '../../utils/util'
  4. import d from '../../utils/d.js'
  5. var app = getApp()
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {},
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. history: [],
  16. symbol: 'DOGE',
  17. icon: '/images/one.png',
  18. total: 0.0,
  19. progress: 0.0, // 0 表示已停止
  20. step: 0.00001, // 步长
  21. price: 0.0,
  22. displaying: false, // 当前是否在这个界面
  23. // isHidden: false,
  24. // isGoIndex: false,
  25. stepTimer: null,
  26. syncTimer: null
  27. },
  28. /**
  29. * 组件的方法列表
  30. */
  31. methods: {
  32. show: function () {
  33. d.debug('mine show:', app.globalData.token)
  34. if (app.globalData.isLog) {
  35. // this.setData({
  36. // isHidden: true
  37. // })
  38. this.getStatus()
  39. this.getHistory()
  40. } else {
  41. this.triggerEvent('onNeedLogin')
  42. }
  43. this.setData({
  44. displaying: true
  45. })
  46. },
  47. hide: function () {
  48. d.debug('mine hidden')
  49. if (this.data.stepTimer) {
  50. clearInterval(this.data.stepTimer)
  51. }
  52. if (this.data.syncTimer) {
  53. clearInterval(this.data.syncTimer)
  54. }
  55. this.setData({
  56. stepTimer: null,
  57. syncTimer: null,
  58. displaying: false
  59. })
  60. },
  61. // 获取数据 1
  62. getStatus: function () {
  63. d.debug('isLog:', app.globalData.isLog)
  64. if (app.globalData.isLog) {
  65. var that = this
  66. getMineStatus().then(
  67. 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. },
  80. err => {
  81. console.log(err)
  82. that.setData({
  83. total: 0,
  84. progress: 0
  85. })
  86. }
  87. )
  88. }
  89. },
  90. // 获取数据 2
  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. d.debug('mine 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 (this.data.syncTimer == null) {
  127. this.setData({
  128. syncTimer: setInterval(
  129. () => {
  130. that.getStatus()
  131. },
  132. 1000 * 60 * 2
  133. )
  134. })
  135. }
  136. if (this.data.stepTimer == null && this.data.step > 0.0) {
  137. this.setData({
  138. stepTimer: setInterval(() => {
  139. that.clientStep()
  140. }, 1000)
  141. })
  142. }
  143. },
  144. onClickStart: function () {
  145. if (!app.globalData.isLog) {
  146. this.triggerEvent('onNeedLogin')
  147. return
  148. }
  149. if (this.data.progress > 0.0) {
  150. return
  151. }
  152. var that = this
  153. bootCoin()
  154. .then(res => {
  155. if (res.status === 200) {
  156. that.setData({
  157. symbol: res.data.symbol,
  158. coin: res.data.coin,
  159. price: res.data.price,
  160. step: res.data.step,
  161. progress: res.data.progress,
  162. total: res.data.total
  163. })
  164. if (that.data.progress > 0.0) {
  165. that.setupTimers()
  166. }
  167. }
  168. })
  169. .catch(err => {
  170. wx.showToast({
  171. title: err,
  172. icon: 'none',
  173. duration: 2000
  174. })
  175. })
  176. }
  177. }
  178. })