uni-app方法封装

请求数据方法封装

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
}

使用说明

  1. 方法单独放到一个js文件中
  2. 通过import {ajax} from '../config/index.js' 方法所在文件路径
  3. 页面中直接ajax({
    url:'',
    data:'',
    method:'',
    success:function(res){
    },
    error:function(){
    }
    })

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

你可能感兴趣的:(uni-app方法封装)