request.js 3.9 KB

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