request.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 method from '@/common/methodTool.js'
  5. import api from './api.js'
  6. var num = 1
  7. //接口api
  8. export const BASE_URL = 'https://api.xyyxt.net' //release
  9. // export const BASE_URL = 'https://test.xyyxt.net' //预发布
  10. // export const BASE_URL = 'http://120.79.166.78:19012' // 测试环境
  11. // export const BASE_URL = 'http://42.192.164.187:19005' //test(后端本地)
  12. // export const BASE_URL = 'http://192.168.1.222:5055' //后端本地更新到222
  13. // export const BASE_URL = 'http://192.168.1.24:5055' //后端本地
  14. // export const BASE_URL = 'http://192.168.1.7:5055' //后端本地
  15. //图片上传api
  16. export const BASE_IMG_URL = 'https://file.xyyxt.net/' //release
  17. // export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/' //test
  18. // web_view的
  19. // export const WEBVIEW_URL = 'https://m.xyyxt.net/' //release-祥粤云学堂
  20. // export const WEBVIEW_URL = 'https://cm.xyyxt.net/' //release-祥粤学堂
  21. export const WEBVIEW_URL = 'https://zstcm.xyyxt.net/' //release-中建云学堂
  22. // export const WEBVIEW_URL = 'https://testm.xyyxt.net/' //预发布
  23. // export const WEBVIEW_URL = 'http://120.79.166.78:18001/' // 测试环境-祥粤云学堂
  24. // export const WEBVIEW_URL = 'http://120.79.166.78:18011/' // 测试环境-祥粤学堂
  25. // export const WEBVIEW_URL = 'http://api.xyyxt.net:6066/' //预发布(弃用了)
  26. // export const socket_url = 'ws://42.192.164.187:19005/webSocket/' //test
  27. // export const socket_url = 'ws://120.79.166.78:19009/webSocket/' //预发布
  28. // export const socket_url = 'wss://api.xyyxt.net/webSocket/' //release
  29. export const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
  30. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4qkbwIKErstK1sFESPEhOShpR
  31. pj4+sOVpJHxl5r/2xLBfA/MrXcAEra5Ro9cXNQSqmLLt8wecoLk/glfa5IdhXV0h
  32. RVQplIVs5z3MxcUa9ptKPHUTgh8xMCBvl8sUJKwkmn4vYWeDfHT22EL7Hr1pTMwU
  33. hF6WiNlWfQTVoF1rhwIDAQAB
  34. -----END PUBLIC KEY-----`;
  35. export const version = '1.0.3'
  36. export const tenantId = '567735392758918520' // 祥粤云学堂:867735392558919680,祥粤学堂:667735392758919630, 中建云学堂-567735392758918520
  37. export const myRequest = (options) => {
  38. if (store.state.allowLoading && !options.noLoading) {
  39. uni.showLoading({
  40. title: '拼命加载中...',
  41. mask:true,
  42. })
  43. }
  44. return new Promise((resolve, reject) => {
  45. let token = uni.getStorageSync('token')
  46. uni.request({
  47. url: BASE_URL + options.url,
  48. method: options.method || 'GET',
  49. data: options.data,
  50. header: options.noToken ? {
  51. TenantId: tenantId,
  52. } : {
  53. AuthorizationToken: 'WX ' + (token ? token : uni.getStorageSync('token_temp')),
  54. TenantId: tenantId
  55. },
  56. success: async (res) => {
  57. // if(num == 2) {
  58. // return;
  59. // } else {
  60. // num++
  61. // }
  62. // doRequest(options)
  63. if (!options.compleLoading) { // 请求的接口有带compleLoading 就不隐藏加载中
  64. uni.hideLoading()
  65. }
  66. if (res.data.code == 401) {
  67. if (num <= 2) {
  68. if (!uni.getStorageSync('user_account')) {
  69. var pages = getCurrentPages() // 获取栈实例
  70. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  71. console.log('request-->1111:', currentRoute);
  72. if(currentRoute != 'pages4/login/login') {
  73. uni.navigateTo({
  74. url: '/pages4/login/login'
  75. });
  76. }
  77. } else {
  78. num++
  79. res = await doRequest(options)
  80. }
  81. }else{
  82. uni.removeStorageSync('user_account');
  83. var pages = getCurrentPages() // 获取栈实例
  84. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  85. console.log('request-->2222:', currentRoute);
  86. if(currentRoute != 'pages4/login/login') {
  87. uni.navigateTo({
  88. url: '/pages4/login/login'
  89. });
  90. }
  91. }
  92. }
  93. resolve(res)
  94. },
  95. fail: (err) => {
  96. uni.hideLoading()
  97. uni.showToast({
  98. title: "请求接口失败",
  99. icon: 'none'
  100. })
  101. console.log('请求失败err---: ', err);
  102. if (options.url == '/face/certification/CompareFace') {
  103. console.log('人脸识别错误---1:', err)
  104. }
  105. reject(JSON.stringify(err))
  106. },
  107. complete: () => {
  108. // uni.hideLoading()
  109. // uni.hideToast()
  110. }
  111. })
  112. })
  113. async function doRequest(response) {
  114. let user_account = uni.getStorageSync('user_account')
  115. var datas = {
  116. url: '/refreshToken/' + user_account,
  117. method: 'get',
  118. noToken: true
  119. }
  120. const res = await myRequest(datas)
  121. if (res.data.code === 200) {
  122. uni.setStorageSync('token', res.data.data.token)
  123. var userInfo = {
  124. url: '/app/user/getInfo',
  125. method: 'get',
  126. data: { fromPlat: 1 }, // 来源平台 1小程序 2PC网站
  127. }
  128. const resUser = await myRequest(userInfo)
  129. if (resUser.data.code === 200) {
  130. store.state.userInfo = resUser.data.data
  131. uni.setStorageSync('user_account',user_account)
  132. num = 1;
  133. }
  134. let onset = await myRequest(response)
  135. return onset
  136. } else {
  137. var pages = getCurrentPages() // 获取栈实例
  138. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  139. console.log('request-->333:', currentRoute);
  140. if(currentRoute != 'pages4/login/login') {
  141. uni.navigateTo({
  142. url: '/pages4/login/login'
  143. });
  144. }
  145. }
  146. }
  147. }