qrcode_be/lib/utils.py
2020-12-02 22:48:54 +08:00

157 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
import datetime
import time
# 生成唯一不重名字符串
def randomId():
nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S") # 生成当前时间
randomNum = random.randint(0, 100) # 生成的随机整数n其中0<=n<=100
if randomNum <= 10:
randomNum = str(0) + str(randomNum)
uniqueNum = str(nowTime) + str(randomNum)
return uniqueNum
# 判断str转换完是否为空
def isKong(arg):
if arg == 'None' or arg == '' or arg == None:
return True
else:
return False
# 上传参数校验
def checkData(data):
hrefCount = 0
usernameCount = 0
openIdCount = 0
timeoutCount = 0
# 可能的参数
# wxp://f2f0e4PCkhToyNDT-zfA-Nn6zoAgPKvK9HUl
# https://qr.alipay.com/fkx03165mn5e2hx4gygpx04
# HTTPS://QR.ALIPAY.COM/FKX01227ZSFRLWLKZSHL9C
# https://payapp.weixin.qq.com/qr/AQEGbDUlzvPBxYKSJst3hENW?t=GAAG#wechat_pay
# https://payapp.weixin.qq.com/qr/AQHoz2ywjCZbBKqDrvUuHDqG?t=GAAG#wechat_pay
# 微信验证
# 微信为36位长度只包含-、_、数字和字母使用正则
wxp = re.compile(r'[^\w-]')
wxcode = str(data['wxcode'])
if isKong(wxcode):
wxcode = ''
else:
wxCache1 = re.findall(r"wxp://(.+)", wxcode)
wxCache2 = re.findall(
r"https://payapp.weixin.qq.com/qr/(.+)\?t", wxcode)
if wxCache1:
# 第一种
if(len(wxCache1[0]) == 36):
if not (wxp.search(wxCache1[0])):
hrefCount = hrefCount + 1
else:
return False
else:
return False
elif wxCache2:
# 第二种
if(len(wxCache2[0]) == 24):
if not (wxp.search(wxCache2[0])):
hrefCount = hrefCount + 1
else:
return False
else:
return False
else:
# 都不是
return False
# openId验证
# openId为28位长度只包含-、_、数字和字母使用正则
openId = str(data['openId'])
if isKong(openId):
openId = ''
else:
if(len(openId) == 28):
if not (wxp.search(openId)):
openIdCount = 1
else:
return False
else:
return False
# 支付宝验证
# 支付宝为23位或22位长度只有数字和字母使用isalnum
alcode = str(data['alcode'])
alCache1 = re.findall(r"https://qr.alipay.com/(.+)", alcode)
alCache2 = re.findall(r"HTTPS://QR.ALIPAY.COM/(.+)", alcode)
if isKong(alcode):
alcode = ''
else:
if alCache1:
# 第一种
if len(alCache1[0]) == 23:
if alCache1[0].isalnum():
hrefCount = hrefCount + 1
else:
return False
else:
return False
elif alCache2:
# 第二种
if len(alCache2[0]) == 22:
if alCache2[0].isalnum():
hrefCount = hrefCount + 1
else:
return False
else:
return False
else:
return False
# QQ验证
# QQ 一定包含 'qianbao.qq.com',使用正则
# *!()_-.% 字母 数字 ,正则写不明白了woc
qqp = re.compile(r'qianbao.qq.com')
qqcode = str(data['qqcode'])
if isKong(qqcode):
qqcode = ''
else:
if(qqp.search(qqcode)):
hrefCount = hrefCount + 1
else:
return False
# 用户名验证
# 正则替换掉引号,$
username = str(data['username'])
if(not isKong(username)):
username = username.replace('"', '')
username = username.replace("'", '')
username = username.replace('$', '')
usernameCount = 1
else:
return False
# 备注验证
# 正则替换掉引号,$
node = str(data['node'])
if(not isKong(node)):
node = node.replace('"', '')
node = node.replace("'", '')
node = node.replace('$', '')
else:
node = ''
# 过期时间验证
timeout = str(data['timeout'])
if(not isKong(timeout)):
if len(timeout) == 13:
timeoutCount = 1
else:
return False
else:
return False
if hrefCount >= 2 and usernameCount and openIdCount and timeoutCount:
return {'username': username, 'node': node, 'wxcode': wxcode, 'alcode': alcode, 'qqcode': qqcode, 'openId': openId, 'timeout': timeout, 'create_time': int(time.time()), 'id': randomId()}
else:
return False