request.js 6.7 KB

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