js怎么生成一个订单号

最近公司在做一个网上商城,现在我要求每次支付成功之后前端生成做一个订单号。

订单号的合成规则有很多,如:

识别码+日期+字符 //组成的方式不尽相同

我这里是每次支付成功时获取手机:当前时间,发送给后台,后台在后面加上公司的识别码生成

思路:获取当前时间的时间戳,分别求出年、月、日、时、分、秒,用一个变量接收把他们和起来就行了

<body>
	<div>
		<span>订单号:</span>
		<input type="text" id="order" name="WIDout_trade_no" />
	</div>
	<button id="btn">生成订单</button>
</body>
var btn=document.getElementById('btn')
var order=document.getElementById('order')
btn.onclick = function(){
    
    
  GetDateNow();
}

function GetDateNow(){
    
    
	// 时间戳
	var time = new Date();
	// 年
	var year= String(time.getFullYear());
	// 月
	var mouth= String(time.getMonth() + 1);
	// 日
	var day= String(time.getDate());
	// 时
	var hours= String(time.getHours());
	if(hours.length<2){
    
    
		hours='0' + hours
	}
	// 分
	var minutes= String(time.getMinutes());
	if(minutes.length<2) {
    
    
		minutes='0' + minutes
	}
	// 秒
	var seconds= String(time.getSeconds());
	if(seconds.length<2) {
    
    
		seconds='0' + seconds
	}
	var str = year + mouth + day + hours + minutes + seconds
	order.value = str
}

前端页面简单的订单生成完成!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44469200/article/details/103597398
今日推荐