request.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //用BASEURL来表示域名地址
  2. // const BASE_URL = 'http://192.168.110.153:9002'
  3. // const BASE_URL = 'https://zswl.api.jpy.wang/zswl-cloud-bdb'
  4. // const BASE_URL = 'https://api.dev.zonelife.cn'
  5. import {
  6. BASE_URL
  7. } from '@/utils/config.js'
  8. //封装请求方法
  9. export let request = (options) => {
  10. return new Promise((resolve, reject) => {
  11. //请求路径拼接,,其中options.url就是通过下面方法myRequest获取到接口部分的url
  12. //method--请求方法,不是method的post就是get
  13. //请求的参数,当没有参数的时候就是空对象
  14. request.requestTask =uni.request({
  15. url: options.url.indexOf('http')==-1?(BASE_URL + options.url):options.url,
  16. method: options.method || 'GET',
  17. data: options.data || {},
  18. header: {
  19. accessToken: uni.getStorageSync('token'),
  20. code:1
  21. },
  22. success: (res) => {
  23. if (res.statusCode !== 200) {
  24. return uni.showToast({
  25. title: '获取失败',
  26. icon: 'error',
  27. duration: 5000
  28. })
  29. // uni.hideLoading()
  30. } else if (res.data.msg != '成功') { //接口返回报错
  31. setTimeout(()=>{
  32. uni.showToast({
  33. title: res.data.content || res.data.msg ||(res.data.exception && res.data.exception.message) || '请求失败',
  34. icon: 'none',
  35. duration:3000
  36. })
  37. })
  38. console.error('报错接口:',BASE_URL + options.url,'报错信息:', res.data);
  39. // token过期或出了问题
  40. if (res.data.exception && res.data.exception.type ==
  41. 'AuthenticationCredentialsNotFoundException') {
  42. uni.showToast({
  43. title: '登录过期',
  44. icon: 'error',
  45. duration: 5000
  46. })
  47. if (uni.getStorageSync('refreshToken') && uni.getStorageSync('token') ) {
  48. // 刷新token
  49. uni.showLoading({
  50. title: '刷新登录中',
  51. duration: 5000
  52. })
  53. uni.request({
  54. url: BASE_URL + '/zswl-cloud-bdb/user/refreshToken',
  55. method: 'GET',
  56. data: {
  57. refreshToken: uni.getStorageSync('refreshToken')
  58. },
  59. success(r) {
  60. uni.hideLoading()
  61. if (r.data.content == '刷新令牌错误') {
  62. setTimeout(()=>{
  63. uni.showToast({
  64. title: '登录失效',
  65. icon: 'fail'
  66. })
  67. },0)
  68. uni.clearStorageSync()
  69. uni.reLaunch({
  70. url: '/pages/index/index'
  71. })
  72. } else {
  73. uni.setStorageSync('token', r.data.content) //登录状态
  74. // 刷新页面
  75. var pages = getCurrentPages();
  76. var url = '/' + pages[pages.length - 1].route
  77. uni.reLaunch({
  78. url
  79. })
  80. }
  81. }
  82. })
  83. }else{
  84. uni.hideLoading()
  85. console.log(111);
  86. uni.showModal({
  87. title:'请登录',
  88. confirmText:'去登录',
  89. success(res){
  90. if(res.confirm){
  91. var pages = getCurrentPages()
  92. let parmas = pages[pages.length - 1].options
  93. console.log(11111,parmas);
  94. let str = ''
  95. for (let key in parmas) {
  96. str += `&${key}=${parmas[key]}`
  97. }
  98. console.log(2222222,str);
  99. var url = '/' + pages[pages.length - 1].route
  100. console.log(333333,url);
  101. uni.reLaunch({
  102. url:'/login/login/login?redirect='+url+str
  103. })
  104. }else if (res.cancel) {
  105. console.log('拒绝登录,跳转首页');
  106. uni.switchTab({
  107. url:'/pages/index/index'
  108. })
  109. }
  110. }
  111. })
  112. }
  113. }
  114. }
  115. resolve(res.data)
  116. },
  117. //请求失败
  118. fail: (err) => {
  119. setTimeout(()=>{
  120. uni.showToast({
  121. title: '请求接口失败',
  122. icon:"none",
  123. duration: 5000
  124. })
  125. },0)
  126. reject(err)
  127. }
  128. })
  129. })
  130. }