web.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : web.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. import os
  7. from flask import request,jsonify
  8. import hashlib
  9. # from utils.cfg import cfg
  10. from controllers.service import storage_service
  11. from utils.ua import *
  12. def getParmas(key=None,value=''):
  13. """
  14. 获取链接参数
  15. :param key:
  16. :return:
  17. """
  18. content_type = request.headers.get('Content-Type')
  19. # print(content_type)
  20. args = {}
  21. if request.method == 'POST':
  22. if 'application/x-www-form-urlencoded' in content_type or 'multipart/form-data' in content_type:
  23. args = request.form
  24. elif 'application/json' in content_type:
  25. args = request.json
  26. elif 'text/plain' in content_type:
  27. args = request.data
  28. else:
  29. args = request.args
  30. elif request.method == 'GET':
  31. args = request.args
  32. if key:
  33. return args.get(key,value)
  34. else:
  35. return args
  36. def layuiBack(msg:str, data=None,code:int=0,count:int=0):
  37. if data is None:
  38. data = []
  39. return jsonify({
  40. 'msg':msg,
  41. 'code':code,
  42. 'data':data,
  43. 'count':count or len(data)
  44. })
  45. def md5(str):
  46. return hashlib.md5(str.encode(encoding='UTF-8')).hexdigest()
  47. def verfy_token(token=None):
  48. if not token:
  49. cookies = request.cookies
  50. token = cookies.get('token', '')
  51. if not token or len(str(token)) != 32:
  52. return False
  53. lsg = storage_service()
  54. # username = cfg.get('UNAME','')
  55. username = lsg.getItem('UNAME','')
  56. # pwd = cfg.get('PWD','')
  57. pwd = lsg.getItem('PWD','')
  58. ctoken = md5(f'{username};{pwd}')
  59. # print(f'username:{username},pwd:{pwd},current_token:{ctoken},input_token:{ctoken}')
  60. if token != ctoken:
  61. return False
  62. return True