This commit is contained in:
hrx 2025-10-27 17:02:06 +08:00
commit 02d1371928

View File

@ -197,6 +197,63 @@ async def baidu_order_cancel(ns={}):
'status': True, 'status': True,
'msg': 'order cancel success' 'msg': 'order cancel success'
} }
async def get_time_diff(time_old=None, time_new=None):
# # 定义两个时间字符串
# time_old = '2023-12-01 09:40:00'
# time_new = '2023-12-01 23:15:00'
# 将时间字符串解析为datetime对象
time_dt1 = datetime.datetime.strptime(time_old, '%Y-%m-%d %H:%M:%S')
time_dt2 = datetime.datetime.strptime(time_new, '%Y-%m-%d %H:%M:%S')
# 计算时间差
time_difference = time_dt2 - time_dt1
# 提取时间差中的分钟数
minutes_difference = time_difference.total_seconds() / 60
# day = time_difference.days
# hour = time_difference.total_seconds() / 3600
return minutes_difference
async def diff_sms_send_save(sor=None, productname=None, time_interval=24*60, send_type='用户欠费通知', user_orgid=None, sms_send_dict=None):
sms_should_send = False
send_type = send_type
# 记录到欠费表中
# 首先查询欠费表中时间是否再范围内
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
print('引用发送短信...')
customer_phone = (await sor.R('organization', {'id': user_orgid}))[0]['contactor_phone']
# customer_name = (await sor.R('organization', {'id': user_orgid}))[0]['orgname']
sms_exist_li = await sor.R('sms_record', {'mobile': customer_phone, 'send_type': send_type, 'send_status': '1',
'sort': ['send_time desc']})
if sms_exist_li:
# 如果原来发送成功 时间范围大于24小时 再次发送
if sms_exist_li[0]['send_status']:
sms_exist_time = sms_exist_li[0]['send_time']
time_diff = await get_time_diff(sms_exist_time, time.strftime('%Y-%m-%d %H:%M:%S'))
# 如果发送过的短信在一天范围内 不再发送短信
if time_diff >= time_interval:
sms_should_send = True
else:
print('%s %s短信已经发送过, 但是没有超过阈值时间, 不再发送...' % (send_type, customer_phone))
else:
print('%s %s短信没有发送过, 发送...' % (send_type, customer_phone))
sms_should_send = True
if sms_should_send or not sms_exist_li:
# 给个人发送短信
await send_vcode(customer_phone, send_type, sms_send_dict)
except Exception as e:
print('发送短信失败: %s' % e)
return {
'status': False,
'msg': '发送短信失败',
'data': e
}
async def get_baidu_orderlist(ns={}): async def get_baidu_orderlist(ns={}):
""" """
@ -579,20 +636,21 @@ async def get_baidu_orderlist(ns={}):
else: else:
#取消订单 #取消订单
await sor.rollback() await sor.rollback()
paydata = {'queryAccountId':baidu_users[0]['baidu_id'],'orderIds':[ns.get('order_id')]} # 余额不足不cancle订单
ns_format = '&'.join(['%s=%s' % (k, v) for k, v in ns.items()]) # paydata = {'queryAccountId':baidu_users[0]['baidu_id'],'orderIds':[ns.get('order_id')]}
url = 'https://billing.baidubce.com/v1/order/cancel?%s' % ns_format # ns_format = '&'.join(['%s=%s' % (k, v) for k, v in ns.items()])
method = 'POST' # url = 'https://billing.baidubce.com/v1/order/cancel?%s' % ns_format
header = { # method = 'POST'
"Host": "billing.baidubce.com" # header = {
} # "Host": "billing.baidubce.com"
header = await get_auth_header(method=method, url=url, header=header) # }
async with aiohttp_client.request( # header = await get_auth_header(method=method, url=url, header=header)
method=method, # async with aiohttp_client.request(
url=url, # method=method,
headers=header, # url=url,
json=paydata) as res: # headers=header,
await res.json() # json=paydata) as res:
# await res.json()
ns_record = { ns_record = {
'orderid': ns.get('order_id'), 'orderid': ns.get('order_id'),
'ordertype': orders[0]['type'], 'ordertype': orders[0]['type'],
@ -600,6 +658,14 @@ async def get_baidu_orderlist(ns={}):
'reason': '该账号余额不足,无法完成购买' 'reason': '该账号余额不足,无法完成购买'
} }
await user_action_record(ns_record) await user_action_record(ns_record)
# 发送短信
sms_send_dict = {
'time': time.strftime('%Y-%m-%d %H:') + '00:00',
'productname': None
}
await diff_sms_send_save(sor=sor, time_interval=24*60, send_type='用户欠费通知', user_orgid=orgid, sms_send_dict=sms_send_dict)
return {'status': False,'msg': '该账号余额不足,无法完成购买'} return {'status': False,'msg': '该账号余额不足,无法完成购买'}
except Exception as e: except Exception as e:
await baidu_order_cancel({'baidu_id': baidu_users[0]['baidu_id'], 'order_id': ns.get('order_id')}) await baidu_order_cancel({'baidu_id': baidu_users[0]['baidu_id'], 'order_id': ns.get('order_id')})