123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import deepMerge from "../function/deepMerge";
- import validate from "../function/test";
- class Request {
-
- setConfig(customConfig) {
-
- this.config = deepMerge(this.config, customConfig);
- }
-
- request(options = {}) {
-
- if (this.interceptor.request && typeof this.interceptor.request === 'function') {
- let tmpConfig = {};
- let interceptorRequest = this.interceptor.request(options);
- if (interceptorRequest === false) {
-
- return new Promise(()=>{});
- }
- this.options = interceptorRequest;
- }
- options.dataType = options.dataType || this.config.dataType;
- options.responseType = options.responseType || this.config.responseType;
- options.url = options.url || '';
- options.params = options.params || {};
- options.header = Object.assign(this.config.header, options.header);
- options.method = options.method || this.config.method;
- return new Promise((resolve, reject) => {
- options.complete = (response) => {
-
- uni.hideLoading();
-
- clearTimeout(this.config.timer);
- this.config.timer = null;
-
- if(this.config.originalData) {
-
- if (this.interceptor.response && typeof this.interceptor.response === 'function') {
- let resInterceptors = this.interceptor.response(response);
-
- if (resInterceptors !== false) {
- resolve(resInterceptors);
- } else {
-
- reject(response);
- }
- } else {
-
- resolve(response);
- }
- } else {
- if (response.statusCode == 200) {
- if (this.interceptor.response && typeof this.interceptor.response === 'function') {
- let resInterceptors = this.interceptor.response(response.data);
- if (resInterceptors !== false) {
- resolve(resInterceptors);
- } else {
- reject(response.data);
- }
- } else {
-
- resolve(response.data);
- }
- } else {
-
-
-
-
-
-
- reject(response)
- }
- }
- }
-
- options.url = validate.url(options.url) ? options.url : (this.config.baseUrl + (options.url.indexOf('/') == 0 ?
- options.url : '/' + options.url));
-
-
-
-
- if(this.config.showLoading && !this.config.timer) {
- this.config.timer = setTimeout(() => {
- uni.showLoading({
- title: this.config.loadingText,
- mask: this.config.loadingMask
- })
- this.config.timer = null;
- }, this.config.loadingTime);
- }
- uni.request(options);
- })
-
-
-
-
-
- }
- constructor() {
- this.config = {
- baseUrl: '',
-
- header: {},
- method: 'POST',
-
- dataType: 'json',
-
- responseType: 'text',
- showLoading: true,
- loadingText: '请求中...',
- loadingTime: 800,
- timer: null,
- originalData: false,
- loadingMask: true,
- }
-
-
- this.interceptor = {
-
- request: null,
-
- response: null
- }
-
- this.get = (url, data = {}, header = {}) => {
- return this.request({
- method: 'GET',
- url,
- header,
- data
- })
- }
-
- this.post = (url, data = {}, header = {}) => {
- return this.request({
- url,
- method: 'POST',
- header,
- data
- })
- }
-
-
- this.put = (url, data = {}, header = {}) => {
- return this.request({
- url,
- method: 'PUT',
- header,
- data
- })
- }
-
-
- this.delete = (url, data = {}, header = {}) => {
- return this.request({
- url,
- method: 'DELETE',
- header,
- data
- })
- }
- }
- }
- export default new Request
|