request.js 3.8 KB

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