index.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <u-toast ref="uToast"></u-toast>
  7. <view class="u-no-demo-here" style="text-align: left;">
  8. 根据当前时间,返回类似"刚刚,5分钟前,8小时前,3天前"等字样
  9. </view>
  10. <view class="u-demo-result-line">
  11. {{result}}
  12. </view>
  13. </view>
  14. </view>
  15. <view class="u-config-wrap">
  16. <view class="u-config-title u-border-bottom">
  17. 参数配置
  18. </view>
  19. <view class="u-config-item">
  20. <view class="u-item-title">时间</view>
  21. <u-subsection :list="timeArr1" @change="timeArr1Change"></u-subsection>
  22. <u-gap></u-gap>
  23. <u-subsection style="margin-top: 40rpx;" :list="timeArr2" @change="timeArr2Change"></u-subsection>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. // 微信小程序无法动态修改u-subsection的list参数,导致onLoad中赋值timeArr1,timeArr2无效,故在data中直接赋值
  32. let nowTime = Number(+ new Date());
  33. let threeDayAgo = nowTime - 2 * 86400000;
  34. let arr1 = [0, 0], arr2 = [0, 0];
  35. [0, 0].map((val, index) => {
  36. arr1[index] = this.$u.timeFormat(this.$u.random(threeDayAgo, nowTime), 'yyyy/mm/dd hh:MM:ss');
  37. arr2[index] = this.$u.timeFormat(this.$u.random(threeDayAgo, nowTime), 'yyyy/mm/dd hh:MM:ss');
  38. })
  39. return {
  40. timeArr1: arr1,
  41. timeArr2: arr2,
  42. result: null
  43. }
  44. },
  45. onLoad() {
  46. this.timeArr1Change(0);
  47. },
  48. methods: {
  49. timeArr1Change(index) {
  50. this.result = this.$u.timeFrom((new Date(this.timeArr1[index])).getTime());
  51. },
  52. timeArr2Change(index) {
  53. this.result = this.$u.timeFrom((new Date(this.timeArr2[index])).getTime());
  54. }
  55. }
  56. }
  57. </script>
  58. <style lang="scss" scoped>
  59. .u-demo {}
  60. </style>