request.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: 'https://www.xsip.cn/test', // 开发环境地址
  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. wx.showModal({
  79. title: '',
  80. content: '前往会员中心开通会员',
  81. complete: (res) => {
  82. if (res.cancel) {}
  83. if (res.confirm) {
  84. wx.navigateTo({
  85. url: '/pages/memberCenter/menberCenter2',
  86. })
  87. }
  88. }
  89. })
  90. break;
  91. // case 607://已开通会员且超过额度
  92. // break;
  93. default:
  94. // obj.message = '请求失败'
  95. obj.message = res.data.message
  96. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  97. }
  98. // wx.showToast({
  99. // title: '请求失败',
  100. // icon: 'error',
  101. // duration: 2000
  102. // })
  103. console.log(obj)
  104. if(obj.message){
  105. Notify(obj)
  106. }
  107. reject(res.data.message || '未知错误');
  108. }
  109. // if($stopWuxRefresher()){
  110. // $stopWuxRefresher()
  111. // }
  112. },
  113. fail: function(error) {
  114. console.log(error)
  115. wx.hideLoading()
  116. var obj = getPages()
  117. // 处理请求失败
  118. // wx.showToast({
  119. // title: '请求失败',
  120. // icon: 'error',
  121. // duration: 2000
  122. // })
  123. obj.message = '请求超时'
  124. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  125. Notify(obj)
  126. reject(error);
  127. // if($stopWuxRefresher()){
  128. // $stopWuxRefresher()
  129. // }
  130. },
  131. });
  132. });
  133. }
  134. function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
  135. return new Promise((resolve, reject) => {
  136. wx.uploadFile({
  137. url: config.baseURL + url, //仅为示例,非真实的接口地址
  138. filePath: path,
  139. name: 'multipartFile',
  140. formData: formData,
  141. header: {
  142. ...config.header,
  143. ...header,
  144. "Authorization": wx.getStorageSync('token')
  145. },
  146. timeout: config.timeout,
  147. success: function(res){
  148. wx.hideLoading()
  149. var obj = getPages()
  150. if(res.data){
  151. res.data = JSON.parse(res.data)
  152. }
  153. // 根据业务逻辑处理成功响应
  154. if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
  155. resolve(res.data);
  156. }else {
  157. switch(res.data.code){
  158. case 401:
  159. if(isLogin){
  160. // Notify({ type: 'danger', message: '未登录',top:'90' });
  161. obj.message = '未登录'
  162. wx.navigateTo({
  163. url: '/pages/login/login',
  164. })
  165. }
  166. wx.setStorageSync('token', '')
  167. break;
  168. default:
  169. // obj.message = '请求失败'
  170. obj.message = res.data.message
  171. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  172. }
  173. // wx.showToast({
  174. // title: '请求失败',
  175. // icon: 'error',
  176. // duration: 2000
  177. // })
  178. console.log(obj)
  179. if(obj.message){
  180. Notify(obj)
  181. }
  182. reject(res.data.message || '未知错误');
  183. }
  184. // if($stopWuxRefresher()){
  185. // $stopWuxRefresher()
  186. // }
  187. },
  188. fail: function(error) {
  189. console.log(error)
  190. wx.hideLoading()
  191. var obj = getPages()
  192. // 处理请求失败
  193. // wx.showToast({
  194. // title: '请求失败',
  195. // icon: 'error',
  196. // duration: 2000
  197. // })
  198. obj.message = '请求超时'
  199. // Notify({ type: 'danger', message: '请求失败',top:'90' });
  200. Notify(obj)
  201. reject(error);
  202. },
  203. });
  204. });
  205. }
  206. // 导出请求方法
  207. module.exports = {
  208. get: (url, data,isLogin, header) => request(url, 'GET', data, header,isLogin),
  209. post: (url, data,isLogin, header) => request(url, 'POST', data, header,isLogin),
  210. // 可以继续添加其他方法,如put, delete等
  211. // ...
  212. upload: ( path,formData,url, header) => upload(url, path, header,formData),
  213. };