服务器更替
This commit is contained in:
+29
-46
@@ -1,34 +1,16 @@
|
||||
import CryptoJS from 'crypto-js'
|
||||
import {Base64} from 'js-base64'
|
||||
import {i18n} from '@/locale'
|
||||
const baseUrl = import.meta.env.VITE_SERVER_BASEURL
|
||||
|
||||
// === 👉 请在这里填写你的 AWS 信息 ===
|
||||
const S3Config = {
|
||||
bucket: 'cheflink', // 你的存储桶名称
|
||||
region: 'us-east-1', // 桶所在区域
|
||||
accessKeyId: 'AKIA3LKQVMAPD3KFPJGS',
|
||||
secretKey: 'tV7yvBESDPemtiHGlFOg/oFxz3L1BNXu8KutKjZS',
|
||||
timeout: 0.1 // 签名有效时间(小时)
|
||||
}
|
||||
const UPLOAD_ENDPOINT = '/resource/oss/upload'
|
||||
const CLIENT_ID = 'e5cd7e4891bf95d1d19206ce24a7b32e'
|
||||
|
||||
// === 签名函数 ===
|
||||
function getPolicyBase64(): string {
|
||||
const expiration = new Date(Date.now() + 10 * 60 * 1000).toISOString() // 10分钟有效
|
||||
const policyText = {
|
||||
expiration,
|
||||
conditions: [
|
||||
{bucket: S3Config.bucket},
|
||||
['starts-with', '$key', 'cheflink/'],
|
||||
['starts-with', '$Content-Type', 'image/']
|
||||
]
|
||||
|
||||
function parseUploadResponse(data: any): string {
|
||||
const parsed = typeof data === 'string' ? JSON.parse(data || '{}') : (data || {})
|
||||
if (parsed.code && parsed.code !== 200) {
|
||||
throw new Error(parsed.msg || 'upload failed')
|
||||
}
|
||||
return Base64.encode(JSON.stringify(policyText))
|
||||
}
|
||||
|
||||
|
||||
function getSignature(policyBase64: string): string {
|
||||
const bytes = CryptoJS.HmacSHA1(policyBase64, S3Config.secretKey)
|
||||
return CryptoJS.enc.Base64.stringify(bytes)
|
||||
return parsed?.data?.url || parsed?.data || parsed?.url || ''
|
||||
}
|
||||
|
||||
// === 图片压缩函数 ===
|
||||
@@ -89,7 +71,7 @@ function compressImageIfNeeded(filePath: string, size: number): Promise<string>
|
||||
})
|
||||
}
|
||||
|
||||
// === S3 上传函数 ===
|
||||
// 上传文件到后端接口
|
||||
const uploadToS3 = async function (filePath: string, suffix = '.jpg', dir = 'cheflink/'): Promise<string> {
|
||||
if (!filePath) {
|
||||
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.picture-wrong-please-try-again')})
|
||||
@@ -120,32 +102,33 @@ const uploadToS3 = async function (filePath: string, suffix = '.jpg', dir = 'che
|
||||
return Promise.reject(err)
|
||||
}
|
||||
|
||||
// 文件名
|
||||
const filename = Date.now() + Math.floor(Math.random() * 1000) + suffix
|
||||
const key = dir + filename
|
||||
const policyBase64 = getPolicyBase64()
|
||||
const signature = getSignature(policyBase64)
|
||||
const uploadUrl = `https://${S3Config.bucket}.s3.${S3Config.region}.amazonaws.com/`
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
url: uploadUrl,
|
||||
url:baseUrl+ UPLOAD_ENDPOINT,
|
||||
filePath: uploadPath,
|
||||
name: 'file',
|
||||
formData: {
|
||||
key,
|
||||
AWSAccessKeyId: S3Config.accessKeyId,
|
||||
policy: policyBase64,
|
||||
signature,
|
||||
'Content-Type': 'image/jpeg',
|
||||
header: {
|
||||
clientid: CLIENT_ID,
|
||||
},
|
||||
formData: {},
|
||||
success: res => {
|
||||
if (res.statusCode === 204) {
|
||||
uni.showToast({icon: 'none', title: i18n.global.t('common.operation-success')})
|
||||
resolve(`${uploadUrl}${key}`)
|
||||
} else {
|
||||
if (res.statusCode !== 200) {
|
||||
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.up-failed')})
|
||||
reject(new Error(`上传失败,状态码: ${res.statusCode}`))
|
||||
return
|
||||
}
|
||||
try {
|
||||
const uploadedUrl = parseUploadResponse(res.data)
|
||||
if (!uploadedUrl) {
|
||||
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.up-failed')})
|
||||
reject(new Error('上传失败,返回地址为空'))
|
||||
return
|
||||
}
|
||||
uni.showToast({icon: 'none', title: i18n.global.t('common.operation-success')})
|
||||
resolve(uploadedUrl)
|
||||
} catch (error) {
|
||||
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.up-failed')})
|
||||
reject(error)
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
|
||||
Reference in New Issue
Block a user