
//加载一些js文件
jQuery.getScript("js/Tools.js", function(data, status, jqxhr) {});
jQuery.getScript("https://unpkg.com/qiniu-js@2.5.1/dist/qiniu.min.js", function(data, status, jqxhr) {});



//获取上传图片的token
var UploadToken="";
function getUploadToken(){
	var str="";
	DYB_POST("../../Common/FindQNYToken",str,function(data){
		UploadToken=data.data;
	});
}

//上传图片到七牛云
function DYB_UploadQiniu(file,callback) {
	var baseUrl="http://qiniu.xnsystem.com/";//显示图片的最根七牛云地址，到时候会换掉
	var key="imga"+new Date().getTime()+new Date().getTime();
	var putExtra = {
			  fname: "",
			  params: {},
			  mimeType: ["image/png", "image/jpeg", "image/gif"]
			};
	var config = {
			  useCdnDomain: true,
			  region: qiniu.region.z0
			};
	var observable = qiniu.upload(file, key, UploadToken, putExtra, config);
	var observer = {
        next(res){
            console.log(res);
        },
        error(err){
            console.log(err);
            callback(err);
        },
        complete(res){
            console.log(res);
            //拼凑地址
            var url = baseUrl+key;
            console.log("图片请求地址:"+url);
            callback(url);
        }
	}
	var subscription = observable.subscribe(observer); // 上传开始
	// or
	//var subscription = observable.subscribe(next, error, complete) // 这样传参形式也可以
	//subscription.unsubscribe() // 上传取消
}


//发送一个post请求:请求地址，参数，成功回调
function DYB_POST(url,parm,callback) {
	$.ajax({
        url: url,
        type: "post",
       	data: parm,
        success: function (data) {
			if(data.status==2001){//需要登录
					top.location.href="../../manager/admin/";
					return ;
			}else if(data.status==1){//成功
				callback(data);
			}else{
				callback(data);
				layer.msg(data.msg);
			}
        }
    });
}

//发送一个post请求:请求地址，参数，成功回调
function DYB_POST_JSON(url,parm,callback) {
	$.ajax({
        url: url,
        type: "post",
       	data: parm,
       	dataType: "json",
        success: function (data) {
			if(data.status==2001){//需要登录
					top.location.href="../../manager/out";
					return ;
			}else if(data.status==1){//成功
				callback(data);
			}else{
				callback(data);
				layer.msg(data.msg);
			}
        }
    });
}

//把json数组以逗号隔开
function HandleArray(obj) {
	var con="";
	for(var i=0;i<obj.length;i++){
		con+=obj[i];
		if((i+1)<obj.length){//还有需要加上逗号
			con+=",";
		}
	}
	return con;
}



//加载完后执行
$(function() {
	setTimeout(function() {
		getUploadToken();
	}, 500);
});
