<template>
	<view class="ezy-ghtx-page">
		<view class="icon-title-navBar-box">
			<view @click="handleBack" class="nav-bar-icon"></view>
			<text class="nav-bar-title">更换头像</text>
		</view>
		<img class="ghtx-img" :src="userImg" alt="">

		<view @click="chooseImage" class="xc-btn">相册</view>
		<view @click="handleBack" class="qx-btn">取消</view>
	</view>
</template>

<script setup>
	import agreeContentDialog from '@/pages/login/agreeContentDialog.vue';
	import {
		toast,
		getUserIdentity
	} from "@/utils/common";
	import cacheManager from '@/utils/cacheManager.js';
	import {
		getFilePolicy,
		updataHead,myInfo
	} from '@/api/my.js'
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import axios from 'axios';
	import {
		reactive,
		ref
	} from "vue";
	import {
		onShow
	} 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 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) {
							userImg.value = `${policyData.downloadUrl}/${policyData.key}`;					
							updateIcon(userImg.value)
						} else {
							uni.showToast({
								title: '阿里云上传错误，请重试！',
							});
							return false
						}
					},
					fail(err) {
						console.log('err', err);
					}
				});


			}
		})

	}



	function handleBack() {
		uni.redirectTo({
			url: '/pages/my/yingyongshezhi'
		})
	}

	function openXiangce2() {

	}


	function openXiangce() {

	}

	function isIOSorAndroid() {
		const systemInfo = uni.getSystemInfoSync();

		console.log('systemInfo', systemInfo);

		if (systemInfo.platform == 'ios') {
			return currentPlatform.value = 'ios'
		} else {
			return currentPlatform.value = 'android'
		}
	}
	function getMyInfo() {
			myInfo({}).then(res => {
				if (res.code == 0) {
					userImg.value = res.data.icon;
				} else {
					toast('获取头像失败')
					return false
				}
	
			})
		}
			
	onLoad((options) => {
		isIOSorAndroid();
		getMyInfo();
	})
</script>