代码拉取完成,页面将自动刷新
同步操作将从 Yiman/VisualizationProject 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import datetime
import redis
from flask import Flask, render_template, request, session, redirect, flash
from views.china import china
from views.checkin import stu
from views.manage import manage
from views.search import search
from views.world import world
from service.user_service import register, login, logout, send_verification
from enums.return_enum import ResType
from enums.role_enum import Roles
from settings import MAX_USERNAME_LENGTH, MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH
from settings import MIN_USERNAME_LENGTH
db = redis.Redis(host='localhost', decode_responses=True)
main = Flask(__name__)
# 设置session过期时间,设置为30分钟
main.permanent_session_lifetime = datetime.timedelta(seconds=60 * 30)
main.config['SECRET_KEY'] = 'jk12la1s2df21h'
main.config.update(DEBUG=True)
# 注册蓝图
main.register_blueprint(world)
main.register_blueprint(china)
main.register_blueprint(stu)
main.register_blueprint(manage)
main.register_blueprint(search)
# 拦截器
@main.before_first_request
def init_session():
session["login_user"] = ""
session["role"] = Roles.NOTHING.value
session["last_view"] = ""
# 主页
@main.route('/homePage')
def home_page():
if session.get("role") in [Roles.NOTHING, None]:
flash("请先登录!")
return redirect('/')
return render_template("homePage.html", login_user=session.get("login_user"), role=session.get("role"))
# 首页
@main.route('/', methods=["Get", "POST"])
def index():
if session.get("role") not in [Roles.NOTHING, None, Roles.TOURIST]:
return redirect('/homePage')
return render_template("index.html")
# 登录
@main.route('/login', methods=["Get", "POST"])
def login_user() -> dict:
username = request.form.get("username")
password = request.form.get("password")
remember = request.form.get("remember")
if not all([username, password]):
return {"status": ResType.PARAM_ERR.value, "msg": "登录信息不完整!"}
result = login(username, password)
if result["status"] == ResType.OK.value:
session["login_user"] = username
session["role"] = result["role"]
session["last_view"] = "中国"
if remember == "true":
session.permanent = True
else:
session.permanent = False
return result
# 游客登录
@main.route("/tourist")
def login_tourist():
session.clear()
session["login_user"] = "tourist"
session["role"] = Roles.TOURIST.value
session["last_view"] = "中国"
return redirect('/homePage')
# 注册
@main.route('/register', methods=["POST"])
def register_user() -> dict:
username = request.form.get("username")
password = request.form.get("password")
email = request.form.get("email")
verification = request.form.get("verification")
# 输入参数合法性验证,判断是否为空
if not all([username, password, email, verification]):
return {"status": ResType.PARAM_ERR.value, "msg": "输入参数不完整!"}
# 用户名长度校验
if len(username) < MIN_USERNAME_LENGTH or len(username) > MAX_USERNAME_LENGTH:
return {"status": ResType.PARAM_ERR.value,
"msg": "用户名长度应在" + str(MIN_USERNAME_LENGTH) + "到" + str(MAX_USERNAME_LENGTH) + "个字符之间"}
# 密码长度校验
if len(password) < MIN_PASSWORD_LENGTH or len(password) > MAX_PASSWORD_LENGTH:
return {"status": ResType.PARAM_ERR.value,
"msg": "密码长度应在" + str(MIN_PASSWORD_LENGTH) + "到" + str(MAX_PASSWORD_LENGTH) + "个字符之间"}
# 验证码校验
code = db.get(email)
# 输入的验证码为空或错误
if not verification or verification != code:
return {"status": ResType.VERIFICATION_ERR.value, "msg": "验证码错误或已失效"}
return register(username, password, email)
# 注销
@main.route('/logout', methods=["Get", "POST"])
def logout_user():
if session.get("role") in [Roles.NOTHING, None]:
flash("请先登录!")
return redirect('/')
return logout()
# 验证码
@main.route('/verify', methods=["POST"])
def verify():
# 获取邮箱地址并校验是否为空
email = request.form.get("email")
if not email:
return {"status": ResType.PARAM_ERR.value, "msg": "请输入完整的参数"}
send_verification(email)
return {"status": ResType.OK.value, "msg": "注册码已发送,注意查收!"}
if __name__ == '__main__':
main.run(host="127.0.0.1", port=8000, debug=True)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。