更新COS

This commit is contained in:
xyc 2025-04-25 13:01:19 +08:00
parent 12841688af
commit b5a9bdbac1
6 changed files with 11205 additions and 107 deletions

View File

@ -1,5 +1,6 @@
{
"dependencies": {
"cos-js-sdk-v5": "^1.8.7",
"html2canvas": "^1.4.1",
"rich-text-parser": "^1.0.2"
},

View File

@ -80,11 +80,11 @@
limit="9"></uni-file-picker>
</view>
</view>
<!-- 提示窗示例 -->
<uni-popup ref="alertDialog" type="dialog">
<uni-popup-dialog cancelText="取消" confirmText="创建" iconPath="/static/detail/card.png"
content="没有可选择的收货地址,请先创建" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
</uni-popup>
<!-- 提示窗示例 -->
<uni-popup ref="alertDialog" type="dialog">
<uni-popup-dialog cancelText="取消" confirmText="创建" iconPath="/static/detail/card.png"
content="没有可选择的收货地址,请先创建" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
</uni-popup>
<view class="item-field" style="align-items: center;">
<view class="submit-box" @click="submitForm">提交</view>
</view>
@ -113,14 +113,14 @@
},
data() {
return {
uploadUrl: config.baseUrl+'/common/upload',
headers: {},
uploadUrl: config.baseUrl + '/common/upload',
headers: {},
//
range: [],
sizeType: ['compressed'],
//
fileList: [],
imageUrl:[],
imageUrl: [],
//
addressList: [],
//
@ -146,33 +146,33 @@
},
methods: {
dialogConfirm(){
this.$tab.navigateTo('/pages/mine/addr/addr-list')
},
dialogConfirm() {
this.$tab.navigateTo('/pages/mine/addr/addr-list')
},
dialogClose(){
dialogClose() {
},
},
/**初始化数据*/
initData(id) {
this.fileList = []
this.imageUrl = []
this.fileList = []
this.imageUrl = []
if (id) {
getBusiCardById({
id: id
}).then(res => {
console.log(res.data, 133)
this.dataObj = res.data
if (this.dataObj.image != null && this.dataObj.image != ''){
console.log(this.dataObj.image,'this.dataObj.image')
this.imageUrl = this.dataObj.image.split(',')
this.imageUrl.map((item)=>{
this.fileList.push({
url:config.baseUrl + item
})
})
}
if (this.dataObj.image != null && this.dataObj.image != '') {
console.log(this.dataObj.image, 'this.dataObj.image')
this.imageUrl = this.dataObj.image.split(',')
this.imageUrl.map((item) => {
this.fileList.push({
url: item
})
})
}
}).catch((e) => {
uni.showToast({
icon: 'error',
@ -213,9 +213,9 @@
initAddress() {
uniSelectList().then(res => {
this.addressList = res.data
if (this.addressList.length == 0){
this.$refs.alertDialog.open()
}
if (this.addressList.length == 0) {
this.$refs.alertDialog.open()
}
}).catch((e) => {
uni.showToast({
icon: 'error',
@ -259,11 +259,11 @@
toast("收货地址不能为空")
return
}
if (this.fileList.length == 0) {
toast("证明材料不能为空")
return
}
this.dataObj.image = this.imageUrl.join(',');
if (this.fileList.length == 0) {
toast("证明材料不能为空")
return
}
this.dataObj.image = this.imageUrl.join(',');
applyBusiCard(this.dataObj).then(res => {
if (res.code == 200) {
uni.showToast({
@ -288,10 +288,11 @@
url: '',
filePath: file.tempFilePaths[i]
}).then((res) => {
console.log('上传图片', res);
this.fileList.push({
url: res.url
})
this.imageUrl.push(res.fileName)
this.imageUrl.push(res.fileName)
})
}
},
@ -300,12 +301,12 @@
this.fileList.splice(index, 1);
this.imageUrl.splice(index, 1);
},
uploadSuccess(res, file) {
console.log('上传成功', res);
},
uploadFail(error, file) {
console.log('上传失败', error);
},
uploadSuccess(res, file) {
console.log('上传成功', res);
},
uploadFail(error, file) {
console.log('上传失败', error);
},
}
}
</script>

View File

@ -253,7 +253,7 @@
viewMy: false,
//ID
noticeId: null,
imageUrl: config.baseUrl,
imageUrl: '',
//--
showBottom: false,
//

11083
utils/cos-wx-sdk-v5.js Normal file

File diff suppressed because one or more lines are too long

42
utils/cos.js Normal file
View File

@ -0,0 +1,42 @@
import config from '@/config'
const COS = require('./cos-wx-sdk-v5.js'); // 开发时使用
// const COS = require('./lib/cos-wx-sdk-v5.min.js'); // 上线时使用压缩包
const baseUrl = config.baseUrl
const cos = new COS({
SimpleUploadMethod: 'putObject', // 强烈建议高级上传、批量上传内部对小文件做简单上传时使用putObject,sdk版本至少需要v1.3.0
getAuthorization: function(options, callback) {
// 初始化时不会调用,只有调用 cos 方法(例如 cos.putObject时才会进入
// 异步获取临时密钥
// 服务端 JS 示例https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
// 服务端其他语言参考 COS STS SDK https://github.com/tencentyun/qcloud-cos-sts-sdk
// STS 详细文档指引看https://cloud.tencent.com/document/product/436/14048
const stsUrl = baseUrl + '/cos/sts'; // stsUrl 替换成您自己的后端服务
wx.request({
url: stsUrl,
data: {
bucket: options.Bucket,
region: options.Region,
},
dataType: 'json',
success: function(result) {
const data = result.data;
const credentials = data && data.credentials;
if (!data || !credentials) return console.error('credentials invalid');
// 检查 credentials 格式
console.log(credentials);
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
// v1.2.0之前版本的 SDK 使用 XCosSecurityToken 而不是 SecurityToken
SecurityToken: credentials.sessionToken,
// 建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
StartTime: data.startTime, // 时间戳单位秒1580000000
ExpiredTime: data.expiredTime, // 时间戳单位秒1580000900
});
}
});
}
});
export default cos;

View File

@ -1,70 +1,41 @@
import store from '@/store'
import config from '@/config'
import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { toast, showConfirm, tansParams } from '@/utils/common'
import cos from '@/utils/cos' // 您已有的COS实例
import {
getToken
} from '@/utils/auth'
import {
toast
} from '@/utils/common'
let timeout = 10000
const baseUrl = config.baseUrl
const upload = config => {
// 是否需要设置 token
const isToken = (config.headers || {}).isToken === false
config.header = config.header || {}
if (getToken() && !isToken) {
config.header['Authorization'] = 'Bearer ' + getToken()
}
// get请求映射params参数
if (config.params) {
let url = config.url + '?' + tansParams(config.params)
url = url.slice(0, -1)
config.url = url
}
return new Promise((resolve, reject) => {
uni.uploadFile({
timeout: config.timeout || timeout,
url: baseUrl + '/common/upload' + config.url,
filePath: config.filePath,
name: config.name || 'file',
header: config.header,
formData: config.formData,
success: (res) => {
let result = JSON.parse(res.data)
const code = result.code || 200
const msg = errorCode[code] || result.msg || errorCode['default']
if (code === 200) {
resolve(result)
} else if (code == 401) {
showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
if (res.confirm) {
store.dispatch('LogOut').then(res => {
uni.reLaunch({ url: '/pages/login/login' })
})
}
})
reject('无效的会话,或者会话已过期,请重新登录。')
} else if (code === 500) {
toast(msg)
reject('500')
} else if (code !== 200) {
toast(msg)
reject(code)
}
},
fail: (error) => {
let { message } = error
if (message == 'Network Error') {
message = '后端接口连接异常'
} else if (message.includes('timeout')) {
message = '系统接口请求超时'
} else if (message.includes('Request failed with status code')) {
message = '系统接口' + message.substr(message.length - 3) + '异常'
}
toast(message)
reject(error)
}
})
})
const upload = async (config) => {
try {
console.log('config', config);
// 生成文件名(避免中文乱码)
const fileName = '/images/' + config.filePath.split('/').pop(); // 提取最后一段路径
const data = await cos.uploadFile({
Bucket: 'notice-1348525010', // 填写自己的 bucket必须字段
Region: 'ap-beijing', // 存储桶所在地域,必须字段
Key: fileName,
FilePath: config.filePath,
/* v1.4.3之前的版本必须v1.4.3及以后的版本非必须 */
SliceSize: 1024 * 1024 * 5, // 触发分块上传的阈值超过5MB 使用分块上传小于5MB使用简单上传。可自行设置非必须
onProgress: function(progressData) {
console.log('上传进度:', progressData);
},
// onTaskReady: function(id) { // 非必须
// taskId = id;
// },
});
console.log('上传成功', data);
// 上传成功
const fileUrl = `https://${data.Location}`;
return {
url: fileUrl,
fileName: fileUrl
}
} catch (e) {
console.error('上传失败', e);
}
}
export default upload
// 明确导出类型
export default upload