<template>
    <a :href="destUrl">
        <slot></slot>
    </a>
</template>

<script>
    import {Core} from  'eye-core'
    export default{
        name: "junpLink",
        props:{
            "action":{
                type: Object,
                default:()=>{
                    return {
                        page: "",
                        param: ""
                    }
                }
            }
        },
        data(){
            return {
                wakeApp: "http://mobile.cnhnb.com/html/app/openapp/openapp.html",
                destUrl: ""

            }
        },
        mounted(){
            this.destUrl = this.fixUrl(this.action)
        },
        methods:{
            /**
             * 解析统跳协议 根据参数拼接URL
             *
             * @param action
             * @returns {string} URL
             */
            fixUrl(action){
                let dest = "";        // 静态下载地址
                let parms = [];       // 参数拼接地址
                if(!this.isApp()) {   // 不在APP内
                    action['page'] == 'web'?
                        "":(action['page']?parms.push("?name="+action['page']):"");
                    action['param']&&Core._.isString(action['param'])? parms.push([
                            action['page'] == 'web'? "":"param=",
                            action['param']
                        ].join("")):"";

                    action['param']&&Core._.isObject(action['param']) ?
                        parms.push(this.objectToUrl(action['param'])) : "";

                    dest = (action['page'] == 'web'?"":this.wakeApp) + parms.join("&")
                }
                // TODO 在APP内解析url

                return dest
            },
            /**
             * 将对象转成url参数
             *
             * @param obj
             * @returns {string}  xxx=xxx&xxx=xxx
             */
            objectToUrl(obj){
                let temArr = []
                Object.keys(obj).map(item=>{
                    Core._.isEmpty(obj[item])?"":temArr.push(`${item}=${obj[item]}`);
                })
                return temArr.join("&")
            },

            isApp () {
                return this.isIos() || this.isAndroid();
            },
            isIos() {
                return navigator.userAgent.indexOf('cnhnb_iOS') !== -1;
            },
            isAndroid() {
                return navigator.userAgent.indexOf('cnhnb_Android_') !== -1;
            }
        }
    }
</script>

<style scoped>

</style>