登录页面前后端

ops/2025/3/5 5:10:38/

目录

  • 一、Vue前端部署
    • 整体结构
    • 页面
    • 路由跳转
  • 二、后端
    • 结构
    • 路由
    • 监听端口和业务逻辑
  • 三、数据库
    • 数据库操作方法
    • 数据模型
    • 数据库绑定
  • 四、小结

一、Vue前端部署

整体结构

这是vue的项目结构
在这里插入图片描述

页面

Login_view.vue是用来写登录界面的,其中scripts部分中的methods用来发送post请求到后端服务器ip监听的8090端口下的后端路由上,axios 是一个基于 Promise 的 HTTP 客户端,用于发送 HTTP 请求。
axios.post 是 axios 提供的一个方法,用于发送 POST 请求。

<template><div class="login-container"><div class="login-box"><h2>登录</h2><form @submit.prevent="handleLogin"><div class="form-group"><label for="username">用户名</label><input type="text" id="username" v-model="loginForm.username" placeholder="请输入用户名" required></div><div class="form-group"><label for="password">密码</label><input type="password" id="password" v-model="loginForm.password" placeholder="请输入密码" required></div><button type="submit" class="login-btn">登录</button></form></div></div></template><script>import axios from 'axios';export default {name: 'Login_view',data () {return {loginForm: {username: '',password: ''}}},methods: {handleLogin () {const response = axios.post('http://140.207.xxxx:8090/auth/login', {username: this.loginForm.username,password: this.loginForm.password});// 处理返回的响应数据if (response.data) {const { success, answer, sources, figure, files_name} = response.data; // 确保提取 sourcesthis.response.sources = sources || []; // 确保 sources 被赋值this.response.figure = figure || []; // 确保 figure 被赋值this.response.files_name = files_name || []; // 确保 files_name 被赋值console.log('sources:', sources, 'figure:', figure, 'files_name:', files_name);if (success) {// 添加助手的回复到聊天记录const assistantMessage = {contactText: answer || '抱歉,我没有收到有效的回复',nickName: '智能助手',timestamp: new Date().toISOString(),mineMsg: false,isReference: false,isDisplayed: true,references: this.response.sources, // 引用文件files_name: this.response.files_name, // 引用文件名references_figure: this.response.figure // 引用图表};console.log('references_figure:', this.response.figure);this.recordContent.push(assistantMessage); // 记录助手的回复} else {console.error('请求失败:', answer); // 输出失败信息}}}}}</script><style scoped>.login-container {height: 100vh;display: flex;justify-content: center;align-items: center;background-color: #f5f5f5;}.login-box {width: 400px;padding: 20px;background: white;border-radius: 8px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);}h2 {text-align: center;color: #333;margin-bottom: 30px;}.form-group {margin-bottom: 20px;}label {display: block;margin-bottom: 5px;color: #666;}input {width: 100%;padding: 10px;border: 1px solid #ddd;border-radius: 4px;box-sizing: border-box;}input:focus {outline: none;border-color: #42b983;}.login-btn {width: 100%;padding: 12px;background-color: #42b983;color: white;border: none;border-radius: 4px;cursor: pointer;font-size: 16px;}.login-btn:hover {background-color: #3aa876;}</style>

路由跳转

index,js负责路由跳转,/是根目录,代开8090端口第一个页面是登录页面,然后再由后续的逻辑决定

import { createRouter, createWebHistory } from 'vue-router';
import Main from '@/views/main.vue' 
import Login_view from '@/views/Login_view.vue' const routes = [{ path: '/', component: Login_view },{ path: '/main', component: Main },
];const router = createRouter({history: createWebHistory(),routes
});export default router;

二、后端

结构

在这里插入图片描述

路由

蓝图
Flask 是一个 Python 的 Web 框架,而蓝图是 Flask 中用于组织和管理路由的工具,可以将应用拆分为多个模块,便于维护和扩展。
有了蓝图就不用把所有的路由都写在一块,逻辑上看起来清晰,而且可复用


from .knowledge_route import knowledge_bp
from .session_route import session_bp
from .chat_route import chat_bp
from .auth import auth_bp
# 可以在这里添加更多的蓝图注册逻辑
def register_routes(app):app.register_blueprint(knowledge_bp, url_prefix='/knowledge')app.register_blueprint(session_bp, url_prefix='/session')app.register_blueprint(chat_bp, url_prefix='/chat')app.register_blueprint(auth_bp, url_prefix='/auth')

监听端口和业务逻辑

/auth/login对应着监听的端口,监听前端发送过来的post或者get请求
在这里插入图片描述

def login():if request.method=='GET':return render_template('login.html')else:print("\n=== 开始处理登录请求 ===")data = request.get_json()print(f"接收到的请求数据: {data}")username = data.get('username')password = data.get('password')print(f"用户名: {username}")print(f"密码: {password}")# 根据用户名查询数据库中用户的密码db_password=AuthService.get_password_by_username(username)print(f"数据库中的密码: {db_password}")# 检查密码是否正确if not check_password_hash(db_password,password):return jsonify({"code":400,"message":"密码错误"})return jsonify({"code":200,"message":"登录成功"})

三、数据库

数据库操作方法

业务逻辑调用数据库操作方法,在这个services下面就是放的都是各个数据库的对应着业务的增删改查操作
在这里插入图片描述
主要完成根据用户名查找对应密码,这一步数据库操作是对数据模型进行操作,user,user是写在model中的数据库模型类

from app.models import  User  # 假设你有一个Knowledge模型
from app.exts import dbclass AuthService:def get_password_by_username(username):try:with db.session.begin():info = db.session.query(User).filter_by(user_name=username).first()if info:password = info.password  # 获取 original_nameprint(f"密码: {password}")return passwordelse:print("未找到对应的密码")except Exception as e:print(f"查询密码时出错: {str(e)}")return None

数据模型

在这里插入图片描述
定义了该数据库的字段和类
在这里插入图片描述

数据库绑定

在这里插入图片描述


from flask_sqlalchemy import SQLAlchemydb = SQLAlchemy()

db = SQLAlchemy() 是 Flask 中用于初始化 SQLAlchemy 对象的代码。SQLAlchemy 是一个强大的 Python ORM(对象关系映射)工具,用于与关系型数据库(如 MySQL、PostgreSQL、SQLite 等)进行交互。通过 SQLAlchemy,开发者可以用 Python 类和对象的方式操作数据库,而不需要直接编写 SQL 语句。
当我们在初始化的时候,把这个db和数据库进行绑定
在这里插入图片描述
启用 CORS(跨域资源共享),允许前端应用从不同的域名访问后端 API。

CORS 是 flask_cors 库提供的功能,用于解决浏览器的跨域限制。
Config 是一个配置类,通常定义在单独的模块中(如 config.py),包含数据库连接、密钥等配置项。
将 SQLAlchemy 实例 (db) 与 Flask 应用实例 (app) 绑定。
register_routes 是一个自定义函数,通常用于将蓝图(Blueprint)或其他路由注册到应用中。

四、小结

数据库的操作就是一层一层的抽象,各个层面完成各自的事情,然后由上层来调用。
前端如果需要一直开启页面的话,可以打包成dist然后放到apache服务器上,这个服务器他会一直开启。这样自己本机关闭了,还是能够使用前端服务。后端一般是在linux里面写的,所以服务可以一直开启。


http://www.ppmy.cn/ops/162742.html

相关文章

基于SpringBoot和Leaflet的行政区划地图掩膜效果实战

目录 前言 一、掩膜小知识 1、GIS掩膜的实现原理 2、图层掩膜流程? 二、使用插件 1、leaflet-mask介绍 2、核心代码解释? 三、完整实例实现 1、后台逻辑实现 2、省级行政区划查询实现 3、行政区划定位及掩膜实现? 4、成果展示 总结 前言 在之前的博客提过按空…

网络空间安全(6)web应用程序技术

前言 Web应用程序技术是指用于开发和构建基于Web的应用程序的技术和工具&#xff0c;涵盖了前端开发、后端开发、数据库管理、安全性等多个方面的技术。 一、前端开发技术 HTML/CSS/JavaScript&#xff1a;HTML用于构建网页结构&#xff0c;CSS用于进行样式设计&#xff0c;Jav…

vue3自定义指令

在 Vue 3 中&#xff0c;自定义指令是一种强大的工具&#xff0c;它允许你对普通 DOM 元素进行底层操作&#xff0c;以实现一些特殊的交互效果或功能。下面将详细介绍 Vue 3 自定义指令的相关内容&#xff0c;包括基本用法、指令钩子函数、传参以及全局和局部自定义指令的使用。…

TCP的三次握手与四次挥手:建立与终止连接的关键步骤

引言 ‌TCP&#xff08;传输控制协议&#xff09;工作在OSI模型的传输层‌。OSI模型将计算机网络功能划分为七个层级&#xff0c;从底层到顶层依次是&#xff1a;物理层、数据链路层、网络层、传输层、会话层、表示层和应用层。传输层负责在网络节点之间提供可靠的端到端通信&a…

【算法系列】经典的堆排序算法

文章目录 堆排序算法什么是堆排序&#xff1f;二叉堆的概念 堆排序的基本步骤堆排序的详细流程构建最大堆维护最大堆排序过程Java代码实现 堆排序的图示步骤1. 初始的数组与堆2. 构建最大堆2.1. 检查节点9&#xff08;序号为3&#xff09;2.2. 检查节点6&#xff08;序号为2&am…

CF 118A.String Task(Java实现)

题目分析 输入一个字符串&#xff0c;遍历每一个字符&#xff0c;如果是元音字母就删除&#xff0c;辅音字母就在其前面增加一个.&#xff0c;且所有字母输出都是小写。 思路分析 将输入的字符串改为字符数组&#xff0c;考虑到任意位置插入的情况&#xff0c;所以主要选择Lin…

C++的类和对象入门

目录 目录 目录 一、类 1.1类的定义 1.2访问限定符 1.3类域 1.4类的命名规范 1.5class和struct的默认访问权限 二、类的实例化 2.2对象的大小和存储 2.3空类的大小 三、this指针 3.1this指针的定义 3.2this指针的作用 3.2.1区分同名变量和局部变量 3.2.2返回对象…

Lesson 2 Breakfast or lunch?

Lesson 2 Breakfast or lunch? 词汇 until prep. 直到…… 用法&#xff1a;not until 时间点 直到……才 区别&#xff1a;untiltill 意思相同 例句&#xff1a;直到午夜&#xff0c;我才睡着。    I did not fall in asleep until midnight.    她直到50多岁才结婚…