UNPKG

@hzy1123581324/z-view-ui

Version:

z-view-ui是使用vue3开发的组件,开发中,有部分组件功能未实现,慎用

157 lines (131 loc) 3.86 kB
import { guid } from "./guid.js"; export const requestTask = {}; let loadingCount = 0; let token; function getToken() { token = uni.getStorageSync('token'); return token; } export function abort(path) { requestTask && requestTask[path]?.abort(); } export class RequestAPI { constructor(param = {}) { this.base = param.base // 推送用 var cid = undefined; // #ifdef APP-PLUS var pinf = plus.push.getClientInfo(); cid = pinf.clientid || ''; //客户端标识 console.log(pinf) // #endif // #ifdef APP-PLUS console.log(pinf) this.header = param.header || { "content-type": "application/json;charset=utf-8", cid, } this.interceptors = Object.assign({ response: this.defaultResponse, request: this.defaultRequest }, param.interceptors); // post this.post = (url,data = {},method="POST", header = {},ohter,isWrongFunc = (data, header) => false)=>{ console.log(header,'1234170234891723049817234') return this.api(url,data,method, header,ohter,isWrongFunc); } // get请求 this.get=(url,data = {},method="GET", header = {},ohter,isWrongFunc = (data, header) => false)=>{ return this.api(url,data,method, header,ohter,isWrongFunc); } // 请求主体 this.api=(url, data = {},method="GET", header = {},ohter={},isWrongFunc = (data, header) => false )=> { let isWrong=false; ({ header={}, data={}, isWrong=false } = this.interceptors.request(data,header,ohter,isWrongFunc ) ) if(isWrong){ return Promise.reject(); } return new Promise((resolve, reject) => { // console.log(i18n.global.t('esm1')); if (ohter.showLoading) { loadingCount++; // console.log(showLoading,loadingCount,'(((((((((())))))))))') uni.showLoading({ mask: true }) } // 中断请求任务 // requestTask.abort(); // 当多个接口同时调用是可能会出现只中断最后一个 // requestTask = uni.request({ let iscout = false; requestTask[url] = uni.request({ url: this.base + url, data, method, header: this.header, success: (res) => { this.interceptors.response(res,{...data,...ohter}, resolve, reject); }, fail: (err) => { reject(err) }, complete: () => { // console.log('^^^^^^^^^^^^^^^6') // requestTask = null; if (ohter.showLoading) { !iscout && loadingCount--; if (loadingCount <= 0) { uni.hideLoading(); } } delete requestTask[url]; } }); }); } } // 默认请求拦截器 defaultRequest(data, header, isWrongFunc = (data, header) => false) { return { data, header, isWrong: isWrongFunc(data, header), } } // 默认响应拦截器 defaultResponse(res, config={}, resolve = Promise.resolve, reject = Promise.reject) { switch (res.data.code) { case 200: return resolve(res.data); case 402: // 跳转到登录页 return reject(res.data); default: if (config.showLoading) { loadingCount--; iscout = true; if (loadingCount <= 0) { uni.hideLoading(); } } if (res.data.msg) { uni.showToast({ title: res.data.msg || '', icon: 'none' }) } return reject(res.data); } } } // export class UpdateFileAPI { // constructor(param= {}){ // } // }