/**
* app-update.vue
* Version: 0.1
* User: wujie
* Date: 2018-01-08
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* app-update
*
******************************************************************************
*  依赖 

*  支持环境
    App
*
*/
<template>
    <div>

    </div>
</template> 

<script>
/**
 * @vue
 * @name notify-dialog 通知栏弹出框组件
 * @author wj
 * @version 0.1
 * @copyright  2018. U-GEN Tech.Co,Ltd. All Rights Reserved.
 * @property {String} url   -请求应用版本信息的接口地址
 * @property {String} showFlag -是否检查版本更新 

 * @example  <u-app-update :show-flag="true"> </u-app-update>
 */
    export default {
        props: ['showFlag'],
        data () {
            return {
                reqUrl:"device/getVersion",
                isNeedShow:this.showFlag?this.showFlag:true
            }
        },
        mounted: function () {
            console.log(this.showFlag);

            this.onCheckVersion();
        },
        watch: {
            
        },
        methods: {
            onCheckVersion(){
                checkVersion(this);
            }
        }
    };

function checkVersion(self){

    console.log(self.reqUrl);
    console.log("是否需要进行版本更新检查："+self.isNeedShow);
    if (!self.showFlag) return;

    console.log("检查版本...");

    let type =  null;
    if (plus.os.name == "iOS") {
        type = "ios";
    }else{
        type = "android";
    }
    
    iot.business.api.send(self.reqUrl,
            {
                data: {
                    "type":type
                },
                success: (response) => {
                    console.log(response);
                    if (response.code == 0) {
                        dealWithVersion(self,response,type);
                    }
                },
                error: (error) => {
                    console.log(error);
                },
                complete: () => {}
            },false);
}

function dealWithVersion(self,response,type){

    var v = plus.runtime.version;

    var curVersion = transformVersion(v);

    let newVersion = response.data[type].version;// 新版本 无论是安卓还是iOS都用此变量存储

    newVersion =  transformVersion(newVersion);

    let downloadUrl = response.data[type].url;// 下载地址 无论是安卓还是iOS都用此变量存储

    console.log("curVersion:"+curVersion);

    console.log("newVersion:"+newVersion)
    

    console.log(curVersion);
    console.log(newVersion);
    if(Number(newVersion) > Number(curVersion)){
        console.log("有新版本更新");
        plus.nativeUI.confirm( Vue.t('newFeature.newVersion'), (e)=>{
                            console.log("Close confirm: "+e.index);
                            if (e.index == 0) {// 点击了确定按钮
                                plus.runtime.openURL(downloadUrl);

                            }else if (e.index == 1) {// 点击了取消按钮

                            }
                        },Vue.t('newFeature.tip'), [Vue.t('newFeature.update'),Vue.t('newFeature.miss')] );

    }else{
        console.log("不需要更新版本");

    }

}

// 将系统版本转换为可读写版本
function transformVersion(v){
        var array = v.split(".");
        var floatVersion = "";
        console.log("下面是获取系统版本");
        console.log(array);
        if(array.length>2){
            for (var i = 0; i < array.length; i++) {
                if(i == 1){
                    floatVersion = floatVersion + ".";
                }
                floatVersion = floatVersion + array[i];
            }
        }else{
            floatVersion = v;
        }
        if(isNaN(floatVersion)){
            return null;
        }
        
        return floatVersion;
    }

</script>






