upload.js 1.8 KB

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