【VUE】案例:商场会员管理系统

embedded/2024/12/23 5:39:44/

编写vue+dfr实现对会员进行基本增删改查

1. drf项目初始化

  • 请求:

    POST
    http://127/0.0.0.1:8000/api/auth/
    {"username":"cqn", "password":"123"}
    
  • 返回:

    {"username":"cqn", "token":"fwjkfbj"}
    
  • 创建项目
    在这里插入图片描述

  • 项目目录结构
    在这里插入图片描述

1.1 安装并注册drf

  • pip install djangorestframework
  • settings.py
    INSTALLED_APPS = [...'django.contrib.staticfiles','api.apps.ApiConfig','rest_framework',
    ]
    

1.2 创建数据库表并添加数据

  • models.py
    from django.db import modelsclass UserInfo(models.Model):username = models.CharField(verbose_name="用户名", max_length=64)password = models.CharField(verbose_name="密码", max_length=64)token = models.CharField(verbose_name="token", max_length=64, null=True, blank=True)
    
  • makemigrations
  • migrate
  • 插入两条数据“root 123”,“cqn 123”

1.3 创建apiview文件

views/account.py

import uuid
from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.response import Response
from api import modelsclass AuthSerializer(serializers.Serializer):username = serializers.CharField(required=True)password = serializers.CharField(required=True)class AuthView(APIView):def post(self, request):# 1. 获取用户提交数据 request.data = {"username": "xxx", "password": "...}# 2. 表单校验ser = AuthSerializer(data=request.data)if not ser.is_valid():return Response({"code": 1000, "msg": "校验失败", "detail": ser.errors})# 3. 数据库校验user_object = models.UserInfo.objects.filter(**ser.data).first()if not user_object:return Response({"code": -1, "msg": "用户名或密码错误"})token = uuid.uuid4()user_object.token = tokenuser_object.save()# 4. 数据返回return Response({"code": 0,"data": {"id": user_object.id, "name": user_object.username, "token": user_object.token}})

1.4 添加url配置

urls.py

from django.urls import path
from api.views import accounturlpatterns = [path('api/auth/', account.AuthView.as_view()),
]

1.5 启动并验证

在这里插入图片描述

2. vue项目初始化

  • 创建项目
    在这里插入图片描述
  • 项目目录
    在这里插入图片描述

2.1 main.js注释全局样式

// import './assets/main.css'

2.2 App.vue

<script setup>
</script><template><RouterView />
</template><style scoped>
</style>

2.3 views

LoginView.vue

<template><div class="box"><p>用户名:<input type="text" placeholder="用户名" v-model="msg.username"></p><p>密码:<input type="text" placeholder="密码" v-model="msg.password"></p><p><button @click="doLogin">登录</button></p></div>
</template><script setup>
import {ref} from "vue";
import {useRouter} from "vue-router";
import {userInfoStore} from "@/stores/user.js";
import _axios from "@/plugins/axios.js";const msg = ref({username: "",password: ""
})
const router = useRouter()
const store = userInfoStore()const doLogin = function () {// 1、获取数据console.log(msg.value)// 2、发送网络请求_axios.get("/api/v1/course/category/free/?courseType=free").then((res) => {console.log(res.data)})// 3、本地存储用户信息(登录凭证)// localStorage.setItem("name", msg.value.username)let info = {id: 1, name: msg.value.username, token: "ddsafhsjdfkj"}store.doLogin(info)// 3、跳转到首页router.push({name: "mine"})
}</script><style scoped>
.box {width: 300px;margin: 100px auto;
}
</style>

AdminView.vue

<template><div class="page-header"><div class="container"><RouterLink to="/admin/mine">我的</RouterLink>|<RouterLink to="/admin/order">订单</RouterLink><div style="float:right;"><a>{{store.userName}}</a><a @click="doLogout">退出1</a></div></div></div><div class="container"><RouterView/></div>
</template><script setup>
import {useRouter} from "vue-router";
import {userInfoStore} from "@/stores/user.js";const router = useRouter()
const store = userInfoStore()function doLogout() {store.doLogout()router.push({name: "login"})
}
</script><style scoped>
body {margin: 0;
}.page-header {height: 48px;background-color: cornflowerblue;line-height: 48px;
}.page-header a {display: inline-block;padding: 0 10px;cursor: pointer;
}.container {width: 980px;margin: 0 auto;
}</style>

