request.js 5.1 KB

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