From d7b8298e174d39256beb3803d120de191b60621f Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期五, 04 七月 2025 15:32:21 +0800 Subject: [PATCH] 上传调整 --- pages/mine/set/personMsg.vue | 79 ++++++++++++++++++++++++++++----------- 1 files changed, 57 insertions(+), 22 deletions(-) diff --git a/pages/mine/set/personMsg.vue b/pages/mine/set/personMsg.vue index aa80980..9441176 100644 --- a/pages/mine/set/personMsg.vue +++ b/pages/mine/set/personMsg.vue @@ -1,7 +1,8 @@ <template> <view class="person-msg"> <view class="head c-content" @click="changeFace"> - <image :src="form.face || '/static/missing-face.png'" mode=""></image> + <image v-if="form.face" :src="endpoint + '/' + form.face" mode=""></image> + <image v-else :src="'/static/missing-face.png'" mode=""></image> <view>鐐瑰嚮淇敼澶村儚</view> </view> <u-form :model="form" ref="uForm" class="form"> @@ -42,20 +43,25 @@ </template> <script> import '@/components/uview-components/uview-ui'; - +import { getSTSToken } from "@/api/common.js"; import { saveUserInfo, getUserInfo } from "@/api/members.js"; import { upload } from "@/api/common.js"; +import { getFileKey } from "@/utils/file.js"; import storage from "@/utils/storage.js"; import city from "@/components/m-city/m-city.vue"; export default { components: { "m-city": city }, data() { return { + cosClient: null, + bucket: '', + region: '', + endpoint: '', lightColor: this.$lightColor, //楂樹寒棰滆壊 form: { nickName: storage.getUserInfo().nickName || "", birthday: storage.getUserInfo().birthday || "", - face: storage.getUserInfo().face || "/static/missing-face.png", //榛樿澶村儚 + face: storage.getUserInfo().face, //榛樿澶村儚 regionId: [], //鍦板潃Id region: storage.getUserInfo().region || [], //鍦板潃 sex: storage.getUserInfo().sex, //鎬у埆 @@ -79,7 +85,31 @@ showBirthday: false, //鏄剧ず鐢熸棩鏃ユ湡 }; }, + onLoad() { + this.initCOS() + }, methods: { + // 鍒濆鍖栬吘璁簯cos瀹㈡埛绔� + initCOS() { + // 璋冪敤鍚庣鑾峰彇sts涓存椂璁块棶鍑瘉 + getSTSToken().then(res => { + const COS = require('@/lib/cos-wx-sdk-v5.js'); // 寮�鍙戞椂浣跨敤 + // const COS = require('./lib/cos-wx-sdk-v5.min.js'); // 涓婄嚎鏃朵娇鐢ㄥ帇缂╁寘 + + // console.log(COS.version); sdk 鐗堟湰闇�瑕佷笉浣庝簬 1.7.2 + this.cosClient = new COS({ + SecretId: res.data.data.tmpSecretId, // sts 鏈嶅姟涓嬪彂鐨勪复鏃� secretId + SecretKey: res.data.data.tmpSecretKey, // sts 鏈嶅姟涓嬪彂鐨勪复鏃� secretKey + SecurityToken: res.data.data.sessionToken, // sts 鏈嶅姟涓嬪彂鐨勪复鏃� SessionToken + StartTime: res.data.data.stsStartTime, // 寤鸿浼犲叆鏈嶅姟绔椂闂达紝鍙伩鍏嶅鎴风鏃堕棿涓嶅噯瀵艰嚧鐨勭鍚嶉敊璇� + ExpiredTime: res.data.data.stsEndTime, // 涓存椂瀵嗛挜杩囨湡鏃堕棿 + SimpleUploadMethod: 'putObject', // 寮虹儓寤鸿锛岄珮绾т笂浼犮�佹壒閲忎笂浼犲唴閮ㄥ灏忔枃浠跺仛绠�鍗曚笂浼犳椂浣跨敤 putObject,sdk 鐗堟湰鑷冲皯闇�瑕乿1.3.0 + }); + this.bucket = res.data.data.bucket + this.region = res.data.data.region + this.endpoint = res.data.data.endpoint + }) + }, /** * 閫�鍑虹櫥褰� */ @@ -135,21 +165,30 @@ */ changeFace(index) { uni.chooseImage({ + count: 1, + sizeType: ['compressed'], + sourceType: ['album'], success: (chooseImageRes) => { - const tempFilePaths = chooseImageRes.tempFilePaths; - uni.uploadFile({ - url: upload, - filePath: tempFilePaths[0], - name: "file", - header: { - accessToken: storage.getAccessToken(), - }, - success: (uploadFileRes) => { - let data = JSON.parse(uploadFileRes.data); - - this.form.face = data.result; - }, - }); + const tempFilePath = chooseImageRes.tempFilePaths[0]; + let fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1); + // 澶勭悊瀹夊崜鍙兘鐨刄RI缂栫爜 + if(fileName.indexOf('%') > -1) { + fileName = decodeURIComponent(fileName); + } + const fileKey = getFileKey(fileName); + this.cosClient.uploadFile({ + Bucket: this.bucket, + Region: this.region, + Key: fileKey, + FilePath: tempFilePath, + SliceSize: 1024 * 1024 * 5 /* 瑙﹀彂鍒嗗潡涓婁紶鐨勯槇鍊�,5M */ + }, (err, data) => { + if (err) { + console.log('涓婁紶澶辫触', err); + } else { + this.form.face = fileKey; + } + }); }, }); }, @@ -178,12 +217,8 @@ url: '/pages/mine/set/securityCenter/bindMobile' + '?username=' + username, }); }, - }, + } - /** - * 鍔犺浇鏁版嵁 - */ - onLoad() {}, }; </script> <style> -- Gitblit v1.8.0