Commit 617a7828 by 姜鹏凯

aliyun oss

1 parent d6d9c9c5
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
}, },
"dependencies": { "dependencies": {
"@riophae/vue-treeselect": "0.4.0", "@riophae/vue-treeselect": "0.4.0",
"ali-oss": "^6.17.1",
"awesome-qr": "^2.0.1-rc.0", "awesome-qr": "^2.0.1-rc.0",
"axios": "^0.26.0", "axios": "^0.26.0",
"benz-amr-recorder": "^1.1.3", "benz-amr-recorder": "^1.1.3",
......
<script> <script>
import { getCosConfig, getVideoPic } from '@/api/common' import { getCosConfig, getVideoPic } from '@/api/common'
import { dateFormat, uuid } from '@/utils/index' import { dateFormat, uuid } from '@/utils/index'
// import Video from 'video.js' // import Video from 'video.js'
import Cos from 'cos-js-sdk-v5' import Cos from 'cos-js-sdk-v5'
import BenzAMRRecorder from 'benz-amr-recorder' import BenzAMRRecorder from 'benz-amr-recorder'
import MP4Box from 'mp4box' import MP4Box from 'mp4box'
export default { import OSS from 'ali-oss'
export default {
components: {}, components: {},
props: { props: {
// 单文件上传时使用 // 单文件上传时使用
fileUrl: { fileUrl: {
type: String, type: String,
default: '', default: ''
}, },
fileName: { fileName: {
type: String, type: String,
default: '', default: ''
}, },
// 多文件上传时使用,例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}] // 多文件上传时使用,例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]
fileList: { fileList: {
type: Array, type: Array,
default: () => [], default: () => []
}, },
// 0 图片(image)、1 语音(voice)、2 视频(video),3 普通文件(file) // 0 图片(image)、1 语音(voice)、2 视频(video),3 普通文件(file)
...@@ -32,38 +34,38 @@ export default { ...@@ -32,38 +34,38 @@ export default {
validator: function (value) { validator: function (value) {
// 这个值必须匹配下列字符串中的一个 // 这个值必须匹配下列字符串中的一个
return ['0', '1', '2', '3'].includes(value) return ['0', '1', '2', '3'].includes(value)
}, }
}, },
// 上传文件大小不能超过 maxSize MB, 各类型有默认限制 参见: maxSizeDefault // 上传文件大小不能超过 maxSize MB, 各类型有默认限制 参见: maxSizeDefault
maxSize: { maxSize: {
type: Number, type: Number,
default: undefined, default: undefined
}, },
// 图片的宽高像素限制 [width(number), height(number)],默认null不限制 // 图片的宽高像素限制 [width(number), height(number)],默认null不限制
maxImgPx: { maxImgPx: {
type: Array, type: Array,
default: null, // () => [100, 100] default: null // () => [100, 100]
}, },
// 允许上传的文件格式后缀名 eg:["jpg", "png"],['*']为不限制,各类型有默认限制 参见: formatDefault // 允许上传的文件格式后缀名 eg:["jpg", "png"],['*']为不限制,各类型有默认限制 参见: formatDefault
format: { format: {
type: Array, type: Array,
default: undefined, default: undefined
}, },
// 选择上传文件类型, 用于过滤系统选择文件类型, 默认根据类型自动匹配:见 acceptAuto, // 选择上传文件类型, 用于过滤系统选择文件类型, 默认根据类型自动匹配:见 acceptAuto,
accept: { accept: {
type: String, type: String,
default: '', default: ''
}, },
// 是否能上传多个 // 是否能上传多个
multiple: { multiple: {
type: Boolean, type: Boolean,
default: false, default: false
}, },
// multiple为true时有效,最多上传几个,默认不限制,最小为1 // multiple为true时有效,最多上传几个,默认不限制,最小为1
limit: { limit: {
type: Number, type: Number,
default: undefined, default: undefined
}, }
// onProgress: { // onProgress: {
// type: Function, // type: Function,
// default: null, // default: null,
...@@ -97,41 +99,56 @@ export default { ...@@ -97,41 +99,56 @@ export default {
// cos配置信息 // cos配置信息
cosConfig: { bucketName: '', cosImgUrlPrefix: '', region: '' }, cosConfig: { bucketName: '', cosImgUrlPrefix: '', region: '' },
cosInstance: undefined, // cos实例 cosInstance: undefined, // cos实例
ossObj: undefined
} }
}, },
watch: { watch: {
fileUrl: { fileUrl: {
handler(value) { handler(value) {
this.fileUrlWatch = value this.fileUrlWatch = value
}, }
}, },
fileName: { fileName: {
handler(value) { handler(value) {
this.fileNameWatch = value this.fileNameWatch = value
}, }
}, },
fileList: { fileList: {
handler(value) { handler(value) {
this.fileListWatch = value this.fileListWatch = value
}, }
}, },
loading(val) { loading(val) {
this.$emit('loadingChange', val) this.$emit('loadingChange', val)
}, }
}, },
computed: { computed: {
// 识别选择文件类型 // 识别选择文件类型
acceptAuto() { acceptAuto() {
return ['image/*', 'amr/*', 'video/*'][this.type] return ['image/*', 'amr/*', 'video/*'][this.type]
}, }
}, },
created() { created() {
getCosConfig().then((res) => { getCosConfig().then((res) => {
this.cosConfig = res this.cosConfig = res
console.log(this.cosConfig)
if (this.cosConfig.fileObject == 'tencentOss') {
this.cosInstance = new Cos({ this.cosInstance = new Cos({
SecretId: res.secretId, SecretId: res.secretId,
SecretKey: res.secretKey, SecretKey: res.secretKey
}) })
} else {
let region = this.cosConfig.region.split('//')[1]
this.ossObj = new OSS({
// yourRegion填写Bucket所在地域。Region填写为oss-cn-hangzhou。
region: region.split('.')[0],
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
accessKeyId: this.cosConfig.secretId,
accessKeySecret: this.cosConfig.secretKey,
// 填写Bucket名称。
bucket: this.cosConfig.bucketName
})
}
}) })
}, },
mounted() { mounted() {
...@@ -169,6 +186,73 @@ export default { ...@@ -169,6 +186,73 @@ export default {
}, },
methods: { methods: {
upload() { upload() {
if (this.cosConfig.fileObject == 'tencentOss') {
this.tencentFn()
} else {
this.aliOss()
}
},
aliOss() {
this.loading = true
let file = undefined
if (!this.multiple || this.limit == 1) {
file = this.file
} else {
// 多选上传是多次调用单传的
file = this.file.shift()
}
let date = new Date()
let format = file.name.match(/\.(\w+)$/g)
format = format && format[0]
// 实例可能未初始化完成
if (!this.ossObj) {
this.$message.error('存储空间正忙,请稍后再试')
return
}
let name = `/${dateFormat(date, 'yyyy-MM-dd')}/t${date.getTime()}-${uuid()}${format}`
this.ossObj
.multipartUpload(name, file, {
progress: (progressData) => {
console.log(progressData)
this.percentage = progressData.percent * 100
this.speed = (progressData.speed / 1024 / 1024).toFixed(2)
}
})
.then((err1, data) => {
this.percentage = this.speed = 0
if (err1) {
this.loading = false
this.$message.error('上传失败,请稍后再试')
} else {
let location = 'https://' + data.Location
this.$emit('upSuccess', location)
this.type == 2
? //获取视频第一帧画面
getVideoPic({ url: location }).then((res) => {
this.loading = false
this.$emit('getPicUrl', res.data.url)
})
: (this.loading = false)
// 使用本地链接提供预览,避免上传后下载的问题
let url = window.URL.createObjectURL(file)
let name = file.name
if (!this.multiple) {
this.fileUrlWatch = url
this.$emit('update:fileUrl', location)
this.$emit('update:fileName', (this.fileNameWatch = name))
} else {
this.fileListWatch = this.fileListWatch.concat({ name, url })
this.$emit('update:fileList', this.fileList.concat({ name, url: location }))
}
}
})
.catch((err) => {
console.log(err)
})
},
tencentFn() {
this.loading = true this.loading = true
let file = undefined let file = undefined
if (!this.multiple || this.limit == 1) { if (!this.multiple || this.limit == 1) {
...@@ -183,7 +267,7 @@ export default { ...@@ -183,7 +267,7 @@ export default {
const params = { const params = {
Bucket: this.cosConfig.bucketName /* 填入您自己的存储桶,必须字段 */, Bucket: this.cosConfig.bucketName /* 填入您自己的存储桶,必须字段 */,
Region: this.cosConfig.region /* 存储桶所在地域,例如ap-beijing,必须字段 */, Region: this.cosConfig.region /* 存储桶所在地域,例如ap-beijing,必须字段 */,
Key: `/${dateFormat(date, 'yyyy-MM-dd')}/t${date.getTime()}-${uuid()}${format}`, Key: `/${dateFormat(date, 'yyyy-MM-dd')}/t${date.getTime()}-${uuid()}${format}`
/* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段。*/ /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段。*/
// 此处使用的格式: /日期/t时间戳-uid-文件后缀名 // 此处使用的格式: /日期/t时间戳-uid-文件后缀名
} }
...@@ -202,17 +286,17 @@ export default { ...@@ -202,17 +286,17 @@ export default {
this.percentage = progressData.percent * 100 this.percentage = progressData.percent * 100
this.speed = (progressData.speed / 1024 / 1024).toFixed(2) this.speed = (progressData.speed / 1024 / 1024).toFixed(2)
// this.onProgress && this.onProgress(this.percentage, this.speed) // this.onProgress && this.onProgress(this.percentage, this.speed)
}, }
}, },
(err1, data) => { (err1, data) => {
this.percentage = this.speed = 0 this.percentage = this.speed = 0
console.log(data)
if (err1) { if (err1) {
this.loading = false this.loading = false
this.$message.error('上传失败,请稍后再试') this.$message.error('上传失败,请稍后再试')
} else { } else {
let location = 'https://' + data.Location let location = 'https://' + data.Location
this.$emit('upSuccess',location) this.$emit('upSuccess', location)
this.type == 2 this.type == 2
? //获取视频第一帧画面 ? //获取视频第一帧画面
getVideoPic({ url: location }).then((res) => { getVideoPic({ url: location }).then((res) => {
...@@ -234,14 +318,14 @@ export default { ...@@ -234,14 +318,14 @@ export default {
this.$emit('update:fileList', this.fileList.concat({ name, url: location })) this.$emit('update:fileList', this.fileList.concat({ name, url: location }))
} }
} }
}, }
) )
}, },
remove(i) { remove(i) {
this.$confirm('确定要删除吗?', '提示', { this.$confirm('确定要删除吗?', '提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
cancelButtonText: '取消', cancelButtonText: '取消',
type: 'warning', type: 'warning'
}).then(() => { }).then(() => {
let clone = JSON.parse(JSON.stringify(this.fileList)) let clone = JSON.parse(JSON.stringify(this.fileList))
this.fileListWatch.splice(i, 1) this.fileListWatch.splice(i, 1)
...@@ -269,7 +353,7 @@ export default { ...@@ -269,7 +353,7 @@ export default {
0: { tip: 'png/jpg', value: ['png', 'jpg', 'jpeg'] }, 0: { tip: 'png/jpg', value: ['png', 'jpg', 'jpeg'] },
1: { value: ['amr'] }, 1: { value: ['amr'] },
2: { value: ['mp4'] }, 2: { value: ['mp4'] },
3: { tip: 'word/pdf/ppt', value: ['doc', 'docx', 'pdf', 'ppt', 'pptx', 'pps', 'pptsx'] }, 3: { tip: 'word/pdf/ppt', value: ['doc', 'docx', 'pdf', 'ppt', 'pptx', 'pps', 'pptsx'] }
} }
format = formatDefault[this.type].value format = formatDefault[this.type].value
tip = formatDefault[this.type].tip tip = formatDefault[this.type].tip
...@@ -409,9 +493,9 @@ export default { ...@@ -409,9 +493,9 @@ export default {
}) })
}) })
// this.$refs.image.$refs.closeViewer(); // this.$refs.image.$refs.closeViewer();
}, }
}, }
} }
</script> </script>
<template> <template>
...@@ -428,7 +512,8 @@ export default { ...@@ -428,7 +512,8 @@ export default {
:src="item.url" :src="item.url"
fit="contain" fit="contain"
:preview-src-list="fileListWatch.map((e) => e.url)" :preview-src-list="fileListWatch.map((e) => e.url)"
alt="" /> alt=""
/>
<div class="action-mask"> <div class="action-mask">
<i class="el-icon-search mr5" @click="showView(index)"></i> <i class="el-icon-search mr5" @click="showView(index)"></i>
<!-- <span v-if="action.includes('download')" @click="download(item)"> <!-- <span v-if="action.includes('download')" @click="download(item)">
...@@ -456,7 +541,8 @@ export default { ...@@ -456,7 +541,8 @@ export default {
:limit="limit" :limit="limit"
:on-error="onError" :on-error="onError"
:on-exceed="handleExceed" :on-exceed="handleExceed"
:before-upload="handleBeforeUpload"> :before-upload="handleBeforeUpload"
>
<!-- <!--
element-loading-text="正在上传..." element-loading-text="正在上传..."
:on-success="onSuccess" :on-success="onSuccess"
...@@ -474,7 +560,7 @@ export default { ...@@ -474,7 +560,7 @@ export default {
<circle cx="50" cy="50" r="20" fill="none" class="path"></circle> <circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
</svg> </svg>
</div> </div>
<div class="cc" style="margin-top: 35px"> <div class="cc" style="margin-top: 35px;">
{{ speed + 'M/s' }} {{ speed + 'M/s' }}
</div> </div>
</div> </div>
...@@ -488,7 +574,8 @@ export default { ...@@ -488,7 +574,8 @@ export default {
:src="fileUrlWatch" :src="fileUrlWatch"
class="upload-img upload-img-single uploader-size" class="upload-img upload-img-single uploader-size"
:preview-src-list="[fileUrlWatch]" :preview-src-list="[fileUrlWatch]"
fit="contain" /> fit="contain"
/>
<div class="action-mask" @click.self.stop> <div class="action-mask" @click.self.stop>
<i class="el-icon-search" @click.prevent.stop="showView()"></i> <i class="el-icon-search" @click.prevent.stop="showView()"></i>
...@@ -512,10 +599,11 @@ export default { ...@@ -512,10 +599,11 @@ export default {
playsinline="true" playsinline="true"
:autoplay="false" :autoplay="false"
:key="fileUrlWatch" :key="fileUrlWatch"
preload="auto"> preload="auto"
>
<source :src="fileUrlWatch" type="video/mp4" /> <source :src="fileUrlWatch" type="video/mp4" />
</video> </video>
<div class="action-mask" style="height: 30%" @click.self.stop> <div class="action-mask" style="height: 30%;" @click.self.stop>
<i class="el-icon-edit"></i> <i class="el-icon-edit"></i>
</div> </div>
</template> </template>
...@@ -524,7 +612,7 @@ export default { ...@@ -524,7 +612,7 @@ export default {
<i class="el-icon-edit ml10"></i> <i class="el-icon-edit ml10"></i>
<!-- a链接用本地视频打不开,视频地址使用远程地址 --> <!-- a链接用本地视频打不开,视频地址使用远程地址 -->
<a @click.stop :href="/\.mp4$/.test(fileNameWatch) ? fileUrl : fileUrlWatch" target="_blank"> <a @click.stop :href="/\.mp4$/.test(fileNameWatch) ? fileUrl : fileUrlWatch" target="_blank">
<i class="el-icon-view ml10" style="vertical-align: middle"></i> <i class="el-icon-view ml10" style="vertical-align: middle;"></i>
</a> </a>
</template> </template>
</div> </div>
...@@ -540,7 +628,7 @@ export default { ...@@ -540,7 +628,7 @@ export default {
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
::v-deep.uploader { ::v-deep.uploader {
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
.el-upload { .el-upload {
...@@ -550,17 +638,17 @@ export default { ...@@ -550,17 +638,17 @@ export default {
position: relative; position: relative;
overflow: hidden; overflow: hidden;
} }
} }
// 默认组件大小,如需修改,请通过外部重写该类样式 // 默认组件大小,如需修改,请通过外部重写该类样式
.uploader-size { .uploader-size {
width: 178px; width: 178px;
height: 178px; height: 178px;
} }
.upload-action { .upload-action {
position: relative; position: relative;
text-align: center; text-align: center;
} }
.uploader-icon { .uploader-icon {
display: flex; display: flex;
font-size: 28px; font-size: 28px;
align-items: center; align-items: center;
...@@ -573,17 +661,17 @@ export default { ...@@ -573,17 +661,17 @@ export default {
border-color: var(--color); border-color: var(--color);
color: var(--color); color: var(--color);
} }
} }
.progress { .progress {
overflow: hidden; overflow: hidden;
} }
.upload-img-single { .upload-img-single {
border: 1px dashed #eee; border: 1px dashed #eee;
} }
.upload-img { .upload-img {
display: block; display: block;
} }
.upload-video { .upload-video {
display: block; display: block;
width: 300px; width: 300px;
height: 150px; height: 150px;
...@@ -597,20 +685,20 @@ export default { ...@@ -597,20 +685,20 @@ export default {
font-weight: normal; font-weight: normal;
font-style: normal; font-style: normal;
word-break: initial; word-break: initial;
} }
.tip { .tip {
margin-top: 10px; margin-top: 10px;
color: #aaa; color: #aaa;
font-size: 12px; font-size: 12px;
} }
.img-item { .img-item {
position: relative; position: relative;
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
margin: 0 10px 10px 0; margin: 0 10px 10px 0;
transition: all 0.3s; transition: all 0.3s;
} }
.action-mask { .action-mask {
position: absolute; position: absolute;
display: flex; display: flex;
justify-content: space-around; justify-content: space-around;
...@@ -628,12 +716,12 @@ export default { ...@@ -628,12 +716,12 @@ export default {
transition: opacity 0.3s; transition: opacity 0.3s;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 1; z-index: 1;
} }
.upload-item { .upload-item {
&:hover { &:hover {
.action-mask { .action-mask {
opacity: 1; opacity: 1;
} }
} }
} }
</style> </style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!