86 lines
2.0 KiB
Python
86 lines
2.0 KiB
Python
import os
|
||
import json
|
||
import random
|
||
|
||
from uuid import uuid4
|
||
|
||
from setting import APP_NAME
|
||
|
||
|
||
def file_path(param):
|
||
"""
|
||
返回文件真实路径
|
||
避免因为文件路径问题带来的麻烦
|
||
Parameters:
|
||
param: 文件相对路径
|
||
Returns:
|
||
path: 文件真实路径
|
||
"""
|
||
abs_path = os.path.abspath(os.path.dirname(__file__))
|
||
rel_path = abs_path[:abs_path.find(APP_NAME) + len(APP_NAME)]
|
||
path = os.path.abspath(rel_path + param)
|
||
return path
|
||
|
||
|
||
def read_json_file(param):
|
||
"""
|
||
读取json文件
|
||
Parameters:
|
||
param: json文件真实路径
|
||
Returns:
|
||
result: json内容
|
||
"""
|
||
with open(param, "r", encoding='utf-8') as f:
|
||
result = json.load(f)
|
||
return result
|
||
|
||
|
||
def read_file(param):
|
||
"""
|
||
读取文件内容
|
||
Parameters:
|
||
param: 文件真实路径
|
||
Returns:
|
||
result: 文件内容
|
||
"""
|
||
with open(param, "r", encoding="utf-8") as f:
|
||
result = f.read()
|
||
return result
|
||
|
||
|
||
def make_id():
|
||
"""
|
||
简化32位uuid为8位id
|
||
Parameters:
|
||
-
|
||
Returns:
|
||
result: 8位ID
|
||
"""
|
||
uuid_chars = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
||
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
|
||
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
|
||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
|
||
uuid = str(uuid4()).replace('-', '')
|
||
res = ''
|
||
for i in range(0, 8):
|
||
sub = uuid[i * 4: i * 4 + 4]
|
||
x = int(sub, 16)
|
||
res += uuid_chars[x % 0x3E]
|
||
return res
|
||
|
||
|
||
def make_verify_code():
|
||
"""
|
||
随机生成6位数字,主要用于验证码
|
||
Parameters:
|
||
-
|
||
Returns:
|
||
code: 6位数字
|
||
"""
|
||
code = ""
|
||
for i in range(6):
|
||
ch = chr(random.randrange(ord('0'), ord('9') + 1))
|
||
code += ch
|
||
return code
|