cl-countdown.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view class="cl-countdown">
  3. <template v-for="(item, index) in list">
  4. <view class="cl-countdown-item" :key="index" :style="[customStyle]" v-if="item.visible">
  5. <text class="cl-countdown__number" :style="[numberStyle]">{{ item.value }}</text>
  6. <text class="cl-countdown__splitor" :style="[splitorStyle]">{{
  7. item.splitor
  8. }}</text>
  9. </view>
  10. </template>
  11. <view class="cl-countdown-item" v-if="isMillisecond">
  12. <text class="cl-countdown__number">{{ millisecond }}</text>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import { isArray } from "../../utils";
  18. /**
  19. * countdown 倒计时
  20. * @description 倒计时, 支持布局显示, 自定义样式
  21. * @tutorial https://docs.cool-js.com/uni/components/view/countdown.html
  22. * @property {String, Array} splitor 图片链接
  23. * @property {Array} layout 布局, ["day", "hour", "minute", "second"]
  24. * @property {String} mode default 默认 | simple 是否精简:为00时自动隐藏,默认dfault
  25. * @property {Number} day 还有多少天
  26. * @property {Number} hour 还有多少小时
  27. * @property {Number} minute 还有多少分钟
  28. * @property {Number} second 还有多少秒
  29. * @property {Date, String} datetime 结束时间
  30. * @property {Boolean} isMillisecond 是否带有毫秒
  31. * @property {Object} customStyle 自定义样式
  32. * @property {Object} numberStyle 数字样式
  33. * @property {Object} splitorStyle 分隔符样式
  34. * @example <cl-countdown :day="5" />
  35. */
  36. export default {
  37. name: "cl-countdown",
  38. props: {
  39. // 分隔符 [day, hour, minute, second]
  40. splitor: {
  41. type: [String, Array],
  42. default: "default"
  43. },
  44. // 布局
  45. layout: {
  46. type: Array,
  47. default: () => ["day", "hour", "minute", "second"]
  48. },
  49. // simple 是否精简:为00时自动隐藏
  50. mode: {
  51. type: String,
  52. default: "default"
  53. },
  54. // 还有多少天
  55. day: {
  56. type: Number,
  57. default: 0
  58. },
  59. // 还有多少小时
  60. hour: {
  61. type: Number,
  62. default: 0
  63. },
  64. // 还有多少分钟
  65. minute: {
  66. type: Number,
  67. default: 0
  68. },
  69. // 还有多少秒
  70. second: {
  71. type: Number,
  72. default: 0
  73. },
  74. // 结束时间
  75. datetime: [Date, String],
  76. // 是否带有毫秒
  77. isMillisecond: Boolean,
  78. // 自定义样式
  79. customStyle: Object,
  80. // 数字样式
  81. numberStyle: Object,
  82. // 分隔符样式
  83. splitorStyle: Object
  84. },
  85. data() {
  86. return {
  87. timer: null,
  88. millisecondTimer: null,
  89. syncFlag: false,
  90. seconds: 0,
  91. list: [],
  92. millisecond: 9,
  93. status: false
  94. };
  95. },
  96. watch: {
  97. day() {
  98. this.changeFlag();
  99. },
  100. hour() {
  101. this.changeFlag();
  102. },
  103. minute() {
  104. this.changeFlag();
  105. },
  106. second() {
  107. this.changeFlag();
  108. },
  109. datetime() {
  110. this.changeFlag();
  111. }
  112. },
  113. beforeDestroy() {
  114. this.clear();
  115. },
  116. created() {
  117. this.start();
  118. },
  119. methods: {
  120. // 转成秒
  121. toSeconds({ day = 0, hour = 0, minute = 0, second = 0, datetime }) {
  122. if (datetime) {
  123. return (
  124. (new Date(datetime.replace(/-/g, "/")).getTime() - new Date().getTime()) / 1000
  125. );
  126. } else {
  127. return day * 60 * 60 * 24 + hour * 60 * 60 + minute * 60 + second;
  128. }
  129. },
  130. // 开始倒计时
  131. start(options) {
  132. let { day, hour, minute, second, datetime } = options || {};
  133. if (!day) {
  134. day = this.day;
  135. }
  136. if (!hour) {
  137. hour = this.hour;
  138. }
  139. if (!minute) {
  140. minute = this.minute;
  141. }
  142. if (!second) {
  143. second = this.second;
  144. }
  145. if (!datetime) {
  146. datetime = this.datetime;
  147. }
  148. this.seconds = this.toSeconds({
  149. day,
  150. hour,
  151. minute,
  152. second,
  153. datetime
  154. });
  155. this.next();
  156. },
  157. // 继续倒计时
  158. next() {
  159. if (this.seconds <= 0) {
  160. return;
  161. }
  162. if (this.status) {
  163. return;
  164. }
  165. this.status = true;
  166. // Start coundown
  167. const next = () => {
  168. this.countDown();
  169. if (this.seconds <= 0) {
  170. this.done();
  171. return;
  172. } else {
  173. this.seconds--;
  174. this.timer = setTimeout(next, 1000);
  175. }
  176. };
  177. // Use setTimeout instead setInterval
  178. next();
  179. // milli second
  180. if (this.isMillisecond) {
  181. const next = () => {
  182. this.millisecond--;
  183. if (this.millisecond < 0) {
  184. this.millisecond = 9;
  185. }
  186. this.millisecondTimer = setTimeout(next, 100);
  187. };
  188. next();
  189. }
  190. },
  191. // 停止
  192. stop() {
  193. this.clear();
  194. this.$emit("stop");
  195. },
  196. // 结束
  197. done() {
  198. this.clear();
  199. this.$emit("done");
  200. },
  201. // 清除定时器
  202. clear() {
  203. clearTimeout(this.timer);
  204. clearTimeout(this.millisecondTimer);
  205. this.timer = null;
  206. this.millisecondTimer = null;
  207. this.status = false;
  208. },
  209. // 倒计时执行
  210. countDown() {
  211. let seconds = this.seconds;
  212. let [day, hour, minute, second] = [0, 0, 0, 0];
  213. day = Math.floor(seconds / (60 * 60 * 24));
  214. hour = Math.floor(seconds / (60 * 60)) - day * 24;
  215. minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60;
  216. second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
  217. if (day < 10) {
  218. day = "0" + day;
  219. }
  220. if (hour < 10) {
  221. hour = "0" + hour;
  222. }
  223. if (minute < 10) {
  224. minute = "0" + minute;
  225. }
  226. if (second < 10) {
  227. second = "0" + second;
  228. }
  229. const obj = {
  230. day,
  231. hour,
  232. minute,
  233. second
  234. };
  235. let flag = day === "00";
  236. this.list = this.layout.map((e, i) => {
  237. let item = {
  238. value: obj[e],
  239. splitor: this.splitorText(i),
  240. visible: this.mode === "simple" ? (flag ? obj[e] !== "00" : true) : true
  241. };
  242. if (flag) {
  243. flag = obj[e] === "00";
  244. }
  245. return item;
  246. });
  247. this.$emit(
  248. "change",
  249. this.list.map(e => e.value)
  250. );
  251. },
  252. // 分隔符内容
  253. splitorText(i) {
  254. let arr = [];
  255. if (isArray(this.splitor)) {
  256. arr = this.splitor;
  257. } else {
  258. switch (this.splitor) {
  259. case "cn":
  260. arr = ["天", "时", "分", "秒"];
  261. break;
  262. case "en":
  263. arr = ["Day", "Hour", "Minute", "Second"];
  264. break;
  265. case "":
  266. case false:
  267. case null:
  268. arr = ["", "", "", ""];
  269. break;
  270. case "default":
  271. default:
  272. arr = ["天", ":", ":", ""];
  273. break;
  274. }
  275. }
  276. return arr[i];
  277. },
  278. changeFlag() {
  279. this.$nextTick(() => {
  280. this.start();
  281. });
  282. }
  283. }
  284. };
  285. </script>