123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- export default class Request {
- // 判断url是否为绝对路径
- static absolutelyUrl(url) {
- return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
- };
- // 发起请求前
- static beforeRequest(config) {
- return config
- };
- // 发起请求后
- static afterRequest(response) {
- return response
- };
- // 基本配置
- config = {
- baseUrl: '',
- header: {
- 'Content-Type': 'application/json;charset=UTF-8'
- },
- method: 'GET',
- dataType: 'json',
- responseType: 'text',
- success() {},
- fail() {},
- complete() {}
- };
- // 过滤器
- interceptor = {
- beforeRequest(callback) {
- if (callback) {
- Request.beforeRequest = callback
- }
- },
- afterRequest(callback) {
- if (callback) {
- Request.afterRequest = callback
- }
- }
- };
- // 设置配置
- setConfig(callback) {
- this.config = callback(this.config)
- }
- // 发起请求
- request(options = {}) {
- // 基本参数
- options.baseUrl = options.baseUrl || this.config.baseUrl
- options.dataType = options.dataType || this.config.dataType
- options.url = Request.absolutelyUrl(options.url) ? options.url : (options.baseUrl + options.url)
- options.data = options.data || {}
- options.header = options.header || this.config.header
- options.method = options.method || this.config.method
- // console.log(options.header);
- // options.
- // promise 请求
- return new Promise((resolve, reject) => {
- let execute = true
- let _config = null
- // 请求完成
- options.complete = (response) => {
- response.config = _config
- return resolve(Request.afterRequest(response))
- }
- // 默认配置 + 用户配置【复制作用】
- let mConfig = {
- ...this.config,
- ...options
- }
- // 取消请求 - 回调
- let cancel = (msg = 'request cancel') => {
- execute = false
- return resolve([{
- config: mConfig,
- data: {
- code: 0,
- msg: msg
- }
- }])
- }
- // 默认配置 + 用户配置 + 初始配置【复制作用】
- _config = {
- ...mConfig,
- ...Request.beforeRequest(mConfig, cancel)
- }
- // 是否取消请求【如果execute == false, 那么已经resolve了,所以不需要return resolve】
- if (!execute) return
- // 发起请求
- if (_config.hasOwnProperty('uploadFile')) {
- const data = _config.data
- let tConfig = {
- url: _config.url,
- filePath: data.file.file,
- name: data.file.name,
- formData: data.form,
- // #ifdef MP-WEIXIN
- header: _config.header,
- // #endif
- success: _config.success,
- fail: _config.fail,
- complete: _config.complete,
- }
- uni.uploadFile(tConfig)
- } else {
- uni.request(_config)
- }
- })
- };
- // 接口请求 - 支持重试
- autoRetry(options, retryMax = 3) {
- // console.log('autoRetry', options, retryMax);
- return new Promise((resolve, reject) => {
- if (retryMax <= 0) {
- return resolve(['网络错误']);
- }
- if (retryMax == 2) {
- // console.log('this.request exect');
- this.request(options).then(([err, res]) => {
- // console.log('this.request', err, res);
- if (!err) {
- resolve([err, res]);
- } else {
- this.autoRetry(options, retryMax - 1);
- }
- });
- } else {
- return this.autoRetry(options, retryMax - 1);
- }
- });
- }
- // GET 请求
- get(url, data, options = {}) {
- options.url = url
- options.data = data
- options.method = 'GET'
- return this.request(options)
- }
- // POST 请求
- post(url, data, options = {}) {
- options.url = url
- options.data = data
- options.method = 'POST'
- return this.request(options)
- }
- }
|