wangguoyu 2 months ago
parent
commit
3a13f830a7
2 changed files with 105 additions and 7 deletions
  1. 1 1
      pages/my/headImg.vue
  2. 104 6
      pages/my/yingyongshezhi.vue

+ 1 - 1
pages/my/headImg.vue

@@ -47,7 +47,7 @@
 	}
 	const chooseImage = async () => {
 		try {
-			//await checkAlbumPermission()
+			await checkAlbumPermission()
 			uni.chooseImage({
 				count: 1,
 				sizeType: ['compressed'],

+ 104 - 6
pages/my/yingyongshezhi.vue

@@ -8,7 +8,8 @@
 			<view class="list-row" @click="headClick">
 				<icon class="list-icon tel-icon"></icon>
 				<text>头像</text>
-				<icon style="width: 20px;height: 20px;" class="head-img-box" :style="{backgroundImage: 'url(' + myInfoData.icon + ')'}"></icon>
+				<icon style="width: 20px;height: 20px;" class="head-img-box"
+					:style="{backgroundImage: 'url(' + myInfoData.icon + ')'}"></icon>
 			</view>
 			<view class="list-row" @click="nichengClick">
 				<icon class="list-icon tel-icon"></icon>
@@ -72,7 +73,9 @@
 		duihuanmaCode,
 		nichengUpdate,
 		myCardList,
-		commonCardList
+		commonCardList,
+		getFilePolicy,
+		updataHead
 	} from '@/api/my.js'
 	import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
 	import {
@@ -127,11 +130,104 @@
 	}
 
 	function headClick() {
-	uni.redirectTo({
-			url: '/pages/my/headImg'
+		chooseImage()
+	}
+
+	const checkAlbumPermission = async () => {
+		const status = await uni.getSetting()
+		if (!status.authSetting['scope.album']) {
+			await uni.authorize({
+				scope: 'scope.album'
+			})
+		}
+	}
+	const chooseImage = async () => {
+		try {
+		//	await checkAlbumPermission()
+			uni.chooseImage({
+				count: 1,
+				sizeType: ['compressed'],
+				sourceType: ['album'], // 仅限相册选择‌:ml-citation{ref="1" data="citationList"}
+				success: (res) => {
+
+					uploadFile(res.tempFilePaths[0])
+				},
+				fail: (err) => {
+					console.error('选择失败:', err)
+				}
+			})
+		} catch (err) {
+			uni.showModal({
+				title: '权限申请失败',
+				content: '请前往设置开启相册权限'
+			})
+		}
+	}
+
+	function updateIcon(data) {
+		updataHead({
+			icon: data
+		}).then(res => {
+			if (res.code == 0) {
+				uni.showToast({
+					title: '上传成功',
+					icon: 'success'
+				});
+			} else {
+				uni.showToast({
+					title: '上传失败',
+				});
+			}
 		})
 	}
 
+	function uploadFile(filePath) {
+		const suffix = filePath.split('.').pop();
+		let req = {
+			prefix: 'resource/',
+			suffix: suffix
+		}
+		getFilePolicy(req).then(res => {
+			if (res.code === 0) {
+				const policyData = res.data;
+				console.log('policyData', policyData);
+				uni.uploadFile({
+					url: policyData.uploadUrl,
+					filePath: filePath,
+					name: 'file',
+					formData: {
+						key: policyData.key,
+						policy: policyData.policy,
+						OSSAccessKeyId: policyData.accessid,
+						signature: policyData.signature,
+						success_action_status: '200'
+					},
+					header: {
+						'Content-Type': 'multipart/form-data'
+					},
+					success(uploadRes) {
+						console.log('uploadRes', uploadRes);
+						if (uploadRes.statusCode === 200) {
+							myInfoData.icon = `${policyData.downloadUrl}/${policyData.key}`;
+							updateIcon(myInfoData.icon)
+						} else {
+							uni.showToast({
+								title: '阿里云上传错误,请重试!',
+							});
+							return false
+						}
+					},
+					fail(err) {
+						console.log('err', err);
+					}
+				});
+
+
+			}
+		})
+
+	}
+
 	function nichengClick() {
 		nichengDialogRef.value.handleShow();
 	}
@@ -239,7 +335,7 @@
 			myInfoData.userName = res.data.userName;
 			myInfoData.credit = res.data.credit;
 			myInfoData.nickName = res.data.nickName;
-			
+
 			if (res.data.nickName) {
 				myInfoData.nickName = res.data.nickName;
 			} else {
@@ -250,9 +346,10 @@
 			} else {
 				getUserImg(res.data.growthType)
 			}
-			
+
 		})
 	}
+
 	function getUserImg(data) {
 		switch (data) {
 			case 0:
@@ -273,6 +370,7 @@
 
 		}
 	}
+
 	function isIOSorAndroid() {
 		const systemInfo = uni.getSystemInfoSync();