import os

from flask import Flask, request, jsonify, redirect, url_for, render_template
from flask_mysqldb import MySQL
from flask_cors import CORS

app = Flask(name)
CORS(app, resources={r"/*": {"origins": "*"}})  # 允许所有来源

# MySQL 配置
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '123456'
app.config['MYSQL_DB'] = '金融管理'

mysql = MySQL(app)

os.environ["FLASK_RUN_FROM_CLI"] = "false"  # 禁用 flask run 的优先级
@app.route('/',methods=['GET'])
def index():
    return redirect(url_for('login_web'))
@app.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')

    cursor = mysql.connection.cursor()
    cursor.execute("SELECT * FROM users WHERE user_name = %s AND password = %s", (username, password))
    user = cursor.fetchone()
    cursor.close()

    if user:
        print('检测到有新的手机连接本服务器，请管理员注意安全问题！')
        return jsonify({
            "Content-type": "application/json",
            "success": True,
            "message": "欢迎来到Submerge Rain尊敬的用户：" + username
        }), 200
    else:
        return jsonify({
            "Content-type": "application/json",
            "Id-Connect":"Error",
            "message": "Invalid username or password"
        }), 401
@app.route('/weblogin', methods=['GET'])
def login_web():
    print("检测到有服务器连接！！！")
if name == 'main':
    app.run(host='0.0.0.0',port=5000,debug=True)