| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 | 
							- <template>
 
- 	<view v-if="changjingType =='shenfenzheng'" class="sfz-upload-box" @click="showActionSheet">
 
- 		<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>
 
- 	</view>
 
- 	<view v-if="bottomText" class="upload-bottom-text">
 
- 		{{bottomText}}
 
- 	</view>
 
- </template>
 
- <script>
 
- 	import axios from 'axios';
 
- 	import {
 
- 		getAliyunPolicy
 
- 	} from "@/api/jiazheng.js"
 
- 		import cacheManager from "@/utils/cacheManager.js";
 
- 	export default {
 
- 		emits: ['getFileUrl'], // 声明自定义事件
 
- 		data() {
 
- 			return {
 
- 				imageUrl: ""
 
- 			}
 
- 		},
 
- 		props: {
 
- 			bottomText: { // 试题序号
 
- 				type: String
 
- 			},
 
- 			changjingType: {
 
- 				type: String,
 
- 				default: ''
 
- 			},
 
- 			img: {
 
- 				type: String,
 
- 				default: ''
 
- 			}
 
- 		},
 
- 		watch: {
 
- 			img: {
 
- 				handler(newVal) {
 
- 					this.imageUrl = newVal
 
- 				}
 
- 			},
 
- 		},
 
- 		methods: {
 
- 			showActionSheet() {
 
- 				// 判断是否是H5环境
 
- 				// #ifdef H5
 
- 				this.chooseImage('album'); // H5环境下直接选择相册
 
- 				// #endif
 
- 				// #ifndef H5
 
- 				uni.showActionSheet({
 
- 					itemList: ['拍照', '从相册选择'],
 
- 					success: (res) => {
 
- 						if (res.tapIndex === 0) {
 
- 							this.chooseImage('camera');
 
- 						} else if (res.tapIndex === 1) {
 
- 							this.chooseImage('album');
 
- 						}
 
- 					}
 
- 				});
 
- 				// #endif
 
- 			},
 
- 			chooseImage(sourceType) {
 
- 				uni.chooseImage({
 
- 					count: 1, // 只能选择一张图片
 
- 					sourceType: [sourceType], // 'camera' 或 'album'
 
- 					success: (res) => {
 
- 						console.log('res', res);
 
- 						// #ifndef H5
 
- 						const filePath = res.tempFilePaths[0];
 
- 						this.uploadFileToAliyun(filePath);
 
- 						// #endif
 
- 						// #ifdef H5
 
- 						const filePath = res.tempFiles[0];
 
- 						this.uploadFileToAliyun(filePath);
 
- 						// #endif
 
- 					}
 
- 				});
 
- 			},
 
- 			uploadFileToAliyun(filePath) {
 
- 				console.log('filePath', filePath);
 
- 				const loading = uni.showLoading({
 
- 					title: '上传中...',
 
- 					mask: true
 
- 				});
 
- 				try {
 
- 					// #ifdef H5
 
- 					const suffixList = filePath.name.split('.');
 
- 					let req = {
 
- 						prefix: 'resource/',
 
- 						suffix: suffixList[suffixList.length - 1],
 
- 					}
 
- 					// #endif
 
- 					// #ifndef H5
 
- 					const suffix = filePath.split('.').pop();
 
- 					let req = {
 
- 						prefix: 'resource/',
 
- 						suffix: suffix
 
- 					}
 
- 					// #endif
 
- 					getAliyunPolicy(req).then(res => {
 
- 						let that = this;
 
- 						if (res.code == 0) {
 
- 							const policyData = res.data;
 
- 							console.log('policyData', policyData);
 
- 							// #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');
 
- 							formData.append('file', filePath);
 
- 							axios.post(policyData.uploadUrl, formData)
 
- 								.then(alRes => {
 
- 									if (alRes.status === 200) {
 
- 										that.imageUrl =
 
- 											`${policyData.downloadUrl}/${policyData.key}`;
 
- 										uni.showToast({
 
- 											title: '上传成功',
 
- 											icon: 'success'
 
- 										});
 
- 										that.$emit('getFileUrl', that.imageUrl);
 
- 									} else {
 
- 										uni.showToast({
 
- 											title: '上传失败',
 
- 										});
 
- 									}
 
- 								})
 
- 							// #endif
 
- 							// #ifndef H5
 
- 							console.log('appappappappappapp');
 
- 							// 非 H5 环境下使用 uni.uploadFile 上传
 
- 							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) {
 
- 										that.imageUrl = `${policyData.downloadUrl}/${policyData.key}`;
 
- 										uni.showToast({
 
- 											title: '上传成功',
 
- 											icon: 'success'
 
- 										});
 
- 										that.$emit('getFileUrl', that.imageUrl);
 
- 									} else {
 
- 										uni.showToast({
 
- 											title: '上传失败',
 
- 										});
 
- 									}
 
- 								},
 
- 								fail(err) {
 
- 									console.log('err', err);
 
- 								}
 
- 							});
 
- 							// #endif
 
- 						} else {
 
- 							uni.showToast({
 
- 								title: '获取凭证失败',
 
- 							});
 
- 						}
 
- 					});
 
- 				} catch (error) {
 
- 					uni.showToast({
 
- 						title: '上传失败',
 
- 						icon: 'none'
 
- 					});
 
- 					console.error('上传失败:', error);
 
- 				} 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>
 
 
  |