upload.js 3.5 KB

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