request.js 4.0 KB

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