fix(api): 替换所有接口请求的域名为 guilixu-api.websoft.top
- 将获取 STS Token 的接口地址由 paopao-api 改为 guilixu-api - 更新图片上传接口地址为新的 guilixu-api 域名 - 修改用户推广页面中邀请码链接和二维码接口的域名 - 更改注册页微信登录接口请求的域名为 guilixu-api
This commit is contained in:
99
src/api/system/file/index.ts
Normal file
99
src/api/system/file/index.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
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('选择图片失败'))
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user