uni封装ajax,uni-app方法封装

这篇博客介绍了如何封装JavaScript的请求方法和文件上传功能,以提高代码的复用性和可维护性。作者提供了ajax和uploadFile两个函数,分别用于常规数据请求和文件上传操作,并且包含了错误处理和数据格式化的方法。此外,还封装了通用的日期格式化函数,便于在项目中统一处理日期格式。这些封装方法可以简化代码,当需要全局修改如添加请求头时,只需在一个地方进行调整即可。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求数据方法封装

const ApiUrl = '';

export{

ApiUrl

}

import {

ApiUrl

} from './env.js';

const ajax = (opt) => {

opt = opt || {};

opt.url = opt.url || '';

opt.data = opt.data || null;

opt.method = opt.method || 'GET';

opt.header = opt.header || {

"Content-Type": "application/json"

};

opt.success = opt.success || function () {};

uni.request({

url: ApiUrl + opt.url,

data: opt.data,

method: opt.method,

header: opt.header,

dataType: 'json',

success: function (res) {

opt.success(res);

},

fail: function () {

uni.showToast({

title: '请稍后重试'

});

}

})

}

export {

ajax

}

文件上传封装

const uploadFile = opt => {

opt = opt || {};

opt.url = opt.url || '';

opt.filePath = opt.filePath || null;//要上传文件资源的路径。

opt.name = opt.name || null;//文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容

opt.filePath = opt.filePath || null;

opt.success = opt.success || function() {};

uni.uploadFile({

url: ApiUrl + opt.url,

filePath:opt.filePath,

name:opt.name,

success:function(res){

opt.success(res);

},

fail: function () {

uni.showToast({

title: '请稍后重试'

});

}

})

}

通用方法

const formatTime = date => {

date = new Date(date);

const year = date.getFullYear()

const month = date.getMonth() + 1

const day = date.getDate()

const hour = date.getHours()

const minute = date.getMinutes()

const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')

}

const formatdate = date => {

date = new Date(date);

const year = date.getFullYear()

const month = date.getMonth() + 1

const day = date.getDate()

const hour = date.getHours()

const minute = date.getMinutes()

const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('.');

}

const formatNumber = n => {

n = n.toString()

return n[1] ? n : '0' + n

}

/**

* 根据key查找数据中对应的值

*/

const SearchData = (key, data) => {

var value = '';

var i = 0;

while (i < data.length) {

if (key == data[i].key) {

value = data[i].title;

break;

} else {

i++;

continue;

}

}

return value;

}

/**

* 根据key查找数据中对应角标

*/

const SearchDataIndex = (key, data) => {

var value = '';

var i = 0;

while (i < data.length) {

if (key == data[i].key) {

value = i;

break;

} else {

i++;

continue;

}

}

return value;

}

module.exports = {

formatTime,

formatdate,

SearchData,

SearchDataIndex

}

使用说明

方法单独放到一个js文件中

通过import {ajax} from '../config/index.js' 方法所在文件路径

页面中直接ajax({

url:'',

data:'',

method:'',

success:function(res){

},

error:function(){

}

})

说明:这种写的好处是以后修改请求方法时候不用每一个请求都该,比如要在请求头中整体都加token,直接在ajax方法总修改就可以了,所以个人觉得在使用的时候把提供的方法按照自己习惯封装下,更好维护。当然直接按官方文档使用也是挺好的,看个人习惯了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值