129 lines
4.3 KiB
Python
129 lines
4.3 KiB
Python
from flask import Flask, render_template, request, flash, redirect, url_for
|
|
import pymysql
|
|
from pymysql.cursors import Cursor
|
|
import os
|
|
from typing import Dict
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = os.environ.get('SECRET_KEY', 'OPTIONALSECRETKEY')
|
|
|
|
def get_db():
|
|
return pymysql.connect(
|
|
host='localhost', user='kejingfan',
|
|
password='xxxxxxxx', database='DBLab_7_1',
|
|
charset='utf8mb4',
|
|
cursorclass=pymysql.cursors.DictCursor
|
|
)
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
@app.route("/signup.html", methods=('GET', 'POST'))
|
|
def signup():
|
|
if request.method == 'GET':
|
|
return render_template('signup.html')
|
|
|
|
if request.method == 'POST':
|
|
id = request.form['cardCode']
|
|
name = request.form['name']
|
|
phone_number = request.form['mobileNo']
|
|
password = request.form['encryptedPassword']
|
|
|
|
db = get_db()
|
|
cursor = db.cursor()
|
|
|
|
try:
|
|
cursor.callproc('RegisterPassenger', (id, name, phone_number, password, "@result_message"))
|
|
cursor.fetchall()
|
|
cursor.execute("SELECT @_RegisterPassenger_4;")
|
|
result_message = cursor.fetchone()['@_RegisterPassenger_4']
|
|
print(result_message)
|
|
flash(result_message)
|
|
db.commit()
|
|
except pymysql.MySQLError as e:
|
|
db.rollback()
|
|
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
|
|
flash("乘客已存在,无法重复注册")
|
|
else:
|
|
print(e)
|
|
flash("数据库异常,注册失败")
|
|
db.close()
|
|
return redirect(url_for('index'))
|
|
|
|
def verify_user(cursor: Cursor, id: str, password: str) -> str:
|
|
try:
|
|
cursor.callproc('VerifyUser', (id, password, "@verify_status"))
|
|
cursor.fetchall()
|
|
cursor.execute("SELECT @_VerifyUser_2;")
|
|
verify_status = cursor.fetchone()['@_VerifyUser_2']
|
|
except pymysql.MySQLError as e:
|
|
print(e)
|
|
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
|
|
return "NO_USER"
|
|
return "DB_ERROR"
|
|
return verify_status
|
|
|
|
class ModifyInfo:
|
|
def __init__(self, form: Dict[str, str]):
|
|
self.id = form['cardCode']
|
|
modifyType = form['modifyType']
|
|
self.new_password = form['encryptedNewPassword']
|
|
self.phone_number = form['mobileNo'] if form['mobileNo'] != "" else "11111111111"
|
|
modifyType2command = {
|
|
'1': 'delete account',
|
|
'2': 'modify Password',
|
|
'3': 'modify Phone_Number'
|
|
}
|
|
self.command = modifyType2command[modifyType]
|
|
|
|
def get_args(self):
|
|
return (self.id, self.command, self.new_password, self.phone_number, "@result_message")
|
|
|
|
def get_ok_message(self, cursor):
|
|
cursor.execute("SELECT @_ModifyPassengerInfo_4;")
|
|
return cursor.fetchone()['@_ModifyPassengerInfo_4']
|
|
|
|
@app.route("/modify.html", methods=('GET', 'POST'))
|
|
def modify():
|
|
if request.method == 'GET':
|
|
return render_template('modify.html')
|
|
|
|
if request.method == 'POST':
|
|
id = request.form['cardCode']
|
|
password = request.form['encryptedPassword']
|
|
db = get_db()
|
|
cursor = db.cursor()
|
|
|
|
verify_info = verify_user(cursor, id, password)
|
|
if verify_info == "NO_USER":
|
|
flash("您未注册过,无法修改账号")
|
|
db.close()
|
|
return redirect(url_for('signup'))
|
|
elif verify_info == "WRONG_PASSWORD":
|
|
flash("密码错误")
|
|
db.close()
|
|
return redirect(url_for('modify'))
|
|
elif verify_info == "DB_ERROR":
|
|
db.close()
|
|
return redirect(url_for('modify'))
|
|
|
|
modifyInfo = ModifyInfo(request.form)
|
|
try:
|
|
cursor.callproc('ModifyPassengerInfo', modifyInfo.get_args())
|
|
cursor.fetchall()
|
|
db.commit()
|
|
flash(modifyInfo.get_ok_message(cursor))
|
|
except pymysql.MySQLError as e:
|
|
db.rollback()
|
|
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
|
|
flash("用户不存在,无法修改")
|
|
else:
|
|
print(e)
|
|
flash("数据库异常,修改失败")
|
|
db.close()
|
|
return redirect(url_for('index'))
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|