<?php

// 用户募捐支付接口
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_set_user_donation_dat.php.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$donationId = ParamUtil::getRequestNumber("donationId", 0);
$money = ParamUtil::getRequestNumber("money", 0);

$result = array();

//参数检查
if(empty($unionId) || empty($circleId)|| empty($title)|| empty($comment)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

$result = array();

//判断用户是否存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseNG($result);
}
$userMst = $userList[0];

//募捐项目是否存在


//调用支付
//生成临时订单
$orderNo = "D" . date("YmdHis") . "_" . $userMst->id;
$userMediaDat = new UserMediaDat();
$userMediaDat->user_id = $userMst->id;
$userMediaDat->course_id = $courseMediaDat->course_id;
$userMediaDat->media_id = $courseMediaDat->id;
$userMediaDat->is_free = $courseMediaDat->is_free;
$userMediaDat->money = $courseMediaDat->price;
$userMediaDat->order_no = $orderNo;
$userMediaDat->status = "NEW";
$userMediaDat->save();

//判断是否为圈主
if($circleDat->owner_id != $userMst->id) {
	$result["message"] = "圈主才有权限发公告！";
	responseNG($result);
}

//发布公告
$noticeDat = new CircleNoticeDat();
$noticeDat->circle_id = $circleDat->id;
$noticeDat->title = $title;
$noticeDat->comment = $comment;
//上传图片到阿里云oss
$aliHandler = new AliUploadHandler();
if(!empty($_FILES['image']['tmp_name'])){
  $upload_file = FileUploadUtil::getScalarFile(@$_FILES['image']);
  $tmp_file_name = date("Ymd") . "_" . trim(basename($upload_file->name));//保存的名字自定义，这里如果图片的名字原来的是aa.jp，保存成20200220_aa.jpg
  
  $url = $aliHandler->uploadImg($tmp_file_name, $upload_file->tmp_name);
  ErrorLogger::doOutput($url);
  //这里的url就是http://compass-dev.oss-cn-beijing.aliyuncs.com/20200220_aa.jpg
  $noticeDat->image = $url;
}
$noticeDat->save();
ErrorLogger::doOutput("Compass...ajax_set_user_donation_dat.php.php....End.", 0);

//返回结果
$result["message"] = "发布成功！";
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>