upload.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 配置项
  2. import Notify from '../miniprogram_npm/@vant/weapp/notify/notify';
  3. import { $startWuxRefresher, $stopWuxRefresher,$stopWuxLoader} from '../miniprogram_npm/wux-weapp/index';
  4. const token = wx.getStorageSync('token')
  5. const app = getApp()
  6. const config = {
  7. // baseURL: 'http://192.168.1.6:8802', // 基础URL
  8. baseURL: 'https://www.xsip.cn', // 基础URL
  9. timeout: 30000, // 超时时间(单位ms)
  10. header: {
  11. 'content-type': 'application/json', // 默认请求头
  12. // 其他全局请求头...
  13. },
  14. // 其他全局配置...
  15. tabbar:["pages/index/index","pages/hotProduct/hotProduct","pages/mine/mine"]
  16. };
  17. // 封装请求方法
  18. function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
  19. return new Promise((resolve, reject) => {
  20. var obj = {
  21. type: 'danger'
  22. }
  23. var pages = getCurrentPages()
  24. var route = pages[pages.length - 1]?.route
  25. console.log(config.tabbar,route)
  26. if(route && config.tabbar.indexOf(route)==-1){
  27. obj.top = app.globalData.navBarHeight
  28. }
  29. wx.uploadFile({
  30. url: config.baseURL + url, //仅为示例,非真实的接口地址
  31. filePath: path,
  32. name: 'files',
  33. formData: {
  34. sourceId:5,
  35. ...formData
  36. },
  37. header: {
  38. ...config.header,
  39. ...header,
  40. "Cookie": wx.getStorageSync('token')
  41. },
  42. timeout: config.timeout,
  43. success: function(res){
  44. wx.hideLoading()
  45. if(res.data){
  46. res.data = JSON.parse(res.data)
  47. }
  48. // 根据业务逻辑处理成功响应
  49. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  50. resolve(res.data);
  51. } else {
  52. switch(res.data.code){
  53. case 401:
  54. if(isLogin){
  55. // Notify({ type: 'danger', message: '未登录',top:'90' });
  56. obj.message = '未登录'
  57. wx.navigateTo({
  58. url: '/pages/login/login',
  59. })
  60. }
  61. wx.setStorageSync('token', '')
  62. break;
  63. default:
  64. obj.message = '请求失败'
  65. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  66. }
  67. // wx.showToast({
  68. // title: '请求失败',
  69. // icon: 'error',
  70. // duration: 2000
  71. // })
  72. console.log(obj)
  73. if(obj.message){
  74. Notify(obj)
  75. }
  76. reject(res.data.message || '未知错误');
  77. }
  78. if($stopWuxRefresher()){
  79. $stopWuxRefresher()
  80. }
  81. },
  82. fail: function(error) {
  83. console.log(error)
  84. wx.hideLoading()
  85. // 处理请求失败
  86. // wx.showToast({
  87. // title: '请求失败',
  88. // icon: 'error',
  89. // duration: 2000
  90. // })
  91. obj.message = '请求失败'
  92. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  93. Notify(obj)
  94. reject(error);
  95. },
  96. });
  97. });
  98. }
  99. // 导出请求方法
  100. module.exports = {
  101. upload: ( path,formData,url, header) => upload(url, path, header,formData),
  102. };