wangguoyu 2 months ago
parent
commit
ff93ed82c7
3 changed files with 110 additions and 4 deletions
  1. 11 0
      api/my.js
  2. 96 3
      pages/my/headImg.vue
  3. 3 1
      pages/my/yingyongshezhi.vue

+ 11 - 0
api/my.js

@@ -77,4 +77,15 @@ export function nichengUpdate(data = {}) {
     data,
     timeout: 20000
   })
+}
+export function getFilePolicy(data = {}) {
+  return request({
+    'url': '/app/file/policy',
+    headers: {
+      isToken: true
+    },
+    method: 'post',
+    data,
+    timeout: 20000
+  })
 }

+ 96 - 3
pages/my/headImg.vue

@@ -1,8 +1,12 @@
 <template>
 	<view class="ezy-my-page">
-		<img :src="userImg" alt="">
+		<view class="icon-title-navBar-box">
+			<view @click="handleBack" class="nav-bar-icon"></view>
+			<text class="nav-bar-title">更换头像</text>
+		</view>
+		<img style="height: 200px;width: 200px;;" :src="userImg" alt="">
 
-		<view @click="openXiangce">相册</view>
+		<view @click="chooseImage">相册</view>
 		<view @click="handleBack">取消</view>
 	</view>
 </template>
@@ -15,11 +19,12 @@
 	} from "@/utils/common";
 	import cacheManager from '@/utils/cacheManager.js';
 	import {
-		myInfo,
+		getFilePolicy
 	} from '@/api/my.js'
 	import {
 		onLoad
 	} from '@dcloudio/uni-app';
+	import axios from 'axios';
 	import {
 		reactive,
 		ref
@@ -29,6 +34,89 @@
 	} from '@dcloudio/uni-app';
 
 	let userImg = ref(null);
+	let currentPlatform = ref(null);
+	const tempPath = ref('')
+	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 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) {
+							userImg.value = `${policyData.downloadUrl}/${policyData.key}`;
+							uni.showToast({
+								title: '上传成功',
+								icon: 'success'
+							});
+						} else {
+							uni.showToast({
+								title: '上传失败',
+							});
+						}
+					},
+					fail(err) {
+						console.log('err', err);
+					}
+				});
+
+
+			}
+		})
+
+	}
+
+
 
 	function handleBack() {
 		uni.redirectTo({
@@ -36,6 +124,11 @@
 		})
 	}
 
+	function openXiangce2() {
+
+	}
+
+
 	function openXiangce() {
 
 	}

+ 3 - 1
pages/my/yingyongshezhi.vue

@@ -126,7 +126,9 @@
 	}
 
 	function headClick() {
-
+	uni.redirectTo({
+			url: '/pages/my/headImg'
+		})
 	}
 
 	function nichengClick() {