自用vue插件

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;
    }
});

window.setupWebViewJavascriptBridge = function (callback) {
    if (window.WebViewJavascriptBridge) {
        return callback(WebViewJavascriptBridge);
    }
    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
    }
    window.WVJBCallbacks = [callback]; // 创建一个 WVJBCallbacks 全局属性数组,并将 callback 插入到数组中。
    var WVJBIframe = document.createElement('iframe'); // 创建一个 iframe 元素
    WVJBIframe.style.display = 'none'; // 不显示
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'; // 设置 iframe 的 src 属性
    document.documentElement.appendChild(WVJBIframe); // 把 iframe 添加到当前文导航上。
    setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
    }, 0)
};
// 这里主要是注册 OC 将要调用的 JS 方法。

setupWebViewJavascriptBridge((bridge) => {
    Vue.prototype.IOSBridge = bridge;
    bridge.registerHandler('getUser', (data, responseCallback) => {
        // data 是 OC 传递过来的数据.
        // responseCallback 是 JS 调用完毕之后传递给 OC 的数据
        store.commit('setIOSUser',data);
        responseCallback({data: "js接收成功", status: 200, from: "JS"});
    });
    bridge.callHandler("jsBridgeSuccess", {status: 200}, (response) => {
    });
});

你可能感兴趣的:(自用vue插件)