request.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.6: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 = wx.getStorageSync('height')
  27. }
  28. var str = ''
  29. if(method === 'GET'){
  30. str ='?' + Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&')
  31. }
  32. wx.request({
  33. url: config.baseURL + url + str,
  34. method: method.toUpperCase(),
  35. data: method === 'GET' ? {} : data, // GET请求时将data置为空对象
  36. header: {
  37. ...config.header,
  38. ...header,
  39. "Cookie": wx.getStorageSync('token')
  40. },
  41. timeout: config.timeout,
  42. success: function(res){
  43. wx.hideLoading()
  44. // 根据业务逻辑处理成功响应
  45. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  46. resolve(res.data);
  47. } else {
  48. switch(res.data.code){
  49. case 401:
  50. if(isLogin){
  51. // Notify({ type: 'danger', message: '未登录',top:'90' });
  52. obj.message = '未登录'
  53. wx.navigateTo({
  54. url: '/pages/login/login',
  55. })
  56. }
  57. wx.setStorageSync('token', '')
  58. break;
  59. default:
  60. obj.message = '请求失败'
  61. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  62. }
  63. // wx.showToast({
  64. // title: '请求失败',
  65. // icon: 'error',
  66. // duration: 2000
  67. // })
  68. console.log(obj)
  69. if(obj.message){
  70. Notify(obj)
  71. }
  72. reject(res.data.message || '未知错误');
  73. }
  74. if($stopWuxRefresher()){
  75. $stopWuxRefresher()
  76. }
  77. },
  78. fail: function(error) {
  79. console.log(error)
  80. wx.hideLoading()
  81. // 处理请求失败
  82. // wx.showToast({
  83. // title: '请求失败',
  84. // icon: 'error',
  85. // duration: 2000
  86. // })
  87. obj.message = '请求失败'
  88. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  89. Notify(obj)
  90. reject(error);
  91. if($stopWuxRefresher()){
  92. $stopWuxRefresher()
  93. }
  94. },
  95. });
  96. });
  97. }
  98. function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
  99. return new Promise((resolve, reject) => {
  100. var obj = {
  101. type: 'danger'
  102. }
  103. var pages = getCurrentPages()
  104. var route = pages[pages.length - 1]?.route
  105. console.log(config.tabbar,route)
  106. if(route && config.tabbar.indexOf(route)==-1){
  107. obj.top = wx.getStorageSync('height')
  108. }
  109. wx.uploadFile({
  110. url: config.baseURL + url, //仅为示例,非真实的接口地址
  111. filePath: path,
  112. name: 'multipartFile',
  113. formData: formData,
  114. header: {
  115. ...config.header,
  116. ...header,
  117. "Cookie": wx.getStorageSync('token')
  118. },
  119. timeout: config.timeout,
  120. success: function(res){
  121. wx.hideLoading()
  122. if(res.data){
  123. res.data = JSON.parse(res.data)
  124. }
  125. // 根据业务逻辑处理成功响应
  126. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  127. resolve(res.data);
  128. } else {
  129. switch(res.data.code){
  130. case 401:
  131. if(isLogin){
  132. // Notify({ type: 'danger', message: '未登录',top:'90' });
  133. obj.message = '未登录'
  134. wx.navigateTo({
  135. url: '/pages/login/login',
  136. })
  137. }
  138. wx.setStorageSync('token', '')
  139. break;
  140. default:
  141. obj.message = '请求失败'
  142. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  143. }
  144. // wx.showToast({
  145. // title: '请求失败',
  146. // icon: 'error',
  147. // duration: 2000
  148. // })
  149. console.log(obj)
  150. if(obj.message){
  151. Notify(obj)
  152. }
  153. reject(res.data.message || '未知错误');
  154. }
  155. if($stopWuxRefresher()){
  156. $stopWuxRefresher()
  157. }
  158. },
  159. fail: function(error) {
  160. console.log(error)
  161. wx.hideLoading()
  162. // 处理请求失败
  163. // wx.showToast({
  164. // title: '请求失败',
  165. // icon: 'error',
  166. // duration: 2000
  167. // })
  168. obj.message = '请求失败'
  169. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  170. Notify(obj)
  171. reject(error);
  172. },
  173. });
  174. });
  175. }
  176. // 导出请求方法
  177. module.exports = {
  178. get: (url, data,isLogin, header) => request(url, 'GET', data, header,isLogin),
  179. post: (url, data,isLogin, header) => request(url, 'POST', data, header,isLogin),
  180. // 可以继续添加其他方法,如put, delete等
  181. // ...
  182. upload: ( path,formData,url, header) => upload(url, path, header,formData),
  183. };