import qs from 'qs'; import config from '../config/index' import { getPlatform, objParseUrlAndParam } from '../utils/index' /** * 通用uni-app网络请求 * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截 */ export class HttpInterface { constructor() { this.config = { baseUrl: config.api, // 请求域名 header: { // 'Content-Type': 'application/json;charset=UTF-8', 'Content-Type': 'application/x-www-form-urlencoded', }, data: {}, method: 'GET', // dataType: 'json' /* 如设为json,会对返回的数据做一次 JSON.parse */, // responseType: 'text', success() {}, fail() {}, complete() {} } this.interceptor = { before: _config => { return _config }, after: null } } request(options) { let that = this if (!options) { options = {} } options.baseUrl = options.baseUrl || this.config.baseUrl //if (process.env.NODE_ENV === // 'development') { // options.url = this.addTemp(options.baseUrl + // 'devapi/' + options.url) // 增加时间戳 //} else { options.url = this.addTemp(options.baseUrl + options.url) // 增加时间戳 //} options.url = this.addPlatform( options.url) // 增加使用平台 options.data = qs.stringify(options.data || {}) options.method = options.method || this.config.method if (options.method === 'get' || options.method === 'GET') { options.url = objParseUrlAndParam( options.url, options.data) } // TODO 加密数据 // TODO 数据签名 return new Promise((resolve, reject) => { let _config = null options.complete = response => { let statusCode = response.statusCode response.config = _config // 请求后的拦截器 if (this.interceptor.after) { let newResponse = this.interceptor .after(response) if (newResponse) { response = newResponse } } // 统一的响应日志记录 that._reslog(response) if (statusCode === 200) { // 成功 resolve(response.data) } else { reject({code: 0, msg: '请求出错'}) } } _config = Object.assign({}, this.config, options) _config.requestId = new Date().getTime() // 请求前的拦截器 if (this.interceptor.before) { let newConfig = this.interceptor .before(_config) if (newConfig) { _config = newConfig } else { return } } // 统一的请求日志记录 that._reqlog(_config) uni.request(_config) }) } get(options) { if (!options) { options = {} } options.method = 'GET' return this.request(options) } post(options) { if (!options) { options = {} } options.method = 'POST' return this.request(options) } /** * 统一的响应日志记录 * @param {*} req */ _reqlog(req) { if (process.env.NODE_ENV === 'development') { if (req.data) { } } // TODO 调接口异步写入日志数据库 } /** * 统一的请求日志记录 * @param {*} res */ _reslog(res) { let _statusCode = res.statusCode if (process.env.NODE_ENV === 'development') { if (res.config.data) { } } // TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库 switch (_statusCode) { case 200: break case 401: break case 404: break default: break } } /** * 增加时间戳,用于防止请求缓存 * @param {*} url */ addTemp(url) { if (url.indexOf('?') === -1) { url = `${url}?_=${new Date().getTime()}` } else { url = url.replace('?', `?_=${new Date().getTime()}&`) } return url } /** * 增加使用平台信息 * @param {*} url */ addPlatform(url) { if (url.indexOf('?') === -1) { url = `${url}?_platform_=${getPlatform()}` } else { url = url.replace('?', `?_platform_=${getPlatform()}&`) } return url } }