Merge pull request '完成列表查询接口的对接,包括前端列表和分页模块的搭建' (#29) from Brunch_LPQ into main

pull/38/head
ppnwsfegt 2 months ago
commit 15c3b7b480

@ -11,5 +11,5 @@ import lombok.Data;
public class ResultVo<T> {
private String msg;
private int code;
private T date;
private T data;
}

@ -23,17 +23,24 @@ public class SysUserController {
// 新增用户
@PostMapping//使用post请求
public ResultVo addUser(@RequestBody SysUser sysUser) {
// 判断用户是否重复
QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(SysUser::getUsername,sysUser.getUsername()).eq(SysUser::getPhone,sysUser.getPhone());
SysUser user = sysUserService.getOne(queryWrapper);
if(user != null){
return ResultUtils.error("用户已存在!或该手机号已被使用!");
// 1. 单独检查用户名
if (sysUserService.lambdaQuery()
.eq(SysUser::getUsername, sysUser.getUsername())
.exists()) {
return ResultUtils.error("用户名已存在!");
}
if(sysUserService.save(sysUser)){
return ResultUtils.success("新增用户成功!");
// 2. 单独检查手机号
if (sysUserService.lambdaQuery()
.eq(SysUser::getPhone, sysUser.getPhone())
.exists()) {
return ResultUtils.error("手机号已被使用!");
}
return ResultUtils.error("新增用户失败!");
// 3. 保存用户
return sysUserService.save(sysUser)
? ResultUtils.success("新增用户成功!", sysUser)
: ResultUtils.error("新增用户失败!");
}
// 编辑用户

@ -1,7 +1,12 @@
import http from "../../http";
import type { UserModel } from "./userModel";
import type { ListUserParm, UserModel } from "./userModel";
// 新增接口
export const addUserApi = (parm:UserModel)=>{
return http.post("/api/user", parm)
}
// 列表查询
export const getListApi = (parm:ListUserParm)=>{
return http.get("api/user/list", parm)
}

@ -4,6 +4,7 @@ export type ListUserParm = {
pageSize: number;
name: string;
phone: string;
total: number; //分页总条数
}
// 定义表单数据类型

@ -1,7 +1,13 @@
// 抽离表格相关业务
import type { ListUserParm } from "@/api/user/userModel";
import { ref, reactive } from "vue";
import { ref, reactive, onMounted, nextTick } from "vue";
// 引入列表查询api
import { getListApi } from "../../api/user/index";
// 设置表格高度(默认值设置为0)
const tableHeight = ref(0)
export default function useUserTable() {
// 列表查询的参数
@ -9,13 +15,20 @@ export default function useUserTable() {
currentPage: 1,
pageSize: 10,
name: '',
phone: ''
phone: '',
total: 0
})
// 定义tableList用来接收列表数据
const tableList = ref([])
// 列表
const getList = () => {
const getList = async () => {
// 在getList方法中调用getListApi
let res = await getListApi(listParm)
console.log(res)
if (res && res.code == 200) {
tableList.value = res.data.records;
listParm.total = res.data.total;
}
}
// 获取show方法
@ -30,12 +43,38 @@ export default function useUserTable() {
const resetBtn = () => {
tableShowBtn.value?.show("重置")
}
// 在组件挂载时加载数据
onMounted(() => {
getList()
// 计算表格高度覆盖默认值
nextTick(()=>{
tableHeight.value = window.innerHeight - 220
})
})
// 定义页容量改变方法
const sizeChange = (size: number) => {
listParm.pageSize = size
getList()
}
// 定义当前页数改变方法
const currentChange = (page: number) => {
listParm.currentPage = page
getList()
}
// 将表格查询的方法返回出去
return {
tableShowBtn,
listParm,
getList,
searchBtn,
resetBtn
resetBtn,
tableList,
sizeChange,
currentChange,
tableHeight
}
}

@ -10,6 +10,13 @@ const config = {
timeout: 10000,
}
// 返回值数据类型
export interface Result<T = any> {
code: number;
msg: string;
data: T;
}
class Http {
private instance: AxiosInstance;
// 初始化axios
@ -114,22 +121,22 @@ class Http{
}
// get请求
get(url:string,params?:object): Promise<any>{
get<T = Result> (url: string, params?: object): Promise<T> {
return this.instance.get(url, { params })
}
// post请求
post(url:string,data?:object): Promise<any>{
post<T = Result> (url: string, data?: object): Promise<T> {
return this.instance.post(url, data)
}
// put请求
put(url:string,data?:object): Promise<any>{
put<T = Result> (url: string, data?: object): Promise<T> {
return this.instance.put(url, data)
}
// delete请求
delete(url:string): Promise<any>{
delete<T = Result> (url: string): Promise<T> {
return this.instance.delete(url)
}
}

@ -2,6 +2,9 @@ import { createApp } from 'vue'
import { createPinia } from 'pinia'//创建一个 pinia 实例 (根 store) 并将其传递给应用
// import './style.css' 将默认样式注释掉,避免影响后续开发
// 引入Element-plus的国际化设置
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import router from './router' //将创建好的路由引入到main.ts中
import App from './App.vue'
@ -16,8 +19,11 @@ const pinia = createPinia()
// 挂载路由
// 导入组件
// 导入pinia
// 配置国际化
const app = createApp(App)
app.use(router).use(ElementPlus).use(pinia).mount('#app');
app.use(router).use(ElementPlus, {
locale: zhCn,
}).use(pinia).mount('#app');
// 全局注册图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {

@ -145,6 +145,7 @@ const commit = () => {
if(valid){
// addUserApi
let res = await addUserApi(addModel)
console.log(res)
if(res && res.code == 200){
onConfirm()
}

@ -16,6 +16,38 @@
</el-form-item>
</el-form>
<!-- 用户列表 -->
<el-table :height="tableHeight" :data="tableList" border stripe>
<el-table-column label="姓名" prop="name"></el-table-column>
<el-table-column label="性别" prop="sex">
<!-- 性别默认显示数字需要添加逻辑转换 -->
<template #default="scope">
<el-tag v-if="scope.row.sex == '1'" type="danger" size="default"></el-tag>
<el-tag v-if="scope.row.sex == '0'" size="default"></el-tag>
</template>
</el-table-column>
<el-table-column label="电话" prop="phone"></el-table-column>
<el-table-column label="邮箱" prop="email"></el-table-column>
<!-- 为表格添加编辑删除按钮 -->
<el-table-column label="操作" min-width="100" align="center">
<template #default>
<el-button type="primary" size="default" @click="editBtn" icon="Edit">
编辑
</el-button>
<el-button type="danger" size="default" @click="deleteBtn" icon="Delete">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 为表格添加分页显示 -->
<el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page.sync="listParm.currentPage"
:page-sizes="[10, 20, 40, 80, 100]" :page-size="listParm.pageSize"
layout="total, sizes, prev, pager, next, jumper" :total="listParm.total" background>
</el-pagination>
</el-main>
<!-- 新增编辑按钮弹框 -->
@ -25,13 +57,24 @@
<script setup lang="ts">
import { Search, Close, Plus } from '@element-plus/icons-vue'
import { Search, Close, Plus, Edit, Delete } from '@element-plus/icons-vue'
// useUserTable,
// vite-env.d.ts
import useUserTable from '@/compositions/user/useUserTable';
// onMounted
import { onMounted } from 'vue';
//
const { tableShowBtn, listParm, getList, searchBtn, resetBtn } = useUserTable()
const { tableShowBtn,
listParm,
getList,
searchBtn,
resetBtn,
tableList,
sizeChange,
currentChange,
tableHeight } = useUserTable()
// useUser,
import useUser from '@/compositions/user/useUser';
@ -40,6 +83,11 @@ const { showBtn, addBtn, deleteBtn, editBtn } = useUser()
// AddUser,
import AddUser from './AddUser.vue';
//
onMounted(() => {
getList()
})
</script>
<style scoped></style>

@ -65,6 +65,7 @@ declare module '@/api/user/userModel' {
pageSize: number;
name: string;
phone: string;
total: number;
}
export type UserModel = {

Loading…
Cancel
Save