当前位置:首页 > 编程技术 > 正文

如何自动生成订单号

如何自动生成订单号

1. 基于时间戳使用当前的时间戳,可以保证订单号的唯一性。例如,使用YYYYMMDDHHMMSS格式。```pythonimport timedef generate_...

1. 基于时间戳

使用当前的时间戳,可以保证订单号的唯一性。例如,使用YYYYMMDDHHMMSS格式。

```python

import time

def generate_order_id():

return time.strftime('%Y%m%d%H%M%S', time.localtime())

order_id = generate_order_id()

print(order_id)

```

2. 使用UUID

UUID(通用唯一识别码)是一种广泛使用的唯一标识符。

```python

import uuid

def generate_order_id():

return str(uuid.uuid4())

order_id = generate_order_id()

print(order_id)

```

3. 基于业务规则

```python

import time

def generate_order_id(user_id, product_id):

prefix = 'ORDER'

suffix = time.strftime('%Y%m%d%H%M%S', time.localtime())

return f"{prefix

最新文章