index.js 3.8 KB

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