<script>
var sourceType = [
['camera'], //拍照
['album'], //相册
['camera', 'album'] //拍照或相册
]
var sizeType = [
['compressed'], //压缩
['original'], //原图
['compressed', 'original'] //压缩或原图
]
export default {
data() {
return {
sourceTypeIndex: 2,
sourceType: ['拍照', '相册', '拍照或相册'],
sizeTypeIndex: 2,
sizeType: ['压缩', '原图', '压缩或原图'],
countIndex: 9,
count: 9,
imageLength: 9, //限制图片张数
}
},
onUnload() {
this.imageList = [],
this.sourceTypeIndex = 2,
this.sourceType = ['拍照', '相册', '拍照或相册'],
this.sizeTypeIndex = 2,
this.sizeType = ['压缩', '原图', '压缩或原图'],
this.countIndex = 8;
},
methods: {
//选择图片
chooseImage: async function() {
uni.chooseImage({
sourceType: sourceType[this.sourceTypeIndex],
// #ifdef MP-WEIXIN
sizeType: sizeType[this.sizeTypeIndex],
// #endif
count: this.imageLength - this.imageList.length,
success: (res) => {
var uploadImgCount = 0;
const tempFilePaths = res.tempFilePaths;
for (var i = 0, h = tempFilePaths.length; i < h; i++) {
uni.request({
url: tempFilePaths[i],
method: 'GET',
responseType: 'arraybuffer',
success: ress => {
let base64 = uni.arrayBufferToBase64(ress.data); //把arraybuffer转成base64
base64 = 'data:image/jpeg;base64,' + base64 //不加上这串字符,在页面无法显示的哦
uni.request({
url:this.url+'/im/v1',
method: 'POST',
dataType: 'file',
header: {
'content-type' : "application/json",
'Im-Token' : this.ImToken,
'Im-Client-Id':this.ImClientId,
'Im-Device-Number':this.ImDeviceNumber,
},
data:{
id:this.ImClientId,
action: 'headPortraitUpload',
type : '2',
payload:{
type:'0',
head_portrait:base64,
}
},
success: function(uploadFileRes) {
uploadImgCount++;
var data = JSON.parse(uploadFileRes.data);
imglis.push(data.payload.head_url);
this.imglist=imglis;
uni.setStorage({
key: 'image',
data: {
image: this.imglist,
},
});
}
});
}
})
}
return;
}
}
})
},
}
}