countdown.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. module.exports = Behavior({
  2. behaviors: [],
  3. properties: {
  4. time: {
  5. type: Date,
  6. value: new Date().getTime() + 86400000,
  7. observer:function(newVal,oldVal) {
  8. if(newVal && !oldVal) {
  9. this.getLatestTime();
  10. }
  11. }
  12. },
  13. status: {
  14. type: Boolean,
  15. value: true,
  16. observer: function (newVal, oldVal, changedPath) {
  17. if (newVal) {
  18. this.init();
  19. } else if (!newVal) {
  20. clearInterval(this.data.timer);
  21. }
  22. }
  23. },
  24. timeType: {
  25. type: String,
  26. value: 'datetime'
  27. },
  28. format: {
  29. type: String,
  30. value: '{%d}天{%h}时{%m}分{%s}秒'
  31. },
  32. isZeroPadd: {
  33. type: Boolean,
  34. value: true,
  35. },
  36. countdownType: {
  37. type: String,
  38. value: "normal"
  39. }
  40. },
  41. data: {
  42. initAddTime: 0,
  43. timer: null,
  44. date: [],
  45. },
  46. ready: function () {
  47. this.getLatestTime();
  48. },
  49. detached: function () {
  50. clearInterval(this.data.timer);
  51. },
  52. pageLifetimes: {
  53. hide() {
  54. clearInterval(this.data.timer);
  55. },
  56. show() {
  57. this.getLatestTime();
  58. }
  59. },
  60. methods: {
  61. // 自动补零
  62. zeroPadding(num) {
  63. num = num.toString()
  64. return num[1] ? num : '0' + num
  65. },
  66. init() {
  67. clearInterval(this.data.timer);
  68. const timer = setTimeout(() => {
  69. this.getLatestTime.call(this);
  70. }, 1000);
  71. this.setData({
  72. timer
  73. })
  74. },
  75. getLatestTime() {
  76. let {
  77. time,
  78. status,
  79. timeType,
  80. initAddTime,
  81. countdownType,
  82. } = this.data;
  83. // IOS不支持2019-04-23 的日期格式
  84. let countDownTime = time
  85. if (countdownType === "normal") { //当countdownType === normal时,不影响之前的代码
  86. if (timeType !== 'second') {
  87. countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') : countDownTime;
  88. countDownTime = Math.ceil((new Date(countDownTime).getTime() - new Date().getTime()) / 1000);
  89. }
  90. if (countDownTime < 0 && timeType !== 'second') {
  91. this._getTimeValue(0);
  92. this.CountdownEnd();
  93. return
  94. }
  95. if (countDownTime - initAddTime > 0) {
  96. this.getLatestForCountDown(countDownTime);
  97. } else if (countDownTime - initAddTime < 0) {
  98. this.getLatestForAddTime(countDownTime);
  99. } else if (countDownTime - initAddTime === 0) {
  100. if (initAddTime <= 0) {
  101. this._getTimeValue(countDownTime);
  102. }
  103. this.CountdownEnd();
  104. }
  105. if (status && countDownTime - initAddTime !== 0) {
  106. this.init.call(this);
  107. }
  108. } else if (countdownType === "anniversary") { // 当countdownType === anniversary时,为纪念日模式
  109. if (timeType === "second") { // 纪念日模式不能设置timeType === second
  110. console.error(`countdownType为${countdownType}类型时,不可设置timeType值为second`)
  111. } else {
  112. countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') : countDownTime;
  113. countDownTime = Math.ceil((new Date().getTime() - new Date(countDownTime).getTime()) / 1000);
  114. if (countDownTime >= 0) { // countDownTime计算结果不能为负数
  115. this.getLatestForCountDown(countDownTime);
  116. this.init.call(this);
  117. } else {
  118. console.error("time传值错误")
  119. }
  120. }
  121. } else { // countdownType 不能设置为 normal,anniversary 以外的值
  122. console.error("错误的countdownType类型")
  123. }
  124. },
  125. getLatestForAddTime(countDownTime) {
  126. let {
  127. initAddTime
  128. } = this.data;
  129. if (initAddTime !== Math.abs(countDownTime)) {
  130. initAddTime++;
  131. this._getTimeValue(initAddTime);
  132. this.setData({
  133. initAddTime
  134. })
  135. }
  136. },
  137. getLatestForCountDown(countDownTime) {
  138. this._getTimeValue(countDownTime);
  139. this.setData({
  140. time: this.data.timeType === 'second' ? --countDownTime : this.data.time,
  141. });
  142. },
  143. _getTimeValue(countDownTime) {
  144. const {
  145. format
  146. } = this.data;
  147. const date = [];
  148. const fomatArray = format.split(/(\{.*?\})/);
  149. const formatType = [{
  150. key: '{%d}',
  151. type: 'day',
  152. count: 86400
  153. }, {
  154. key: '{%h}',
  155. type: 'hour',
  156. count: 3600
  157. }, {
  158. key: '{%m}',
  159. type: 'minute',
  160. count: 60
  161. }, {
  162. key: '{%s}',
  163. type: 'second',
  164. count: 1,
  165. }];
  166. let diffSecond = countDownTime;
  167. formatType.forEach(format => {
  168. const index = this._findTimeName(fomatArray, format.key);
  169. if (index === -1) return;
  170. const name = fomatArray[index];
  171. const formatItem = {
  172. type: format.type,
  173. name,
  174. value: parseInt(diffSecond / format.count)
  175. };
  176. if (this.data.isZeroPadd) {
  177. formatItem.value = this.zeroPadding(formatItem.value);
  178. }
  179. diffSecond %= format.count;
  180. date.push(formatItem);
  181. });
  182. this.setData({
  183. date
  184. });
  185. return date;
  186. },
  187. _findTimeName(fomatArray, str) {
  188. const index = fomatArray.indexOf(str);
  189. if (index === -1) return -1;
  190. return index + 1
  191. },
  192. CountdownEnd() {
  193. this.triggerEvent("linend", {});
  194. },
  195. }
  196. });