request.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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){
  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:'520100' //慧研学520100 中数5201000 贵大 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 != '成功'&&res.data.content!='该手机号已订购【惠生活优享会员】,请在会员到期后再使用激活码') { //接口返回报错
  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. console.error('报错接口:',BASE_URL + options.url,'报错信息:', res.data);
  42. // token过期或出了问题
  43. if (res.data.exception && res.data.exception.type ==
  44. 'AuthenticationCredentialsNotFoundException') {
  45. uni.showToast({
  46. title: '登录过期',
  47. icon: 'error',
  48. duration: 5000
  49. })
  50. if (uni.getStorageSync('refreshToken') && uni.getStorageSync('token') ) {
  51. // 刷新token
  52. uni.showLoading({
  53. title: '刷新登录中',
  54. duration: 5000
  55. })
  56. uni.request({
  57. url: BASE_URL + '/zswl-cloud-bdb/user/refreshToken',
  58. method: 'GET',
  59. data: {
  60. refreshToken: uni.getStorageSync('refreshToken')
  61. },
  62. success(r) {
  63. uni.hideLoading()
  64. if (r.data.content == '刷新令牌错误') {
  65. setTimeout(()=>{
  66. uni.showToast({
  67. title: '登录失效',
  68. icon: 'fail'
  69. })
  70. },0)
  71. uni.clearStorageSync()
  72. uni.reLaunch({
  73. url: '/pages/index/index'
  74. })
  75. } else {
  76. uni.setStorageSync('token', r.data.content) //登录状态
  77. // 刷新页面
  78. var pages = getCurrentPages();
  79. var url = '/' + pages[pages.length - 1].route
  80. uni.reLaunch({
  81. url
  82. })
  83. }
  84. }
  85. })
  86. }else{
  87. uni.hideLoading()
  88. console.log(111);
  89. uni.showModal({
  90. title:'请登录',
  91. confirmText:'去登录',
  92. success(res){
  93. if(res.confirm){
  94. var pages = getCurrentPages()
  95. let parmas = pages[pages.length - 1].options
  96. console.log(11111,parmas);
  97. let str = ''
  98. for (let key in parmas) {
  99. str += `&${key}=${parmas[key]}`
  100. }
  101. console.log(2222222,str);
  102. var url = '/' + pages[pages.length - 1].route
  103. console.log(333333,url);
  104. uni.reLaunch({
  105. url:'/login/login/login?redirect='+url+str
  106. })
  107. }else if (res.cancel) {
  108. console.log('拒绝登录,跳转首页');
  109. uni.switchTab({
  110. url:'/pages/index/index'
  111. })
  112. }
  113. }
  114. })
  115. }
  116. }
  117. }
  118. resolve(res.data)
  119. },
  120. //请求失败
  121. fail: (err) => {
  122. setTimeout(()=>{
  123. uni.showToast({
  124. title: '请求接口失败',
  125. icon:"none",
  126. duration: 5000
  127. })
  128. },0)
  129. reject(err)
  130. }
  131. })
  132. })
  133. }