上传头像比例4:5

This commit is contained in:
Mr.jiang 2024-08-19 10:20:19 +08:00
parent 88c80be724
commit 237ae1e61b
3 changed files with 120 additions and 15 deletions

View File

@ -2,8 +2,8 @@
"name" : "盛安到家",
"appid" : "__UNI__B37C795",
"description" : "",
"versionName" : "2.0.5",
"versionCode" : "100",
"versionName" : "2.1.0",
"versionCode" : 110,
"transformPx" : false,
/* 5+App */
"app-plus" : {

View File

@ -125,7 +125,7 @@
</view> -->
</view>
</view>
<view class="margin-top" @click="addImage(4)" v-if="form.headImg.length<=0">
<view class="margin-top" @click="addImagea(4)" v-if="form.headImg.length<=0">
<view style="width: 200rpx;height: 200rpx;background: #f7f8fa;"
class="flex justify-center align-center">
<view>
@ -781,6 +781,75 @@
}
})
},
validateImageRatio(imagePath, targetRatio) {
return new Promise((resolve, reject) => {
uni.getImageInfo({
src: imagePath,
success: (info) => {
const size = info.size;
if (size / 1024 / 1024 > 4) {
// 2MB
uni.showToast({
icon:'error',
title:'图片超出2MB限制,请重新选择'
})
} else {
const { width, height } = info;
const actualRatio = width / height;
const isValid = Math.abs(actualRatio - targetRatio) < Number.EPSILON;
resolve(isValid);
console.log("asdasdasd")
}
},
fail: () => reject(false),
});
});
},
//
addImagea(e) {
let that = this
uni.chooseImage({
count: 1,
sourceType: ['album', 'camera'],
success: res => {
for (let i = 0; i < res.tempFilePaths.length; i++) {
that.$queue.showLoading("上传中...");
this.validateImageRatio(res.tempFilePaths[i], 4 / 5).then((isValid) => {
if (isValid) {
// 2MB
uni.uploadFile({ //
url: websocketUtils.uploadFileUrl(), //
filePath: res.tempFilePaths[i],
name: 'file',
success: (uploadFileRes) => {
if (e == 1) {
that.form.front = JSON.parse(uploadFileRes.data).data
} else if (e == 2) {
that.form.back = JSON.parse(uploadFileRes.data).data
} else if (e == 4) {
that.form.headImg = JSON.parse(uploadFileRes.data).data
console.log(that.form.headImg)
}
uni.hideLoading();
}
});
// ...
} else {
uni.showToast({
title: '图片比例不符合要求请上传4:5的图片',
icon: 'none',
});
}
});
}
}
})
},
addImage(e) {
let that = this
uni.chooseImage({

View File

@ -183,6 +183,30 @@
url: '../jifen/myaddress'
});
},
validateImageRatio(imagePath, targetRatio) {
return new Promise((resolve, reject) => {
uni.getImageInfo({
src: imagePath,
success: (info) => {
const size = info.size;
if (size / 1024 / 1024 > 4) {
// 2MB
uni.showToast({
icon:'error',
title:'图片超出2MB限制,请重新选择'
})
} else {
const { width, height } = info;
const actualRatio = width / height;
const isValid = Math.abs(actualRatio - targetRatio) < Number.EPSILON;
resolve(isValid);
}
},
fail: () => reject(false),
});
});
},
uploadImg() {
let token = uni.getStorageSync('token')
@ -218,26 +242,38 @@
});
let token = uni.getStorageSync('token');
let userId = uni.getStorageSync('userId');
uni.uploadFile({
url: websocketUtils.uploadFileUrl(), //
filePath: res.tempFilePaths[0],
header: {
token: token
},
name: 'file',
success: uploadFileRes => {
url = JSON.parse(uploadFileRes.data);
that.avatar = url.data
uni.hideLoading();
that.validateImageRatio(res.tempFilePaths[0],4 / 5).then((isValid) => {
if (isValid) {
// 2MB
uni.uploadFile({
url: websocketUtils.uploadFileUrl(), //
filePath: res.tempFilePaths[0],
header: {
token: token
},
name: 'file',
success: uploadFileRes => {
url = JSON.parse(uploadFileRes.data);
that.avatar = url.data
uni.hideLoading();
}
});
// ...
} else {
uni.showToast({
title: '图片比例不符合要求请上传4:5的图片',
icon: 'none',
});
}
});
}
});
}
}
})
},
getUserInfo() {
let userId = uni.getStorageSync('userId')
this.$Request.get("/app/user/selectUserById").then(res => {