Commit a38e0337 by yink

对账单下载

parent 0abbcdec
......@@ -3,6 +3,8 @@
namespace App\Admin\Controllers;
use App\Models\Category;
use App\Command\Tools;
use App\Command\Log;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
......@@ -12,7 +14,7 @@
use Dcat\Admin\Http\Controllers\AdminController;
use NwVVVS\AdapaySdk\AdapayTools;
class CategoryControllerTest extends AdminController
class HfBillDownController extends AdminController
{
public function index(Content $content)
......@@ -24,31 +26,53 @@ public function index(Content $content)
// 这里可以添加你想要展示的内容,例如文本信息---
//$row->column(12, '<p>这是对账单下载页面,请选择合适的操作。</p>');
// 添加时间选择控件和按钮
$row->column(2, '<input type="date" class="form-control" id="download-date" />');
$row->column(3, '<input type="date" class="form-control" id="download-date" />');
$row->column(6, '<button id="download-button" class="btn btn-primary">下载</button>');
$row->column(12, '<div><br></div>');
$row->column(12, '<div>下载文件为zip压缩包,解压后获取csv类型的文件,编码格式为UTF-8,若使用Excel打开可能会出现乱码,解决方案请参考 <a href="https://jingyan.baidu.com/article/7082dc1c359cbbe40a89bd3e.html" target="_blank" rel="noopener">Excel打开CSV文件</a> :</p></div>');
$row->column(12, '<p>Charge开头的文件为支付对账单,参考 <a href="https://cdn.cloudpnr.com/adapayresource/documents/Adapay%20Charge%E5%AF%B9%E8%B4%A6%E5%8D%95%E6%A8%A1%E7%89%88.csv" target="_blank" rel="noopener">支付对账单模版</a></p>');
$row->column(12, '<p>Refund开头的文件为退款对账单,参考 <a href="https://cdn.cloudpnr.com/adapayresource/documents/Adapay%20Refund%E5%AF%B9%E8%B4%A6%E5%8D%95%E6%A8%A1%E7%89%88.csv" target="_blank" rel="noopener">退款对账单模版</a></p>');
$row->column(12, '<p>PaymentConfirm开头的文件为支付确认对账单,参考 <a href="https://cdn.cloudpnr.com/adapayresource/documents/Adapay%20PaymentConfirm%E5%AF%B9%E8%B4%A6%E5%8D%95%E6%A8%A1%E7%89%88.csv" target="_blank" rel="noopener">支付确认对账单模版</a></p>');
$row->column(12, '<p>Div开头的文件为分账对账单,参考 <a href="https://cloudpnrcdn.oss-cn-shanghai.aliyuncs.com/adapayresource/Div对账单模版.csv" target="_blank" rel="noopener">分账对账单模版</a></p>');
$row->column(12, '<p>RefundDiv开头的文件为分账退款对账单,参考 <a href="https://cloudpnrcdn.oss-cn-shanghai.aliyuncs.com/adapayresource/RefundDiv对账单模版.csv" target="_blank" rel="noopener">分账退款对账单模版</a></p>');
// 添加 JavaScript 代码绑定点击事件
$row->column(
12,
<<<HTML
<script>
document.getElementById('download-button').addEventListener('click', function() {
var date = document.getElementById('download-date').value;
if (date) {
fetch('/category-download?date=' + date, {
method: 'GET'
})
.then(response => response.json())
.then(data => {
console.log(data);
// 这里可以添加更多处理逻辑,例如显示提示信息
});
} else {
alert('请选择日期');
}
});
</script>
HTML
<script>
document.getElementById('download-button').addEventListener('click', function() {
var date = document.getElementById('download-date').value;
if (date) {
fetch('/bill-down-download?date=' + date, {
method: 'GET'
})
.then(response => response.json())
.then(data => {
console.log(data);
// 判断 code 是否为 200
if (data.code === 200) {
// 创建一个隐藏的 a 标签
var a = document.createElement('a');
a.href = data.data.url;
a.download = 'downloaded_file.zip'; // 可以根据需要修改文件名
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
alert('下载失败:请查看日志 ');
}
});
} else {
alert('请选择日期');
}
});
</script>
HTML
);
});
}
......@@ -59,13 +83,49 @@ public function index(Content $content)
public function download()
{
$date = request()->input('date');
$date = str_replace('-','', $date);
# 初始化对账单下载对象类
$bill = new AdapayTools;
# 对账单下载
$bill->download(["bill_date" => "20180808"]);
return response()->json(['message' => '下载请求已接收', 'date' => $bill]);
$bill->download(strval($date));
//Log::addByName('billDownloadUrl', $date);
// 检查 $bill->result 是否为数组
if (is_array($bill->result)) {
$responseData = $bill->result;
} else {
$responseData = json_decode($bill->result, true);
}
// 解码第二个元素(是一个JSON字符串)
$resultData = json_decode($responseData[1], true);
// 提取data字段,并解码其中的JSON字符串
$data = json_decode($resultData['data'], true);
// 初始化变量
$billDownloadUrl = '';
// 检查响应数据是否存在且第一个元素是否为 200
if (isset($responseData[0]) && $responseData[0] == 200) {
// 获取bill_download_url的值
$billDownloadUrl = $data['bill_download_url'];
}else{
// 获取error_msg的值
$errorMsg = $data['error_msg'];
return Tools::JsonResponse(['url' => ''], "下载失败,请查看日志:".$errorMsg, 501);
}
if($billDownloadUrl == ''){
Log::addByName('billDownloadUrl', $responseData);
return Tools::JsonResponse(['url' => $billDownloadUrl], "下载失败,请查看日志", 501);
}
return Tools::JsonResponse(['url' => $billDownloadUrl]);
}
/**
......
......@@ -44,8 +44,8 @@
$router->resource('category', 'CategoryController'); //分类管理
$router->resource('category-test', 'CategoryControllerTest'); //对账单下载-页面
$router->get('category-download','CategoryControllerTest@download'); //对账单下载-下载
$router->resource('bill-down', 'HfBillDownController'); //对账单下载-页面
$router->get('bill-down-download','HfBillDownController@download'); //对账单下载-下载
$router->resource('article', 'ArticleController'); //文章管理
......
......@@ -47,6 +47,50 @@ static public function add(string $logKey, mixed $logInfo, ?Request $request = n
}
}
static public function addByName(string $logKey, mixed $logInfo, ?Request $request = null) :void{
try {
// 判断当天的日志文件是否存在
$logFileName = 'runLog_'.$logKey.'.log';
$logPath = storage_path("logs/".$logFileName);
// 检查日志目录是否存在,如果不存在则创建
if (!is_dir(dirname($logPath))) {
mkdir(dirname($logPath), 0777, true);
}
// 拼接用户信息
if ($request && $request->user()) {
$userObj = $request->user();
$userInfo = "【-----------用户信息----------】".PHP_EOL;
$userInfo .= "【用户ID】: ".$userObj->id.";".PHP_EOL;
$userInfo .= "【用户名称】: ".$userObj->name.";".PHP_EOL;
// 添加请求参数到日志信息
$requestParams = $request->all();
$paramInfo = "【请求参数】: ".print_r($requestParams, true).PHP_EOL;
$userInfo .= $paramInfo;
} else {
$userInfo = "【-----------用户信息----------】".PHP_EOL;
$userInfo .= "【未获取到用户信息】".PHP_EOL;
}
// 获取请求路径
$requestPath = $request ? $request->getPathInfo() : 'N/A';
// 添加请求路径到日志信息
$logMessage = "--------------------".str_repeat("-", 20).PHP_EOL;
$logMessage .= "【".date('Y-m-d H:i:s')."】".PHP_EOL."【返回值:】".$userInfo."【请求路径】: $requestPath 【".$logKey."】: ".print_r($logInfo, true).PHP_EOL;
$logMessage .= "--------------------".str_repeat("-", 20).PHP_EOL.PHP_EOL;
// 添加换行符
file_put_contents($logPath, $logMessage, FILE_APPEND);
} catch (\Exception $e) {
// 记录异常信息到另一个日志文件
$errorLogPath = storage_path("logs/error_log.log");
file_put_contents($errorLogPath, "【".date('Y-m-d H:i:s')."】【add日志 方法出错】: ". $e->getMessage(). PHP_EOL, FILE_APPEND);
}
}
}
<?php
namespace App\Command;
use Illuminate\Http\Response;
class Tools{
/**
* @param $data mixed 返回的数据
* @param $msg string 状态码
* @param $code int 返回的自定义信息
* @return Response
*/
public static function JsonResponse($data, string $msg = 'Success', int $code = 200): Response
{
//组建返回结果
$result = [
'code' => $code,
'message' => $msg,
'data' => $data,
];
return response($result, 200);
}
// 计算俩地距离
function get_two_point_distance($lat1,$lng1,$lat2,$lng2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment