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