1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- // 'http://192.168.1.222:8088','https://file-dev.xyyxt.net/'线下
- // 'https://api.xyyxt.net' 'https://file.xyyxt.net/'线上
- import store from '@/store/index.js'
- import api from './api.js'
- var num = 1
- export const BASE_URL = 'http://192.168.1.222:5055' //接口api http://42.192.164.187:19005 http://192.168.1.222:5055
- export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/' //图片上传api 'https://file.xyyxt.net/
- export const tenantId = '867735392558919680'
- export const myRequest = (options) => {
- if (store.state.allowLoading) {
- uni.showLoading({
- title: '拼命加载中...'
- })
- }
- return new Promise((resolve, reject) => {
- let token = uni.getStorageSync('token')
- uni.request({
- url: BASE_URL + options.url,
- method: options.method || 'GET',
- data: options.data,
- header: options.noToken ? {
- TenantId: tenantId,
- } : {
- AuthorizationToken: 'WX ' + (token ? token : uni.getStorageSync('token_temp')),
- TenantId: tenantId
- },
- success: async (res) => {
- if (res.data.code == 401) {
- if (num <= 3) {
- if (!uni.getStorageSync('user_account')) {
- uni.navigateTo({
- url: '/pages/login/login'
- });
- } else {
- num++
- res = await doRequest(options)
- }
- };
- }
- resolve(res)
- },
- fail: (err) => {
- uni.showToast({
- title: "请求接口失败",
- icon: 'none'
- })
- reject(JSON.stringify(err))
- },
- complete: () => {
- uni.hideLoading()
- // uni.hideToast()
- }
- })
- })
- async function doRequest(response) {
- var datas = {
- url: '/refreshToken/' + uni.getStorageSync('user_account'),
- method: 'get',
- noToken: true
- }
- const res = await myRequest(datas)
- if (res.data.code === 200) {
- uni.setStorageSync('token', res.data.data.token)
- var userInfo = {
- url: '/app/user/getInfo',
- method: 'get',
- }
- const resUser = await myRequest(userInfo)
- if (resUser.data.code === 200) {
- store.state.userInfo = resUser.data.data
- }
- let onset = await myRequest(response)
- return onset
- } else {
- uni.navigateTo({
- url: '/pages/login/login'
- });
- }
- }
- }
|