|
@@ -0,0 +1,114 @@
|
|
|
+<template>
|
|
|
+ <view class="upload-image">
|
|
|
+ <u-upload
|
|
|
+ ref="uUpload"
|
|
|
+ :file-list="fileList"
|
|
|
+ :max-size="20 * 1024 * 1024"
|
|
|
+ :max-count="config.length || 3"
|
|
|
+ :before-upload="beforeUpload"
|
|
|
+ :action="action"
|
|
|
+ :header="header"
|
|
|
+ :form-data="{ type: 'FILE' }"
|
|
|
+ @on-list-change="change"
|
|
|
+ ></u-upload>
|
|
|
+ <view class="tip">最多只能上传 {{ config.length || 3 }}张图片</view>
|
|
|
+ </view>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+ import SparkMD5 from 'spark-md5'
|
|
|
+ import { getAuthorization, DEVICE_ID } from '@/utils/crypto'
|
|
|
+ export default {
|
|
|
+ props: ['config', 'onChange'],
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ fileList: [],
|
|
|
+ action: process.env.VUE_APP_BASE_API + '/api/admin/common/file/upload',
|
|
|
+ md5: '',
|
|
|
+ header: {
|
|
|
+ 'Content-Type': 'multipart/form-data',
|
|
|
+ md5: '92eeda52d99adc1e4640520e20bda65f',
|
|
|
+ deviceId: DEVICE_ID,
|
|
|
+ platform: 'WEB',
|
|
|
+ Authorization: '',
|
|
|
+ time: ''
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ this.fileList = this.config.value || []
|
|
|
+ },
|
|
|
+
|
|
|
+ methods: {
|
|
|
+ // ab2str(buffer, decodeType = 'utf-8') {
|
|
|
+ // // 默认是uft-8格式
|
|
|
+ // let decoder = new TextDecoder(decodeType)
|
|
|
+ // return decoder.decode(buffer)
|
|
|
+ // },
|
|
|
+
|
|
|
+ change(lists) {
|
|
|
+ //非常蛋疼,这个change事件里的回调里提供的参数lists里,并不会立刻包含response属性,会有延迟!于是使用了一个定时器!
|
|
|
+ setTimeout(() => {
|
|
|
+ let responseList = lists.map((item) => {
|
|
|
+ return item.response.data
|
|
|
+ })
|
|
|
+ //该组件兼容动态表单使用和常规业务使用,通过config参数里是否有formName来区分
|
|
|
+ if (this.config.formName) {
|
|
|
+ this.onChange({
|
|
|
+ prop: this.config.formName,
|
|
|
+ value: responseList
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ this.$emit('getLists', responseList)
|
|
|
+ }
|
|
|
+ }, 200)
|
|
|
+ },
|
|
|
+ beforeUpload(index, list) {
|
|
|
+ console.log('beforeUpload:')
|
|
|
+ const _this = this
|
|
|
+ const path = list[index].file.path
|
|
|
+ let user = uni.getStorageSync('user')
|
|
|
+ let timestamp = Date.now()
|
|
|
+ const authorization = getAuthorization(
|
|
|
+ {
|
|
|
+ method: 'post',
|
|
|
+ uri: '/api/admin/common/file/upload',
|
|
|
+ timestamp,
|
|
|
+ sessionId: user.sessionId,
|
|
|
+ token: user.accessToken
|
|
|
+ },
|
|
|
+ 'token'
|
|
|
+ )
|
|
|
+ this.header.Authorization = authorization
|
|
|
+ this.header.time = timestamp
|
|
|
+ this.header = { ...this.header }
|
|
|
+
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ wx.getFileSystemManager().readFile({
|
|
|
+ filePath: path,
|
|
|
+ encoding: 'binary',
|
|
|
+ success: (res) => {
|
|
|
+ let spark = new SparkMD5()
|
|
|
+ spark.appendBinary(res.data)
|
|
|
+ var md5 = spark.end()
|
|
|
+ _this.header.md5 = md5
|
|
|
+ setTimeout(() => {
|
|
|
+ resolve()
|
|
|
+ }, 0)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+ .upload-image {
|
|
|
+ .tip {
|
|
|
+ color: #999;
|
|
|
+ font-size: 24rpx;
|
|
|
+ line-height: 40rpx;
|
|
|
+ }
|
|
|
+ }
|
|
|
+</style>
|