request.js 6.9 KB

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