request.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. function getPages(){
  17. var obj = {
  18. type: 'danger'
  19. }
  20. var pages = getCurrentPages()
  21. var route = pages[pages.length - 1]?.route
  22. console.log(config.tabbar,route)
  23. if(route && config.tabbar.indexOf(route)==-1){
  24. obj.top = wx.getStorageSync('height')
  25. }
  26. return obj
  27. }
  28. // 封装请求方法
  29. function request(url, method = 'GET', data = {}, header = {},isLogin=true) {
  30. return new Promise((resolve, reject) => {
  31. var str = ''
  32. if(method === 'GET'){
  33. str ='?' + Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&')
  34. }
  35. wx.request({
  36. url: config.baseURL + url + str,
  37. method: method.toUpperCase(),
  38. data: method === 'GET' ? {} : data, // GET请求时将data置为空对象
  39. header: {
  40. ...config.header,
  41. ...header,
  42. "Cookie": wx.getStorageSync('token')
  43. },
  44. timeout: config.timeout,
  45. success: function(res){
  46. var obj = getPages()
  47. wx.hideLoading()
  48. // 根据业务逻辑处理成功响应
  49. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  50. resolve(res.data);
  51. } else if(res.data && !res.data.code){
  52. resolve(res.data);
  53. }
  54. else {
  55. switch(res.data.code){
  56. case 401:
  57. if(isLogin){
  58. // Notify({ type: 'danger', message: '未登录',top:'90' });
  59. obj.message = '未登录'
  60. wx.navigateTo({
  61. url: '/pages/login/login',
  62. })
  63. }
  64. wx.setStorageSync('token', '')
  65. break;
  66. default:
  67. obj.message = '请求失败'
  68. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  69. }
  70. // wx.showToast({
  71. // title: '请求失败',
  72. // icon: 'error',
  73. // duration: 2000
  74. // })
  75. console.log(obj)
  76. if(obj.message){
  77. Notify(obj)
  78. }
  79. reject(res.data.message || '未知错误');
  80. }
  81. if($stopWuxRefresher()){
  82. $stopWuxRefresher()
  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. if($stopWuxRefresher()){
  100. $stopWuxRefresher()
  101. }
  102. },
  103. });
  104. });
  105. }
  106. function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
  107. return new Promise((resolve, reject) => {
  108. wx.uploadFile({
  109. url: config.baseURL + url, //仅为示例,非真实的接口地址
  110. filePath: path,
  111. name: 'multipartFile',
  112. formData: formData,
  113. header: {
  114. ...config.header,
  115. ...header,
  116. "Cookie": wx.getStorageSync('token')
  117. },
  118. timeout: config.timeout,
  119. success: function(res){
  120. wx.hideLoading()
  121. var obj = getPages()
  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. var obj = getPages()
  163. // 处理请求失败
  164. // wx.showToast({
  165. // title: '请求失败',
  166. // icon: 'error',
  167. // duration: 2000
  168. // })
  169. obj.message = '请求失败'
  170. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  171. Notify(obj)
  172. reject(error);
  173. },
  174. });
  175. });
  176. }
  177. // 导出请求方法
  178. module.exports = {
  179. get: (url, data,isLogin, header) => request(url, 'GET', data, header,isLogin),
  180. post: (url, data,isLogin, header) => request(url, 'POST', data, header,isLogin),
  181. // 可以继续添加其他方法,如put, delete等
  182. // ...
  183. upload: ( path,formData,url, header) => upload(url, path, header,formData),
  184. };