0
点赞
收藏
分享

微信扫一扫

(记录)实用方法封装 vue

成义随笔 2022-03-11 阅读 64
export default {
    install(vue, option) {
//获取设备列表
        vue.prototype.$getDevices = async function () {
            await navigator.mediaDevices.getUserMedia({ video: true, Audio: true });
            return navigator.mediaDevices.enumerateDevices().then(res => {
                // deviceId: "afb89fef315b054706d2e9ae75ab8aa37f5bccc7fc825fa2217adad13cb68faf"
                // groupId: "8e7a3120f636b0bb00b0ceedab249fde027e1a346cb9dd428682d3485684eafc"
                // kind: "audioinput"
                // label: "麦克风 (2- Logitech Webcam C930e) (046d:0843)"

                const videoinputgroup = [];
                for (let i of res.filter(x => x.kind == "videoinput")) {
                    const index = videoinputgroup.findIndex(x => x.groupId == i.groupId)
                    if (index == -1) {
                        videoinputgroup.push(i);
                    } else {
                        videoinputgroup[index] = i;
                    }
                }
                const audioinputgroup = [];
                for (let i of res.filter(x => x.kind == "audioinput")) {
                    const index = audioinputgroup.findIndex(x => x.groupId == i.groupId)
                    if (index == -1) {
                        audioinputgroup.push(i);
                    } else {
                        audioinputgroup[index] = i;
                    }
                }
                return {
                    videoinput: videoinputgroup,
                    audioinput: audioinputgroup
                };
            })
        }

//cookie
        vue.prototype.$setCookieValue = function (name, value) {
            var Days = 30;
            var exp = new Date();
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
            document.cookie = name + "=" + value + ";expires=" + exp.toGMTString();;
        }
        vue.prototype.$getCookieValue = function (name) {
            var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
            if (arr = document.cookie.match(reg))
                return arr[2];
            else
                return null;
        }

        vue.prototype.$getParams = function (name) {
            if (new RegExp(name + '=([^&]+)').test(location.href)) {
                return RegExp.$1;
            }
            return '';
        }
//判断是否手机
        vue.prototype.$isMobile = function () {
            let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
            return flag;
        }
//对象拷贝
        vue.prototype.$clone = function (myObj) {
                if (typeof myObj != "object") {
                    return myObj;
                }
                if (myObj == null) {
                    return myObj;
                }
                if (myObj instanceof Array) {
                    var myNewObj = new Array();
                } else {
                    var myNewObj = new Object();
                }
                for (var i in myObj) {
                    myNewObj[i] = vue.prototype.$clone(myObj[i]);
                }
                return myNewObj;
            },
//屏幕分享停止处理
            vue.prototype.$videoTracks = function () {
                return new Promise(resolve => {
                    navigator.mediaDevices.getDisplayMedia().then(stream => {
                        //监听手动点击“停止分享”
                        stream.getVideoTracks()[0].onended = () => {
                            //监听以后的处理逻辑……
                            resolve();
                        }
                    }).catch(e => {
                        console.error(e)
                    })
                })

            }

    }
}
举报

相关推荐

0 条评论