picture.js
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// packageStoreAssistant/packageEquityClaims/components/picture/picture.js
import { commonApi } from '~/api/api'
import { getSystemInfoSync, getSystemInfo } from '~/utils/systemInfo'
Component({
/**
* 组件的属性列表
*/
externalClasses: ['custom-class'],
properties: {
configOptions: {
type: Object,
value: ()=> {
}
},
url: {
type: String,
value: ''
},
exampleUrl: {
type: String,
value: ''
},
disabled: {
type: Boolean,
value: ''
},
batchFlag: {
type: Boolean,
value: false
},
fileList: {
type: Object,
value: () => {}
},
batchIndex: {
type: Number,
value: 0
}
},
/**
* 组件的初始数据
*/
data: {
curUrl: '',
previewVisible: false
},
/**
* 组件的方法列表
*/
methods: {
previewImg(e, viewUrl) {
const { batchFlag, fileList, batchIndex } = this.data
const { url } = e.currentTarget.dataset
let current, urls
if (batchFlag) { // 批量预览
urls = fileList
?.filter(item => item.attachmentFile)
?.map(item => item.attachmentFile)
current = urls[batchIndex]
} else { // viewUrl是示例图片预览
current = viewUrl || url
urls = viewUrl ? [viewUrl] : [url]
}
wx.previewImage({
current,
urls
})
},
viewExample(e) {
this.previewImg(e, this.data.exampleUrl)
},
stop() {},
chooseFile(e){
let { batchFlag, fileList } = this.data // 其他资料支持批量上传 最大50张
if(this.data.configOptions.onlyExample) {
return
}
// this.triggerEvent('upload', {url: 'https://img02.glsx.com.cn/weapp/resource/saas/scrm/assets/glsx/logo_collapse_new_common.png',type : 'png'});
// if(e) return
const {count} = this.data.configOptions
let sysInfo = getSystemInfoSync()
let maxNum = sysInfo?.environment ? 9 : 20 // 企微打开的话最大是9张。如果count大于9 会无法上传有异常
let aleadyLength = 0
if (batchFlag) {
aleadyLength = fileList?.filter(val => val.attachmentFile)?.length || 0
}
wx.chooseMedia({
count: batchFlag ? ((50 - aleadyLength) > maxNum ? maxNum : 50 - aleadyLength) : (count || 1),
sizeType: ['original'],
sourceType: ['album'],
mediaType: ['image'],
success: (res)=> {
const uploadImages = async (tempFiles) => {
wx.showLoading({
title: '上传中',
mask: true
})
try {
const uploadResults = [] // 存储所有上传结果
for (const item of tempFiles) {
const val = item.tempFilePath
const imgType = val.split('.')[1]
const type = imgType === 'unknown' ? 'jpg' : imgType
// 读取文件
const _res = await new Promise((resolve, reject) => {
wx.getFileSystemManager().readFile({
filePath: val,
encoding: 'base64',
success: resolve,
fail: reject,
})
})
// 上传图片
const url = await commonApi.uploadImg({
file: `${type}@${_res.data}`,
})
// 存储当前上传结果
uploadResults.push({
url,
type
})
}
// 所有上传完成后,统一触发事件
wx.hideLoading()
this.triggerEvent('upload', uploadResults);
} catch (error) {
wx.$api.showToast('上传失败,请重试');
// showToast会将loading关闭,重新显示loading
wx.showLoading({
title: '上传中',
mask: true
})
} finally {
wx.hideLoading()
}
}
uploadImages(res?.tempFiles)
// const [tempFiles] = res.tempFilePaths
// let imgType = tempFiles.split('.')[1]
// let type = imgType == 'unknown' ? 'jpg' : imgType
// wx.getFileSystemManager().readFile({
// filePath: tempFiles,
// encoding: 'base64',
// success: (_res)=> {
// wx.showLoading('上传中...')
// commonApi.uploadImg({
// file: `${type}@${_res.data}`
// }).then(url => {
// wx.hideLoading()
// this.triggerEvent('upload', {url, type});
// })
// }
// })
}
})
// console.log('上传')
// this.triggerEvent('upload', this.data.fileObj);
},
delImg(e) {
console.log(2)
this.triggerEvent('del');
},
}
})