!(function($) {
	$.fileStorId="";
	$.fileStorKey="";
	$.fn.extend({        
        /**
         * @param: picData,图片base64编码后去掉前缀(data:image/png;base64,)后的数据
         */
        'putb64': function(key,picData,domain,callback){
            /*picUrl用来存储返回来的url*/
            //var picUrl;
            var token = this.uptoken_func(key);
            /*通过base64编码字符流计算文件流大小函数*/
            function fileSize(str){
                var fileSize;
                if(str.indexOf('=')>0){
                    var indexOf=str.indexOf('=');
                   //把末尾的'='号去掉
                    str=str.substring(0,indexOf);
                }
                fileSize=parseInt(str.length-(str.length/8)*2);
                return fileSize;
            }

            /*把字符串转换成json*/
            function strToJson(str){ 
                var json = eval('(' + str + ')'); 
                return json; 
            }
            var url = "http://up.qiniu.com/putb64/"+fileSize(picData)+"/key/"+Qiniu.URLSafeBase64Encode(key);
            var xhr = new XMLHttpRequest();
            /**/
            xhr.onreadystatechange=function(){            	 
            	if (xhr.readyState==4){
                    var keyText=xhr.responseText;                   
                    if (xhr.status == 200) {                    	
                    	keyText=strToJson(keyText);                        
                        // http://oleco2u3s.bkt.clouddn.com/是我的七牛云空间网址，keyText.key 是返回的图片文件名                       
                        var key = keyText.key;
                        //console.log("http://oleco2u3s.bkt.clouddn.com/"+key);                       
                        //callback(domain+key);
                        callback(key);
                    }
                }
            }

            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/octet-stream");
            console.log("token:"+token);
            xhr.setRequestHeader("Authorization", "UpToken "+token);
            xhr.send(picData);

         }, 
         //获取测试token
         'getTestToken':function(){
         	return '1o90vmY9OmRcueIeIeFBID6q2uy8peFiFBxpnM78:rdalpapK8yThJayH3FakQwqQbp8=:eyJzY29wZSI6InRlc3QiLCJkZWFkbGluZSI6MTYwNTA2NDI3MX0=';
         },
         'getFileStorId':function(){
          	return $.fileStorId;
          },
          'getFileStorKey':function(){
            return $.fileStorKey;
          },

        //获取正式token
        'uptoken_func': function(file){
        	if(file.indexOf("&")>0 ||file.indexOf("'")>0||file.indexOf("\"")>0){
	    		alert("文件名中不能含有“&”、“'”、“\"”等特殊符号。");
        	}else{
        		var ajax = new XMLHttpRequest();    	    	
    	    	ajax.open('POST', ctx+'/adm/realNameAuthToken', false);
    	    	ajax.setRequestHeader("If-Modified-Since", "0");
    	    	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	    	ajax.send('fileName='+file);
    	    	if (ajax.status === 200) {
    	    		if(isOK(ajax.responseText)){
        	    		var res = JSON.parse(ajax.responseText);
        	    		console.log('获取token from server uptoken:'+res.uptoken);
        	    		file['filekey'] = res.filekey;
        	    		$.fileStorId = res.fileStoreId;
        	    		$.fileStorKey = res.filekey;
        	    		return res.uptoken;
    	    		}
    	    		return '';
    	    	} else {
    	    		console.log('custom uptoken_func err');
    	    		return '';
    	    	}
        	}
        	
        }
         
    });

})(jQuery);
