123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // 引入配置文件
- import config from "@/common/config.js";
- // #ifdef MP-WEIXIN
- import wxApp from "@/common/wecaht.min.app.js"
- import {
- getToken,
- clearToken
- } from '@/common/token.js'
- // #endif
- const filterNoToken = ["/api/auth/refreshToken", "/api/auth/login"];
- export default {
- config: {
- baseUrl: config.apiUrl,
- header: {
- 'Content-Type': 'application/json;charset=UTF-8',
- },
- data: {},
- method: "GET",
- dataType: "json",
- },
- async request(options = {}) {
- // 请求拦截器
- uni.addInterceptor('request', {
- invoke: (request) => {
- // 在发送请求之前的处理逻辑,例如添加请求头、请求日志等
- // request.header = {
- // 'Content-Type': 'application/json', // 设置请求头
- // // 在这里可以添加其他自定义请求头
- // };
- // console.log('Request:', request);
- return request;
- },
- });
- // 响应拦截器
- uni.addInterceptor('response', {
- invoke: (response) => {
- // 在接收到响应数据之后的处理逻辑,例如统一处理错误码、响应日志等
- // console.log('Response:', response);
- return response;
- },
- });
- options.header = options.header || this.config.header;
- options.method = options.method || this.config.method;
- options.dataType = options.dataType || this.config.dataType;
- let version = 'web';
- // #ifdef MP-WEIXIN
- // 微信小程序处理
- options.header.appid = wxApp.getWXAppId();
- options.header.source = 'wxapp';
- // 过滤不需要token的api
- if (filterNoToken.indexOf(options.url) === -1) {
- // console.log('getTokenString ',options.url )
- options.header.Authorization = await (this.getTokenString());
- }
- // #endif
- options.url = this.config.baseUrl + options.url;
- console.log('options.url ', options.url)
- return new Promise((resolve, reject) => {
- options.success = (e) => {
- // console.log(e, 'successsuccesssuccesssuccesssuccess')
- let res = e.data;
- if (e.statusCode === 200) {
- if (res.code == 500201) {
- // 未登录
- clearToken();
- getToken();
- if (filterNoToken.indexOf(options.url) === -1) {
- return this.request(options);
- }
- } else {
- if (res.code != 0) {
- uni.showToast({
- title: res.msg,
- duration: 1000,
- icon: 'none'
- });
- }
- }
- resolve(res);
- } else {
- uni.showToast({
- title: "网络异常,请稍后再试",
- duration: 1000,
- icon: 'none'
- });
- reject(new Error('Request failed'));
- }
- }
- options.fail = (err) => {
- console.log(err)
- reject(err);
- }
- uni.request(options)
- })
- },
- get(url, data, options = {}) {
- options.url = url;
- options.data = data;
- options.method = 'GET';
- return this.request(options);
- },
- post(url, data, options = {}) {
- options.url = url;
- options.data = data;
- options.method = 'POST';
- return this.request(options);
- },
- // 上传图片
- upload(url, options = {}) {
- options.url = this.config.baseUrl + url;
- options.header = options.header || this.config.header;
- options.fileType = options.fileType || "image";
- options.formData = options.formData || {};
- options.filePath = options.filePath;
- options.name = options.name;
- // TODO:token增加等操作
- if (options.token) {
- // 验证是否登录
- if (!this.checkToken(options.checkToken)) return;
- // 验证权限
- if (!this.checkAuth(options.checkAuth)) return;
- options.header.token = User.token;
- }
- return uni.uploadFile(options);
- },
- // 错误处理
- errorCheck(err, res, errfun = false, resfun = false) {
- if (err) {
- typeof errfun === 'function' && errfun();
- uni.showToast({
- title: '加载失败',
- icon: "none"
- });
- return false;
- }
- if (res.data.errorCode) {
- typeof errfun === 'function' && resfun();
- uni.showToast({
- title: res.data.msg,
- icon: "none"
- });
- return false;
- }
- return true;
- },
- async getTokenString() {
- // console.log('getTokenString-----start');
- await getToken();
- let token = uni.getStorageSync('token');
- // token格式化
- if (token && typeof token === "string") {
- token = JSON.parse(token);
- }
- // console.log('getTokenString--end', token);
- return token.token;
- }
- }
|