wew
This commit is contained in:
parent
908502d5c1
commit
fd60f1fe42
112
api/upload.js
112
api/upload.js
@ -1,105 +1,29 @@
|
|||||||
import request from './request'
|
|
||||||
|
|
||||||
const uploadFile = async (file, callback) => {
|
|
||||||
|
|
||||||
|
|
||||||
const policyData = await request('admin/policy_token', 'post')
|
function uploadFile(filePath) {
|
||||||
const res = JSON.parse(policyData.token)
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!filePath) {
|
||||||
|
const err = new Error('uploadFile requires a filePath parameter');
|
||||||
|
console.error('上传失败:未提供文件路径', err);
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
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({
|
wx.uploadFile({
|
||||||
url: 'https://image-fudan.oss-cn-beijing.aliyuncs.com/',
|
filePath: filePath, // 图片临时文件路径(由调用方传入)
|
||||||
method: 'put',
|
name: 'file', // 服务器接收文件的字段名,需与后端对应
|
||||||
filePath: file.tempFilePath,
|
url: 'https://mini-chat.1024tool.vip/api/admin/upload/image', // 服务器接收图片的接口地址
|
||||||
name: 'file', //固定值为file
|
success: (res) => {
|
||||||
formData: formData,
|
resolve(res);
|
||||||
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) {
|
fail: (err) => {
|
||||||
console.error('上传失败:', err); // 输出错误信息
|
console.error('上传失败', err);
|
||||||
wx.showToast({
|
reject(err);
|
||||||
title: '上传失败,请重试!',
|
|
||||||
icon: 'none'
|
|
||||||
});
|
|
||||||
callback(err); // 调用回调处理错误
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const uploadFileToOSS = () => {
|
// 导出请求和服务地址(CommonJS 兼容)
|
||||||
|
module.exports = uploadFile;
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
|
|
||||||
wx.chooseMedia({
|
|
||||||
count: 1, // 选择一个文件
|
|
||||||
mediaType: ['image'],
|
|
||||||
sourceType: ['album', 'camera'],
|
|
||||||
// type: 'all', // 支持所有类型的文件
|
|
||||||
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('未选择文件!')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail: (err) => {
|
|
||||||
wx.showToast({
|
|
||||||
title: '选择文件失败!',
|
|
||||||
icon: 'none'
|
|
||||||
});
|
|
||||||
console.error('选择文件失败:', err); // 输出选择文件的错误信息
|
|
||||||
reject('选择文件失败!')
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default uploadFileToOSS;
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
// pages/contact/index.js
|
// pages/contact/index.js
|
||||||
const request = require('../../api/request.js');
|
const request = require('../../api/request.js');
|
||||||
|
|
||||||
|
const uploadFile = require('../../api/upload.js');
|
||||||
Page({
|
Page({
|
||||||
/**
|
/**
|
||||||
* 页面的初始数据
|
* 页面的初始数据
|
||||||
@ -282,7 +282,7 @@ Page({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
fail: function(err) {
|
fail: function (err) {
|
||||||
console.error('wx.login失败:', err);
|
console.error('wx.login失败:', err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -567,12 +567,7 @@ Page({
|
|||||||
scrollToId: id
|
scrollToId: id
|
||||||
});
|
});
|
||||||
}, 50);
|
}, 50);
|
||||||
|
uploadFile(tempFilePaths[0]).then((res) => {
|
||||||
wx.uploadFile({
|
|
||||||
filePath: tempFilePaths[0], // 图片临时文件路径
|
|
||||||
name: 'file', // 服务器接收文件的字段名,需与后端对应
|
|
||||||
url: 'https://mini-chat.1024tool.vip/admin/upload/image', // 服务器接收图片的接口地址
|
|
||||||
success: (res) => {
|
|
||||||
const data = JSON.parse(res.data);
|
const data = JSON.parse(res.data);
|
||||||
console.log('上传成功', data);
|
console.log('上传成功', data);
|
||||||
const accountInfo = wx.getAccountInfoSync();
|
const accountInfo = wx.getAccountInfoSync();
|
||||||
@ -580,17 +575,34 @@ Page({
|
|||||||
request('app/send_message', 'POST', {
|
request('app/send_message', 'POST', {
|
||||||
"app_id": accountInfo.miniProgram.appId,
|
"app_id": accountInfo.miniProgram.appId,
|
||||||
"content": JSON.stringify({
|
"content": JSON.stringify({
|
||||||
messages: 'https://mini-chat.1024tool.vip/'+ data.preview_image_url
|
messages: 'https://mini-chat.1024tool.vip/' + data.preview_image_url
|
||||||
}),
|
}),
|
||||||
"from_user_id": userInfo.openid,
|
"from_user_id": userInfo.openid,
|
||||||
"from_user_name": userInfo.user_name,
|
"from_user_name": userInfo.user_name,
|
||||||
"msg_type": 2
|
"msg_type": 2
|
||||||
})
|
}).catch((err) => {
|
||||||
},
|
wx.showToast({
|
||||||
fail: (err) => {
|
title: '图片发送失败',
|
||||||
console.error('上传失败', err);
|
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(() => {
|
// setTimeout(() => {
|
||||||
|
|||||||
@ -6,13 +6,13 @@
|
|||||||
"appid": "wx26ad074017e1e63f",
|
"appid": "wx26ad074017e1e63f",
|
||||||
"libVersion": "3.9.2",
|
"libVersion": "3.9.2",
|
||||||
"setting": {
|
"setting": {
|
||||||
"es6": false,
|
"es6": true,
|
||||||
"postcss": false,
|
"postcss": false,
|
||||||
"compileWorklet": false,
|
"compileWorklet": false,
|
||||||
"minified": false,
|
"minified": false,
|
||||||
"uglifyFileName": false,
|
"uglifyFileName": false,
|
||||||
"uploadWithSourceMap": true,
|
"uploadWithSourceMap": true,
|
||||||
"enhance": false,
|
"enhance": true,
|
||||||
"packNpmManually": false,
|
"packNpmManually": false,
|
||||||
"packNpmRelationList": [],
|
"packNpmRelationList": [],
|
||||||
"minifyWXSS": true,
|
"minifyWXSS": true,
|
||||||
|
|||||||
@ -32,7 +32,7 @@
|
|||||||
{
|
{
|
||||||
"name": "pages/index/detail",
|
"name": "pages/index/detail",
|
||||||
"pathName": "pages/index/detail",
|
"pathName": "pages/index/detail",
|
||||||
"query": "",
|
"query": "url=1",
|
||||||
"launchMode": "default",
|
"launchMode": "default",
|
||||||
"scene": null
|
"scene": null
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user