request.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // 'http://192.168.1.222:8088','https://file-dev.xyyxt.net/'线下
  2. // 'https://api.xyyxt.net' 'https://file.xyyxt.net/'线上
  3. import store from '@/store/index.js'
  4. import api from './api.js'
  5. var num = 1
  6. //接口api
  7. // export const BASE_URL = 'https://api.xyyxt.net' //release
  8. export const BASE_URL = 'https://test.xyyxt.net' //预发布
  9. // export const BASE_URL = 'http://42.192.164.187:19005' //test
  10. // export const BASE_URL = 'http://192.168.1.222:5055' //dev
  11. //图片上传api
  12. // export const BASE_IMG_URL = 'https://file.xyyxt.net/' //release
  13. export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/' //test
  14. // web_view的
  15. // export const WEBVIEW_URL = 'http://192.168.1.38:8082/' //test
  16. // export const WEBVIEW_URL = 'http://api.xyyxt.net:6066/' //预发布(弃用了)
  17. export const WEBVIEW_URL = 'https://testm.xyyxt.net/' //预发布
  18. // export const WEBVIEW_URL = 'https://m.xyyxt.net/' //release
  19. // export const socket_url = 'ws://42.192.164.187:19005/webSocket/' //test
  20. // export const socket_url = 'ws://120.79.166.78:19009/webSocket/' //预发布
  21. // export const socket_url = 'wss://api.xyyxt.net/webSocket/' //release
  22. export const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
  23. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4qkbwIKErstK1sFESPEhOShpR
  24. pj4+sOVpJHxl5r/2xLBfA/MrXcAEra5Ro9cXNQSqmLLt8wecoLk/glfa5IdhXV0h
  25. RVQplIVs5z3MxcUa9ptKPHUTgh8xMCBvl8sUJKwkmn4vYWeDfHT22EL7Hr1pTMwU
  26. hF6WiNlWfQTVoF1rhwIDAQAB
  27. -----END PUBLIC KEY-----`;
  28. export const version = '6.0.0'
  29. export const tenantId = '867735392558919680'
  30. export const myRequest = (options) => {
  31. if (store.state.allowLoading && !options.noLoading) {
  32. uni.showLoading({
  33. title: '拼命加载中...',
  34. mask:true,
  35. })
  36. }
  37. return new Promise((resolve, reject) => {
  38. let token = uni.getStorageSync('token')
  39. uni.request({
  40. url: BASE_URL + options.url,
  41. method: options.method || 'GET',
  42. data: options.data,
  43. header: options.noToken ? {
  44. TenantId: tenantId,
  45. } : {
  46. AuthorizationToken: 'WX ' + (token ? token : uni.getStorageSync('token_temp')),
  47. TenantId: tenantId
  48. },
  49. success: async (res) => {
  50. // if(num == 2) {
  51. // return;
  52. // } else {
  53. // num++
  54. // }
  55. // doRequest(options)
  56. if (res.data.code == 401) {
  57. if (num <= 2) {
  58. if (!uni.getStorageSync('user_account')) {
  59. var pages = getCurrentPages() // 获取栈实例
  60. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  61. if(currentRoute != 'pages4/login/login') {
  62. uni.navigateTo({
  63. url: '/pages4/login/login'
  64. });
  65. }
  66. } else {
  67. num++
  68. res = await doRequest(options)
  69. }
  70. }else{
  71. uni.removeStorageSync('user_account');
  72. var pages = getCurrentPages() // 获取栈实例
  73. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  74. if(currentRoute != 'pages4/login/login') {
  75. uni.navigateTo({
  76. url: '/pages4/login/login'
  77. });
  78. }
  79. }
  80. }
  81. resolve(res)
  82. },
  83. fail: (err) => {
  84. uni.showToast({
  85. title: "请求接口失败",
  86. icon: 'none'
  87. })
  88. reject(JSON.stringify(err))
  89. },
  90. complete: () => {
  91. uni.hideLoading()
  92. // uni.hideToast()
  93. }
  94. })
  95. })
  96. async function doRequest(response) {
  97. let user_account = uni.getStorageSync('user_account')
  98. var datas = {
  99. url: '/refreshToken/' + user_account,
  100. method: 'get',
  101. noToken: true
  102. }
  103. const res = await myRequest(datas)
  104. if (res.data.code === 200) {
  105. uni.setStorageSync('token', res.data.data.token)
  106. var userInfo = {
  107. url: '/app/user/getInfo',
  108. method: 'get',
  109. }
  110. const resUser = await myRequest(userInfo)
  111. if (resUser.data.code === 200) {
  112. store.state.userInfo = resUser.data.data
  113. uni.setStorageSync('user_account',user_account)
  114. num = 1;
  115. }
  116. let onset = await myRequest(response)
  117. return onset
  118. } else {
  119. var pages = getCurrentPages() // 获取栈实例
  120. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  121. if(currentRoute != 'pages4/login/login') {
  122. uni.navigateTo({
  123. url: '/pages4/login/login'
  124. });
  125. }
  126. }
  127. }
  128. }