web.py 1.8 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : web.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
H
hjdhnx 已提交
5
# Date  : 2022/9/6
H
hjdhnx 已提交
6
import os
H
hjdhnx 已提交
7

H
hjdhnx 已提交
8
from flask import request,jsonify
H
hjdhnx 已提交
9
import hashlib
H
hjdhnx 已提交
10
# from utils.cfg import cfg
H
hjdhnx 已提交
11
from controllers.service import storage_service
H
hjdhnx 已提交
12
from utils.ua import *
H
hjdhnx 已提交
13

H
hjdhnx 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
def getParmas(key=None,value=''):
    """
    获取链接参数
    :param key:
    :return:
    """
    content_type = request.headers.get('Content-Type')
    args = {}
    if request.method == 'POST':
        if 'application/x-www-form-urlencoded' in content_type or 'multipart/form-data' in content_type:
            args = request.form
        elif 'application/json' in content_type:
            args = request.json
        elif 'text/plain' in content_type:
            args = request.data
        else:
            args = request.args
    elif request.method == 'GET':
        args = request.args
    if key:
        return args.get(key,value)
H
hjdhnx 已提交
35
    else:
H
hjdhnx 已提交
36
        return args
H
hjdhnx 已提交
37

H
hjdhnx 已提交
38 39 40 41 42 43 44 45 46 47
def layuiBack(msg:str, data=None,code:int=0,count:int=0):
    if data is None:
        data = []
    return jsonify({
        'msg':msg,
        'code':code,
        'data':data,
        'count':count or len(data)
    })

H
hjdhnx 已提交
48 49 50
def md5(str):
    return hashlib.md5(str.encode(encoding='UTF-8')).hexdigest()

H
hjdhnx 已提交
51 52 53 54 55
def verfy_token(token=None):
    if not token:
        cookies = request.cookies
        token = cookies.get('token', '')
    if not token or len(str(token)) != 32:
H
hjdhnx 已提交
56
        return False
H
hjdhnx 已提交
57 58 59 60 61
    lsg = storage_service()
    # username = cfg.get('UNAME','')
    username = lsg.getItem('UNAME','')
    # pwd = cfg.get('PWD','')
    pwd = lsg.getItem('PWD','')
H
hjdhnx 已提交
62 63 64 65
    ctoken = md5(f'{username};{pwd}')
    # print(f'username:{username},pwd:{pwd},current_token:{ctoken},input_token:{ctoken}')
    if token != ctoken:
        return False
H
hjdhnx 已提交
66
    return True