<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;
}
}
})
},
}
}