|
@@ -4,22 +4,19 @@
|
|
|
<el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
|
|
<el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
|
|
|
<el-card class="box-card">
|
|
<el-card class="box-card">
|
|
|
<div slot="header" class="clearfix">
|
|
<div slot="header" class="clearfix">
|
|
|
- <span>上传 IM 图片</span>
|
|
|
|
|
|
|
+ <span>上传文件</span>
|
|
|
</div>
|
|
</div>
|
|
|
<div class="text item">
|
|
<div class="text item">
|
|
|
- <el-tooltip v-if="imgOssUrl !== null" class="item" effect="dark" content="点击上传图片" placement="top-end">
|
|
|
|
|
|
|
+ <el-tooltip class="item" effect="dark" content="点击上传文件" placement="top-end">
|
|
|
<el-upload
|
|
<el-upload
|
|
|
- class="avatar-uploader"
|
|
|
|
|
- :action="imgOssUrl"
|
|
|
|
|
- :headers="imgHeaders"
|
|
|
|
|
- :data="imgData"
|
|
|
|
|
- :with-credentials="true"
|
|
|
|
|
|
|
+ class=""
|
|
|
|
|
+ action=""
|
|
|
:show-file-list="false"
|
|
:show-file-list="false"
|
|
|
- :before-upload="beforeAvatarUpload"
|
|
|
|
|
|
|
+ :http-request="fnUploadRequest"
|
|
|
:on-success="handleAvatarSuccess"
|
|
:on-success="handleAvatarSuccess"
|
|
|
- :on-change="handleOnChange"
|
|
|
|
|
|
|
+ :before-upload="beforeAvatarUpload"
|
|
|
>
|
|
>
|
|
|
- <img v-if="coverUrl" :src="coverUrl" class="avatar">
|
|
|
|
|
|
|
+ <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">
|
|
|
<i v-else class="el-icon-plus avatar-uploader-icon" />
|
|
<i v-else class="el-icon-plus avatar-uploader-icon" />
|
|
|
</el-upload>
|
|
</el-upload>
|
|
|
</el-tooltip>
|
|
</el-tooltip>
|
|
@@ -34,108 +31,94 @@
|
|
|
<span>OSS 地址</span>
|
|
<span>OSS 地址</span>
|
|
|
</div>
|
|
</div>
|
|
|
<div class="text item">
|
|
<div class="text item">
|
|
|
- <el-form ref="form" :model="formData" label-width="80px">
|
|
|
|
|
- <el-form-item label="视频地址">
|
|
|
|
|
- <el-input v-model="formData.uploadId" style="padding-right: 1px" readonly />
|
|
|
|
|
|
|
+ <span>{{ imageUrl }}</span>
|
|
|
|
|
+ <!-- <el-form ref="form" :model="imageUrl" label-width="80px">
|
|
|
|
|
+ <el-form-item label="文件地址">
|
|
|
|
|
+ <el-input v-model="imageUrl" style="padding-right: 1px" readonly />
|
|
|
</el-form-item>
|
|
</el-form-item>
|
|
|
- </el-form>
|
|
|
|
|
|
|
+ </el-form>-->
|
|
|
</div>
|
|
</div>
|
|
|
</el-card>
|
|
</el-card>
|
|
|
</el-row>
|
|
</el-row>
|
|
|
- <el-row v-if="imageUrl !== null" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
|
|
|
|
|
- <el-image :src="imageUrl" />
|
|
|
|
|
- </el-row>
|
|
|
|
|
</el-col>
|
|
</el-col>
|
|
|
</el-row>
|
|
</el-row>
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
<script>
|
|
|
-import { getServerInfo } from '@/api/content'
|
|
|
|
|
-import { putImageFile } from '@/api/im'
|
|
|
|
|
|
|
+import OSS from 'ali-oss'
|
|
|
|
|
+import { getSignedUrl, getStsToken } from '@/api/file'
|
|
|
|
|
|
|
|
export default {
|
|
export default {
|
|
|
name: 'PublishFile',
|
|
name: 'PublishFile',
|
|
|
data() {
|
|
data() {
|
|
|
return {
|
|
return {
|
|
|
- imgOssUrl: null,
|
|
|
|
|
- imgHeaders: {
|
|
|
|
|
- Authorization: ''
|
|
|
|
|
- },
|
|
|
|
|
- imgData: {
|
|
|
|
|
- channelId: process.env.VUE_APP_UPLOAD_IMIMG_CHANNEL
|
|
|
|
|
- },
|
|
|
|
|
- coverUrl: null,
|
|
|
|
|
- // ****************************************************************************************************************
|
|
|
|
|
- formData: {
|
|
|
|
|
- receiverId: 10011,
|
|
|
|
|
- channelId: null,
|
|
|
|
|
- uploadId: null
|
|
|
|
|
- },
|
|
|
|
|
- imageUrl: null
|
|
|
|
|
|
|
+ imageUrl: '',
|
|
|
|
|
+ ossUrl: ''
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
created() {
|
|
created() {
|
|
|
- getServerInfo(this.imgData.channelId).then(res => {
|
|
|
|
|
- if (res.code === 0) {
|
|
|
|
|
- const resData = res.data
|
|
|
|
|
- this.imgOssUrl = resData.ossUrl
|
|
|
|
|
- this.imgHeaders.Authorization = 'Bearer ' + resData.token
|
|
|
|
|
- } else {
|
|
|
|
|
- this.$notify({
|
|
|
|
|
- title: '提示',
|
|
|
|
|
- message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
|
|
|
|
|
- type: 'error',
|
|
|
|
|
- duration: 3000
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- }).catch(error => {
|
|
|
|
|
- this.$notify({
|
|
|
|
|
- title: '提示',
|
|
|
|
|
- message: error.message,
|
|
|
|
|
- type: 'warning',
|
|
|
|
|
- duration: 3000
|
|
|
|
|
- })
|
|
|
|
|
- })
|
|
|
|
|
- },
|
|
|
|
|
- mounted() {
|
|
|
|
|
},
|
|
},
|
|
|
methods: {
|
|
methods: {
|
|
|
- // ****************************************************************************************************************
|
|
|
|
|
|
|
+ // 图片上传成功回调
|
|
|
|
|
+ handleAvatarSuccess(res) {
|
|
|
|
|
+ if (res) {
|
|
|
|
|
+ const objectName = res.url.replace(this.ossUrl, '')
|
|
|
|
|
+ const payload = {}
|
|
|
|
|
+ payload.objectName = objectName
|
|
|
|
|
+ getSignedUrl(payload).then(resp => {
|
|
|
|
|
+ if (resp.code === 0) {
|
|
|
|
|
+ this.imageUrl = resp.data
|
|
|
|
|
+ // this.imageUrl = res.url
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
beforeAvatarUpload(file) {
|
|
beforeAvatarUpload(file) {
|
|
|
const isJPG = file.type === 'image/jpeg'
|
|
const isJPG = file.type === 'image/jpeg'
|
|
|
- const isLt2M = file.size / 1024 / 1024 < 10
|
|
|
|
|
|
|
+ const isLt2M = file.size / 1024 / 1024 < 2
|
|
|
|
|
+
|
|
|
if (!isJPG) {
|
|
if (!isJPG) {
|
|
|
- this.$message.error('封面图片只能是 JPG 格式!')
|
|
|
|
|
|
|
+ this.$message.error('上传头像图片只能是 JPG 格式!')
|
|
|
}
|
|
}
|
|
|
if (!isLt2M) {
|
|
if (!isLt2M) {
|
|
|
- this.$message.error('封面图片大小不能超过 10MB!')
|
|
|
|
|
|
|
+ this.$message.error('上传头像图片大小不能超过 2MB!')
|
|
|
}
|
|
}
|
|
|
return isJPG && isLt2M
|
|
return isJPG && isLt2M
|
|
|
},
|
|
},
|
|
|
- handleAvatarSuccess(res, file) {
|
|
|
|
|
- if (res.code === 0) {
|
|
|
|
|
- const resData = res.data
|
|
|
|
|
- this.coverUrl = URL.createObjectURL(file.raw)
|
|
|
|
|
-
|
|
|
|
|
- this.formData.channelId = this.imgData.channelId
|
|
|
|
|
- this.formData.uploadId = resData.uploadId
|
|
|
|
|
- putImageFile(this.formData).then(resp => {
|
|
|
|
|
|
|
+ async fnUploadRequest(options) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const file = options.file // 拿到 file
|
|
|
|
|
+ const index = file.name.lastIndexOf('.')
|
|
|
|
|
+ const suffix = file.name.substr(index)
|
|
|
|
|
+ getStsToken().then(resp => {
|
|
|
if (resp.code === 0) {
|
|
if (resp.code === 0) {
|
|
|
- this.imageUrl = resp.data
|
|
|
|
|
|
|
+ const credentials = resp.data
|
|
|
|
|
+ this.ossUrl = credentials.ossUrl
|
|
|
|
|
+ const client = new OSS({
|
|
|
|
|
+ region: credentials.region,
|
|
|
|
|
+ bucket: credentials.bucket,
|
|
|
|
|
+ accessKeyId: credentials.accessKeyId,
|
|
|
|
|
+ accessKeySecret: credentials.accessKeySecret,
|
|
|
|
|
+ stsToken: credentials.securityToken
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const objectId = credentials.objectId
|
|
|
|
|
+ const objectName = 'image/i/' + objectId + suffix
|
|
|
|
|
+ client.put(objectName, file).then(resp1 => {
|
|
|
|
|
+ if (resp1.res.statusCode === 200) {
|
|
|
|
|
+ console.log(resp1)
|
|
|
|
|
+ options.onSuccess(resp1)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.log(resp1)
|
|
|
|
|
+ options.onError('上传失败')
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
- } else {
|
|
|
|
|
- this.$notify({
|
|
|
|
|
- title: '提示',
|
|
|
|
|
- message: '视频封面上传失败,请重试!' + res.msg,
|
|
|
|
|
- type: 'warning',
|
|
|
|
|
- duration: 3000
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ options.onError('上传失败')
|
|
|
}
|
|
}
|
|
|
- },
|
|
|
|
|
- handleOnChange(file, fileList) {
|
|
|
|
|
}
|
|
}
|
|
|
- // ****************************************************************************************************************
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
</script>
|
|
</script>
|