upload.js 3.2 KB

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