123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <template>
- <view class="ezy-my-page">
- <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="chooseImage">相册</view>
- <view @click="handleBack">取消</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
- } 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'
- }
- }
- onLoad((options) => {
- isIOSorAndroid()
- })
- </script>
|