upload.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // 配置项
  2. const config = {
  3. baseURL: 'http://192.168.1.6:8801', // 基础URL
  4. timeout: 30000, // 超时时间(单位ms)
  5. header: {
  6. 'content-type': 'application/json', // 默认请求头
  7. // 其他全局请求头...
  8. },
  9. // 其他全局配置...
  10. };
  11. // 封装请求方法
  12. function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
  13. return new Promise((resolve, reject) => {
  14. wx.uploadFile({
  15. url: config.baseURL + url, //仅为示例,非真实的接口地址
  16. filePath: path,
  17. name: 'file',
  18. formData: formData,
  19. header: {
  20. ...config.header,
  21. ...header,
  22. },
  23. timeout: config.timeout,
  24. success: function(res){
  25. // 根据业务逻辑处理成功响应
  26. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  27. resolve(res.data);
  28. } else {
  29. // wx.showToast({
  30. // title: '请求失败',
  31. // icon: 'error',
  32. // duration: 2000
  33. // })
  34. Notify({ type: 'danger', message: '上传失败' });
  35. reject(res.data.message || '未知错误');
  36. }
  37. },
  38. fail: function(error) {
  39. console.log(error)
  40. // 处理请求失败
  41. // wx.showToast({
  42. // title: '请求失败',
  43. // icon: 'error',
  44. // duration: 2000
  45. // })
  46. Notify({ type: 'danger', message: '上传失败' });
  47. reject(error);
  48. },
  49. });
  50. });
  51. }
  52. // 导出请求方法
  53. module.exports = {
  54. upload: ( path,formData,url, header) => upload(url, path, header,formData),
  55. };