request.js 4.0 KB

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