1111 v-model =" nowTime"
1212 size =" mini"
1313 class =" now-time"
14- placeholder =" 当前时间 "
14+ placeholder =" 当前时间戳 "
1515 ></el-input >
1616 <div class =" now-time" >
1717 <el-button size =" mini" @click =" this.unixtimestampToTimeStr"
2626 size =" mini"
2727 class =" now-time"
2828 placeholder =" 当前时间"
29+ @change =" changeShowTime"
2930 ></el-input >
3031 </div >
3132 </div >
3233</template >
3334<script >
35+ const lastTimStr = " time:last:showTime" ;
36+ const copyFromClipboard = " time:copy:clipboard" ;
37+
3438export default {
3539 data () {
3640 return {
3741 mTime: {
3842 str: " " ,
3943 unixtime: 0 ,
4044 },
41- nowTime: 0 ,
45+ nowTime: " " ,
4246 showTime: " " ,
4347 year: 2020 ,
4448 month: 1 ,
@@ -50,21 +54,48 @@ export default {
5054 };
5155 },
5256 created () {
53- const thisTime = new Date ().getTime ();
54- this .nowTime = Math .round (thisTime / 1000 );
55- this .showTime = this .getTimestamp (new Date (thisTime));
57+ setInterval (() => {
58+ navigator .clipboard
59+ .readText ()
60+ .then ((res ) => {
61+ if (! this .checkClipboardExist (res)) {
62+ if (parseInt (res) == res) {
63+ this .nowTime = res;
64+ this .unixtimestampToTimeStr ();
65+ this .$message (" 从剪贴板获取时间戳" );
66+ }
67+ localStorage .setItem (copyFromClipboard, res);
68+ }
69+ })
70+ .catch ((e ) => {
71+ console .log (e);
72+ });
73+ }, 1000 );
74+
75+ let item = localStorage .getItem (lastTimStr);
76+ if (item !== null && item !== " " ) {
77+ this .showTime = item;
78+ }
5679 setInterval (() => {
5780 const now = new Date ();
5881 this .mTime .unixtime = Math .round (now .getTime () / 1000 );
5982 this .mTime .str = this .getTimestamp (now);
6083 }, 500 );
6184 },
6285 methods: {
86+ checkClipboardExist (res ) {
87+ let item = localStorage .getItem (copyFromClipboard);
88+ if (item === " " || item === null ) {
89+ return false ;
90+ }
91+ return res === item;
92+ },
6393 copyNowUnixtime () {
6494 this .nowTime = this .mTime .unixtime ;
6595 },
6696 copyTimeStr () {
6797 this .showTime = this .mTime .str ;
98+ this .changeShowTime ();
6899 },
69100 getTimestamp (timestamp ) {
70101 return ` ${ timestamp .getFullYear ()} -${
@@ -93,6 +124,9 @@ export default {
93124 this .$message .error (" 解析失败" );
94125 }
95126 },
127+ changeShowTime () {
128+ localStorage .setItem (lastTimStr, this .showTime );
129+ },
96130 unixtimestampToTimeStr () {
97131 let unixNumber;
98132 if (` ${ this .nowTime } ` .length === 10 ) {
@@ -101,6 +135,7 @@ export default {
101135 unixNumber = parseInt (this .nowTime , 10 );
102136 }
103137 this .showTime = this .getTimestamp (new Date (unixNumber));
138+ this .changeShowTime ();
104139 },
105140 },
106141};
0 commit comments