From 32cdaa5c3fc7702471b035a45ccfbbb6748bc3f5 Mon Sep 17 00:00:00 2001 From: xtu202105570133 <1827197512@qq.com> Date: Wed, 31 May 2023 00:13:21 +0800 Subject: [PATCH] ADD file via upload --- app.py | 232 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..8fca794 --- /dev/null +++ b/app.py @@ -0,0 +1,232 @@ +from flask import Flask, render_template, request,redirect,url_for,jsonify +from flask_sqlalchemy import SQLAlchemy +from werkzeug.security import generate_password_hash,check_password_hash +from flask_cors import CORS +import re +import requests +from bs4 import BeautifulSoup +app = Flask(__name__,static_folder="C:/Users/陈公子/PycharmProjects/flaskProject3/static") +CORS(app) +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:1827197512@localhost/mydatabase' +db = SQLAlchemy(app) + +#定义用户表 +class User(db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(50), unique=True, nullable=False) + email = db.Column(db.String(120), unique=True, nullable=False) + password = db.Column(db.String(128), nullable=False) + def __repr__(self): + return f'' +#定义管理员表 +class Admin(db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(50), unique=True, nullable=False) + password = db.Column(db.String(128), nullable=False) + def __repr__(self): + return f'' +class Game(db.Model): + gameId = db.Column(db.Integer, primary_key=True) + gameName = db.Column(db.String(100), nullable=False) + coverImage = db.Column(db.String(100)) +#编辑用户信息 +@app.route('/User_edit/', methods=["GET", "POST"]) +def User_edit(id): + user = User.query.get_or_404(id) + if request.method == "POST": + user.username = request.form["username"] + password = request.form["password"] + password_hash = generate_password_hash(password) + user.password =password_hash; + db.session.commit() + return redirect("/user_admin") + return render_template("edit.html", user=user) +#主页面 +@app.route('/') +def index(): + url = "https://www.3dmgame.com/jqremphb.html" + url1 = "https://www.3dmgame.com/phb.html" + response = requests.get(url) + response1 = requests.get(url1) + html = response.text + html1 = response1.text + soup = BeautifulSoup(html, 'html.parser') + soup1 = BeautifulSoup(html1, 'html.parser') + a_tag = soup.find_all('div', class_='phlist') + a_tag1 = soup1.find_all('div', class_='phlist') + select_tag = a_tag[:6] + select_tag1 = a_tag1[:6] + srcs = [] + src1s = [] + for i in select_tag: + message = {} + message['img'] = i.find('a', class_='img').find('img')['data-original'] + message['word'] = i.find('div', class_='bt').find('a').get_text() + message['url'] = i.find('div',class_='bt').find('a')['href'] + srcs.append(message) + for i in select_tag1: + message = {} + message['img'] = i.find('a', class_='img').find('img')['data-original'] + message['word'] = i.find('div', class_='bt').find('a').get_text() + message['url'] = i.find('div', class_='bt').find('a')['href'] + src1s.append(message) + return render_template('none-login.html',srcs=srcs,src1s=src1s) +#用户搜索功能 +@app.route('/search', methods=['GET']) +def search_games(): + query = request.args.get('query') # 获取查询参数 + games = Game.query.all() + # 在游戏列表中使用正则表达式进行模糊匹配 + results = [] + pattern = re.compile('.*{}.*'.format(re.escape(query)), re.IGNORECASE) + for game in games: + if re.match(pattern, game['name']): + results.append(game) + + return jsonify(results) +#注册页面 +@app.route('/register') +def register_html(): + return render_template('register.html') +#登录页面 +@app.route('/login') +def login_html(): + return render_template('login.html') +#管理员登录页面 +@app.route('/admin_login') +def admin_login(): + return render_template('admin.html') +#注册处理 +@app.route('/register_dispose', methods=['POST']) +def register(): + username = request.form['username'] + email = request.form['email'] + password = request.form['password'] + confirm_password = request.form['confirm_password'] + if password != confirm_password: + return 'Password does not match!' + user = User.query.filter_by(username=username).first() + if user is not None: + return 'Username already exists!' + user = User.query.filter_by(email=email).first() + if user is not None: + return 'Email already exists!' + password_hash = generate_password_hash(password) + user = User(username=username, email=email, password=password_hash) + db.session.add(user) + db.session.commit() + return 'Registration successful!' +#登录处理 +@app.route('/login_dispose', methods=['POST']) +def login(): + username = request.form['username'] + password = request.form['password'] + url = "https://www.3dmgame.com/jqremphb.html" + url1 = "https://www.3dmgame.com/phb.html" + response = requests.get(url) + response1 = requests.get(url1) + html = response.text + html1 = response1.text + soup = BeautifulSoup(html, 'html.parser') + soup1 = BeautifulSoup(html1, 'html.parser') + a_tag = soup.find_all('div', class_='phlist') + a_tag1 = soup1.find_all('div', class_='phlist') + select_tag = a_tag[:6] + select_tag1 = a_tag1[:6] + srcs = [] + src1s = [] + for i in select_tag: + message = {} + message['img'] = i.find('a', class_='img').find('img')['data-original'] + message['word'] = i.find('div', class_='bt').find('a').get_text() + message['url'] = i.find('div',class_='bt').find('a')['href'] + srcs.append(message) + for i in select_tag1: + message = {} + message['img'] = i.find('a', class_='img').find('img')['data-original'] + message['word'] = i.find('div', class_='bt').find('a').get_text() + message['url'] = i.find('div', class_='bt').find('a')['href'] + src1s.append(message) + user = User.query.filter_by(username=username).first() + if user is None or not check_password_hash(user.password, password): + return 'Invalid username or password' + # valid login + else : + return render_template('game.html',user=user,srcs=srcs,src1s=src1s) +#管理员登录处理 +@app.route('/admin', methods=['POST']) +def admin(): + username = request.form['username'] + password = request.form['password'] + admin = Admin.query.filter_by(username=username).first() + if admin is None or not admin.password==password: + return 'Invalid username or password' + # valid login + else : + users = User.query.all() + return render_template('user_admin.html', users=users) +#用户密码管理页面 +@app.route('/user_admin') +def user_admin(): + users = User.query.all() + return render_template('user_admin.html', users =users) +#删除用户信息 +@app.route("/User_delete/") +def User_delete(id): + user = User.query.get_or_404(id) + db.session.delete(user) + db.session.commit() + return redirect('/user_admin') +#编辑用户信息 +@app.route("/edit",methods=['POST']) +def edit(): + email = request.form.get('email') # 使用 request.form 获取表单数据 + user = User.query.filter_by(email=email).first() + return render_template('edit.html',user=user) + +@app.route("/data_edit",methods=['POST']) +def data_edit(): + data = request.get_json() + newusername = data['newusername'] + username=data['username'] + user = User.query.filter_by(username=username).first() + if user is None: + return '发生了异常错误' + else: + user.username = newusername + db.session.commit() + return user.email +@app.route('/username_edit',methods=['GET']) +def username_edit(): + username = request.args.get('username') + user = User.query.filter_by(username=username).first() + if user is None: + return '用户名不存在' + else: + return render_template('username-edit.html',username=username) +@app.route('/edit2',methods=['GET']) +def edit2(): + username = request.args.get('username') + user = User.query.filter_by(username=username).first() + if user is None: + return '用户名不存在' + else: + return render_template('password-edit.html',username=username) +@app.route("/password_edit",methods=['POST']) +def password_edit(): + data = request.get_json() + oldpassoword = data['oldpassword'] + newpassword = data['newpassword'] + username=data['username'] + user = User.query.filter_by(username=username).first() + if user is None: + return '发生了异常错误' + else: + if check_password_hash(user.password, oldpassoword): + user.password = generate_password_hash(newpassword) + db.session.commit() + return user.email + else : + return '旧密码错误' +if __name__ == '__main__': + app.run(debug=True)