asd/asd-wx/common/api/request.js
愉快的大福 340a467ba1 init
2024-11-21 11:32:11 +08:00

63 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
baseUrl
} from "@/common/utils/config.js" //请求根路径(服务器地址)
import store from '@/common/store/index.js'
import Vue from "vue"
import Common from '@/common/utils/common.js'
// process.env.NODE_ENV === 'development' ? 'http://192.168**:6002' : 'http://***/api'; //环境配置
//向外暴露一个方法,传入一个空对象
export function service(options = {}, contentType) {
options.url = `${baseUrl}${options.url}`; //前面为你的服务器地址,后面为具体接口地址
//配置请求头
options.header = {
'content-type': contentType ? contentType : "application/json", //默认请求头,可不写
'Authorization': store.getters.token //Bearer ,你请求数据需要的自定义请求头(令牌)
};
// 创建promise
return new Promise((resolved, rejected) => {
//成功
options.success = (res) => {
if (res.data.code === 401) {
uni.navigateTo({
url: '/loginPackage/pages/Login'
})
}
if (Number(res.data.code) == 200||res.data.paySign||res.data.isPay) { //请求成功
resolved(res.data); //请求成功时返回接口数据
} else {
uni.showToast({
icon: 'none',
duration: 3000,
title: res.data.message
});
if (res.data.message == "Token失效请重新登录!") {
store.commit("logout");
setTimeout(() => {
uni.navigateTo({
url: '/loginPackage/pages/Login'
})
}, 1500)
}
rejected(res); //请求失败时返回错误信息
}
}
//错误
options.fail = (err) => {
if (res.data.code === 401) {
uni.navigateTo({
url: '/loginPackage/pages/Login'
})
}
rejected(err); //请求失败时返回错误信息
}
console.log(options);
uni.request(options); //传入配置好的对象
});
}