wew
This commit is contained in:
parent
908502d5c1
commit
fd60f1fe42
114
api/upload.js
114
api/upload.js
@ -1,105 +1,29 @@
|
||||
import request from './request'
|
||||
|
||||
const uploadFile = async (file, callback) => {
|
||||
|
||||
|
||||
const policyData = await request('admin/policy_token', 'post')
|
||||
const res = JSON.parse(policyData.token)
|
||||
|
||||
const fileName = file.tempFilePath.split('/').pop(); // hello.png
|
||||
// const fileName = fileNameWithExt.split('.').slice(0, -1).join('.'); // hello
|
||||
|
||||
const formData = {
|
||||
key: 'upload_file/' + fileName, //上传文件名称
|
||||
policy: res.policy, //表单域
|
||||
'x-oss-signature-version': res.x_oss_signature_version, //指定签名的版本和算法
|
||||
'x-oss-credential': res.x_oss_credential, //指明派生密钥的参数集
|
||||
'x-oss-date': res.x_oss_date, //请求的时间
|
||||
'x-oss-signature': res.signature, //签名认证描述信息
|
||||
'x-oss-security-token': res.security_token, //安全令牌
|
||||
success_action_status: "200" //上传成功后响应状态码
|
||||
};
|
||||
// console.log(filePath)
|
||||
// return
|
||||
// 发送请求上传文件
|
||||
wx.uploadFile({
|
||||
url: 'https://image-fudan.oss-cn-beijing.aliyuncs.com/',
|
||||
method: 'put',
|
||||
filePath: file.tempFilePath,
|
||||
name: 'file', //固定值为file
|
||||
formData: formData,
|
||||
success(res) {
|
||||
console.log('上传响应:', res);
|
||||
if (res.statusCode === 200) {
|
||||
callback(null, 'https://image-fudan.oss-cn-beijing.aliyuncs.com/upload_file/' + fileName); // 上传成功
|
||||
} else {
|
||||
console.error('上传失败,状态码:', res.statusCode);
|
||||
console.error('失败响应:', res);
|
||||
callback(res); // 上传失败,返回响应
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
console.error('上传失败:', err); // 输出错误信息
|
||||
wx.showToast({
|
||||
title: '上传失败,请重试!',
|
||||
icon: 'none'
|
||||
});
|
||||
callback(err); // 调用回调处理错误
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
const uploadFileToOSS = () => {
|
||||
|
||||
function uploadFile(filePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!filePath) {
|
||||
const err = new Error('uploadFile requires a filePath parameter');
|
||||
console.error('上传失败:未提供文件路径', err);
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
wx.chooseMedia({
|
||||
count: 1, // 选择一个文件
|
||||
mediaType: ['image'],
|
||||
sourceType: ['album', 'camera'],
|
||||
// type: 'all', // 支持所有类型的文件
|
||||
wx.uploadFile({
|
||||
filePath: filePath, // 图片临时文件路径(由调用方传入)
|
||||
name: 'file', // 服务器接收文件的字段名,需与后端对应
|
||||
url: 'https://mini-chat.1024tool.vip/api/admin/upload/image', // 服务器接收图片的接口地址
|
||||
success: (res) => {
|
||||
wx.showToast({
|
||||
title: '文件上传中,请稍等!',
|
||||
icon: 'none'
|
||||
});
|
||||
console.log('选择的文件:', res.tempFiles); // 输出选择的文件信息
|
||||
if (res.tempFiles.length > 0) {
|
||||
const tempFilePath = res.tempFiles[0];
|
||||
console.log('选择的文件路径:', tempFilePath); // 输出文件路径
|
||||
uploadFile(tempFilePath, (error, data) => {
|
||||
if (error) {
|
||||
wx.showToast({
|
||||
title: '上传失败!',
|
||||
icon: 'none'
|
||||
});
|
||||
console.error('上传失败:', error); // 输出具体的错误信息
|
||||
reject(error)
|
||||
} else {
|
||||
resolve(data)
|
||||
console.log('上传成功:', data); // 输出上传成功后的数据
|
||||
}
|
||||
});
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: '未选择文件!',
|
||||
icon: 'none'
|
||||
});
|
||||
reject('未选择文件!')
|
||||
}
|
||||
resolve(res);
|
||||
},
|
||||
fail: (err) => {
|
||||
wx.showToast({
|
||||
title: '选择文件失败!',
|
||||
icon: 'none'
|
||||
});
|
||||
console.error('选择文件失败:', err); // 输出选择文件的错误信息
|
||||
reject('选择文件失败!')
|
||||
console.error('上传失败', err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
})
|
||||
|
||||
export default uploadFileToOSS;
|
||||
}
|
||||
|
||||
|
||||
// 导出请求和服务地址(CommonJS 兼容)
|
||||
module.exports = uploadFile;
|
||||
@ -1,7 +1,7 @@
|
||||
// pages/contact/index.js
|
||||
const request = require('../../api/request.js');
|
||||
|
||||
|
||||
const uploadFile = require('../../api/upload.js');
|
||||
Page({
|
||||
/**
|
||||
* 页面的初始数据
|
||||
@ -567,12 +567,7 @@ Page({
|
||||
scrollToId: id
|
||||
});
|
||||
}, 50);
|
||||
|
||||
wx.uploadFile({
|
||||
filePath: tempFilePaths[0], // 图片临时文件路径
|
||||
name: 'file', // 服务器接收文件的字段名,需与后端对应
|
||||
url: 'https://mini-chat.1024tool.vip/admin/upload/image', // 服务器接收图片的接口地址
|
||||
success: (res) => {
|
||||
uploadFile(tempFilePaths[0]).then((res) => {
|
||||
const data = JSON.parse(res.data);
|
||||
console.log('上传成功', data);
|
||||
const accountInfo = wx.getAccountInfoSync();
|
||||
@ -585,12 +580,29 @@ Page({
|
||||
"from_user_id": userInfo.openid,
|
||||
"from_user_name": userInfo.user_name,
|
||||
"msg_type": 2
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('上传失败', err);
|
||||
}
|
||||
}).catch((err) => {
|
||||
wx.showToast({
|
||||
title: '图片发送失败',
|
||||
icon: 'none'
|
||||
});
|
||||
});
|
||||
}).catch((err) => {
|
||||
wx.showToast({
|
||||
title: '图片发送失败',
|
||||
icon: 'none'
|
||||
});
|
||||
});
|
||||
// wx.uploadFile({
|
||||
// filePath: tempFilePaths[0], // 图片临时文件路径
|
||||
// name: 'file', // 服务器接收文件的字段名,需与后端对应
|
||||
// url: 'https://mini-chat.1024tool.vip/api/admin/upload/image', // 服务器接收图片的接口地址
|
||||
// success: (res) => {
|
||||
|
||||
// },
|
||||
// fail: (err) => {
|
||||
// console.error('上传失败', err);
|
||||
// }
|
||||
// });
|
||||
|
||||
// 模拟客服返回图片确认
|
||||
// setTimeout(() => {
|
||||
|
||||
@ -6,13 +6,13 @@
|
||||
"appid": "wx26ad074017e1e63f",
|
||||
"libVersion": "3.9.2",
|
||||
"setting": {
|
||||
"es6": false,
|
||||
"es6": true,
|
||||
"postcss": false,
|
||||
"compileWorklet": false,
|
||||
"minified": false,
|
||||
"uglifyFileName": false,
|
||||
"uploadWithSourceMap": true,
|
||||
"enhance": false,
|
||||
"enhance": true,
|
||||
"packNpmManually": false,
|
||||
"packNpmRelationList": [],
|
||||
"minifyWXSS": true,
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
{
|
||||
"name": "pages/index/detail",
|
||||
"pathName": "pages/index/detail",
|
||||
"query": "",
|
||||
"query": "url=1",
|
||||
"launchMode": "default",
|
||||
"scene": null
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user