request.js 4.4 KB

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