当前位置: 首页 > news >正文

Vue接口平台学习七——接口调试页面请求体

一、实现效果图及简单梳理

在这里插入图片描述
请求体部分的左边,展示参数,分text和file类型。
右边部分一个el-upload的上传文件按钮,一个table列表展示,一个显示框,用于预览选择的文件,点击可大图展示。

二、页面内容实现

0. 整体布局

这里分左右两边,左边用于添加和显示参数,右边用于上传文件和预览文件

<el-row :gutter="5"><el-col :span="15"></el-col><el-col :span="9"></el-col></el-row>

1. 左边的参数值部分

单个参数

左边包含内容,参数名(输入框),参数类型(下拉选择),参数值(选择text时为输入框,选择file时,为下拉选择),一个删除按钮,一个添加参数按钮

<el-col :span="15"><el-row  :gutter="5" style="margin-top: 5px;"><el-col :span="5"><el-input size="small"  placeholder="参数名" clearable/></el-col><el-col :span="4"><el-select v-model="paramsType" placeholder="参数类型" size="small" style="width: 100%;"><el-option label="Text" value="text"/><el-option label="File" value="file"/></el-select></el-col><el-col :span="11"><!-- 文字输入框 --><el-input v-if="paramsType === 'text'"  placeholder="参数值" size="small"clearable/><el-select v-else  size="small" placeholder="选择已有文件" style="width: 100%;"><el-option  value="123"/></el-select></el-col><el-col :span="4"><el-button icon="Delete"  size="small" type="danger" plain></el-button></el-col></el-row><el-button style="margin-top: 10px;" icon="Plus" size="small" type="primary" plain></el-button></el-col>

选择Text时
选择File时
单个参数大概就这样,现在需要定义一个变量,用来存储所有参数,然后遍历显示,并且点击添加时,需要向这个列表增加新的值。

多个参数循环遍历

const params = ref([]); // 参数列表 ['name','value']
<el-col :span="15"><el-row v-for="(item, index) in params" :key="index" :gutter="5" style="margin-top: 5px;"><el-col :span="5"><el-input size="small" v-model="item[0]" placeholder="参数名" clearable/></el-col><el-col :span="4"><el-select v-model="paramsType[index]" placeholder="参数类型" size="small" style="width: 100%;"><el-option label="Text" value="text"/><el-option label="File" value="file"/></el-select></el-col><el-col :span="11"><!-- 文字输入框 --><el-input v-if="paramsType[index] === 'text'" v-model="item[1]" placeholder="参数值" size="small"clearable/><el-select v-else v-model="item[1][0]" size="small"  placeholder="选择已有文件" style="width: 100%;"><el-option  value="123"/></el-select></el-col><el-col :span="4"><el-button icon="Delete"  size="small" type="danger" plain></el-button></el-col></el-row><el-button @click="params.push(['',''])"  style="margin-top: 10px;" icon="Plus" size="small" type="primary" plain></el-button></el-col>

在这里插入图片描述
由于后端定义的文件内容,包含名字,文件类型,文件地址三部分。所以如果选择类型是File,则有包含三部分内容。而下拉选择的仅仅只是绑定文件名,也就是列表第一个值。

删除

绑定点击事件

<el-button icon="Delete" @click="params.splice(index, 1)" size="small" type="danger" plain></el-button></el-col>

从数组中删除元素splice。

文件相关

定义接口

根据接口文档,在vue中封装接口
在这里插入图片描述

页面组件挂载完毕,获取一下文件列表,存储在files中,然后将数据中的info字段的第一个,也就是info[0] 绑定给文件下拉选择的选项,作为name

text和file的参数区别

后端返回文件列表的数据格式如下:
在这里插入图片描述
之前定义的params是一个列表,如下

params = [name,value] # 2部分组成,name和value,
# 而这个value,需要区分text和file
# 1.当选择text,value只有一个文本值
params = [name,[text_value]]# 2.当选择file,value则包含3部分
params = [name,[file_name,file_type,file_url]]

所以这里需要区分一下选择Text和选择File

 <el-select @change="selectType($event, index)" v-model="paramsType[index]" placeholder="参数类型" size="small" style="width: 100%;"><el-option label="Text" value="text"/><el-option label="File" value="file"/></el-select>
