Browse Source

优化二维码接口地址、新增下载pdf地址

master
wanghongjun 12 months ago
parent
commit
f19d99fb87
  1. 71
      app/controller/api/InvoiceIssuance.php
  2. 4
      app/model/InvoiceIssuance.php
  3. 20
      app/service/invoice/InvoiceIssuanceService.php
  4. 2
      route/apiRoute.php

71
app/controller/api/InvoiceIssuance.php

@ -4,12 +4,14 @@ namespace app\controller\api;
use app\model\InvoiceHead;
use app\model\InvoiceIssuance as InvoiceIssuanceModel;
use app\model\InvoiceIssuanceData;
use app\model\WechatPucode;
use app\service\invoice\InvoiceIssuanceService;
use app\service\wechat\WechatService;
use app\util\ReturnCode;
use app\validate\InvoiceHeadValidate;
use app\validate\InvoiceIssuanceValidate;
use fast\FuncException;
use think\facade\App;
use think\facade\Db;
use think\Response;
@ -139,7 +141,7 @@ class InvoiceIssuance extends Base
'expire_time' => strtotime($params['expire_time']),
];
$where = [['status', '>', 0], ['delete_time', '=', 0]];
$where = [['status', '<>', 3], ['delete_time', '=', 0]];
if ($id) {
$where[] = ['id', '!=', $id];
}
@ -166,15 +168,28 @@ class InvoiceIssuance extends Base
$data['update_time'] = time();
(new InvoiceIssuanceModel())->update($data, ['id' => $id]);
$InvoiceIssuanceData = (new InvoiceIssuanceModel())->where('id', $id)->find();
if ($InvoiceIssuanceData['status'] == 2) {
$result = (new InvoiceIssuanceService())->cancelInitiateAgain($InvoiceIssuanceData);
if ($result !== true) {
throw new \Exception($result);
}
}
} else {
validate(InvoiceIssuanceValidate::class)->scene('add')->check($params);
$data['create_time'] = time();
$InvoiceModel = (new InvoiceIssuanceModel())->save($data);
if (!$InvoiceModel) {
$id = (new InvoiceIssuanceModel())->insertGetId($data);
if (!$id) {
throw new \Exception('申请失败');
}
$InvoiceIssuanceData = (new InvoiceIssuanceModel())->where('id', $id)->find();
$result = (new InvoiceIssuanceService())->IssueAnInvoice($InvoiceIssuanceData);
if ($result !== true) {
throw new \Exception($result);
}
}
Db::commit();
@ -209,16 +224,60 @@ class InvoiceIssuance extends Base
/**
* 获取二维码
* @return Response
* @throws \Exception
*/
public function getQrCode(): Response
{
// 验证
$id = $this->request->get('id', '');
if (empty($id)) {
throw new \Exception('缺少必传参数');
}
// 生成
$qrCodeImage = InvoiceIssuanceService::getQrCode();
$qrCodeImage = InvoiceIssuanceService::getQrCode($id);
// 返回
return $this->buildSuccess(['qrCodeImage' => $qrCodeImage]);
}
public function downFile()
{
try {
$id = $this->request->get('id', '');
if (empty($id)) {
throw new \Exception('缺少必传参数');
}
$InvoiceIssuanceData = (new InvoiceIssuanceData)->where(['invoice_issuance_id' => $id, 'status' => 1])->find();
if (!$InvoiceIssuanceData) {
throw new FuncException('PDF文件为下载成功');
}
$filePath = $InvoiceIssuanceData['pdf_filepath'];
$file_dir = App::getRootPath() . 'public/' . $filePath;
if (file_exists($file_dir)) {
// 打开文件
$file1 = fopen($file_dir, "r");
//文件名,自定义名称+文件后缀
$file_houzhui = strrchr($file_dir, '.');
$file_name = time().rand(0000,9999).$file_houzhui;
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".filesize($file_dir));
Header("Content-Disposition: attachment;filename=" . $file_name);//自定义文件名
ob_clean(); // 重点!!!
flush(); // 重点!!!!可以清除文件中多余的路径名以及解决乱码的问题:
//输出文件内容
//读取文件内容并直接输出到浏览器
echo fread($file1, filesize($file_dir));
fclose($file1);
exit();
} else {
throw new \Exception('文件不存在');
}
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage());
}
}
}
}

4
app/model/InvoiceIssuance.php

@ -9,6 +9,6 @@ class InvoiceIssuance extends Model
public static $projectArr = ['', '垃圾费', '污水费', '游泳'];
public static $statusArr = ['已作废', '申请中', '已开票'];
public static $statusArr = ['未申报', '申报中', '未申报成功', '已申报'];
}
}

20
app/service/invoice/InvoiceIssuanceService.php

@ -127,22 +127,12 @@ class InvoiceIssuanceService
* 获取二维码
* @param $id
* @return string
* @throws FuncException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function getQrCode($id): string
{
//$ChinaTaxes = new ChinaTaxes();
$InvoiceIssuanceData = (new InvoiceIssuanceData)->where(['invoice_issuance_id' => $id, 'status' => 1])->find();
if (!$InvoiceIssuanceData) {
throw new FuncException('不存在现在地址');
}
$url = Request::url();
$codeUrl = $url . $InvoiceIssuanceData['pdf_filepath'];
$url = Request::domain();
$codeUrl = $url . '/api/InvoiceIssuance/downFile?id='.$id;
$result = Builder::create()
->writer(new PngWriter())
@ -278,7 +268,7 @@ class InvoiceIssuanceService
(new InvoiceIssuanceData())->saveData($invoiceIssuance['id'], $bdznsrsbh, $jsyj, $assetID, $pdfFilepath);
(new InvoiceIssuance())->where('id', $invoiceIssuance['id'])->save(['status' => 4]);
(new InvoiceIssuance())->where('id', $invoiceIssuance['id'])->save(['status' => 3]);
} else if ($realTimeProcessing['code'] == 302) {
// 加工失败
@ -295,7 +285,7 @@ class InvoiceIssuanceService
(new InvoiceIssuanceData())->saveField($invoiceIssuance['id'], 'ycms', $ycms);
// 开票失败
(new InvoiceIssuance())->where('id', $invoiceIssuance['id'])->save(['status' => 3]);
(new InvoiceIssuance())->where('id', $invoiceIssuance['id'])->save(['status' => 2]);
//throw new FuncException('加工失败流程待定!');
}
return true;
@ -367,7 +357,7 @@ class InvoiceIssuanceService
$downPath = 'pdffile' . $now_date;
$fileDirPath = (new App())->getRootPath() . 'public' . $d . $downPath;
$filenamePath = $fileDirPath . $d . $fileName;
$savePath = $downPath . $fileName;
$savePath = $downPath . $d . $fileName;
if (!file_exists($fileDirPath)) {
mkdir($fileDirPath, 0777, true);
}

2
route/apiRoute.php

@ -16,6 +16,8 @@ Route::group('api', function() {
Route::rule('InvoiceIssuance/validateFeePay', 'api.InvoiceIssuance/validateFeePay', 'post')->middleware(\app\middleware\WechatAuth::class);
// 测试FeeService 接口
Route::rule('Test/index', 'api.Test/index', 'get');
Route::rule('Test1/index', 'api.Test1/index', 'get');
Route::rule('InvoiceIssuance/downFile', 'api.InvoiceIssuance/downFile', 'get');
//MISS路由定义
//Route::miss('api.Miss/index');
});//->middleware(app\middleware\ApiResponse::class)

Loading…
Cancel
Save