金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <template>
  2. <view class="uni-countdown">
  3. <text v-if="showDay" :style="{ borderColor: borderColor, color: color }"
  4. class="uni-countdown__number">{{ d }}</text>
  5. <text v-if="showDay" :style="{ color: splitorColor }" class="uni-countdown__splitor">{{dayText}}</text>
  6. <text v-if="showHour" :style="{ borderColor: borderColor, color: color }"
  7. class="uni-countdown__number">{{ h }}</text>
  8. <text v-if="showHour" :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? '' : hourText }}</text>
  9. <text :style="{ borderColor: borderColor, color: color }"
  10. class="uni-countdown__number">{{ i }}</text>
  11. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? '分' : minuteText }}</text>
  12. <text :style="{ borderColor: borderColor, color: color }"
  13. class="uni-countdown__number">{{ s }}</text>
  14. <text v-if="!showColon" :style="{ color: splitorColor }" class="uni-countdown__splitor">{{secondText}}</text>
  15. </view>
  16. </template>
  17. <script>
  18. import {
  19. initVueI18n
  20. } from '@dcloudio/uni-i18n'
  21. import messages from './i18n/index.js'
  22. const { t } = initVueI18n(messages)
  23. /**
  24. * Countdown 倒计时
  25. * @description 倒计时组件
  26. * @tutorial https://ext.dcloud.net.cn/plugin?id=25
  27. * @property {String} backgroundColor 背景色
  28. * @property {String} color 文字颜色
  29. * @property {Number} day 天数
  30. * @property {Number} hour 小时
  31. * @property {Number} minute 分钟
  32. * @property {Number} second
  33. * @property {Number} timestamp 时间戳
  34. * @property {Boolean} showDay = [true|false] 是否显示天数
  35. * @property {Boolean} showColon = [true|false] 是否以冒号为分隔符
  36. * @property {String} splitorColor 分割符号颜色
  37. * @event {Function} timeup 倒计时时间到触发事件
  38. * @example <uni-countdown :day="1" :hour="1" :minute="12" :second="40"></uni-countdown>
  39. */
  40. export default {
  41. name: 'UniCountdown',
  42. emits:['timeup'],
  43. props: {
  44. showDay: {
  45. type: Boolean,
  46. default: true
  47. },
  48. showHour: {
  49. type: Boolean,
  50. default: true
  51. },
  52. showColon: {
  53. type: Boolean,
  54. default: true
  55. },
  56. start: {
  57. type: Boolean,
  58. default: true
  59. },
  60. backgroundColor: {
  61. type: String,
  62. default: '#FFFFFF'
  63. },
  64. borderColor: {
  65. type: String,
  66. default: '#15716E'
  67. },
  68. color: {
  69. type: String,
  70. default: '#15716E'
  71. },
  72. splitorColor: {
  73. type: String,
  74. default: '#15716E'
  75. },
  76. day: {
  77. type: Number,
  78. default: 0
  79. },
  80. hour: {
  81. type: Number,
  82. default: 0
  83. },
  84. minute: {
  85. type: Number,
  86. default: 0
  87. },
  88. second: {
  89. type: Number,
  90. default: 0
  91. },
  92. timestamp: {
  93. type: Number,
  94. default: 0
  95. }
  96. },
  97. data() {
  98. return {
  99. timer: null,
  100. syncFlag: false,
  101. d: '00',
  102. h: '00',
  103. i: '00',
  104. s: '00',
  105. leftTime: 0,
  106. seconds: 0
  107. }
  108. },
  109. computed: {
  110. dayText() {
  111. return t("uni-countdown.day")
  112. },
  113. hourText(val) {
  114. return t("uni-countdown.h")
  115. },
  116. minuteText(val) {
  117. return t("uni-countdown.m")
  118. },
  119. secondText(val) {
  120. return t("uni-countdown.s")
  121. },
  122. },
  123. watch: {
  124. day(val) {
  125. this.changeFlag()
  126. },
  127. hour(val) {
  128. this.changeFlag()
  129. },
  130. minute(val) {
  131. this.changeFlag()
  132. },
  133. second(val) {
  134. this.changeFlag()
  135. },
  136. start: {
  137. immediate: true,
  138. handler(newVal, oldVal) {
  139. if (newVal) {
  140. this.startData();
  141. } else {
  142. if (!oldVal) return
  143. clearInterval(this.timer)
  144. }
  145. }
  146. }
  147. },
  148. created: function(e) {
  149. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  150. this.countDown()
  151. },
  152. // #ifndef VUE3
  153. destroyed() {
  154. clearInterval(this.timer)
  155. },
  156. // #endif
  157. // #ifdef VUE3
  158. unmounted() {
  159. clearInterval(this.timer)
  160. },
  161. // #endif
  162. methods: {
  163. toSeconds(timestamp, day, hours, minutes, seconds) {
  164. if (timestamp) {
  165. return timestamp - parseInt(new Date().getTime() / 1000, 10)
  166. }
  167. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  168. },
  169. timeUp() {
  170. clearInterval(this.timer)
  171. this.$emit('timeup')
  172. },
  173. countDown() {
  174. let seconds = this.seconds
  175. let [day, hour, minute, second] = [0, 0, 0, 0]
  176. if (seconds > 0) {
  177. day = Math.floor(seconds / (60 * 60 * 24))
  178. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  179. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  180. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  181. } else {
  182. this.timeUp()
  183. }
  184. if (day < 10) {
  185. day = '0' + day
  186. }
  187. if (hour < 10) {
  188. hour = '0' + hour
  189. }
  190. if (minute < 10) {
  191. minute = '0' + minute
  192. }
  193. if (second < 10) {
  194. second = '0' + second
  195. }
  196. this.d = day
  197. this.h = hour
  198. this.i = minute
  199. this.s = second
  200. },
  201. startData() {
  202. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  203. if (this.seconds <= 0) {
  204. return
  205. }
  206. clearInterval(this.timer)
  207. this.countDown()
  208. this.timer = setInterval(() => {
  209. this.seconds--
  210. if (this.seconds < 0) {
  211. this.timeUp()
  212. return
  213. }
  214. this.countDown()
  215. }, 1000)
  216. },
  217. changeFlag() {
  218. if (!this.syncFlag) {
  219. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  220. this.startData();
  221. this.syncFlag = true;
  222. }
  223. }
  224. }
  225. }
  226. </script>
  227. <style lang="scss" scoped>
  228. $countdown-height: 48rpx;
  229. $countdown-width: 52rpx;
  230. .uni-countdown {
  231. /* #ifndef APP-NVUE */
  232. display: flex;
  233. /* #endif */
  234. flex-direction: row;
  235. justify-content: flex-start;
  236. padding: 2rpx 0;
  237. }
  238. .uni-countdown__splitor {
  239. /* #ifndef APP-NVUE */
  240. display: flex;
  241. /* #endif */
  242. justify-content: center;
  243. line-height: $countdown-height;
  244. padding: 5rpx;
  245. font-size: $uni-font-size-sm;
  246. }
  247. .uni-countdown__number {
  248. /* #ifndef APP-NVUE */
  249. display: flex;
  250. /* #endif */
  251. justify-content: center;
  252. align-items: center;
  253. line-height: $countdown-height;
  254. margin: 5rpx;
  255. text-align: center;
  256. font-size: $uni-font-size-sm;
  257. }
  258. </style>