完成大作业服务端代码,新建管理端,管理端不可用
This commit is contained in:
34
Project/Manager/func/login.py
Normal file
34
Project/Manager/func/login.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from flask import request, jsonify, session, url_for, render_template
|
||||
from .config import db
|
||||
import pymysql
|
||||
|
||||
|
||||
def connect(managerID, encrypted_password):
|
||||
conn = pymysql.connect(**db)
|
||||
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
||||
args = (managerID, encrypted_password)
|
||||
verify_sql = "SELECT COUNT(*) FROM Managers WHERE ID = %s AND `Password` = %s;"
|
||||
cursor.execute(verify_sql, args)
|
||||
verified = cursor.fetchone()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return verified['COUNT(*)'] > 0
|
||||
|
||||
def login():
|
||||
if request.method == 'GET':
|
||||
return render_template('login.html')
|
||||
|
||||
if request.method == 'POST':
|
||||
session.pop('user_id', None)
|
||||
managerID = request.json.get('username')
|
||||
encrypted_password = request.json.get('password')
|
||||
try:
|
||||
user = connect(managerID, encrypted_password)
|
||||
if not user:
|
||||
return jsonify({'message': '账号或密码错误'}), 401
|
||||
session['user_id'] = managerID
|
||||
session.modified = True
|
||||
return jsonify({'redirect': url_for('index')})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'message': '数据库错误,请稍后再试'}), 500
|
||||
Reference in New Issue
Block a user