wangguoyu 2 months ago
parent
commit
4067ec0bdc

+ 163 - 34
pages/admin/Jiazheng/common/jiazhengUpload.vue

@@ -3,13 +3,13 @@
 		<view v-if="!imageUrl" class="sfz-content"><text>+</text> 添加身份证</view>
 		<image class="sfz-image" v-else :src="imageUrl" mode=""></image>
 	</view>
-	
+
 	<view v-else-if="changjingType =='more'" class="admin-upload-btn-box" @click="showActionSheet">
 		<text>+</text>
 	</view>
 	<view v-else class="admin-upload-btn-box" @click="showActionSheet">
-	<text v-if="!imageUrl">+</text>
-	<image v-else class="other-image"   :src="imageUrl" mode=""></image>
+		<text v-if="!imageUrl">+</text>
+		<image v-else class="other-image" :src="imageUrl" mode=""></image>
 	</view>
 	<view v-if="bottomText" class="upload-bottom-text">
 		{{bottomText}}
@@ -21,21 +21,21 @@
 		getAliyunPolicy
 	} from "@/api/jiazheng.js"
 	export default {
-		 emits: ['getFileUrl'], // 声明自定义事件
-		data(){
+		emits: ['getFileUrl'], // 声明自定义事件
+		data() {
 			return {
-				imageUrl:""
+				imageUrl: ""
 			}
 		},
 		props: {
 			bottomText: { // 试题序号
 				type: String
 			},
-			changjingType:{
-					type: String,
-					default: ''
+			changjingType: {
+				type: String,
+				default: ''
 			},
-			img:{
+			img: {
 				type: String,
 				default: ''
 			}
@@ -43,13 +43,20 @@
 		watch: {
 			img: {
 				handler(newVal) {
-				this.imageUrl  =newVal
+					this.imageUrl = newVal
 				}
 			},
-	
+
 		},
 		methods: {
 			showActionSheet() {
+
+				// 判断是否是H5环境
+				// #ifdef H5
+				this.chooseImage('album'); // H5环境下直接选择相册
+				// #endif
+
+				// #ifndef H5
 				uni.showActionSheet({
 					itemList: ['拍照', '从相册选择'],
 					success: (res) => {
@@ -60,6 +67,8 @@
 						}
 					}
 				});
+				// #endif
+
 			},
 			chooseImage(sourceType) {
 				uni.chooseImage({
@@ -77,35 +86,80 @@
 					title: '上传中...',
 					mask: true
 				});
-
+			
 				try {
 					const suffix = filePath.split('.').pop();
 					let req = {
 						prefix: 'resource/',
 						suffix: suffix
 					}
-
+			
 					getAliyunPolicy(req).then(res => {
-						let that   = this
+						let that = this;
 						if (res.code == 0) {
 							const policyData = res.data;
 							console.log('policyData', policyData);
-							const formData = {
-								key: policyData.key,
-								policy: policyData.policy,
-								OSSAccessKeyId: policyData.accessid,
-								signature: policyData.signature,
-								success_action_status: '200',
-								file: {
-									name: policyData.key,
-									uri: filePath
-								}
-							};
+			
+							// #ifdef H5
+							const formData = new FormData();
+							formData.append('key', policyData.key);
+							formData.append('policy', policyData.policy);
+							formData.append('OSSAccessKeyId', policyData.accessid);
+							formData.append('signature', policyData.signature);
+							formData.append('success_action_status', '200');
+		
+							fetch(filePath)
+								.then(response => response.blob())
+								.then(blob => {
+									formData.append('file', blob, policyData.key);
+									fetch(policyData.uploadUrl, {
+										method: 'POST',
+										body: formData
+									})
+									.then(response => {
+										if (response.ok) {
+											that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
+											uni.showToast({
+												title: '上传成功',
+												icon: 'success'
+											});
+											that.$emit('getFileUrl', that.imageUrl);
+										} else {
+											uni.showToast({
+												title: '上传失败',
+											});
+										}
+									})
+									.catch(error => {
+										console.error('上传失败:', error);
+										uni.showToast({
+											title: '上传失败',
+											icon: 'none'
+										});
+									});
+								})
+								.catch(error => {
+									console.error('文件转换失败:', error);
+									uni.showToast({
+										title: '文件转换失败',
+										icon: 'none'
+									});
+								});
+							// #endif
+			
+							// #ifndef H5
+							// 非 H5 环境下使用 uni.uploadFile 上传
 							uni.uploadFile({
 								url: policyData.uploadUrl,
 								filePath: filePath,
 								name: 'file',
-								formData: formData,
+								formData: {
+									key: policyData.key,
+									policy: policyData.policy,
+									OSSAccessKeyId: policyData.accessid,
+									signature: policyData.signature,
+									success_action_status: '200'
+								},
 								header: {
 									'Content-Type': 'multipart/form-data'
 								},
@@ -117,27 +171,25 @@
 											title: '上传成功',
 											icon: 'success'
 										});
-										console.log('imageUrl', that.imageUrl);
 										that.$emit('getFileUrl', that.imageUrl);
 									} else {
 										uni.showToast({
 											title: '上传失败',
 										});
-										return false
 									}
 								},
 								fail(err) {
 									console.log('err', err);
 								}
 							});
-
+							// #endif
+			
 						} else {
 							uni.showToast({
 								title: '获取凭证失败',
 							});
-							return false
 						}
-					})
+					});
 				} catch (error) {
 					uni.showToast({
 						title: '上传失败',
@@ -147,7 +199,84 @@
 				} finally {
 					uni.hideLoading();
 				}
-			},
+			}
+			// uploadFileToAliyun(filePath) {
+			// 	console.log('filePath', filePath);
+			// 	const loading = uni.showLoading({
+			// 		title: '上传中...',
+			// 		mask: true
+			// 	});
+
+			// 	try {
+			// 		const suffix = filePath.split('.').pop();
+			// 		let req = {
+			// 			prefix: 'resource/',
+			// 			suffix: suffix
+			// 		}
+
+			// 		getAliyunPolicy(req).then(res => {
+			// 			let that = this
+			// 			if (res.code == 0) {
+			// 				const policyData = res.data;
+			// 				console.log('policyData', policyData);
+			// 				const formData = {
+			// 					key: policyData.key,
+			// 					policy: policyData.policy,
+			// 					OSSAccessKeyId: policyData.accessid,
+			// 					signature: policyData.signature,
+			// 					success_action_status: '200',
+			// 					file: {
+			// 						name: policyData.key,
+			// 						uri: filePath
+			// 					}
+			// 				};
+			// 				uni.uploadFile({
+			// 					url: policyData.uploadUrl,
+			// 					filePath: filePath,
+			// 					name: 'file',
+			// 					formData: formData,
+			// 					header: {
+			// 						'Content-Type': 'multipart/form-data'
+			// 					},
+			// 					success(uploadRes) {
+			// 						console.log('uploadRes', uploadRes);
+			// 						if (uploadRes.statusCode === 200) {
+			// 							that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
+			// 							uni.showToast({
+			// 								title: '上传成功',
+			// 								icon: 'success'
+			// 							});
+			// 							console.log('imageUrl', that.imageUrl);
+			// 							that.$emit('getFileUrl', that.imageUrl);
+			// 						} else {
+			// 							uni.showToast({
+			// 								title: '上传失败',
+			// 							});
+			// 							return false
+			// 						}
+			// 					},
+			// 					fail(err) {
+			// 						console.log('err', err);
+			// 					}
+			// 				});
+
+			// 			} else {
+			// 				uni.showToast({
+			// 					title: '获取凭证失败',
+			// 				});
+			// 				return false
+			// 			}
+			// 		})
+			// 	} catch (error) {
+			// 		uni.showToast({
+			// 			title: '上传失败',
+			// 			icon: 'none'
+			// 		});
+			// 		console.error('上传失败:', error);
+			// 	} finally {
+			// 		uni.hideLoading();
+			// 	}
+			// },
 		}
 	};
-</script>
+</script>

+ 1 - 1
pages/admin/Jiazheng/jiazhengUserManager.vue

@@ -233,7 +233,7 @@
 			},
 			changeTab(data) {
 				console.log('data', data);
-				if (this.status == 'edit') {
+				if (this.status == 'edit' &&data ==3) {
 					this.banzhengXinxiFlag = true
 					return false
 				} else {