request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.5.12'
  36. export const tenantId = '867735392558919680' // 祥粤云学堂: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. uni.removeStorageSync('h5_code')
  77. }
  78. } else {
  79. num++
  80. res = await doRequest(options)
  81. }
  82. }else{
  83. uni.removeStorageSync('user_account');
  84. var pages = getCurrentPages() // 获取栈实例
  85. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  86. console.log('request-->2222:', currentRoute);
  87. if(currentRoute != 'pages4/login/login') {
  88. uni.navigateTo({
  89. url: '/pages4/login/login'
  90. });
  91. uni.removeStorageSync('h5_code')
  92. }
  93. }
  94. }
  95. resolve(res)
  96. },
  97. fail: (err) => {
  98. uni.hideLoading()
  99. uni.showToast({
  100. title: "请求接口失败",
  101. icon: 'none'
  102. })
  103. console.log('请求失败err---: ', options.url);
  104. if (options.url == '/face/certification/CompareFace') {
  105. console.log('人脸识别错误---1:', err)
  106. }
  107. reject(JSON.stringify(err))
  108. },
  109. complete: () => {
  110. // uni.hideLoading()
  111. // uni.hideToast()
  112. }
  113. })
  114. })
  115. async function doRequest(response) {
  116. let user_account = uni.getStorageSync('user_account')
  117. var datas = {
  118. url: '/refreshToken/' + user_account,
  119. method: 'get',
  120. noToken: true
  121. }
  122. const res = await myRequest(datas)
  123. if (res.data.code === 200) {
  124. uni.setStorageSync('token', res.data.data.token)
  125. var userInfo = {
  126. url: '/app/user/getInfo',
  127. method: 'get',
  128. data: { fromPlat: 1 }, // 来源平台 1小程序 2PC网站
  129. }
  130. const resUser = await myRequest(userInfo)
  131. if (resUser.data.code === 200) {
  132. store.state.userInfo = resUser.data.data
  133. uni.setStorageSync('user_account',user_account)
  134. num = 1;
  135. }
  136. let onset = await myRequest(response)
  137. return onset
  138. } else {
  139. var pages = getCurrentPages() // 获取栈实例
  140. let currentRoute = pages[pages.length-1].route; // 获取当前页面路由
  141. console.log('request-->333:', currentRoute);
  142. if(currentRoute != 'pages4/login/login') {
  143. uni.navigateTo({
  144. url: '/pages4/login/login'
  145. });
  146. uni.removeStorageSync('h5_code')
  147. }
  148. }
  149. }
  150. }