
class Barcode{

    constructor() {

    }
	
    startQr(id,keyArrays,success,error){
        console.log("开始扫描....");

        this.scan = iot.navigator.barcode(id);
                    console.log(this.scan);
                    this.scan.start();
                    this.scan.onmarked = (type, result)=>{
                        if(result) {
                            let typeJson = this.formatUrlToJson(result);
                            let type =  typeJson["type"];

                            let bool = this.isInArray(keyArrays,type);
                            if (bool) {
                                success(type);
                            }else{
                                error("当前扫描的二维码不和规范");
                            }

                        }
                    };
    }

    isInArray(arr,value){
        for(var i = 0; i < arr.length; i++){
            if(value === arr[i]){
                return true;
            }
        }
        return false;
    }

    formatUrlToJson(str){
        console.log("测试字符串截取功能");
        let index =  str.indexOf("?");
        let subStr = str.substr(index+1,str.length);
        
        let subIndex =  subStr.indexOf("=");
        let keyStr = subStr.substr(0,subIndex);
        console.log(keyStr);
        let value = subStr.substr(subIndex+1,subStr.length);
        let json = {};
         json[keyStr] = value;
         // alert(JSON.stringify(json));
         return json;
    }


}



export default new Barcode();