from flask import Flask, render_template, redirect, url_for, request
import pymysql
import bcrypt
app = Flask(__name__)
# 数据库配置
db_config = {
'host': 'localhost',
'user':'root',
'password': '123456',
'database': 'zhufang',
'port':3306, # 默认MySQL端口
'charset': 'utf8mb4',
# 'cursorclass': pymysql.cursors.DictCursor
}
try:
conn = pymysql.connect(**db_config)
print("数据库连接成功!")
# 这里可以继续执行数据库操作,如执行查询或更新等
# 关闭数据库连接
# conn.close()
# print("数据库连接已关闭。")
except pymysql.MySQLError as e:
print("数据库连接错误:", e)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# 查询数据库中用户的哈希密码
cursor = conn.cursor()
cursor.execute('SELECT password FROM denglu WHERE user = %s', (username,))
user = cursor.fetchone()
# print(user)
if user is None:
error_message = '账号不存在'
return render_template('login.html', error=error_message)
# 继续处理正常情况下的逻辑
if user[0] == password :
return redirect(url_for('index'))
else:
error_message = '密码错误'
return render_template('login.html')
return render_template('login.html')
@app.route('/zhuce', methods=['GET', 'POST'])
def zhuce():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cursor = conn.cursor()
cursor.execute('INSERT INTO denglu (user, password) VALUES (%s, %s)', (username, password))
conn.commit()
return redirect(url_for('login'))
return render_template('zhuce.html')
def connect_db():
conn = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='denglu',
cursorclass=pymysql.cursors.DictCursor
)
return conn
@app.route('/xiugai', methods=['GET', 'POST'])
def xiugai():
if request.method == 'POST':
current_password = request.form['currentPassword']
new_password = request.form['newPassword']
confirm_password = request.form['confirmPassword']
# 连接数据库
conn = connect_db()
cursor = conn.cursor()
# 假设当前登录用户的用户名为假设的 'username'
username = 'username'
# 查询当前用户的密码
cursor.execute("SELECT password FROM denglu WHERE username=%s", (username,))
stored_password = cursor.fetchone()['password']
# 验证当前密码是否正确
if current_password == stored_password:
# 确认新密码和确认密码是否匹配
if new_password == confirm_password:
# 更新密码
cursor.execute("UPDATE denglu SET password=%s WHERE username=%s", (new_password, username))
conn.commit()
conn.close()
return "密码修改成功!"
else:
conn.close()
return "新密码和确认密码不匹配,请重新输入。"
else:
conn.close()
return "当前密码错误,请重新输入。"
# GET 请求返回修改密码的页面
return render_template('xiugai.html')
@app.route('/') #未登录的主页面
def xindex():
return render_template('xindex.html')
@app.route('/index') #主页面
def index():
return render_template('index.html')
@app.route('/kefang') #客房
def kefang():
return render_template('kefang.html')
@app.route('/fukuan') #客房
def fukuan():
return render_template('fukuan.html')
@app.route('/shangwu') #商务套房
def shangwu():
return render_template('shangwu.html')
@app.route('/dachuang') #大床房
def dachuang():
return render_template('dachuang.html')
@app.route('/shuangchuang') #双床房
def shuangchuang():
return render_template('shuangchuang.html')
@app.route('/zhongdian') #钟点房
def zhongdian():
return render_template('zhongdian.html')
@app.route('/tejia') #特价客房
def tejia():
return render_template('tejia.html')
@app.route('/lianxi') #联系我们
def lianxi():
return render_template('lianxi.html')
@app.route('/fuwu') #客房服务
def fuwu():
return render_template('fuwu.html')
@app.route('/qingjie') #客房服务>预约清洁
def qingjie():
return render_template('qingjie.html')
@app.route('/songcan') #客房服务>预约送餐
def songcan():
return render_template('songcan.html')
@app.route('/ewai') #客房服务>额外服务
def ewai():
return render_template('ewai.html')
if __name__ == '__main__':
app.run(debug=True)