const selectType = (val, index) => {if (val === 'file') {params.value[index][1] = ['', '', ''];} else {params.value[index][1] = '';}
};
下拉选项绑定File

在下拉选择时,绑定change函数,如果选择file,那参数就有3个部分组成.

 <el-select v-else @change="selectFile($event, index)" v-model="item[1][0]" size="small"placeholder="选择已有文件" style="width: 100%;"><el-option v-for="item in files" :label="item.info[0]" :value="item.info[0]"/></el-select>
const files = ref([]);const getAllFile = async () => {// 获取文件列表const response = await http.pro.getFiles();if (response.status === 200) {files.value = response.data;}
};onMounted(() => {
if (props.modelValue.length > 0) {params.value = props.modelValue;} else {params.value = [['', '']];}getAllFile();
});

在这里插入图片描述

2. 右边的上传及预览部分

<el-col :span="9"><el-card style="padding: 0;"><el-upload class="upload-demo" :action="uploadApi" :on-success="uploadSuccess" :on-error="uploadError" :show-file-list="false" name="file" ><el-button type="primary" plain size="small">上传文件</el-button></el-upload><el-table :data="files" style="width: 100%" size="small" height="200px"><el-table-column label="已有文件"><template #default="scope"><el-tag type="success">{{ scope.row.info[0] }}</el-tag></template></el-table-column><el-table-column label="文件类型"><template #default="scope"><el-tag type="info">{{ scope.row.info[1] }}</el-tag></template></el-table-column></el-table><div>。。。图片和视频展示</div></el-card></el-col>

一个上传按钮,使用el-upload,然后一个表单展示组件,一个iframe用于显示已有的文件。

el-upload

https://element-plus.org/zh-CN/component/upload.html

在这里插入图片描述
添加请求上传的url

cosnt uploadApi = request.defaults.baseURL + `/api/testFile/files/`

绑定成功和失败的事件,给出提醒就行了。

const uploadSuccess = (response) => {ElMessage({type: 'success',message: '文件上传成功!',duration: 2000});getAllFile();
};const uploadError = (error) => {ElMessage({type: 'error',message: error,duration: 2000});
};

此时选择文件就能直接调用接口,上传了。
在这里插入图片描述

el-table

 <el-table :data="files" style="width: 100%" size="small" height="200px"><el-table-column label="已有文件" show-overflow-tooltip><template #default="scope"><el-tag type="success">{{ scope.row.info[0] }}</el-tag></template></el-table-column><el-table-column label="文件类型"><template #default="scope"><el-tag type="info">{{ scope.row.info[1] }}</el-tag></template></el-table-column>
</el-table>

表单部分,与之前下拉选择时一致。内容从files读取,显示名字的地方,超长,则悬浮展示全称。

单个文件数据

{"id": 5,"info": ["procard.jpg","image/jpeg","https://testapi-13xxxx-chengdu.myqcloud.com/procard.jpg "],"file": "procard.jpg"
}

之前也看过,文件类型数据info定义时含3个字段,名字,图片,和存储地址(使用了腾讯云的对象存储)。
所以显示文件名是scope.row.info[0] ,文件类型是scope.row.info[1]

图片与视频预览展示

点击文件名,预览一下图片/视频。给文件名增加点击事件,把info传过去

<el-tag type="success" @click="viewfile( scope.row.info)">{{ scope.row.info[0] }}</el-tag>

点击后,判断点击的是图片还是视频,用不同的标签进行渲染展示

