from flask import *
from flask import Blueprint

from func import decryption
import db.fz_data,app_conf


conf = app_conf.conf()

api_receive_bp = Blueprint("api_receive_bp", __name__)



@api_receive_bp.route('/receive',methods=["POST"])
def receive():
    if request.method == 'POST':
        # 定义返回的内容
        response_data = {'code': 200, 'msg': '', 'data': ''}
        # 接收数据
        text_encrypted_base64 = json.loads(request.get_data())['data']
        # 接收表单数据
        p = f"""-----BEGIN PRIVATE KEY-----\n{conf.get("RSA","key")}\n-----END PRIVATE KEY-----"""
        try:
            text_decrypted = json.loads(decryption.decryption(text_encrypted_base64, p))
            response_data['code'] = 200
            response_data['msg'] = '数据解密成功'
        except Exception as e:
            response_data['code'] = 500
            response_data['msg'] = '数据解密失败'
            return jsonify(response_data)
        if db.fz_data.saveFzData(text_decrypted):
            print("数据已保存")
        else:
            print("数据保存失败\n",text_decrypted)
        return jsonify(response_data)