request.js 3.9 KB

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