import {iot, uPublic, uComponents} from './public.js';

class Request{
	constructor(json) {//当前版本号、最新版本号以及获取最新安装包地址
	}

    versionRequest(url,need,notneed){
        this.getDownloadUrl(url,need,notneed);
    }

    getDownloadUrl(url,need,notneed){
        let type = null;
        if (iot.navigator.osName() == "iOS") {
            type = "ios";
        }else{
            type = "android";
        }
        iot.business.api.send(url,
                {
                    data: {
                        type:type
                    },
                    success: (response) => {
                        console.log(response);

                        if (response.code == 0) {
                            this.dealWithVersion(response,need,notneed);
                        }
                    },
                    error: (error) => {
                        console.log(error);
                        notneed(error);
                    },
                    complete: () => {}
                },false);
        
    }

     dealWithVersion(response,need,notneed){
        let type = null;
        if (iot.navigator.osName() == "iOS") {
            type = "ios";
        }else{
            type = "android";
        }

        let info = null;
        if (type == "ios") {
            info = response.data["ios"];
        }else{
            info = response.data["android"];
        }
        console.log(info);
        let newVersion = info.version;// 新版本 无论是安卓还是iOS都用此变量存储
        let downloadUrl = info.url;

        let backinfo = {
                url:downloadUrl,
                version:newVersion
        };

        need(backinfo);
        
    }
}

export default new Request();