request.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. },
  21. success: (res) => {
  22. if (res.statusCode !== 200) {
  23. return uni.showToast({
  24. title: '获取失败',
  25. icon: 'error',
  26. duration: 5000
  27. })
  28. // uni.hideLoading()
  29. } else if (res.data.msg != '成功') { //接口返回报错
  30. uni.showToast({
  31. title: res.data.content || res.data.msg || '请求失败',
  32. icon: 'none',
  33. duration:3000
  34. })
  35. console.error('报错接口:',BASE_URL + options.url,'报错信息:', res.data);
  36. // token过期或出了问题
  37. if (res.data.exception && res.data.exception.type ==
  38. 'AuthenticationCredentialsNotFoundException') {
  39. uni.showToast({
  40. title: '登录过期',
  41. icon: 'error',
  42. duration: 5000
  43. })
  44. if (uni.getStorageSync('refreshToken') && uni.getStorageSync('token') ) {
  45. // 刷新token
  46. uni.showLoading({
  47. title: '刷新登录中',
  48. duration: 5000
  49. })
  50. uni.request({
  51. url: BASE_URL + '/zswl-cloud-bdb/user/refreshToken',
  52. method: 'GET',
  53. data: {
  54. refreshToken: uni.getStorageSync('refreshToken')
  55. },
  56. success(r) {
  57. uni.hideLoading()
  58. if (r.data.content == '刷新令牌错误') {
  59. uni.showToast({
  60. title: '登录失效',
  61. icon: 'fail',
  62. duration: 5000
  63. })
  64. uni.clearStorageSync()
  65. uni.reLaunch({
  66. url: '/pages/index/index'
  67. })
  68. } else {
  69. uni.setStorageSync('token', r.data.content) //登录状态
  70. // 刷新页面
  71. var pages = getCurrentPages();
  72. var url = '/' + pages[pages.length - 1].route
  73. uni.reLaunch({
  74. url
  75. })
  76. }
  77. }
  78. })
  79. }else{
  80. uni.hideLoading()
  81. console.log(111);
  82. uni.showModal({
  83. title:'请登录',
  84. confirmText:'去登录',
  85. success(res){
  86. if(res.confirm){
  87. var pages = getCurrentPages()
  88. let parmas = pages[pages.length - 1].options
  89. console.log(11111,parmas);
  90. let str = ''
  91. for (let key in parmas) {
  92. str += `&${key}=${parmas[key]}`
  93. }
  94. console.log(2222222,str);
  95. var url = '/' + pages[pages.length - 1].route
  96. console.log(333333,url);
  97. uni.reLaunch({
  98. url:'/login/login/login?redirect='+url+str
  99. })
  100. }else if (res.cancel) {
  101. console.log('拒绝登录,跳转首页');
  102. uni.switchTab({
  103. url:'/pages/index/index'
  104. })
  105. }
  106. }
  107. })
  108. }
  109. }
  110. }
  111. resolve(res.data)
  112. },
  113. //请求失败
  114. fail: (err) => {
  115. uni.showToast({
  116. title: '请求接口失败',
  117. icon:"none",
  118. duration: 5000
  119. })
  120. reject(err)
  121. }
  122. })
  123. })
  124. }