vue注册全局方法:生成单号------年月日(4+2+2)+随机数n位 (前端生成单号,从接口取单号)

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 // 自动生成单号
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值