request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (!options.compleLoading) { // 请求的接口有带compleLoading 就不隐藏加载中
  59. uni.hideLoading()
  60. }
  61. if (res.data.code == 401) {
  62. if (num <= 2) {
  63. if (!uni.getStorageSync('user_account')) {
  64. var pages = getCurrentPages() // 获取栈实例
  65. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  66. if(currentRoute != 'pages4/login/login') {
  67. uni.navigateTo({
  68. url: '/pages4/login/login'
  69. });
  70. }
  71. } else {
  72. num++
  73. res = await doRequest(options)
  74. }
  75. }else{
  76. uni.removeStorageSync('user_account');
  77. var pages = getCurrentPages() // 获取栈实例
  78. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  79. if(currentRoute != 'pages4/login/login') {
  80. uni.navigateTo({
  81. url: '/pages4/login/login'
  82. });
  83. }
  84. }
  85. }
  86. resolve(res)
  87. },
  88. fail: (err) => {
  89. uni.hideLoading()
  90. uni.showToast({
  91. title: "请求接口失败",
  92. icon: 'none'
  93. })
  94. reject(JSON.stringify(err))
  95. },
  96. complete: () => {
  97. // uni.hideLoading()
  98. // uni.hideToast()
  99. }
  100. })
  101. })
  102. async function doRequest(response) {
  103. let user_account = uni.getStorageSync('user_account')
  104. var datas = {
  105. url: '/refreshToken/' + user_account,
  106. method: 'get',
  107. noToken: true
  108. }
  109. const res = await myRequest(datas)
  110. if (res.data.code === 200) {
  111. uni.setStorageSync('token', res.data.data.token)
  112. var userInfo = {
  113. url: '/app/user/getInfo',
  114. method: 'get',
  115. }
  116. const resUser = await myRequest(userInfo)
  117. if (resUser.data.code === 200) {
  118. store.state.userInfo = resUser.data.data
  119. uni.setStorageSync('user_account',user_account)
  120. num = 1;
  121. }
  122. let onset = await myRequest(response)
  123. return onset
  124. } else {
  125. var pages = getCurrentPages() // 获取栈实例
  126. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  127. if(currentRoute != 'pages4/login/login') {
  128. uni.navigateTo({
  129. url: '/pages4/login/login'
  130. });
  131. }
  132. }
  133. }
  134. }