123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- /**
- * 2019年4月5日12:44:58
- * 简单封装uni-app请求,下载,上传。
- */
- let _baseuUrl = '';
- let _isUpOpenDown = false; //是否在上传js中引入下载的js
- let _defaultReq = {
- isreq: true, //是否已经打开ajax,默认为true
- url: '', //独立的url ajax
- baseData: {}, //ajax基本参数
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- type: 'GET',
- dataType: 'json',
- responseType: 'text',
- beforeSend:r=>r,
- beforeFinsh: r=> r
- }
- let _defaultUp = {
- url: '', //独立的url
- baseData: {},
- header: {
- 'content-type': 'multipart/form-data;'
- },
- }
- /**
- * 代理控制 2019年4月6日16:06:05
- */
- let ProxyControll = (obj, callback = (key, val, oldval) => {}) => {
- for (let key in obj) {
- let itemval = obj[key];
- Object.defineProperty(obj, key, {
- enumerable: true,
- get: function() {
- return this[`HHYANG_${key}`]
- },
- set: function(newvalue) {
- callback(key, newvalue, this[`HHYANG_${key}`]);
- this[`HHYANG_${key}`]= newvalue;
- }
- })
- obj[key] = itemval;
- }
- }
- ProxyControll(_defaultReq);
- ProxyControll(_defaultUp);
- class Request {
- constructor(arg) {
- this.platform = this.platformChunk();
- this.defaultReq = _defaultReq;
- this.defaultUp = _defaultUp;
- }
- set baseuUrl(value) {
- _baseuUrl = value;
- _defaultReq.url = value;
- _defaultUp.url = value;
- }
- get baseuUrl() {
- return _baseuUrl;
- }
- set isUpOpenDown(value) {
- _isUpOpenDown = value;
- }
- get isUpOpenDown() {
- return _isUpOpenDown;
- }
- /**
- * 基本ajax请求
- */
- ajax({
- path = '', //请求路径
- title = false, //请求头部 默认为false不显示, 传入字符串则显示 推荐7个字内
- header = this.defaultReq.header, //请求header 默认为"application/x-www-form-urlencoded"
- data = {}, //请求数据,默认为空对象
- type = this.defaultReq.type, //请求类型 默认为'GET'
- dataType = this.defaultReq.dataType, //返回数据类型,默认为json。会对返回数据做一个JSON.parse
- responseType = this.defaultReq.responseType, //设置响应的数据类型默认为'text'
- abortFun = _bt => {}
- } = {}, ...extra) {
- return new Promise(async (resolve, reject) => {
- if (!this.defaultReq.isreq) {
- return reject('要想使用ajax,请开放isreq 设置为true');
- }
- Object.assign(data, this.defaultReq.baseData); //合并参数
- if (typeof header === 'string') { //如果用户只想设置content-type
- header = {
- 'content-type': header
- };
- }
- let beforeInfo={
- url: this.defaultReq.url + path,
- method: type,
- dataType,
- responseType,
- data,
- header,
- }
- let verifyBeforeInfo =await this.defaultReq.beforeSend(beforeInfo); //用户自定义后的请求参数
- if(!verifyBeforeInfo){
- return reject( Object.assign(beforeInfo,{beforeClose:true}));
- }
- if (title) { //显示请求提示
- uni.showLoading({
- title,
- mask: true,
- });
- }
- const requestTask = uni.request({
- ...beforeInfo,
- complete:async ({
- statusCode,
- ...finsh
- }={}) => {
- let callData = Object.assign({
- extra
- }, finsh,{statusCode});
- if (statusCode == 200) {
- try {
- let verifyRes = await this.defaultReq.beforeFinsh(callData);
- resolve(verifyRes);
- } catch (error) {
- reject(error)
- }
- // let verifyRes =await this.defaultReq.beforeFinsh(callData);
- // if(verifyRes){
- // resolve(verifyRes);
- // }
- }else{
- reject(callData)
- }
- if (title) {
- uni.hideLoading();
- }
- }
- });
- abortFun(beforeInfo,requestTask);
- })
- }
- /**
- * 2019年4月6日12:05:55
- * 封装上传文件功能
- */
- ajaxFile({
- path = '',
- title = false,
- header = this.defaultUp.header,
- filePath = '',
- fileName = '',
- extra = {},
- abort = bt => {},
- _isFirst = true,
- _autoClose = true,
- ...args
- } = {}) {
- Object.assign(extra,this.defaultUp.baseData);
- return new Promise((resolve, reject) => {
- if (title && _isFirst) { //显示请求提示
- uni.showLoading({
- title,
- mask: true,
- });
- }
- const url=this.defaultUp.url + path;
- let beforeInfo=Object.assign({},{path:url,header,filePath,fileName,extra,args})
- const uploadTask = uni.uploadFile({
- url,
- filePath,
- name: fileName,
- header,
- formData: extra,
- complete: ({
- statusCode = 0,
- ...finsh
- } = {}) => {
- if (title && _autoClose) {
- uni.hideLoading();
- }
- if (statusCode == 200) {
- return resolve(finsh);
- }
- return reject(finsh);
- }
- });
- abort(beforeInfo,uploadTask);
- })
- }
- /**
- * 内部下载文件,仅内部调用
- */
- downFiles({
- abort = () => {},
- path = '',
- title = false,
- index=0, //所属下载索引
- ...extra
- } = {}) {
- return new Promise((resolve, reject) => {
- if (!path) {
- reject('请选设置请求路径');
- }
- if (title) {
- uni.showLoading({
- title,
- mask: true,
- });
- }
- const downloadTask = uni.downloadFile({
- url: path,
- ...extra,
- complete: ({
- statusCode = 0,
- ...finsh
- } = {}) => {
- if (title) {
- uni.hideLoading();
- }
- if (statusCode === 200) {
- return resolve(Object.assign({}, {
- statusCode,
- params: extra,
- ...finsh
- }));
- }
- return reject(finsh)
- },
- })
- abort({
- abort,
- path,
- title,
- index,
- ...extra
- },downloadTask);
- })
- }
- /**
- * 设置代理
- */
- proxy(obj, callback) {
- ProxyControll(obj, callback);
- }
- /**
- * 运行环境判断
- */
- platformChunk() {
- if (typeof plus == 'undefined') {
- return 1;
- }
- return 0;
- }
- }
- export const req = new Request();
- export const RQ = Request;
|