request.js 4.6 KB

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