/* * @Author: your name * @Date: 2021-01-25 11:32:04 * @LastEditTime: 2021-03-08 21:22:17 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \rebatedine-mini\src\api\request.js */ /** * 通用uni-app网络请求 * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截 */ import { USER_TOKEN } from '../store/storageKeys' export default { config: { baseUrl: process.env.NODE_ENV === 'development1' ? 'http://localhost:18333/' : 'https://api.qcbwc.cn/', data: '', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, method: 'GET', timeout: 60000, dataType: 'json', responseType: '', success: () => {}, fail: () => {}, complete: () => {} }, request (options = {}) { options = Object.assign({}, this.config, options) options.url = options.baseUrl + options.url if (uni.getStorageSync(USER_TOKEN)) { options.header.token = uni.getStorageSync(USER_TOKEN) } return new Promise(resolve => { options.complete = ({ statusCode, data }) => { console.log(data) if ( statusCode !== 200 || !Object.prototype.hasOwnProperty.call(data, 'code') ) { uni.showToast({ title: '系统异常', icon: 'none' }) resolve(false) return } data.code = data.code * 1 if (data.code === 200) { resolve(data) } else if (data.code === 1000) { console.log(1111) uni.navigateTo({ url: '/pagesSub/other/login' }) } else { resolve(data) } } uni.request(options) }) }, get (options) { options.method = 'GET' return this.request(options) }, post (options) { options.method = 'POST' return this.request(options) } }