request.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // request.js
  2. import Notify from '../miniprogram_npm/@vant/weapp/notify/notify';
  3. import { $startWuxRefresher, $stopWuxRefresher,$stopWuxLoader} from '../miniprogram_npm/wux-weapp/index';
  4. // 配置项
  5. const config = {
  6. baseURL: 'http://192.168.1.2:8901', // 基础URL
  7. // baseURL: 'https://www.xsip.cn', // 基础URL
  8. timeout: 30000, // 超时时间(单位ms)
  9. header: {
  10. 'content-type': 'application/json', // 默认请求头
  11. // 其他全局请求头...
  12. },
  13. // 其他全局配置...
  14. tabbar:["pages/index/index","pages/hotProduct/hotProduct","pages/mine/mine"]
  15. };
  16. // 封装请求方法
  17. function request(url, method = 'GET', data = {}, header = {},isLogin=true) {
  18. return new Promise((resolve, reject) => {
  19. var obj = {
  20. type: 'danger'
  21. }
  22. var pages = getCurrentPages()
  23. var route = pages[pages.length - 1]?.route
  24. console.log(config.tabbar,route)
  25. if(route && config.tabbar.indexOf(route)==-1){
  26. obj.top = "90"
  27. }
  28. wx.request({
  29. url: config.baseURL + url,
  30. method: method.toUpperCase(),
  31. data: method === 'GET' ? {} : data, // GET请求时将data置为空对象
  32. header: {
  33. ...config.header,
  34. ...header,
  35. "Cookie": "token="+wx.getStorageSync('token')
  36. },
  37. timeout: config.timeout,
  38. success: function(res){
  39. // 根据业务逻辑处理成功响应
  40. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  41. resolve(res.data);
  42. } else {
  43. switch(res.data.code){
  44. case 401:
  45. if(isLogin){
  46. // Notify({ type: 'danger', message: '未登录',top:'90' });
  47. obj.message = '未登录'
  48. wx.navigateTo({
  49. url: '/pages/login/login',
  50. })
  51. }
  52. wx.setStorageSync('token', '')
  53. break;
  54. default:
  55. obj.message = '请求失败'
  56. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  57. }
  58. // wx.showToast({
  59. // title: '请求失败',
  60. // icon: 'error',
  61. // duration: 2000
  62. // })
  63. console.log(obj)
  64. if(obj.message){
  65. Notify(obj)
  66. }
  67. reject(res.data.message || '未知错误');
  68. }
  69. if($stopWuxRefresher()){
  70. $stopWuxRefresher()
  71. }
  72. },
  73. fail: function(error) {
  74. console.log(error)
  75. // 处理请求失败
  76. // wx.showToast({
  77. // title: '请求失败',
  78. // icon: 'error',
  79. // duration: 2000
  80. // })
  81. obj.message = '请求失败'
  82. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  83. Notify(obj)
  84. reject(error);
  85. if($stopWuxRefresher()){
  86. $stopWuxRefresher()
  87. }
  88. },
  89. });
  90. });
  91. }
  92. function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
  93. return new Promise((resolve, reject) => {
  94. var obj = {
  95. type: 'danger'
  96. }
  97. var pages = getCurrentPages()
  98. var route = pages[pages.length - 1]?.route
  99. console.log(config.tabbar,route)
  100. if(route && config.tabbar.indexOf(route)==-1){
  101. obj.top = "90"
  102. }
  103. wx.uploadFile({
  104. url: config.baseURL + url, //仅为示例,非真实的接口地址
  105. filePath: path,
  106. name: 'multipartFile',
  107. formData: formData,
  108. header: {
  109. ...config.header,
  110. ...header,
  111. "Cookie": "token="+wx.getStorageSync('token')
  112. },
  113. timeout: config.timeout,
  114. success: function(res){
  115. if(res.data){
  116. res.data = JSON.parse(res.data)
  117. }
  118. // 根据业务逻辑处理成功响应
  119. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  120. resolve(res.data);
  121. } else {
  122. switch(res.data.code){
  123. case 401:
  124. if(isLogin){
  125. // Notify({ type: 'danger', message: '未登录',top:'90' });
  126. obj.message = '未登录'
  127. wx.navigateTo({
  128. url: '/pages/login/login',
  129. })
  130. }
  131. wx.setStorageSync('token', '')
  132. break;
  133. default:
  134. obj.message = '请求失败'
  135. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  136. }
  137. // wx.showToast({
  138. // title: '请求失败',
  139. // icon: 'error',
  140. // duration: 2000
  141. // })
  142. console.log(obj)
  143. if(obj.message){
  144. Notify(obj)
  145. }
  146. reject(res.data.message || '未知错误');
  147. }
  148. if($stopWuxRefresher()){
  149. $stopWuxRefresher()
  150. }
  151. },
  152. fail: function(error) {
  153. console.log(error)
  154. // 处理请求失败
  155. // wx.showToast({
  156. // title: '请求失败',
  157. // icon: 'error',
  158. // duration: 2000
  159. // })
  160. obj.message = '请求失败'
  161. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  162. Notify(obj)
  163. reject(error);
  164. },
  165. });
  166. });
  167. }
  168. // 导出请求方法
  169. module.exports = {
  170. get: (url, data,isLogin, header) => request(url, 'GET', data, header,isLogin),
  171. post: (url, data,isLogin, header) => request(url, 'POST', data, header,isLogin),
  172. // 可以继续添加其他方法,如put, delete等
  173. // ...
  174. upload: ( path,formData,url, header) => upload(url, path, header,formData),
  175. };