index.js 5.3 KB

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