自用plugin

import Vue from 'vue';
import $http from "../api/api";
import store from "../store";


import Confirm from '../views/components/confirm/index';
import ShowImage from '../views/components/showImage/index';
import {Message} from 'iview';

Vue.prototype.$Confirm = Confirm;
Vue.prototype.$ShowImage = ShowImage;
Vue.prototype.$post = (url, data, config) => {
    //是否转换为formData
    if (config && config.formData) {
        let ret = '';
        for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        data = ret
    }
    return new Promise((resolve) => {
        store.commit('showLoading');
        $http.post(url, data, config).then(res => {
            store.commit('hideLoading');
            if (typeof res === 'string') {
                resolve(JSON.parse(res));
            } else {
                if (res.status == 500) {
                    resolve({...res, data: [], records: []});
                } else {
                    resolve(res);
                }
            }
        }).catch(err => {
            store.commit('hideLoading');
            Message.error('网络繁忙,请稍后再试')
        })
    });
};
Vue.prototype.$get = (url, config) => {
    return new Promise((resolve => {
        store.commit('showLoading');
        $http.get(url, config).then(res => {
            store.commit('hideLoading');
            if (res.status == 500) {
                resolve({...res, data: [], records: []});
                Message.error(res.msg)
            } else {
                resolve(res);
            }

        }).catch(err => {
            store.commit('hideLoading');
            Message.error('网络繁忙,请稍后再试')
        })
    }))
};

Vue.prototype.$compile = function (template) {
    let result = Vue.compile(template);
    if (result.staticRenderFns.length) {
        return result.staticRenderFns[0].call(this);
    } else {
        return result.render.call(this)
    }

};
//数字格式化
Vue.filter('NumFormat', function (value) {
    if (!value) return '0.00';
    if (typeof value == 'number') {
        return value
    }

    let intPart = Number(value).toFixed(0); //获取整数部分
    let intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断

    let floatPart = ".00"; //预定义小数部分
    let value2Array = value.split(".");

    //=2表示数据有小数位
    if (value2Array.length == 2) {
        floatPart = value2Array[1].toString(); //拿到小数部分

        if (floatPart.length == 1) { //补0,实际上用不着
            return intPartFormat + "." + floatPart + '0';
        } else {
            return intPartFormat + "." + floatPart;
        }

    } else {
        return intPartFormat + floatPart;
    }
});


你可能感兴趣的:(自用plugin)