request.js 6.3 KB

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