request.js 4.2 KB

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