widgetTime.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="text" :style="styleColor">{{ timestr }}</div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "WidgetTime",
  7. components: {},
  8. props: {
  9. value: Object,
  10. ispreview: Boolean
  11. },
  12. data() {
  13. return {
  14. timestr: "",
  15. options: {}
  16. };
  17. },
  18. computed: {
  19. transStyle() {
  20. console.log(this.objToOne(this.options));
  21. return this.objToOne(this.options);
  22. },
  23. styleColor() {
  24. return {
  25. position: this.ispreview ? "absolute" : "static",
  26. color: this.transStyle.color || "#fff",
  27. text: this.transStyle.text || "文本框",
  28. "font-size": this.transStyle.fontSize + "px" || "30px",
  29. "letter-spacing": this.transStyle.letterSpacing + "em",
  30. background: this.transStyle.background,
  31. "font-weight": this.transStyle.fontWeight,
  32. "text-align": this.transStyle.textAlign,
  33. display:
  34. this.transStyle.hideLayer == undefined
  35. ? "block"
  36. : this.transStyle.hideLayer
  37. ? "none"
  38. : "block",
  39. width: this.transStyle.width + "px",
  40. height: this.transStyle.height + "px",
  41. left: this.transStyle.left + "px",
  42. top: this.transStyle.top + "px",
  43. right: this.transStyle.right + "px"
  44. };
  45. }
  46. },
  47. watch: {
  48. value: {
  49. handler(val) {
  50. this.options = val;
  51. },
  52. deep: true
  53. }
  54. },
  55. mounted() {
  56. this.options = this.value;
  57. this.getTime();
  58. },
  59. methods: {
  60. formatFunction(date, fmt) {
  61. if (/(y+)/.test(fmt)) {
  62. fmt = fmt.replace(
  63. RegExp.$1,
  64. (date.getFullYear() + "").substr(4 - RegExp.$1.length)
  65. );
  66. }
  67. const o = {
  68. "M+": date.getMonth() + 1, // 月份
  69. "d+": date.getDate(), // 日
  70. "h+": date.getHours(), // 小时
  71. "m+": date.getMinutes(), // 分
  72. "s+": date.getSeconds(), // 秒
  73. "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
  74. S: date.getMilliseconds() // 毫秒
  75. };
  76. for (let k in o) {
  77. if (new RegExp("(" + k + ")").test(fmt)) {
  78. fmt = fmt.replace(
  79. RegExp.$1,
  80. RegExp.$1.length == 1
  81. ? o[k]
  82. : ("00" + o[k]).substr(("" + o[k]).length)
  83. );
  84. }
  85. }
  86. return fmt;
  87. },
  88. check(val) {
  89. if (val < 10) {
  90. return "0" + val;
  91. } else {
  92. return val;
  93. }
  94. },
  95. formatWeek(date, fmt) {
  96. const year = date.getFullYear();
  97. const month = this.check(date.getMonth() + 1);
  98. const day = this.check(date.getDate());
  99. const hours = this.check(date.getHours());
  100. const minutes = this.check(date.getMinutes());
  101. const seconds = this.check(date.getSeconds());
  102. let dayCycle = date.getDay();
  103. const dayCycleArray = ["日", "一", "二", "三", "四", "五", "六"];
  104. for (let i = 0; i < 7; i++) {
  105. if (dayCycle == i) {
  106. dayCycle = " 星期" + dayCycleArray[i];
  107. }
  108. }
  109. if (fmt == "year-week") {
  110. return year + "-" + month + "-" + day + dayCycle;
  111. } else if (fmt == "year-h-m-week") {
  112. return (
  113. year +
  114. "-" +
  115. month +
  116. "-" +
  117. day +
  118. " " +
  119. hours +
  120. ":" +
  121. minutes +
  122. dayCycle
  123. );
  124. } else if (fmt == "year-h-m-s-week") {
  125. return (
  126. year +
  127. "-" +
  128. month +
  129. "-" +
  130. day +
  131. " " +
  132. hours +
  133. ":" +
  134. minutes +
  135. ":" +
  136. seconds +
  137. dayCycle
  138. );
  139. } else if (fmt == "week") {
  140. return dayCycle;
  141. }
  142. },
  143. displayTime() {
  144. this.timestr =
  145. this.transStyle.timeFormat.indexOf("week") > -1
  146. ? this.formatWeek(new Date(), this.transStyle.timeFormat)
  147. : this.formatFunction(new Date(), this.transStyle.timeFormat);
  148. },
  149. getTime() {
  150. setInterval(() => {
  151. this.displayTime();
  152. }, 1000);
  153. }
  154. }
  155. };
  156. </script>
  157. <style lang="scss" scoped>
  158. .text {
  159. width: 100%;
  160. height: 100%;
  161. overflow: hidden;
  162. }
  163. </style>