vue注册全局方法:生成单号------年月日(4+2+2)+随机数n位
1.写方法
因为再vue中多次用到此方法,故而创建一个公用的文件内含多次被调用的方法
/* 公共方法 */
// 单号:4位年度+2位月份+4位流水号
function orderNo() {
// 调用获取当前日期的方法加四位随机数
return getProjectNum() + Math.floor(Math.random() * 10000) // 如果是6位或者8位随机数,相应的 *1000000或者 *100000000就行了
}
function getProjectNum() {
const projectTime = new Date() // 当前中国标准时间
const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
const Month = projectTime.getMonth() + 1 // 获取中国区月份
const Day = projectTime.getDate() // 获取几号
var CurrentDate = Year
if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
CurrentDate += Month
} else {
CurrentDate += '0' + Month
}
if (Day >= 10) {
CurrentDate += Day
} else {
CurrentDate += '0' + Day
}
return CurrentDate
}
// 法二--从接口中取单号
async function orderNoOther(name) {
const params = { srName: name }
let num = null
await api.getSqId(params).then(res => {
if (res.code === 0) {
num = res.data
} else {
Tips({ content: res.msg, type: 'error' })
}
})
return num
}
export default {
orderNo,
orderNoOther
}
2.全局注册
写在vue的main.js文件下
import publicMethods from '@/utils/publicMethods.js'
Vue.prototype.$publicMethods = publicMethods
3.页面中使用此方法
// 直接使用
this.form.grjkJkid = this.$publicMethods.orderNo() // 自动生成申请单号
// 从接口取值为单号
this.$publicMethods.orderNoOther('借款').then(orderIntem => {
this.form.grjkJkid = orderIntem // 自动生成单号
})