util.js 6.64 KB
/*
    *工具 公用方法 全局方法
    *
    *
    *
*/
import { BASE_CONFIG } from '@/config/config'
import axios from 'axios'
import Clipboard from 'clipboard';
import { MessageBox, Toast } from 'mint-ui';
export default {
    install(Vue, options) {
        //公共弹框
        function CommonAlert(msg) {
            return Toast({
                message: msg,
                duration: BASE_CONFIG.MESSAGE_DURATION
            })
        }

        // 不需要登录就能访问
        Vue.prototype.$normalPage = function () {
            return [sessionStorage.getItem('normalPage')] || [];
        }
        Vue.prototype.$isIphonex = function () {
            // X XS, XS Max, XR
            const xSeriesConfig = [
                {
                    devicePixelRatio: 3,
                    width: 375,
                    height: 812,
                },
                {
                    devicePixelRatio: 3,
                    width: 414,
                    height: 896,
                },
                {
                    devicePixelRatio: 2,
                    width: 414,
                    height: 896,
                },
                {
                    devicePixelRatio: 3,
                    width: 390,
                    height: 844,
                },
                {
                    devicePixelRatio: 3,
                    width: 428,
                    height: 926,
                }
            ];
            // h5
            if (typeof window !== 'undefined' && window) {
                const isIOS = /iphone/gi.test(window.navigator.userAgent);
                if (!isIOS) return false;
                const { devicePixelRatio, screen } = window;
                const { width, height } = screen;
                return xSeriesConfig.some(item => item.devicePixelRatio === devicePixelRatio && item.width === width && item.height === height);
            }
            return false;
        }
        Vue.prototype.$message = function (msg) {
            return Toast({
                message: msg,
                duration: BASE_CONFIG.MESSAGE_DURATION
            })
        }
        //获取全局公共配置
        Vue.prototype.$getConfig = function () {
            return BASE_CONFIG
        }


        //清除全局缓存
        Vue.prototype.$removeRootScope = function (key) {
            if (sessionStorage.getItem(key)) {
                sessionStorage.removeItem(key);
            }
        }
        //新增缓存字段
        Vue.prototype.$setRootScope = function (key, value) {
            if (sessionStorage.getItem(key)) {
                sessionStorage.removeItem(key);
            }
            sessionStorage.setItem(key, value)
            //sessionStorage.setItem(key, value);
        }
        //获取缓存
        Vue.prototype.$getRootScope = function (key) {
            let res = sessionStorage.getItem(key)
            return res
        }


        //cookie
        Vue.prototype.$getCookie = function (c_name) {

            if (document.cookie.length > 0) {
                let c_start = document.cookie.indexOf(c_name + "=")
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1
                    let c_end = document.cookie.indexOf(";", c_start)
                    if (c_end == -1)
                        c_end = document.cookie.length
                    //console.log(document.cookie.substring(c_start, c_end))
                    //console.log(decodeURIComponent(document.cookie.substring(c_start, c_end)) )
                    //return decodeURIComponent(document.cookie.substring(c_start, c_end))
                    return unescape(document.cookie.substring(c_start, c_end))
                }
            }
            return ""
        }
        Vue.prototype.$setCookie = function (name, value, expires) {
            //console.log(value)
            //console.log(encodeURIComponent(value))
            var exp = new Date();
            exp.setTime(exp.getTime() + expires * 24 * 60 * 60 * 1000); //3天过期
            //document.cookie = name + "=" + encodeURIComponent(value)
            document.cookie = name + "=" + escape(value)
                + ";expires=" + exp.toGMTString() + ";path=/";
            return true;
        }


        //复制
        Vue.prototype.$copy = function (className) {

            var clipboard = new Clipboard('.' + className)
            clipboard.on('success', e => {
                // console.log('复制成功')

                CommonAlert('复制成功');
                // 释放内存
                clipboard.destroy()
            })
            clipboard.on('error', e => {
                // 不支持复制
                // console.log('该浏览器不支持自动复制')

                CommonAlert('该浏览器不支持自动复制');
                // 释放内存
                clipboard.destroy()
            })
        }
        //正则 从url里获取字段值
        Vue.prototype.$getvl = function (reqUrl, name) {
            var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
          if (reg.test(reqUrl)) return unescape(RegExp.$2.replace(/\//g, " "));
          return ""; 
        }

        const commonExp = {
            mobile: /^[1][0-9][0-9]{9}$/,
            email: /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-zA-Z0-9]+[-a-z0-9]*[a-zA-Z0-9]+.){1,63}[a-zA-Z0-9]+$/,
            account: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{2,200}$/,  // 2-位数 汉字、字母、数字、下划线
            pwdStongMilddle: /^(?=.*[A-Z])(?=.*\d)[^]{6,18}$/,//6-18位数 数字和字母的组合
            pwdStong: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,18}$/,//至少1个大写字母,1个小写字母和1个数字,
            certNo: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
        }
        // 正则匹配 表达式 统一维护
        Vue.prototype.$commonExpression = function (key) {
            return commonExp[key]
        }

        //当日时间格式转换
        Vue.prototype.$TimesStr = function () {
            let date = new Date();
            let month = (date.getMonth() + 1).toString();
            let strDate = date.getDate().toString();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            let currentdate = date.getFullYear() + "-" + month + "-" + strDate;
            this.$store.state.date = currentdate; //转换格式为yyyy-mm-dd
            this.$store.state.dateDot = date.toLocaleDateString().replace(/\//g, ".");
            this.$store.state.monthStr = date.getFullYear() + "-" + month
            return currentdate;
        }
    }
}