既然是根據(jù)當(dāng)前的時(shí)間,那么我們第一步就是獲取當(dāng)前時(shí)間,然后拼接一個(gè)隨機(jī)數(shù),這樣便實(shí)現(xiàn)了隨機(jī)生成一個(gè)流水號(hào),注意由于要求每個(gè)流水號(hào)都是獨(dú)一無二的,生成后提交的過程中還是需要驗(yàn)證當(dāng)前的流水號(hào)是否存在,如果存在則需要提示用戶或者再次生成。
// 根據(jù)當(dāng)前時(shí)間和隨機(jī)數(shù)生成流水號(hào) randomNumber() { const now = new Date() let month = now.getMonth() + 1 let day = now.getDate() let hour = now.getHours() let minutes = now.getMinutes() let seconds = now.getSeconds() month = this.setTimeDateFmt(month) hour = this.setTimeDateFmt(hour) minutes = this.setTimeDateFmt(minutes) seconds = this.setTimeDateFmt(seconds) return now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 89 + 100)).toString() }