const fileType = ref('image')
const mediaIframe = ref(null);const viewFile = (info) => {mediaIframe.value = info[2];const type = info[1].toLowerCase().split('/')[0];console.log(type)if (type === 'image') {fileType.value = 'image'} else {fileType.value = 'video'}
};
<div v-if="mediaIframe"><img v-if="fileType === 'image'" @click="showFullImg" :src="mediaIframe" style="width: 100%; height: 200px;"alt="预览"><videov-else:src="mediaIframe"@click="showFullImg"controlspreload="metadata"controlsList="nodownload"style="width: 100%; height: 200px;">您的浏览器不支持视频播放</video></div><!-- 添加Dialog组件,用于大屏展示 --><el-dialog v-if="fileType==='image'" v-model="dialogVisible" title="图片预览" width="80%"><img :src="mediaIframe" style="width: 100%; height: auto;" alt="完整图片"></el-dialog><el-dialog v-else v-model="dialogVisible" title="视频预览"  :width="dialogWidth"  ref="videoDialog"><video:src="mediaIframe"style="width: 100%; height:500px;"controlspreload="auto"controlsList="nodownload"@loadedmetadata="setDialogWidth"autoplay>您的浏览器不支持视频播放</video></el-dialog>
const dialogVisible = ref(false);
const dialogWidth = ref('80%'); // 默认宽度
const videoDialog = ref(null); // 对话框的 ref//点击全屏展示
const showFullImg = () => {dialogVisible.value = true; // 显示Dialog
};// 根据视频本身大小,来设置弹窗宽高。代码用cursor生成的,能用就行
const setDialogWidth = (event) => {const video = event.target;const videoWidth = video.videoWidth;const videoHeight = video.videoHeight;const maxWidth = window.innerWidth * 0.8; // 最大宽度为视口宽度的80%const maxHeight = window.innerHeight * 0.8; // 最大高度为视口高度的80%let dialogWidthValue = (videoWidth / videoHeight) * maxHeight;if (dialogWidthValue > maxWidth) {dialogWidthValue = maxWidth;}dialogWidth.value = `${dialogWidthValue}px`;
};
// 添加视频相关样式 (AI生成的,直接复制的)
video {object-fit: contain;background: #000; /* 添加黑色背景 */cursor: pointer;&::-webkit-media-controls-download-button {display: none; /* 隐藏 Chrome 下载按钮 */}&::-webkit-media-controls-enclosure {overflow: hidden;}
}// 优化弹窗中的视频显示
.el-dialog {.el-dialog__body {padding: 10px;background: #000;video {max-height: 70vh;margin: 0 auto;display: block;}}
}

在这里插入图片描述
点击全屏展示:
在这里插入图片描述
到此,整个请求体部分就完成了
在这里插入图片描述

三、总结

主要还是elment组件的使用,用到了el-upload,el-table,el-row,el-select等等。
video展示那块还得是AI,真是一边写,一边问。才得到结果。

相关文章:

  • PyTorch实现权重衰退:从零实现与简洁实现
  • 蓝桥杯嵌入式开发板结构分析及功能学习笔记
  • 5.跳表(skiplist)
  • C++ | STL之list详解:双向链表的灵活操作与高效实践
  • 【项目管理】第17章 项目干系人管理-- 知识点整理
  • GEO供应商盈达科技发布:AI信源占位白皮书​
  • IDEA类图标识
  • AI驱动SEO关键词实战策略
  • 基于RV1126开发板的rknn-toolkit-lite使用方法
  • 【Docker-13】Docker Container容器
  • Google提示工程
  • Active Directory域服务管理与高级应用技术白皮书
  • Linux 深入浅出信号量:从线程到进程的同步与互斥实战指南
  • Leetcode 3514. Number of Unique XOR Triplets II
  • python爬虫 线程,进程,协程
  • Oracle数据库数据编程SQL<01. 课外关注:数据库查重方法全面详解>
  • Linux指令和权限(10-3)
  • 聚铭网络亮相2025超云产品技术大会,联合发布“铭智安全运营大模型一体机及解决方案”
  • Rust 之五 所有权、.. 和 _ 语法、引用和切片、Vec<T>、HashMap<K, V>
  • MIT6.S081 - Lab8 Locks(锁优化 | 并发安全)
  • 外贸50城,谁在“扛大旗”?
  • 杭州萧山区两宗地块收金约44.73亿元,最高溢价率74.4%
  • 新增1839个!2024年度本科专业备案和审批结果,公布
  • 西湖大学本科新增临床医学专业,今年本科招生专业增至8个
  • 今年底,全国新拍电视剧、纪录片将基本实现超高清化
  • 中汽协发布规范驾驶辅助宣传与应用倡议书