MineView.vue

<template><h1>Mine</h1>
</template><script setup>
</script><style scoped>
</style>

OrderView.vue

<template><h1>Order</h1>
</template><script setup>
</script><style scoped>
</style>

2.4 stores

user.js

import {ref, computed} from 'vue'
import {defineStore} from 'pinia'export const userInfoStore = defineStore('userInfo', () => {const userString = ref(localStorage.getItem("info"))const userDict = computed(() => userString.value ? JSON.parse(userString.value) : null)const userId = computed(() => userDict.value ? userDict.value.id : null)const userName = computed(() => userDict.value ? userDict.value.name : null)const userToken = computed(() => userDict.value ? userDict.value.token : null)function doLogin(info) {localStorage.setItem("info", JSON.stringify(info))userString.value = JSON.stringify(info)}function doLogout() {localStorage.clear()userString.value = null}return {userId, userName, userToken, doLogin, doLogout}
})

2.5 router

index.js

import { createRouter, createWebHistory } from 'vue-router'
import {userInfoStore} from "@/stores/user.js";const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/login',name: 'login',component: () => import('../views/LoginView.vue')},{path: '/admin',name: 'admin',component: () => import('../views/AdminView.vue'),children: [{path: "",redirect: {name: "mine"}},{path: "mine",name: "mine",component: () => import('../views/MineView.vue')},{path: "order",name: "order",component: () => import('../views/OrderView.vue')}]}]
})
router.beforeEach(function (to,from,next) {// 1.访问登录页面,不需要登录就可以直接去查看if (to.name === "login") {next()return}// 2.检查用户登录状态,登录成功,继续往后走next();未登录,跳转至登录页面// let username = localStorage.getItem("name")const store = userInfoStore()if (!store.userId){next({name:"login"})return;}// 3.登录成功且获取到用户信息,继续向后访问next()
})export default router

2.6 plugins

axios.js

import axios from "axios";let config = {baseURL: "https://api.luffycity.com/",timeout: 20 * 1000
}const _axios = axios.create(config)_axios.interceptors.request.use(function (config){// console.log("请求前:", config)// 1. 去pinia中读取当前用户token// 2. 发送请求时携带tokenif(config.params){config.params["token"] = "djbfkjbdfkj"} else {config.params = {"token": "whejsabjdnfj"}}return config
})export default _axios

2.7 安装并启动应用

  • sudo npm i
  • sudo npm i axios
  • sudo npm run dev
    在这里插入图片描述

3、前端调用后端,代码配置

ajax.js

import axios from "axios";let config = {baseURL: "http://127.0.0.1:8000/",timeout: 20 * 1000
}const _axios = axios.create(config)_axios.interceptors.request.use(function (config){// console.log("请求前:", config)// 1. 去pinia中读取当前用户token// 2. 发送请求时携带token// if(config.params){//     config.params["token"] = "djbfkjbdfkj"// } else {//     config.params = {"token": "whejsabjdnfj"}// }return config
})export default _axios

LoginView.vue

<template><div class="box"><p>用户名:<input type="text" placeholder="用户名" v-model="msg.username"></p><p>密码:<input type="text" placeholder="密码" v-model="msg.password"></p><p><button @click="doLogin">登录</button></p></div>
</template><script setup>
import {ref} from "vue";
import {useRouter} from "vue-router";
import {userInfoStore} from "@/stores/user.js";
import _axios from "@/plugins/axios.js";const msg = ref({username: "",password: ""
})
const router = useRouter()
const store = userInfoStore()const doLogin = function () {// 1、获取数据console.log(msg.value)// 2、发送网络请求_axios.post("/api/auth/", msg.value).then((res) => {console.log(res.data)})// 3、本地存储用户信息(登录凭证)// localStorage.setItem("name", msg.value.username)// let info = {id: 1, name: msg.value.username, token: "ddsafhsjdfkj"}// store.doLogin(info)// // 3、跳转到首页// router.push({name: "mine"})
}</script><style scoped>
.box {width: 300px;margin: 100px auto;
}
</style>

4、现象:vue前端向后端API发送请求时有问题

Access to XMLHttpRequest at ‘http://127.0.0.1:8000/api/auth/’ from origin ‘http://localhost:5173’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
Access to XMLHttpRequest at ‘http://127.0.0.1:8000/api/auth/’ from origin ‘http://localhost:5173’ has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

