const crypto = require('crypto-js')
getsignature(policy, accessKeySecret) {
return crypto.enc.Base64.stringify(crypto.HmacSHA1(policy, accessKeySecret))
},
getPolicyBase64() {
let date = new Date()
date.setHours(date.getHours() + 1)
let srcT = date.toISOString()
const policyText = {
expiration: srcT,
conditions: [['content-length-range', 0, 100 * 1024 * 1024]]
}
const buffer = new Buffer(JSON.stringify(policyText))
return buffer.toString('base64')
},
apiGetOssfsToken(vm, {
param1:'某参数1',
param2:'某参数2',
name:'lidysun',
}).then(res => {
if(res.code==200){
vm.uplpadEntity = res.datas;
console.log(vm.uplpadEntity);
}
})
chooseHeadImg() {
uni.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: res => {
const url = `https://${endpoint}`
const policy = this.getPolicyBase64()
const key = `${uploadPath}${new Date().getTime()}${this.suffix}`
const {
accessKeyId,
accessKeySecret,
securityToken:stsToken,
bucketName:bucket,
uploadPath,
endpoint
} = this.uplpadEntity
const imageSrc = res.tempFilePaths[0]
const signature = this.getsignature(policy, accessKeySecret)
const params = {
'key':uploadPath,
'policy':policy,
'OSSAccessKeyId':accessKeyId,
'success_action_status':200,
'signature':signature,
'x-oss-security-token':stsToken
}
uni.uploadFile({
url:`https://${endpoint}`,
filePath: imageSrc,
fileType: 'image',
name: 'file',
formData:params,
success: (res) => {
console.log('uploadImage success:', res)
uni.showToast({
title: '上传成功',
icon: 'success',
duration: 1000
})
},
fail: (err) => {
console.log('uploadImage fail:', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
}
});
}
});
},
~ mark 备份 ~
|