/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/10/26 by CL $
*/

/**
 * AJAX上传图片 全站通用
 * @param:: string $file input file name
 * @param:: string $url post url
 * @param:: string $callback callback js name
 * @return::
*/
function oeloveUploadImg($file, $url, $callback) {
	if (typeof($callback) == "undefined") {$callback = "";}
	if (_oeloveCheckImgType($file) == true) {
		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}
		$.ajaxFileUpload({
			"url": $url,
			"secureuri": false,
			"fileElementId": $file,
			"dataType": "json",
			"data": {
				uploadpart: $file
			},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
					if ($("#"+$file+"_thumb").length > 0) {
						$("#"+$file+"_thumb").val($imgthumb);
					}
					if ($("#"+$file+"_url").length > 0) {
						$("#"+$file+"_url").val($imgurl);
					}
					if ($("#"+$file+"_preview").length > 0) {
						$img = _ROOT_PATH+$imgurl+"?r="+get_rndnum(8);
						$("#"+$file+"_preview").attr("src", $img);
						$("#"+$file+"_preview").show();
					}
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					_oeloveFileSynYun($attid); //同步云存储
				}
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					if ($result.length > 0 ) {
						ToastTips($result);
					}
					else {
						ToastTips("上传失败，请检查图片。");
					}
				}

				//callback js
				if ($callback.length > 0) {
					$fun = eval($callback);
					if (typeof($fun) == "function") {
						$fun.call(this);
					}
				}

			},
			error: function ($data, e, b) {
				if ($obj_loading.length > 0) {
					$obj_loading.hide();
				}
				ToastTips("系统繁忙，请稍后再试！");
			}
		});

	}
}


/**
 * AJAX上传图片 全站通用
 * @param:: string $file input file name
 * @param:: string $url post url
 * @param:: int $nums img nums
 * @param:: string $callback callback js name
 * @return::
 */
function oeloveNewsUploadImg($file, $url, $nums, $callback) {
    if (typeof($nums) == "undefined") {$num = 1;}
    if (typeof($callback) == "undefined") {$callback = "";}
    if (_oeloveCheckImgType($file) == true) {
        $obj_loading = $("#"+$file+"_loading");
        if ($obj_loading.length > 0) {
            $obj_loading.show();
        }
        $.ajaxFileUpload({
            "url": $url,
            "secureuri": false,
            "fileElementId": $file,
            "dataType": "json",
            "data": {
                uploadpart: $file
            },
            async: this.async,
            beforeSend: function ($xmlobj) {},
            complete: function ($data) {},
            success: function ($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                $imgthumb = $json.thumbimg;
                $imgurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}

                if ($response == "1") {

					_oeloveFileSynYun($attid);//同步云存储

                    for (var i=1;i<=parseInt($nums);i++){
                        $uploadimg =$file+i;
                        if($("#"+$uploadimg+"_thumb").val().length > 0){
                            continue;
                        }

                        if ($("#"+$uploadimg+"_thumb").length > 0) {
                            $("#"+$uploadimg+"_thumb").val($imgthumb);
                        }
                        if ($("#"+$uploadimg+"_url").length > 0) {
                            $("#"+$uploadimg+"_url").val($imgurl);
                        }
                        if ($("#"+$uploadimg+"_preview").length > 0) {
                            $img = _ROOT_PATH+$imgurl+"?r="+get_rndnum(8);
                            $("#"+$uploadimg+"_preview").attr("src", $img);
                            $("#"+$uploadimg+"_preview").show();
                        }
                        $("[f='"+$uploadimg+"_show']").show();
                        break;
                    }
                    if ($obj_loading.length > 0) {
                        $obj_loading.hide();
                    }
                    if(i > $nums){
                        ToastTips("最多只能上传"+$nums+"张图集");
                    }
                }
                else {
                    if ($obj_loading.length > 0) {
                        $obj_loading.hide();
                    }
                    if ($result.length > 0 ) {
                        ToastTips($result);
                    }
                    else {
                        ToastTips("上传失败，请检查图片。");
                    }
                }

                //callback js
                if ($callback.length > 0) {
                    $fun = eval($callback);
                    if (typeof($fun) == "function") {
                        $fun.call(this);
                    }
                }

            },
            error: function ($data, e, b) {
                if ($obj_loading.length > 0) {
                    $obj_loading.hide();
                }
                ToastTips("系统繁忙，请稍后再试");
            }
        });

    }
}


//判断图片格式
function _oeloveCheckImgType(filepart) {
	$res = true;
	$oe_file_obj = $("#"+filepart);
	$oe_file_val = $oe_file_obj.val();
	$oe_file_size = 0;
	if ($oe_file_val.length == 0) {
		ToastTips("请选择要上传的图片");
		$res = false;
	}
	if(!/.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG)$/.test($oe_file_val)){ 
		ToastTips("图片格式不正确");
		$res = false;
	}
	return $res;
}


/**
 * AJAX上传附件 全站通用
 * @param:: string $file input file name
 * @param:: string $url post url
 * @param:: string $callback callback js name
 * @datetime  2016-08-12 arqin
 */
function oeloveUploadEnclosure($file, $url, $callback) {
    if (typeof($callback) == "undefined") {$callback = "";}
    if (_oeloveCheckEnclosureType($file) == true) {
        $obj_loading = $("#"+$file+"_loading");
        if ($obj_loading.length > 0) {
            $obj_loading.show();
        }
        $.ajaxFileUpload({
            "url": $url,
            "secureuri": false,
            "fileElementId": $file,
            "dataType": "json",
            "data": {
                uploadpart: $file
            },
            async: this.async,
            beforeSend: function ($xmlobj) {},
            complete: function ($data) {},
            success: function ($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                $imgthumb = $json.thumbimg;
                $uploadurl = $json.uploadimg;

                if ($response == "1") {
                    if ($("#"+$file+"_url").length > 0) {
                        $("#"+$file+"_url").val($uploadurl);
                        $('[f="file_success"]').html("上传成功，路径：<a target='_blank' href='"+_ROOT_PATH+$uploadurl+"'>"+$uploadurl+"</a>");
                    }
                    if ($obj_loading.length > 0) {
                        $obj_loading.hide();
                    }
                }
                else {
                    if ($obj_loading.length > 0) {
                        $obj_loading.hide();
                    }
                    if ($result.length > 0 ) {
                        ToastTips($result);
                    }
                    else {
                        ToastTips("上传失败，请检查附件。");
                    }
                }

                //callback js
                if ($callback.length > 0) {
                    $fun = eval($callback);
                    if (typeof($fun) == "function") {
                        $fun.call(this);
                    }
                }

            },
            error: function ($data, e, b) {
                if ($obj_loading.length > 0) {
                    $obj_loading.hide();
                }
                ToastTips("上传失败，请检查网络...");
            }
        });

    }
}


//判断附件格式 2016-08-12 arqin
function _oeloveCheckEnclosureType(filepart) {
    $res = true;
    $oe_file_obj = $("#"+filepart);
    $oe_file_val = $oe_file_obj.val();
    $oe_file_size = 0;
    if ($oe_file_val.length == 0) {
        ToastTips("请选择要上传的附件");
        $res = false;
    }
    if(!/.(mp3)$/.test($oe_file_val)){
        ToastTips("附件格式不正确");
        $res = false;
    }
    return $res;
}