【VUE】会员管理(增删改查)

server/2024/10/9 10:30:08/

前端

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: "home"}},{path: "home",name: "home",component: () => import('../views/HomeView.vue')},{path: "vip",name: "vip",component: () => import('../views/VipView.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

views/VipView.vue

<template><h1>会员管理</h1><button @click="doEdit">编辑</button><table border="1"><thead><tr><th>ID</th><th>姓名</th><th>级别</th><th>积分</th><th>操作</th></tr></thead><tbody><tr v-for="(item,idx) in dataList"><td>{{ item.id }}</td><td>{{ item.name }}</td><td>{{ item.level_text }}</td><td>{{ item.score }}</td><td><a>编辑</a>|<a>删除</a></td></tr></tbody></table><div v-show="dialog" class="mask"></div><div v-show="dialog" class="dialog"><input type="text"/><p><button>保存</button><button @click="dialog=false">取消</button></p></div>
</template><script setup>
import {ref} from "vue";const dataList = ref([{id: 1, name: "cc", age: 18, level_text: "SVIP", score: 1000}])
const dialog = ref(false)function doEdit() {dialog.value = true
}
</script><style scoped>
.mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;background-color: black;opacity: 0.8;z-index: 998;
}.dialog {position: fixed;top: 200px;right: 0;left: 0;width: 400px;height: 300px;background-color: white;margin: 0 auto;z-index: 9999;
}
</style>

后端

GET     http://127.0.0.1:8000/api/vip/          -> 会员列表
POST    http://127.0.0.1:8000/api/vip/          -> 新增会员(请求体中传入数据)
DELETE  http://127.0.0.1:8000/api/vip/会员ID/    -> 删除会员
PUT     http://127.0.0.1:8000/api/vip/会员ID/    -> 更新会员(请求体中传入数据)

urls.py

"""
from django.urls import path
from api.views import account
from api.views import vipurlpatterns = [path('api/auth/', account.AuthView.as_view()),path('api/vip/', vip.VipView.as_view()),path('api/vip/<int:vid>/', vip.VipDetailView.as_view()),
]

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)class Vip(models.Model):""" 会员管理 """name = models.CharField(verbose_name="用户名", max_length=32)level = models.IntegerField(verbose_name="级别", choices=[(1, "VIP"), (2, "SVIP"), (3, "SSVIP")])score = models.IntegerField(verbose_name="积分")

views/vip.py

from rest_framework.views import APIView
from api import models
from rest_framework import serializers
from rest_framework.response import Responseclass VipSerializers(serializers.ModelSerializer):level_text = serializers.CharField(source="get_level_display", read_only=True)class Meta:model = models.Vipfields = "__all__"class VipView(APIView):def get(self, request):# 会员列表queryset = models.Vip.objects.all().order_by("id")ser = VipSerializers(instance=queryset, many=True)return Response({"code": 0, "data": ser.data})def post(self, request):# 新增passclass VipDetailView(APIView):def delete(self, request, vid):# 删除passdef put(self, request, vid):# 修改pass

前端:VipView.vue

<template><h1>会员管理</h1><button @click="doEdit">编辑</button><table border="1"><thead><tr><th>ID</th><th>姓名</th><th>级别</th><th>积分</th><th>操作</th></tr></thead><tbody><tr v-for="(item,idx) in dataList"><td>{{ item.id }}</td><td>{{ item.name }}</td><td>{{ item.level_text }}</td><td>{{ item.score }}</td><td><a>编辑</a>|<a>删除</a></td></tr></tbody></table><div v-show="dialog" class="mask"></div><div v-show="dialog" class="dialog"><input type="text"/><p><button>保存</button><button @click="dialog=false">取消</button></p></div>
</template><script setup>
import {ref, onMounted} from "vue";
import _axios from "@/plugins/axios.js";const dataList = ref([{id: 1, name: "cc", age: 18, level_text: "SVIP", score: 1000}])
const dialog = ref(false)onMounted(function (){_axios.get("/api/vip/").then((res) => {console.log(res.data)dataList.value = res.data.data})
})
function doEdit() {dialog.value = true
}
</script><style scoped>
.mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;background-color: black;opacity: 0.8;z-index: 998;
}.dialog {position: fixed;top: 200px;right: 0;left: 0;width: 400px;height: 300px;background-color: white;margin: 0 auto;z-index: 9999;
}
</style>

删除

vip.py

from rest_framework.views import APIView
from api import models
from rest_framework import serializers
from rest_framework.response import Responseclass VipSerializers(serializers.ModelSerializer):level_text = serializers.CharField(source="get_level_display", read_only=True)class Meta:model = models.Vipfields = "__all__"class VipView(APIView):def get(self, request):# 会员列表queryset = models.Vip.objects.all().order_by("id")ser = VipSerializers(instance=queryset, many=True)return Response({"code": 0, "data": ser.data})def post(self, request):# 新增passclass VipDetailView(APIView):def delete(self, request, vid):# 删除models.Vip.objects.filter(id=vid).delete()return Response({"code": 0})def put(self, request, vid):# 修改pass

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'] = "*"response['Access-Control-Allow-Methods'] = "*"return response

前端
VipView.vue

<template><h1>会员管理</h1><button @click="doEdit">新增</button><table border="1"><thead><tr><th>ID</th><th>姓名</th><th>级别</th><th>积分</th><th>操作</th></tr></thead><tbody><tr v-for="(item,idx) in dataList"><td>{{ item.id }}</td><td>{{ item.name }}</td><td>{{ item.level_text }}</td><td>{{ item.score }}</td><td><a>编辑</a>|<button @click="doDelete(item.id, idx)">删除</button></td></tr></tbody></table><div v-show="dialog" class="mask"></div><div v-show="dialog" class="dialog"><input type="text"/><p><button>保存</button><button @click="dialog=false">取消</button></p></div>
</template><script setup>
import {ref, onMounted} from "vue";
import _axios from "@/plugins/axios.js";const dataList = ref([{id: 1, name: "cc", age: 18, level_text: "SVIP", score: 1000}])
const dialog = ref(false)onMounted(function () {_axios.get("/api/vip/").then((res) => {console.log(res.data)dataList.value = res.data.data})
})function doEdit() {dialog.value = true
}function doDelete(vid, idx) {_axios.delete(`/api/vip/${vid}/`).then((res) => {// console.log(res.data)if (res.data.code === 0) {dataList.value.splice(idx, 1)}})
}
</script><style scoped>
.mask {position: fixed;top: 0;bottom: 0;left: 0;right: 0;background-color: black;opacity: 0.8;z-index: 998;
}.dialog {position: fixed;top: 200px;right: 0;left: 0;width: 400px;height: 300px;background-color: white;margin: 0 auto;z-index: 9999;
}
</style>

http://www.ppmy.cn/server/129219.html

相关文章

影视cms泛目录用什么程序?苹果cms二次开发泛目录插件

影视CMS泛目录一般使用的程序有很多种&#xff0c;&#xff08;maccmscn&#xff09;以下是其中几种常见的程序&#xff1a; WordPress&#xff1a;WordPress是一个非常流行的开源内容管理系统&#xff0c;可以通过安装一些插件来实现影视CMS泛目录功能。其中&#xff0c;一款常…

Spring Boot 进阶-详解SpringBoot的复杂数据校验规则

在之前的文章中,我们介绍了SpringBoot整合JSR-303规则来完成数据校验操作。接下来我们来聊一聊关于数据校验的具体用法。 之前的文章中举过一个简单的例子通过学生信息提交的例子来介绍了关于数据校验如何去做。那么接下来这篇文章,我们就来看看对于一些复杂的数据校验如何完…

CDN绕过学习

1.什么是CDN&#xff1f; CDN就是分布在各个地区的服务器&#xff0c;这些服务器储存着数据的副本。 哪些服务器比较接近你&#xff0c;当你发起请求时&#xff0c;提前就会快速为你提供服务。 总结来说就是&#xff1a; 其实就是用来加速访问的&#xff0c;以及缓解压力&a…

基于Dify的工作流简单测试

文章目录 工作流定义工作流构建新建工作流任务分解任务分类任务执行日常聊天任务执行计算字符串长度的三次幂任务执行获取ip地址任务执行其他任务不执行 变量汇集结果返回效果展示 工作流定义 下面是工作流官方文档中给出的工作流定义&#xff0c;其实工作流与Agent调用的对象…

主流反爬虫手段

这题我会&#xff0c;主流反爬虫手段 ①检测浏览器环境动态生成cookie(瑞数) ②验证码(极验&#xff0c;顶象) ③js混淆或加密&#xff0c;obfuscator的js混淆&#xff0c;wasm加密&#xff0c;jsvmp(js虚拟机) ④字体反爬&#xff0c;CSS反爬&#xff0c;雪碧图 ⑤tls指纹…

【Redis】Redis事务

1. 什么是事务&#xff1f; Redis 的事务和 MySQL的事务概念上是类似的,都是把一系列操作绑定成一组.让这一组能够批量执行.但是注意体会 Redis 的事务和 MySQL 事务的区别: 弱化的原子性&#xff1a; redis 没有"回滚机制".只能做到这些操作"批量执行",…

深度学习(7):RNN实战之人名的国籍预测

文章目录 数据集NameDataSetOne-Hot 编码是什么思路实现代码带来的问题 Embedding模型过程分析1.超参数设置2.数据集处理3. 数据 to Tensor4.训练 完整代码NameDateSetsimpleRNN 数据集 格式为(country_name).txt的文本文件包含该国家的常见姓名&#xff0c; 所以数据x是文本里…

Leetcode—152. 乘积最大子数组【中等】

2024每日刷题&#xff08;174&#xff09; Leetcode—152. 乘积最大子数组 C实现代码 class Solution { public:int maxProduct(vector<int>& nums) {int n nums.size();int mx nums[0];int mn nums[0];int ans mx;for(int i 1; i < n; i) {const int prem…