548 lines
19 KiB
PHP
548 lines
19 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||
// +----------------------------------------------------------------------
|
||
// | Author: 萤火科技 <admin@yiovo.com>
|
||
// +----------------------------------------------------------------------
|
||
declare (strict_types=1);
|
||
|
||
namespace app\common\service;
|
||
|
||
use app\common\library\helper;
|
||
use app\common\crypto\RsaUtil;
|
||
|
||
/**
|
||
* 随机数据类
|
||
* Class SybOil
|
||
* @package app\store\service
|
||
*/
|
||
class PartnersOil extends BaseService
|
||
{
|
||
public function mk_dir_store($dir, $mode = 0777)
|
||
{
|
||
if (is_dir($dir) || @mkdir($dir,$mode)) return true;
|
||
if (!$this->mk_dir_store(dirname($dir),$mode)) return false;
|
||
return @mkdir($dir,$mode);
|
||
}
|
||
|
||
/** 记录日志
|
||
* @param $data
|
||
*/
|
||
public function saveLogStore($data){
|
||
$years = date('Ym');
|
||
$day = date('Ymd');
|
||
//设置路径目录信息(store_log_path 保证 CLI / api / store 各进程写同一处,见 app/common.php)
|
||
$dir = store_log_path().$years.'/'.$day;
|
||
//目录不存在就创建
|
||
$this->mk_dir_store($dir);
|
||
$url = $dir.'/'.date('YmdH').'.txt';
|
||
$fp = @fopen($url,"a");//打开文件资源通道 不存在则自动创建
|
||
@fwrite($fp,date("Y-m-d H:i:s ").var_export($data,true)."\r\n");//写入文件
|
||
@fclose($fp);//关闭资源通道
|
||
}
|
||
public function sendRequest($partners, $url, $businessParams = [], $appendHeaders = [], $file = null)
|
||
{
|
||
$this->saveLogStore(($appendHeaders['method'] ?? '').'请求原始数据:'.json_encode($businessParams,320));
|
||
$crypto = new RsaUtil($partners);
|
||
// 签名并组合header,并加密数据
|
||
$requestData = $crypto->prepareRequest($appendHeaders, $businessParams);
|
||
$businessParams = $requestData['encryptData'];
|
||
// 构建默认的公共参数头
|
||
$headers = [];
|
||
foreach ($requestData['header'] as $k=>$val){
|
||
$headers[] = $k.': '.$val;
|
||
}
|
||
$this->saveLogStore($requestData['header']['method'].'请求加密数据:'.json_encode(['header'=>$requestData['header'],'body'=>$businessParams],320));
|
||
// 使用 cURL 发送请求
|
||
$ch = curl_init();
|
||
|
||
// 设置请求 URL
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
|
||
// 设置请求方式为 POST
|
||
curl_setopt($ch, CURLOPT_POST, true);
|
||
|
||
// 返回结果而不是输出
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
||
// 包含响应头在输出中
|
||
curl_setopt($ch, CURLOPT_HEADER, true);
|
||
|
||
$headers[] = 'Charset: UTF-8';
|
||
if ($file) {
|
||
// 如果有文件上传,设置 multipart/form-data 表单
|
||
$filePath = $file->getPathname();
|
||
$fileName = $file->getOriginalName();
|
||
$fileMime = $file->getMimeType();
|
||
$postFields = [
|
||
'file' => new \CURLFile($filePath, $fileMime, $fileName),
|
||
'businessParams' => json_encode($businessParams, JSON_UNESCAPED_UNICODE),
|
||
];
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
|
||
} else {
|
||
// 如果没有文件上传,直接发送业务参数的 JSON 数据
|
||
$postFields = json_encode($businessParams, JSON_UNESCAPED_UNICODE);
|
||
$headers[] = 'Content-Type: application/json';
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
|
||
}
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||
|
||
// 执行请求
|
||
$response = curl_exec($ch);
|
||
// 检查是否有错误
|
||
if (curl_errno($ch)) {
|
||
$error_msg = curl_error($ch);
|
||
curl_close($ch);
|
||
return ['code' => '01', 'message' => $error_msg];
|
||
}
|
||
|
||
// 获取响应头大小
|
||
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||
|
||
// 分离响应头和响应体
|
||
$headerString = substr($response, 0, $header_size);
|
||
$bodyString = substr($response, $header_size);
|
||
$this->saveLogStore($requestData['header']['method'].'返回值原文:'.$bodyString);
|
||
// 关闭 cURL 句柄
|
||
curl_close($ch);
|
||
|
||
// 解析响应头为数组
|
||
$headerArray = [];
|
||
foreach (explode("\r\n", $headerString) as $header) {
|
||
if (trim($header) !== '' && strpos($header, ':') !== false) {
|
||
list($key, $value) = explode(': ', $header, 2);
|
||
$headerArray[strtolower(trim($key))] = trim($value);
|
||
}
|
||
}
|
||
return json_decode($bodyString, true);
|
||
/*
|
||
$res = $crypto->processResponse(['header'=>$headerArray,'encryptData'=>json_decode($bodyString, true)]);
|
||
if(!$res){
|
||
return ['code' => '01', 'message' => $bodyString];
|
||
}
|
||
$this->saveLogStore($headerArray['method'].'返回值:'.json_encode($res,320));
|
||
// 返回结果
|
||
return $res;
|
||
*/
|
||
}
|
||
|
||
/** 油站查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function baseGasStationList($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.base.gasStation.list',
|
||
];
|
||
$body = [
|
||
'oilCardType' => 'zsy_ticket',
|
||
'goodsNo' => $data['goodsNo']??'',
|
||
// 'province' => $data['province']??'',
|
||
// 'city' => $data['city']??'',
|
||
'longitude' => $data['longitude']??'',
|
||
'latitude' => $data['latitude']??'',
|
||
'pageLimit' => $data['pageLimit']??'30',
|
||
'pageNum' => $data['pageNum']??'1',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/base/gasStationList', $body, $header);
|
||
//$this->saveLogStore("油站查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 油券库存
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function stockList($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.oil.oilCard.stockList',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'goodsNo' => $data['goodsNo'] ?? '',
|
||
];
|
||
return $this->sendRequest($company, $company['url'].'/api/1.0.0/oil/oilCard/query', $body, $header);
|
||
}
|
||
|
||
/** 文件上传接口
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function baseFileUpload($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.base.file.upload',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'file' => request()->file('file'),
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/base/uploadFile', $body, $header);
|
||
//$this->saveLogStore("文件上传接口".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 油卡查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function oilOilCardSelect($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.oil.oilCard.select',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'mobile' => $data['mobile']??'',
|
||
'userNo' => $data['userNo']??'',
|
||
'oilCardNo' => $data['oilCardNo']??'',
|
||
'pageLimit' => $data['pageLimit']??'10',
|
||
'pageNum' => $data['pageNum']??'1',
|
||
'oilCardType' => $data['oilCardType']??'card', //不填时默认为“E卡通平台自营卡”
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/oil/oilCard/select', $body, $header);
|
||
//$this->saveLogStore("油卡查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 用户分油
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function oilOilCardShare($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.oil.oilCard.share',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'goodsNo' => $data['goodsNo']??'',
|
||
'amount' => $data['oilCardType']=='card' ? round($data['amount'],2) : '',
|
||
'userNo' => $data['userNo']??'',
|
||
'mobile' => $data['mobile']??'',
|
||
'saleOrderNo' => $data['saleOrderNo']??'',
|
||
'oilCardType' => $data['oilCardType']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/oil/oilCard/share', $body, $header);
|
||
//$this->saveLogStore("用户分油结果".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 分油撤销接口
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function oilOilCardCancelShare($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.oil.oilCard.cancelShare',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'shareNo' => $data['shareNo']??'',
|
||
'amount' => !empty($data['amount']??'') ? round($data['amount'],2) : '',
|
||
'saleOrderNo' => $data['saleOrderNo']??'',
|
||
'type' => $data['type']??'',
|
||
'oilCardType' => $data['oilCardType']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/oil/oilCard/cancelShare', $body, $header);
|
||
//$this->saveLogStore("分油撤销接口".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 用户分油记录查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function oilOilCardShareList($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.oil.oilCard.shareList',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'userNo' => $data['userNo']??'',
|
||
'mobile' => $data['mobile']??'',
|
||
'shareNo' => $data['shareNo']??'',
|
||
'saleOrderNo' => $data['saleOrderNo']??'',
|
||
'beginDate' => $data['beginDate']??'',
|
||
'endDate' => $data['endDate']??'',
|
||
'pageLimit' => $data['pageLimit']??'',
|
||
'pageNum' => $data['pageNum']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/opt/oilCard/shareList', $body, $header);
|
||
//$this->saveLogStore("用户分油记录查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 加油记录查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function orderListSale($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.order.list.sale',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'mobile' => $data['mobile']??'',
|
||
'shareNo' => $data['shareNo']??'',
|
||
'beginDate' => $data['beginDate']??'',
|
||
'endDate' => $data['endDate']??'',
|
||
'pageLimit' => $data['pageLimit']??'',
|
||
'pageNum' => $data['pageNum']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/opt/list/sale', $body, $header);
|
||
//$this->saveLogStore("加油记录查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 付款二维码申请
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function payQrCodeRequest($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.pay.qrCode.request',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'oilCardNo' => $data['oilCardNo']??'',
|
||
'shareNo' => $data['shareNo']??'',
|
||
'mobile' => $data['mobile']??'',
|
||
'oilCardType' => $data['oilCardType']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/pay/getPayQrCode', $body, $header);
|
||
//$this->saveLogStore("付款二维码申请".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 主扫订单创建
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function payActiveScan($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.pay.active.scan',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'oilCardNo' => $data['oilCardNo']??'',
|
||
'qrInfo' => $data['qrInfo']??'',
|
||
'longitude' => $data['longitude']??'',
|
||
'latitude' => $data['latitude']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/pay/activeScan', $body, $header);
|
||
//$this->saveLogStore("主扫订单创建".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 主扫订单信息确认
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function payActiveVerify($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.pay.active.verify',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'activeOrderId' => $data['activeOrderId']??'',
|
||
'amount' => !empty($data['amount']??'') ? round($data['amount'],2) : '',
|
||
'deviceId' => $data['deviceId']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/pay/activeVerify', $body, $header);
|
||
//$this->saveLogStore("主扫订单信息确认".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 账户余额查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function payAmountBalance($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.pay.account.balance',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/finance/account/balance', $body, $header);
|
||
//$this->saveLogStore("账户余额查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 申请发票
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function payInvoiceRequest($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.pay.invoice.request',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'amount' => !empty($data['amount']??'') ? round($data['amount'],2) : '',
|
||
'oilNo' => $data['oilNo']??'',
|
||
'invoiceAddress' => $data['invoiceAddress']??'',
|
||
'invoiceHeader' => $data['invoiceHeader']??'',
|
||
'invoicePhone' => $data['invoicePhone']??'',
|
||
'invoiceOpenBank' => $data['invoiceOpenBank']??'',
|
||
'taxIdentifyNo' => $data['taxIdentifyNo']??'',
|
||
'invoiceCardNo' => $data['invoiceCardNo']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/finance/invoice/request', $body, $header);
|
||
//$this->saveLogStore("申请发票".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 开票状态查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function payInvoiceStatus($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.pay.invoice.status',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'invoiceNo' => $data['invoiceNo']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/finance/invoice/status', $body, $header);
|
||
//$this->saveLogStore("开票状态查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 添加用户
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function userOptAdd($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.user.opt.add',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'mobile' => $data['mobile']??'',
|
||
'userName' => $data['userName']??'',
|
||
'userIdCard' => $data['userIdCard']??'',
|
||
'userType' => $data['userType']??'user',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/user/opt/add', $body, $header);
|
||
//$this->saveLogStore("添加用户".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 优惠券下单
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function voucherCreateOrder($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.opt.voucher.createOrder',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'model' => $data['model']??'',
|
||
'voucherAmount' => $data['voucherAmount']??'',
|
||
'fullAmount' => $data['fullAmount']??'',
|
||
'storeNum' => $data['storeNum']??'',
|
||
'validTime' => $data['validTime']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/voucher/createOrder', $body, $header);
|
||
//$this->saveLogStore("优惠券下单".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 优惠券订单查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function voucherOrderListPage($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.opt.voucher.order.listPage',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'orderNo' => $data['orderNo']??'',
|
||
'model' => $data['model']??'',
|
||
'pageLimit' => $data['pageLimit']??'',
|
||
'pageNum' => $data['pageNum']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/voucher/order/listPage', $body, $header);
|
||
//$this->saveLogStore("优惠券订单查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
|
||
/** 卡券查询
|
||
* @param $data
|
||
* @param $company
|
||
* @return array|mixed|string[]
|
||
*/
|
||
public function voucherItemListPage($data, $company)
|
||
{
|
||
$header = [
|
||
'method' => 'syb.opt.voucher.item.listPage',
|
||
];
|
||
$body = [
|
||
'orgNo' => $company['orgNo'],
|
||
'orderNo' => $data['orderNo']??'',
|
||
'model' => $data['model']??'',
|
||
'pageLimit' => $data['pageLimit']??'',
|
||
'pageNum' => $data['pageNum']??'',
|
||
];
|
||
|
||
$response = $this->sendRequest($company, $company['url'].'/api/1.0.0/voucher/item/listPage', $body, $header);
|
||
//$this->saveLogStore("卡券查询".json_encode($response,320));
|
||
return $response;
|
||
}
|
||
} |