4.1 请求跨域问题

1、浏览器中有同源策略,当发送ajax请求:请求的地址与当前所在网址保持一致,如果不一致,浏览器就会阻止请求
2、为什么postman/requests不报错? 因为这俩软件没有同源策略的要求
3、在网站中用cdn地址? <script src='xxx'></script> <img src='xxx'/> 无影响
4、项目开发时,会经常用到跨域
5、解决方式

  • jsonp,发送ajax请求时,跨域会被浏览器阻止;所以不再使用ajax发送请求,而是构造script标签+src发送请求获取结果。
  • cors:添加响应头,让浏览器别再限制

6、后端API如何实现CORS? 中间件

Access-Control-Allow-Origin:"*"
Access-Control-Allow-Headers: "*"

7、实现方式
utils/cors.py

from django.utils.deprecation import MiddlewareMixinclass CorsMiddleware(MiddlewareMixin):def process_response(self, request, response):response['Access-Control-Allow-Origin'] = "*"response['Access-Control-Allow-Headers'] = "*"return response

setting.py

MIDDLEWARE = [...'utils.cors.CorsMiddleware'
]

http://www.ppmy.cn/embedded/121195.html

相关文章

二、初步编写drf API

2.1基于django #settings.py urlpatterns [path(admin/, admin.site.urls),path(auth,views.auth) #创建一个路由 ]#views.py from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt# Create your views here.c…

蓝桥杯备赛---2.新建工程

推荐根据视频进行工程建立 开发板资源简介&工程模板建立_哔哩哔哩_bilibili 目录 推荐根据视频进行工程建立 1.点击"File"下的"New Project"新建一个工程 ​编辑 2. 查看官方给的数据手册&#xff0c;选择对于的单片机型号 3. 查看原理图&#…

人工智能未来发展大模型和行业APP融合

人工智能大模型和行业APP融合发展 人工智能大模型与行业APP的融合发展是当前人工智能领域的一个重要趋势&#xff0c;这种融合不仅推动了行业的智能化转型&#xff0c;也提升了用户体验和企业的竞争力。以下是对这一融合发展的详细分析&#xff1a; 一、融合发展背景 随着人…

linux dbus介绍,彻底懂linux bluez dbus

零. 前言 由于Bluez的介绍文档有限,以及对Linux 系统/驱动概念、D-Bus 通信和蓝牙协议都有要求,加上网络上其实没有一个完整的介绍Bluez系列的文档,所以不管是蓝牙初学者还是蓝牙从业人员,都有不小的难度,学习曲线也相对较陡,所以我有了这个想法,专门对Bluez做一个系统…

【Kubernetes】常见面试题汇总(五十一)

目录 114. K8S 集群服务访问失败&#xff08;情况一&#xff09;&#xff1f; 115. K8S 集群服务访问失败&#xff08;情况二&#xff09;&#xff1f; 特别说明&#xff1a; 题目 1-68 属于【Kubernetes】的常规概念题&#xff0c;即 “ 汇总&#xff08;一&#xff…

基于Python的自然语言处理系列(22):模型剪枝(Pruning)

在深度学习领域&#xff0c;尤其是当模型部署到资源有限的环境中时&#xff0c;模型压缩技术变得尤为重要。剪枝&#xff08;Pruning&#xff09;是一种常见的模型压缩方法&#xff0c;通过减少模型中不重要的参数&#xff0c;可以在不显著降低模型性能的情况下提升效率。在本文…

用责任链模式改造 if else

我的上一篇文章&#xff0c;因为if else 多了&#xff0c;捣鼓很久&#xff0c;今天用责任链模式改造一下。 代码写着写着&#xff0c;if else if 逻辑忘记了&#xff0c;哎。。。-CSDN博客 责任链模式&#xff08;Chain of Responsibility Pattern&#xff09; 1. 什么是责任…

CSS网格布局

前言 希望元素按照网格的方式进行布局&#xff0c;最简单的方式就是利用网格布局&#xff0c;如图所示&#xff1a; 网格布局 设置网格布局的核心属性&#xff1a; ① display: grid 设置容器为网格布局容器&#xff08;如果希望设置行内的网格容器&#xff0c;可以设置disp…