From be4e2d7c2b330c3b8991a0286ea16cc1285b9ec0 Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Fri, 08 May 2026 17:47:32 +0800
Subject: [PATCH] feat: 微信小程序支持上传头像

---
 src/config/env.js                          |   27 ++++++-------
 src/subPackages/userDetail/infos/index.vue |   86 ++++++++++++++++---------------------------
 2 files changed, 44 insertions(+), 69 deletions(-)

diff --git a/src/config/env.js b/src/config/env.js
index 36ad8ff..a1e7cc8 100644
--- a/src/config/env.js
+++ b/src/config/env.js
@@ -1,23 +1,20 @@
-/*
- * @Author       : yuan
- * @Date         : 2025-12-15 17:51:08
- * @LastEditors  : yuan
- * @LastEditTime : 2025-12-16 15:01:04
- * @FilePath     : \src\config\env.js
- * @Description  :
- * Copyright 2025 OBKoro1, All Rights Reserved.
- * 2025-12-15 17:51:08
- */
 // 如果是打包app,需要改这里,h5不需要改这里
 const development = {
   VITE_APP_ENV:'development',
   // 开发环境这里改为自己的
-  VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
-  // VITE_APP_WEBVIEW_URL: 'http://localhost:5173/drone-app-web-view/#/webViewWrapper',
-  VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
+  // VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
+  // VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
+  // VITE_APP_WS_API_URL:'wss://wrj.shuixiongit.com/drone-wss/api/v1/ws',
+
+
+  // VITE_APP_WEBVIEW_URL: 'https://aisky.org.cn/drone-app-web-view/#/webViewWrapper',
+  VITE_API_BASE_URL: 'https://aisky.org.cn/api',
+  VITE_APP_WS_API_URL: 'wss://aisky.org.cn/drone-wss/api/v1/ws',
+
+  // VITE_APP_WEBVIEW_URL: 'https://aisky.org.cn/drone-app-web-view/#/webViewWrapper',
+  VITE_APP_WEBVIEW_URL: 'http://192.168.1.15:5175/drone-app-web-view/#/webViewWrapper',
   // VITE_API_BASE_URL: 'https://aisky.org.cn/api',
-  VITE_APP_WS_API_URL:'wss://wrj.shuixiongit.com/drone-wss/api/v1/ws',
-  
+  // VITE_APP_WS_API_URL:'wss://aisky.org.cn/drone-wss/api/v1/ws',
   // 静态资源存放地址'
   VITE_APP_ASSETS_URL: 'https://wrj.shuixiongit.com/aiskyminio/cloud-bucket/ztzf_app_assets'
 }
diff --git a/src/subPackages/userDetail/infos/index.vue b/src/subPackages/userDetail/infos/index.vue
index 39d322f..2bec63c 100644
--- a/src/subPackages/userDetail/infos/index.vue
+++ b/src/subPackages/userDetail/infos/index.vue
@@ -38,6 +38,8 @@
 import {getEnvObj} from "@/utils/index.js";
 import {getUserInfo, updateInfo} from '@/api/user/index.js';
 import {useUserStore} from "@/store/index.js";
+import {ref} from "vue";
+import {onShow} from "@dcloudio/uni-app";
 
 const userInfo = ref({
   id: '',
@@ -55,7 +57,6 @@
   if (!phone) return true
   const phoneRegex = /^1[3-9]\d{9}$/
   if (!phoneRegex.test(phone)) {
-
     uni.showToast({
       title: '请输入正确的手机号码',
       icon: 'none',
@@ -108,7 +109,7 @@
 // 头像上传到服务器
 function uploadUtil(options) {
   const {formData, filePath, url} = options;
-
+  uni.showLoading({title: '上传中...'})
   return new Promise((resolve, reject) => {
     let accessToken = useUserStore()?.$state?.userInfo?.access_token;
     uni.uploadFile({
@@ -130,10 +131,35 @@
       },
       fail: (err) => {
         reject(err)
+      },
+      complete: () => {
+        uni.hideLoading()
       }
     });
   })
 }
+
+const handleUploadResult = (filePath, fileName) => {
+  uploadUtil({
+    filePath: filePath,
+    formData: {
+      fileName: fileName,
+      sn: 'avatar_upload'
+    },
+    url: '/blade-resource/oss/endpoint/put-file'
+  }).then(res => {
+    const resData = JSON.parse(res.data);
+    if (resData.code === 200 || resData.code === 0) {
+      // 更新头像显示
+      userInfo.value.avatar = resData.data.link || resData.data.url;
+      uni.showToast({title: '头像上传成功', icon: 'success'});
+    } else {
+      throw new Error(resData.message || '上传失败');
+    }
+  }).catch(err => {
+    uni.showToast({title: err.message || '上传失败', icon: 'none'});
+  });
+};
 
 // 上传头像功能
 const uploadAvatar = () => {
@@ -145,24 +171,8 @@
     maxDuration: 30,
     camera: 'back',
     success(res) {
-      uploadUtil({
-        filePath: res.tempFiles[0].tempFilePath,
-        formData: {
-          fileName: res.tempFiles[0].tempFilePath,
-          sn: 'avatar_upload'
-        },
-        url: '/blade-resource/oss/endpoint/put-file'
-      }).then(res => {
-        const resData = JSON.parse(res.data);
-        if (resData.code === 200 || resData.code === 0) {
-          // 更新头像显示
-          userInfo.value.avatar = resData.data.link || resData.data.url;
-          uni.hideLoading();
-          uni.showToast({title: '头像上传成功', icon: 'success'});
-        } else {
-          throw new Error(resData.message || '上传失败');
-        }
-      })
+      const filePath = res.tempFiles[0].tempFilePath;
+      handleUploadResult(filePath, filePath);
     }
   })
   // #endif
@@ -174,10 +184,7 @@
       const tempFile = res.tempFiles[0]; // 获取文件对象
       const filePath = tempFile.path || tempFile.tempFilePath;
       if (!filePath) {
-        uni.showToast({
-          title: '获取文件路径失败',
-          icon: 'none'
-        });
+        uni.showToast({title: '获取文件路径失败', icon: 'none'});
         return;
       }
       let fileName = tempFile.name;
@@ -185,37 +192,8 @@
         const pathWithoutProtocol = filePath.replace(/^file:\/\//, '');
         fileName = pathWithoutProtocol.split('/').pop() || 'unknown.png';
       }
-
-      // 显示加载中
-      uni.showLoading({
-        title: '上传中...'
-      });
-
       // 上传文件
-      uploadUtil({
-        filePath: filePath,
-        formData: {
-          fileName: fileName,
-          sn: 'avatar_upload'
-        },
-        url: '/blade-resource/oss/endpoint/put-file'
-      }).then(res => {
-        const resData = JSON.parse(res.data);
-        if (resData.code === 200 || resData.code === 0) {
-          // 更新头像显示
-          userInfo.value.avatar = resData.data.link || resData.data.url;
-          uni.hideLoading();
-          uni.showToast({title: '头像上传成功', icon: 'success'});
-        } else {
-          throw new Error(resData.message || '上传失败');
-        }
-      }).catch(err => {
-        uni.hideLoading();
-        uni.showToast({
-          title: err.message || '上传失败',
-          icon: 'none'
-        });
-      });
+      handleUploadResult(filePath, fileName);
     }
   });
   // #endif

--
Gitblit v1.9.3