123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- // 引入配置文件
- import config from "./config.js";
- // #ifdef MP-WEIXIN
- import wxApp from "./wecaht.min.app.js"
- import {
- getToken,
- clearToken
- } from './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 = {}) {
- 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 uni.request(options).then(res => {
- res = res[1].data;
- if (res.code == 500201) {
- // 未登录
- clearToken();
- getToken();
- if (filterNoToken.indexOf(options.url) === -1) {
- return this.request(options);
- }
- }
- return res.data;
- });
- },
- 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;
- }
- }
|