123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /**
- * 通用uni-app网络请求
- * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
- */
- /*
- */
- export default {
- config: {
- baseUrl: "",
- header: {},
- data: {},
- method: "GET",
- dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
- responseType: "text",
- success() {},
- fail() {},
- complete() {}
- },
- interceptor: {
- request: null,
- response: null,
- },
- reqInterceptors : null,
- resInterceptors : null,
- request(options) {
- if (!options) {
- options = {}
- }
- options.baseUrl = options.baseUrl || this.config.baseUrl
- options.dataType = options.dataType || this.config.dataType
- options.url = options.baseUrl + options.url
- options.data = options.data || {}
- options.method = options.method || this.config.method
- return new Promise((resolve, reject) => {
- let _config = null
- const _this = this;
- options.complete = (response) => {
- let res = response;
- let statusCode = response.statusCode
- res.config = _config
- if (process.env.NODE_ENV === 'development') {
- if (statusCode === 200) {
- //console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(res.data))
- }
- }
- _reslog(response);
- if (this.interceptor.response && typeof this.interceptor.response === 'function') {
- let resInterceptors = this.interceptor.response(res);
- /* if (!resInterceptors) {
- reject('返回值已被您拦截!');
- return;
- } else */ if (Object.prototype.toString.call(resInterceptors) === "[object Promise]") {
- try {
- let promiseRes = resInterceptors;
- //console.log('请求拦截处理')
- promiseRes.then((res)=>{
- //console.log('请求数据没有问题视为成功')
- resolve(res)
- }).catch((ret)=>{
- //console.log('请求数据有问题视为失败')
- reject(ret)
- })
- } catch (error) {
- reject(error)
- }
- } else {
- res = resInterceptors;
- resolve(res);
- }
- }
-
-
- }
- _config = Object.assign({}, this.config, options)
- _config.requestId = new Date().getTime()
- if (this.interceptor.request) {
- this.interceptor.request(_config)
- }
- // 统一的请求日志记录
- _reqlog(_config)
- uni.request(_config);
- });
- },
- get(url, data, options) {
- if (!options) {
- options = {}
- }
- options.url = url
- options.data = data
- options.method = 'GET'
- return this.request(options)
- },
- post(url, data, options) {
- if (!options) {
- options = {}
- }
- options.url = url
- options.data = data
- options.method = 'POST'
- return this.request(options)
- },
- put(url, data, options) {
- if (!options) {
- options = {}
- }
- options.url = url
- options.data = data
- options.method = 'PUT'
- return this.request(options)
- },
- delete(url, data, options) {
- if (!options) {
- options = {}
- }
- options.url = url
- options.data = data
- options.method = 'DELETE'
- return this.request(options)
- }
- }
- /**
- * 请求接口日志记录
- */
- function _reqlog(req) {
- if (process.env.NODE_ENV === 'development') {
- //console.log("【" + req.requestId + "】 请求地址:" + req.url)
- if (req.data) {
- //console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
- //console.log("【" + req.requestId + "】 请求头:" + JSON.stringify(req.header))
- }
- }
- //TODO 调接口异步写入日志数据库
- }
- /**
- * 响应接口日志记录
- */
- function _reslog(res) {
- let _statusCode = res.statusCode;
- if (process.env.NODE_ENV === 'development') {
- //console.log("【" + res.config.requestId + "】 请求地址:" + res.config.url)
- if (res.config.data) {
- //console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
- }
- //console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
- }
- //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
- switch(_statusCode){
- case 200:
- break;
- case 401:
- break;
- case 404:
- break;
- default:
- break;
- }
- }
|