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

h5相机功能

h5相机功能

利用vant + input file

<template><div class="mb10"><divv-for="(item, index) in info.imgList":key="index"class="imgItem f32 mr20"@click="preview(item, index)"><img :src="doFileUrl(item)" alt="" srcset="" /><van-iconv-if="!disabled"class="close f34 txt-f6"name="close"@click.stop="deleteImg(item, index)"/></div><divv-show="info.imgList.length < maxCount && !disabled"class="btnUpload f32 mr10"@click="clickUpload"><van-icon name="plus" /><inputref="camera"class="cameraInput"type="file"name="file"accept="image/*"capture="camera"@change="uploadImg"/></div></div><!-- 底部弹出 --><van-action-sheetv-model:show="showChoose":actions="info.actions"cancel-text="取消"close-on-click-action@cancel="onCancel"@select="selectAction"/>
</template><script lang="ts" setup>
import { ref, getCurrentInstance, reactive, onMounted, defineExpose } from 'vue'
import { Action, InfoInterface } from './config'
import { fileUpload } from '@/utils/fileHttp'const emit = defineEmits(['chooseFIle'])
/*** @description: 传参* @return {*}* @Date Changed:*/
interface Props {// 文件listfileList: string | null | any[]// 上传数量maxCount?: number//尺寸  这个暂时没用maxSize?: number// 缩略图大小 px 和 remdisabled?: boolean// 图片类型accept?: string[]type?: number[]// 是否全部允许allowAllAccept?: booleanisRequire?: boolean//
}
const props = withDefaults(defineProps<Props>(), {fileList: () => [],maxCount: 1,maxSize: 5 * 1024 * 1024,disabled: false,accept: () => ['image'],type: () => [0],allowAllAccept: false,isRequire: false
})/*** @description: 通用参数* @return {*}* @Date Changed:*/
const { proxy }: any = getCurrentInstance()
const router = proxy.commond.router()
const route = proxy.commond.route()
const { Toast } = proxy.commondconst info = reactive<InfoInterface>({imgList: [],actions: []// aaaaaa: []
})
let toastLoading: any = null
const token = proxy.commond.getToken()/*** @description: 生命周期* @return {*}* @Date Changed:*/
onMounted(() => {init()
})
const init = () => {// 拍照模式const arr: Action[] = []props.type.forEach((item: number) => {arr.push(list[item])})info.actions = arr
}//----------------------------------------------------------- 底部弹出选择拍照模式
const showChoose = ref(false)
const list: Action[] = [{name: '拍照',type: 'ShowView',value: 'CameraView'},{name: '相册',type: 'ShowView',value: 'PhotoView'},{name: '文件上传',type: 'uploadFile',value: 'fileView'}
]
const clickUpload = () => {showChoose.value = true
}
const onCancel = () => {showChoose.value = false
}
const selectAction = async (action: Action, index: number) => {if (action.value === 'CameraView') {// 拍照console.log(proxy.$refs.camera)proxy.$refs.camera.click()showChoose.value = false}// if (res.length > 0) {// 	const arr = proxy.commond.deepClone(info.imgList)// 	res.map((ele) => {// 		arr.push(`OSS_FILE_${ele.resourcesId}/${ele.fullFileName}`)// 	})// 	emit('success', arr)// 	showChoose.value = false// }
}/*** @description: 上传文件* @return {*}*/const uploadImg = (e: any) => {// 这里可以做一些限制 比如 文件大小 文件类型等// ----------const file = e.target.files[0]// 原生请求toastLoading = proxy.commond.Toast.loading({duration: 0,forbidClick: true,message: '开始上传...'})fileUpload(file).then((data: any) => {const url = `OSS_FILE_${data.result.resourcesId}/${data.result.fullFileName}`info.imgList.push(url)emit('chooseFIle', info.imgList, file)toastLoading.clear()proxy.$refs.camera.value = ''}).catch((err) => {toastLoading.clear()proxy.commond.Toast(err.message)proxy.$refs.camera.value = ''})
}const doFileUrl = (resourcesId: string): string => {return proxy.commond.doFileUrl(resourcesId)
}/*** @description: 删除照片* @param {*} val* @param {*} index* @return {*}*/
const deleteImg = (val: string, index: number) => {info.imgList.splice(index, 1)emit('chooseFIle', info.imgList, '')
}/*** @description: 图片预览* @param {*} val* @param {*} index* @return {*}*/
const preview = (val: string, index: number) => {const arr: string[] = []info.imgList.forEach((ele) => {arr.push(doFileUrl(ele))})proxy.commond.ImagePreview({images: arr,startPosition: index})
}defineExpose({selectAction,preview
})
</script><style lang="scss" scoped>
.cameraInput {position: absolute;left: 0;right: 0;bottom: 0;top: 0;display: none;
}.btnUpload {position: relative;text-align: center;background-color: #fbfdff;border: 1px dashed #c0ccda;border-radius: 6px;box-sizing: border-box;display: inline-block;width: 148px;height: 148px;cursor: pointer;line-height: 146px;vertical-align: top;
}.imgItem {display: inline-block;position: relative;border: 1px dashed #c0ccda;width: 148px;height: 148px;cursor: pointer;img {margin-top: 4px;width: 140px;height: 140px;}.close {position: absolute;right: -14px;top: -10px;background: #fff;border-radius: 100px;color: $danger;}
}
</style>
/*** @description: 上传文件景天参数* @return {*}* @Date Changed:*/
export interface Action {name: stringtype: stringvalue: string
}/*** @description:图片返回值处理* @return {*}* @Date Changed:*/
export interface ImgFace {resourcesId: stringresourceUrl: stringfullFileName: stringfileName: stringid: string
}/*** @descriptioninfo* @return {*}* @Date Changed:*/
export interface InfoInterface {imgList: string[]actions: Action[]
}export interface fileBox {show: booleanurl: string
}

使用

// 调用相机<GlFace ref="face" :file-list="[]" @chooseFIle="chooseFIle"></GlFace>
const face = ref(null)face.value.selectAction({ value: 'CameraView' })
http://www.lryc.cn/news/369485.html

相关文章:

  • IDEA | 安装通义灵码插件,开启智能编码旅程
  • 技术人员如何克服在使用行列视(RCV)过程中遇到的挑战?
  • 手把手教你安装 Vivado2019.2(附安装包)
  • Sql-labs的第一关
  • 10_1 Linunx Web服务管理
  • 苹果WWDC 2024:十三大亮点公布,一切都有关AI|TodayAI
  • Nginx访问日志
  • Java使用Hutool工具类轻松生成验证码
  • leetcode 40. 组合总和 II
  • AMEYA360代理品牌:ROHM开发出世界超小CMOS运算放大器,适用于智能手机和小型物联网设备等应用
  • 第1章Hello world 4/5:对比Rust/Java/C++创建和运行Hello world全过程:运行第一个程序
  • golang优雅代码【lock实现】
  • Dijkstra算法(迪杰斯特拉算法)
  • 用函数指针求a和b中的大者
  • 鸿蒙轻内核M核源码分析系列六 任务及任务调度(2)任务模块
  • 解决找不到MSVCR120.dll,无法执行代码
  • Linux iptables详解
  • Mac电脑arm64芯片Cocoapods 的 ffi 兼容问题
  • 如何提高逻辑性?(小妙招)
  • 2024050501-重学 Java 设计模式《实战命令模式》
  • 0104__Linux 中 nm 命令简介
  • Linux网络服务
  • Vue18-列表渲染
  • 【三维重建】增量SFM系统
  • PyTorch 维度变换-Tensor基本操作
  • spring 事务失效的几种场景
  • 45岁程序员独白:中年打工人出路在哪里?
  • 深度探讨:为何训练精度不高却在测试中表现优异?
  • 动态内存管理<C语言>
  • 第一百零二节 Java面向对象设计 - Java静态内部类