upload.js 1.7 KB

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