- 新增地址类型定义,增强前端地址数据结构 - 新增地址编辑页面,支持地址智能识别和定位选点功能 - 地址编辑支持省市区选择及默认地址设置 - 新增地址列表页面,支持地址展示、删除、编辑和选择功能 - 实现售后申请页面,支持选择售后类型和退款原因 - 售后申请支持商品选择、退款金额计算和凭证上传 - 新增售后详情页面,支持售后状态展示及申请取消 - 优化页面加载和用户交互体验,增加错误提示和权限处理
100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
import request from '@/utils/request';
|
|
import Taro from '@tarojs/taro'
|
|
import dayjs from 'dayjs';
|
|
import crypto from 'crypto-js';
|
|
import {Base64} from 'js-base64';
|
|
import {FileRecord} from "@/api/system/file/model";
|
|
import {TenantId} from "@/config/app";
|
|
|
|
export async function uploadOssByPath(filePath: string) {
|
|
return new Promise(async (resolve) => {
|
|
const date = new Date();
|
|
date.setMinutes(date.getMinutes() + 1);
|
|
const policyText = {
|
|
expiration: date.toISOString(), // 设置policy过期时间。
|
|
conditions: [
|
|
// 限制上传大小。
|
|
["content-length-range", 0, 1024 * 1024 * 1024],
|
|
],
|
|
};
|
|
let sts = Taro.getStorageSync('sts');
|
|
const stsExpired = Taro.getStorageSync('stsExpiredAt');
|
|
if (!sts || (stsExpired && dayjs().isBefore(dayjs(stsExpired)))) {
|
|
// @ts-ignore
|
|
const {data: {data: {credentials}}} = await request.get(`https://shop-api.websoft.top/api/oss/getSTSToken`)
|
|
Taro.setStorageSync('sts', credentials)
|
|
Taro.setStorageSync('stsExpiredAt', credentials.expiration)
|
|
sts = credentials
|
|
}
|
|
|
|
const [filename, ext] = filePath.split('.')
|
|
const key = `headers/${filename + (Math.random() * 100000)}.${ext}`
|
|
const policy = Base64.encode(JSON.stringify(policyText))
|
|
const signature = computeSignature(sts.accessKeySecret, policy)
|
|
Taro.uploadFile({
|
|
url: 'https://oss.wsdns.cn',
|
|
name: 'file',
|
|
filePath,
|
|
formData: {
|
|
key,
|
|
policy,
|
|
OSSAccessKeyId: sts.accessKeyId,
|
|
signature,
|
|
'x-oss-security-token': sts.securityToken
|
|
},
|
|
success: () => {
|
|
resolve(`https://oss.wsdns.cn/${key}`)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
const computeSignature = (accessKeySecret: string, canonicalString: string): string => {
|
|
return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
|
|
}
|
|
|
|
/**
|
|
* 上传阿里云OSS
|
|
*/
|
|
export async function uploadFile() {
|
|
return new Promise(async (resolve: (result: FileRecord) => void, reject) => {
|
|
Taro.chooseImage({
|
|
count: 1,
|
|
sizeType: ['compressed'],
|
|
sourceType: ['album', 'camera'],
|
|
success: async (res) => {
|
|
const tempFilePath = res.tempFilePaths[0];
|
|
// 上传图片到OSS
|
|
Taro.uploadFile({
|
|
url: 'https://shop-api.websoft.top/api/oss/upload',
|
|
filePath: tempFilePath,
|
|
name: 'file',
|
|
header: {
|
|
'content-type': 'application/json',
|
|
TenantId
|
|
},
|
|
success: (res) => {
|
|
try {
|
|
const data = JSON.parse(res.data);
|
|
if (data.code === 0) {
|
|
resolve(data.data)
|
|
} else {
|
|
reject(new Error(data.message || '上传失败'))
|
|
}
|
|
} catch (error) {
|
|
reject(new Error('解析响应数据失败'))
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(new Error('上传请求失败'))
|
|
}
|
|
})
|
|
},
|
|
fail: (err) => {
|
|
reject(new Error('选择图片失败'))
|
|
}
|
|
});
|
|
})
|
|
|
|
}
|