|
- /*
- * 整理ajax 统一入口,方便修改
- *
- * */
- import axios from 'axios'
- import { globalData ,memory, storage,goPage} from "./utils";
- // const baseUrl = process.env.NODE_ENV === 'development' ? 'http://39.99.172.42/api/home' : ''
- const baseUrl = 'https://dingdongapi**.**/api'
- const returnAll = false
- function fetch({ url, data, method = "get", report = false }) {
- console.log('ajax url', url, JSON.stringify(data))
- let user_id = storage.get('user_id')
- console.log('ajax user_id', user_id)
- let headers = {}
- // 写入headers
- if (user_id) {
- headers['user_id'] = user_id,
- headers['Authorization'] = storage.get('token')
- headers['Content-Type'] = "application/json"
- }
- // let arr = memory.get('token').split('.')
- // console.warn(user_id);
- // console.warn(atob(arr[0].split(' ')[1]));
- // console.warn(JSON.parse(atob(arr[1])));
- axios.interceptors.response.use(function (response) {
- return response;
- }, function (error) {
- error = JSON.parse(JSON.stringify(error))
- if(error.status===401){
- goPage("login_pwd");
- }
- return Promise.reject(error);
- });
- return new Promise((resolve, reject) => {
- try {
- let u = url.startsWith('/') ? `${baseUrl}${url}` : url
- // 手机环境是有apicloud的ajax
- if (method !== 'upload') {
- api.ajax({
- url: u,
- method,
- headers: {
- ...headers,
- "Content-Type": "application/json"
- },
- data: {
- body: data
- },
- report,
- returnAll
- }, (ret, err) => {
- if (err) {
- reject(err);
- }
- console.log('ajax result', JSON.stringify(ret))
- resolve(returnAll ? ret.body : ret);
- });
- } else {
- let files = data.files
- delete data.files
- api.ajax({
- url: u,
- method: 'post',
- headers: {
- ...headers,
- },
- data: {
- values: data,
- files
- },
- report,
- returnAll
- }, (ret, err) => {
- if (err) {
- reject(err);
- }
- console.log('ajax upload result', JSON.stringify(ret))
- resolve(returnAll ? ret.body : ret);
- });
- }
- } catch (e) {
- // 本地环境没有api的情况下使用axios 本地测试可用
- if (method === 'upload') {
- headers['Content-Type'] = 'application/x-www-form-urlencoded'
- }
- axios({
- url: url.startsWith('/') ? `/api${url}` : url,
- method: method === 'upload' ? 'post' : method,
- data,
- headers
- }).then((res,err) => {
- let { data } = res
- resolve(data)
- })
- }
- });
- }
- const ajax = {
- get(url) {
- let u = url
- if (!url.includes('ocr**.**-hangzhou.aliyuncs.com')) {
- u = url .includes('?') ? `${url}&r=${+new Date()}` : `${url}?r=${+new Date()}`
- }
- return fetch({ url: u })
- },
- post(url, data) {
- return fetch({ url, method: 'post', data })
- },
- upload(url, data) {
- return fetch({ url, method: 'upload', data })
- }
- }
- export default ajax
复制代码
大家帮我看下,每次登录状态过期的时候,返回401状态码,然后页面就卡死到这里了,错误提示再上图 |
|