/* * @Author: your name * @Date: 2021-01-25 11:32:04 * @LastEditTime: 2021-01-25 13:33:39 * @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 === 'development' ? 'https://apitest.topboom0912.com/bwc/' : 'https://apitest.topboom0912.com/bwc/', 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 }) => { if (statusCode !== 200) { uni.showToast({ title: '系统异常', icon: 'none' }) return } data.code = data.code * 1 if (data.code === 200) { resolve(data) } else if (data.code === 1000) { uni.navigateTo({ url: '/pagesSub/login/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) } }