index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const util = require('../../utils/util.js');
  2. Component({
  3. options: {
  4. addGlobalClass: true
  5. },
  6. properties: {
  7. show:{
  8. type: Boolean,
  9. observer: function (newVal) {
  10. this.setData({ show: newVal,second:this.data.time });
  11. if (newVal) {
  12. this.countDown()
  13. }else{
  14. clearInterval(this.data.timer)
  15. }
  16. }
  17. },
  18. time:{
  19. type: Number,
  20. observer: function (newVal) {
  21. this.setData({ time: newVal,second:newVal });
  22. }
  23. },
  24. taskId:{
  25. type: String,
  26. observer: function (newVal) {
  27. this.setData({ taskId: newVal });
  28. }
  29. },
  30. },
  31. data: {
  32. show:false,
  33. time:0,
  34. second:0,
  35. taskId:'',
  36. timer:null,
  37. finished:false,
  38. coin:0
  39. },
  40. methods: {
  41. countDown(){
  42. let second=this.data.time,that=this;
  43. if (second>0&&this.data.show) {
  44. let timer = setInterval(() => {
  45. if (second>0) {
  46. second--;
  47. that.setData({second})
  48. } else {
  49. clearInterval(timer)
  50. that.finish()
  51. // that.triggerEvent('finishedTime',)
  52. }
  53. }, 1000);
  54. this.setData({timer})
  55. }
  56. },
  57. finish(){
  58. let that=this;
  59. if (!util.isEmpty(this.data.taskId)) {
  60. util.finishCoinTask(this.data.taskId,true).then(res=>{
  61. if (typeof res === 'number') {
  62. this.setData({coin:res})
  63. } else {
  64. util.showTips(res);
  65. that.triggerEvent('finishedTime',)
  66. }
  67. this.setData({finished:true})
  68. })
  69. }
  70. },
  71. jumpPage(){
  72. this.triggerEvent('finishedTime');
  73. wx.redirectTo({
  74. url: '/pages/account/monetaryCenter/index',
  75. })
  76. }
  77. }
  78. })