index.js 4.0 KB

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