服务器更替
This commit is contained in:
Vendored
+2
-2
@@ -4,7 +4,7 @@ NODE_ENV=development
|
|||||||
VITE_DELETE_CONSOLE=false
|
VITE_DELETE_CONSOLE=false
|
||||||
|
|
||||||
#本地环境
|
#本地环境
|
||||||
#VITE_SERVER_BASEURL=https://howhowfresh.com/prod-api
|
VITE_SERVER_BASEURL=https://howhowfresh.com/prod-api
|
||||||
VITE_SERVER_BASEURL=http://192.168.5.58:8080
|
#VITE_SERVER_BASEURL=http://192.168.5.200:8080
|
||||||
#VITE_SERVER_BASEURL=http://192.168.0.148:8888
|
#VITE_SERVER_BASEURL=http://192.168.0.148:8888
|
||||||
#VITE_SERVER_BASEURL=http://liuyao.nat100.top/meiguowaimai
|
#VITE_SERVER_BASEURL=http://liuyao.nat100.top/meiguowaimai
|
||||||
Vendored
+1
-1
@@ -7,4 +7,4 @@ VITE_DELETE_CONSOLE=true
|
|||||||
#正式环境
|
#正式环境
|
||||||
#VITE_SERVER_BASEURL=http://liuyao.nat100.top/meiguowaimai
|
#VITE_SERVER_BASEURL=http://liuyao.nat100.top/meiguowaimai
|
||||||
#VITE_SERVER_BASEURL=https://gosplit.co.nz/apimgwm
|
#VITE_SERVER_BASEURL=https://gosplit.co.nz/apimgwm
|
||||||
VITE_SERVER_BASEURL=https://howhowfresh.com/prod-api
|
VITE_SERVER_BASEURL=https://www.howhowfresh.com/prod-api
|
||||||
|
|||||||
@@ -43,8 +43,9 @@ const httpInterceptor = {
|
|||||||
// #endif
|
// #endif
|
||||||
// TIPS: 如果需要对接多个后端服务,也可以在这里处理,拼接成所需要的地址
|
// TIPS: 如果需要对接多个后端服务,也可以在这里处理,拼接成所需要的地址
|
||||||
}
|
}
|
||||||
// 1. 请求超时
|
// 1. 请求超时:上传接口放宽到 60s,其他接口保持 10s
|
||||||
options.timeout = 10000 // 10s
|
const isUploadRequest = !!(options as any).filePath
|
||||||
|
options.timeout = isUploadRequest ? 60000 : 10000
|
||||||
|
|
||||||
// 谷歌地图的请求不额外增加请求头
|
// 谷歌地图的请求不额外增加请求头
|
||||||
if (!options.url.startsWith('https://maps.googleapis.com')) {
|
if (!options.url.startsWith('https://maps.googleapis.com')) {
|
||||||
|
|||||||
@@ -69,6 +69,7 @@
|
|||||||
"request-incorrect": "Request failed",
|
"request-incorrect": "Request failed",
|
||||||
"save-failed": "Save failed",
|
"save-failed": "Save failed",
|
||||||
"save-successfully": "Save successfully",
|
"save-successfully": "Save successfully",
|
||||||
|
"soldOut": "This item is sold out",
|
||||||
"system-prompt": "System prompts",
|
"system-prompt": "System prompts",
|
||||||
"system-prompt-delete": "Are you sure to delete it?",
|
"system-prompt-delete": "Are you sure to delete it?",
|
||||||
"up-cross": "Loading...",
|
"up-cross": "Loading...",
|
||||||
|
|||||||
@@ -69,6 +69,7 @@
|
|||||||
"request-incorrect": "请求失败",
|
"request-incorrect": "请求失败",
|
||||||
"save-failed": "保存失败",
|
"save-failed": "保存失败",
|
||||||
"save-successfully": "保存成功",
|
"save-successfully": "保存成功",
|
||||||
|
"soldOut": "该商品已售完",
|
||||||
"system-prompt": "系统提示",
|
"system-prompt": "系统提示",
|
||||||
"system-prompt-delete": "是否确认删除?",
|
"system-prompt-delete": "是否确认删除?",
|
||||||
"up-cross": "上传中...",
|
"up-cross": "上传中...",
|
||||||
|
|||||||
+2
-2
@@ -2,8 +2,8 @@
|
|||||||
"name" : "CHEFLINK delivery",
|
"name" : "CHEFLINK delivery",
|
||||||
"appid" : "__UNI__06509BE",
|
"appid" : "__UNI__06509BE",
|
||||||
"description" : "",
|
"description" : "",
|
||||||
"versionName" : "1.0.28",
|
"versionName" : "1.0.29",
|
||||||
"versionCode" : 128,
|
"versionCode" : 129,
|
||||||
"transformPx" : false,
|
"transformPx" : false,
|
||||||
/* 5+App特有相关 */
|
/* 5+App特有相关 */
|
||||||
"app-plus" : {
|
"app-plus" : {
|
||||||
|
|||||||
@@ -24,6 +24,18 @@ const userStore = useUserStore();
|
|||||||
const loading = ref(true); // 加载状态
|
const loading = ref(true); // 加载状态
|
||||||
const selectedSauce = ref([]); // 默认选择第一个
|
const selectedSauce = ref([]); // 默认选择第一个
|
||||||
|
|
||||||
|
function isSoldOutStock(stockLike: unknown) {
|
||||||
|
const n = Number(stockLike)
|
||||||
|
return !Number.isNaN(n) && n <= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function showSoldOutToast() {
|
||||||
|
uni.showToast({
|
||||||
|
title: t('common.prompt.soldOut'),
|
||||||
|
icon: 'none'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const selectSauce = (item, id: string) => {
|
const selectSauce = (item, id: string) => {
|
||||||
if(!item.selectedIds) {
|
if(!item.selectedIds) {
|
||||||
item.selectedIds = [];
|
item.selectedIds = [];
|
||||||
@@ -40,13 +52,8 @@ const selectSauce = (item, id: string) => {
|
|||||||
function handleSubmit() {
|
function handleSubmit() {
|
||||||
console.log('加入购物车', dishDetailData.value)
|
console.log('加入购物车', dishDetailData.value)
|
||||||
console.log('加入购物车', dishDetailData.value.merchantSideDishVoList)
|
console.log('加入购物车', dishDetailData.value.merchantSideDishVoList)
|
||||||
const stock = Number(dishDetailData.value?.stock)
|
if (isSoldOutStock(dishDetailData.value?.stock)) {
|
||||||
|
showSoldOutToast()
|
||||||
if (!Number.isNaN(stock) && stock === 0) {
|
|
||||||
uni.showToast({
|
|
||||||
title: t('common.prompt.stockInsufficient'),
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 查看所有必选的配菜是否已经选择了
|
// 查看所有必选的配菜是否已经选择了
|
||||||
@@ -192,6 +199,8 @@ const totalPrice = computed(() => {
|
|||||||
return (basePrice + sidePrice).toFixed(2);
|
return (basePrice + sidePrice).toFixed(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isSoldOut = computed(() => isSoldOutStock(dishDetailData.value?.stock))
|
||||||
|
|
||||||
const cartDataList = ref<MerchantCartVo[]>([])
|
const cartDataList = ref<MerchantCartVo[]>([])
|
||||||
function getCartInfo() {
|
function getCartInfo() {
|
||||||
if(!userStore.isLogin) return
|
if(!userStore.isLogin) return
|
||||||
@@ -313,8 +322,8 @@ function getStoreDetail() {
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="text-#333 text-30rpx font-500">
|
<view class="text-#333 text-30rpx font-500">
|
||||||
<template v-if="dishDetailData?.stock">
|
<template v-if="dishDetailData?.stock !== undefined && dishDetailData?.stock !== null">
|
||||||
{{ t('common.stock') }}: {{ Number(dishDetailData?.stock).toFixed(0) }}
|
{{ t('common.stock') }}: {{ Number(dishDetailData?.stock || 0).toFixed(0) }}
|
||||||
</template>
|
</template>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
@@ -378,12 +387,15 @@ function getStoreDetail() {
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="!fixed bottom-0 left-0 right-0 px-30rpx">
|
<view class="!fixed bottom-0 left-0 right-0 px-30rpx">
|
||||||
<wd-button
|
<view @click="isSoldOut ? showSoldOutToast() : undefined">
|
||||||
custom-class="!h-98rpx !text-30rpx !text-#fff !lh-98rpx !rounded-16rpx"
|
<wd-button
|
||||||
block
|
custom-class="!h-98rpx !text-30rpx !text-#fff !lh-98rpx !rounded-16rpx"
|
||||||
@click="addCart"
|
block
|
||||||
>{{ t('pages-store.store.addToCart') }} • US${{ totalPrice }}
|
:disabled="isSoldOut"
|
||||||
</wd-button>
|
@click="addCart"
|
||||||
|
>{{ t('pages-store.store.addToCart') }} • US${{ totalPrice }}
|
||||||
|
</wd-button>
|
||||||
|
</view>
|
||||||
<view :style="[configStore.iosSafeBottomPlaceholder]"></view>
|
<view :style="[configStore.iosSafeBottomPlaceholder]"></view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|||||||
@@ -31,15 +31,24 @@ watch(
|
|||||||
)
|
)
|
||||||
|
|
||||||
async function handleCropperAvatarSuccess(avatarUrl: string) {
|
async function handleCropperAvatarSuccess(avatarUrl: string) {
|
||||||
|
if (!avatarUrl) {
|
||||||
|
uni.showToast({ title: t('common.prompt.picture-wrong-please-try-again'), icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await uni.showLoading({
|
uni.showLoading({
|
||||||
title: t('common.prompt.up-cross'),
|
title: t('common.prompt.up-cross'),
|
||||||
mask: true,
|
mask: true,
|
||||||
})
|
})
|
||||||
const res = await upload(avatarUrl, '.png', 'app/avatar/')
|
const res = await upload(avatarUrl)
|
||||||
console.log('111111', res)
|
form.value.avatar = String(res || '')
|
||||||
form.value.avatar = res as string
|
} catch (error: any) {
|
||||||
}finally {
|
console.log('avatar upload error', error)
|
||||||
|
uni.showToast({
|
||||||
|
title: error?.message || t('common.prompt.up-failed'),
|
||||||
|
icon: 'none',
|
||||||
|
})
|
||||||
|
} finally {
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ const { t } = useI18n();
|
|||||||
|
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
|
|
||||||
|
function isSoldOutStock(stockLike: unknown) {
|
||||||
|
const n = Number(stockLike)
|
||||||
|
return !Number.isNaN(n) && n <= 0
|
||||||
|
}
|
||||||
|
|
||||||
function navigateTo(url: string) {
|
function navigateTo(url: string) {
|
||||||
if(userStore.checkLogin()) {
|
if(userStore.checkLogin()) {
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
@@ -373,6 +378,12 @@ const debouncedEmit = debounce(1300, (isCollected: boolean, id: string, type: Co
|
|||||||
class="w-full h-full"
|
class="w-full h-full"
|
||||||
/>
|
/>
|
||||||
</view>
|
</view>
|
||||||
|
<view
|
||||||
|
v-if="isSoldOutStock(item?.stock)"
|
||||||
|
class="absolute z-2 left-20rpx top-20rpx px-16rpx h-52rpx rounded-26rpx bg-#14181B/70 center text-26rpx text-#fff font-500"
|
||||||
|
>
|
||||||
|
已售完
|
||||||
|
</view>
|
||||||
<image
|
<image
|
||||||
:src="item?.dishImage?.split(',')[0]"
|
:src="item?.dishImage?.split(',')[0]"
|
||||||
mode="aspectFill"
|
mode="aspectFill"
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ const poster = computed(() => ({
|
|||||||
type: 'view',
|
type: 'view',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: Config.shareLink + `pages-login/pages/sign-up/index?invitationCode=${userStore.userInfo.invitationCode}`,
|
text: 'http://192.168.5.118:8011/h5/'+ `pages-login/pages/sign-up/index?invitationCode=${userStore.userInfo.invitationCode}`,
|
||||||
type: 'qrcode',
|
type: 'qrcode',
|
||||||
css: {
|
css: {
|
||||||
width: '124rpx',
|
width: '124rpx',
|
||||||
@@ -213,6 +213,8 @@ const poster = computed(() => ({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
//Config.shareLink
|
||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
onAppSaveToPhone()
|
onAppSaveToPhone()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ defineExpose({
|
|||||||
</view>
|
</view>
|
||||||
<view class="shrink-0 ml-20rpx" v-if="userStore.userInfo.invitationCode">
|
<view class="shrink-0 ml-20rpx" v-if="userStore.userInfo.invitationCode">
|
||||||
<uqrcode ref="uqrcode" canvas-id="qrcode"
|
<uqrcode ref="uqrcode" canvas-id="qrcode"
|
||||||
:value="Config.shareLink + `pages-login/pages/sign-up/index?invitationCode=${userStore.userInfo.invitationCode}`"
|
:value="'http://192.168.5.118:8011/h5/' + `pages-login/pages/sign-up/index?invitationCode=${userStore.userInfo.invitationCode}`"
|
||||||
:size="124"
|
:size="124"
|
||||||
sizeUnit="rpx"
|
sizeUnit="rpx"
|
||||||
:options="{}"></uqrcode>
|
:options="{}"></uqrcode>
|
||||||
@@ -91,6 +91,8 @@ defineExpose({
|
|||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
|
<!-- Config.shareLink -->
|
||||||
<view class="mt-34rpx flex justify-between items-center ">
|
<view class="mt-34rpx flex justify-between items-center ">
|
||||||
<wd-button
|
<wd-button
|
||||||
custom-class="!box-border shrink-0 !min-w-auto !h-98rpx !w-316rpx !text-#333 !text-30rpx !lh-42rpx !rounded-16rpx !bg-#fff"
|
custom-class="!box-border shrink-0 !min-w-auto !h-98rpx !w-316rpx !text-#333 !text-30rpx !lh-42rpx !rounded-16rpx !bg-#fff"
|
||||||
|
|||||||
+30
-41
@@ -1,65 +1,54 @@
|
|||||||
import Crypto from 'crypto-js'
|
|
||||||
import {Base64} from 'js-base64'
|
|
||||||
import {i18n} from "@/locale";
|
import {i18n} from "@/locale";
|
||||||
|
import {useUserStore} from '@/store'
|
||||||
|
|
||||||
const env = {
|
const baseUrl = import.meta.env.VITE_SERVER_BASEURL
|
||||||
uploadImageUrl: 'https://wendy123.oss-ap-southeast-1.aliyuncs.com/', // 默认存在根目录,可根据需求改
|
|
||||||
accessKeySecret: 'TSZOD1jULKPFsNp2zKhAopLe3c3AiH', // accessKeySecret 去你的阿里云上控制台上找
|
function getClientId() {
|
||||||
ossAccessKeyId: 'LTAI5tQq1bSBFCfsbvwX6DqQ', // AccessKeyId 去你的阿里云上控制台上找
|
return 'e5cd7e4891bf95d1d19206ce24a7b32e'
|
||||||
timeout: 87600, // 这个是上传文件时Policy的失效时间
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getPolicyBase64 = function (): string {
|
function parseUploadResult(rawData: any): string {
|
||||||
const date = new Date()
|
const data = typeof rawData === 'string' ? JSON.parse(rawData) : rawData
|
||||||
date.setHours(date.getHours() + env.timeout)
|
if (!data || data.code !== 200) {
|
||||||
const expirationTime = date.toISOString()
|
throw new Error(data?.msg || '上传失败')
|
||||||
const policyText = {
|
|
||||||
expiration: expirationTime, // 设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
|
|
||||||
conditions: [
|
|
||||||
['content-length-range', 0, 2000 * 1024 * 1024], // 设置上传文件的大小限制,2G
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
|
return (
|
||||||
return Base64.encode(JSON.stringify(policyText))
|
data?.data?.url ||
|
||||||
|
data?.data?.fullUrl ||
|
||||||
|
data?.data?.fileUrl ||
|
||||||
|
data?.data?.link ||
|
||||||
|
data?.data
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const getSignature = function (policyBase64: string): string {
|
// 上传图片(走后端接口 /resource/oss/upload)
|
||||||
const bytes = Crypto.HmacSHA1(policyBase64, env.accessKeySecret)
|
|
||||||
return Crypto.enc.Base64.stringify(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上传图片到阿里云oss
|
|
||||||
const upload = function (filePath: string, suffix = '.png', dir = 'app/image/') {
|
const upload = function (filePath: string, suffix = '.png', dir = 'app/image/') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
return uni.showToast({icon: "none", title: i18n.global.t('common.prompt.picture-wrong-please-try-again')})
|
return uni.showToast({icon: "none", title: i18n.global.t('common.prompt.picture-wrong-please-try-again')})
|
||||||
}
|
}
|
||||||
|
const userStore = useUserStore()
|
||||||
// 图片名字 可以自行定义,这里是采用当前的时间戳 + 150内的随机数来给图片命名的
|
const token = userStore.token
|
||||||
const fileKey = dir + new Date().getTime() + Math.floor(Math.random() * 150) + suffix
|
const clientId = getClientId()
|
||||||
|
|
||||||
const aliyunServerURL = env.uploadImageUrl // OSS地址,需要https
|
|
||||||
const policyBase64 = getPolicyBase64()
|
|
||||||
const signature = getSignature(policyBase64) // 获取签名
|
|
||||||
|
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: aliyunServerURL, // 开发者服务器 url
|
url: baseUrl + '/resource/oss/upload',
|
||||||
filePath: filePath, // 要上传文件资源的路径
|
filePath: filePath, // 要上传文件资源的路径
|
||||||
name: 'file', // 必须填file
|
name: 'file', // 必须填file
|
||||||
formData: {
|
header: {
|
||||||
key: fileKey,
|
Authorization: token ? `Bearer ${token}` : '',
|
||||||
policy: policyBase64,
|
clientid: clientId,
|
||||||
OSSAccessKeyId: env.ossAccessKeyId,
|
|
||||||
signature: signature,
|
|
||||||
success_action_status: '200',
|
|
||||||
},
|
},
|
||||||
success: function (res) {
|
success: function (res) {
|
||||||
console.log(res)
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||||
if (res.statusCode !== 200) {
|
|
||||||
reject(new Error('upload error:' + JSON.stringify(res)))
|
reject(new Error('upload error:' + JSON.stringify(res)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resolve(aliyunServerURL + fileKey)
|
try {
|
||||||
|
resolve(parseUploadResult(res.data))
|
||||||
|
} catch (error) {
|
||||||
|
reject(error)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
fail: function (err) {
|
fail: function (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
|
|||||||
+46
-50
@@ -1,35 +1,5 @@
|
|||||||
import CryptoJS from 'crypto-js'
|
|
||||||
import {Base64} from 'js-base64'
|
|
||||||
import {i18n} from '@/locale'
|
import {i18n} from '@/locale'
|
||||||
|
import {useUserStore} from '@/store'
|
||||||
// === 👉 请在这里填写你的 AWS 信息 ===
|
|
||||||
const S3Config = {
|
|
||||||
bucket: 'cheflink', // 你的存储桶名称
|
|
||||||
region: 'us-east-1', // 桶所在区域
|
|
||||||
accessKeyId: 'AKIA3LKQVMAPD3KFPJGS',
|
|
||||||
secretKey: 'tV7yvBESDPemtiHGlFOg/oFxz3L1BNXu8KutKjZS',
|
|
||||||
timeout: 0.1 // 签名有效时间(小时)
|
|
||||||
}
|
|
||||||
|
|
||||||
// === 签名函数 ===
|
|
||||||
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/']
|
|
||||||
]
|
|
||||||
}
|
|
||||||
return Base64.encode(JSON.stringify(policyText))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function getSignature(policyBase64: string): string {
|
|
||||||
const bytes = CryptoJS.HmacSHA1(policyBase64, S3Config.secretKey)
|
|
||||||
return CryptoJS.enc.Base64.stringify(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// === 图片压缩函数 ===
|
// === 图片压缩函数 ===
|
||||||
function compressImageIfNeeded(filePath: string, size: number): Promise<string> {
|
function compressImageIfNeeded(filePath: string, size: number): Promise<string> {
|
||||||
@@ -89,8 +59,34 @@ function compressImageIfNeeded(filePath: string, size: number): Promise<string>
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// === S3 上传函数 ===
|
function getClientId() {
|
||||||
const uploadToS3 = async function (filePath: string, suffix = '.jpg', dir = 'cheflink/'): Promise<string> {
|
return 'e5cd7e4891bf95d1d19206ce24a7b32e'
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseUploadResult(rawData: any): string {
|
||||||
|
const data = typeof rawData === 'string' ? JSON.parse(rawData) : rawData
|
||||||
|
// 兼容不同后端返回格式:有的返回 { code: 200, data: ... },有的直接返回 { url: ... }
|
||||||
|
if (data?.code !== undefined && String(data.code) !== '200') {
|
||||||
|
throw new Error(data?.msg || '上传失败')
|
||||||
|
}
|
||||||
|
const result = (
|
||||||
|
data?.data?.url ||
|
||||||
|
data?.data?.fullUrl ||
|
||||||
|
data?.data?.fileUrl ||
|
||||||
|
data?.data?.link ||
|
||||||
|
data?.url ||
|
||||||
|
data?.fileUrl ||
|
||||||
|
data?.link ||
|
||||||
|
data?.data
|
||||||
|
)
|
||||||
|
if (!result || typeof result !== 'string') {
|
||||||
|
throw new Error(data?.msg || '上传成功但未返回文件地址')
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// 走后端上传接口:/resource/oss/upload
|
||||||
|
const uploadToS3 = async function (filePath: string): Promise<string> {
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.picture-wrong-please-try-again')})
|
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.picture-wrong-please-try-again')})
|
||||||
return Promise.reject(new Error('filePath is empty'))
|
return Promise.reject(new Error('filePath is empty'))
|
||||||
@@ -120,32 +116,32 @@ const uploadToS3 = async function (filePath: string, suffix = '.jpg', dir = 'che
|
|||||||
return Promise.reject(err)
|
return Promise.reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 文件名
|
const userStore = useUserStore()
|
||||||
const filename = Date.now() + Math.floor(Math.random() * 1000) + suffix
|
const token = userStore.token
|
||||||
const key = dir + filename
|
const clientId = getClientId()
|
||||||
const policyBase64 = getPolicyBase64()
|
|
||||||
const signature = getSignature(policyBase64)
|
|
||||||
const uploadUrl = `https://${S3Config.bucket}.s3.${S3Config.region}.amazonaws.com/`
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: uploadUrl,
|
url: '/resource/oss/upload',
|
||||||
filePath: uploadPath,
|
filePath: uploadPath,
|
||||||
name: 'file',
|
name: 'file',
|
||||||
formData: {
|
header: {
|
||||||
key,
|
Authorization: token ? `Bearer ${token}` : '',
|
||||||
AWSAccessKeyId: S3Config.accessKeyId,
|
clientid: clientId,
|
||||||
policy: policyBase64,
|
|
||||||
signature,
|
|
||||||
'Content-Type': 'image/jpeg',
|
|
||||||
},
|
},
|
||||||
success: res => {
|
success: res => {
|
||||||
if (res.statusCode === 204) {
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||||
uni.showToast({icon: 'none', title: i18n.global.t('common.operation-success')})
|
|
||||||
resolve(`${uploadUrl}${key}`)
|
|
||||||
} else {
|
|
||||||
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.up-failed')})
|
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.up-failed')})
|
||||||
reject(new Error(`上传失败,状态码: ${res.statusCode}`))
|
reject(new Error(`上传失败,状态码: ${res.statusCode}`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const fileUrl = parseUploadResult(res.data)
|
||||||
|
uni.showToast({icon: 'none', title: i18n.global.t('common.operation-success')})
|
||||||
|
resolve(fileUrl)
|
||||||
|
} catch (error) {
|
||||||
|
uni.showToast({icon: 'none', title: i18n.global.t('common.prompt.up-failed')})
|
||||||
|
reject(error)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: err => {
|
fail: err => {
|
||||||
|
|||||||
Reference in New Issue
Block a user