request.js 5.5 KB

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