提交
This commit is contained in:
@@ -3,7 +3,7 @@ declare (strict_types=1);
|
||||
|
||||
namespace app\miniapi\controller;
|
||||
|
||||
use api\Httpcurl;
|
||||
use app\miniapi\service\Order\OrderService;
|
||||
use app\miniapi\service\Payment\MiniPayService;
|
||||
|
||||
class Order extends Base
|
||||
@@ -15,39 +15,14 @@ class Order extends Base
|
||||
return $userId;
|
||||
}
|
||||
|
||||
$payStatus = (string)$this->request->param('pay_status', 'pay');
|
||||
if (!in_array($payStatus, ['pay', 'nopay'], true)) {
|
||||
return $this->fail('参数错误');
|
||||
$service = new OrderService();
|
||||
$business = $service->businessCode((string)$this->request->param('business', 'evus'));
|
||||
$result = $service->lists($business, (string)$this->request->param('pay_status', 'pay'), $userId);
|
||||
if (intval($result['code'] ?? 0) !== 1) {
|
||||
return $this->fail((string)($result['msg'] ?? '操作失败'), intval($result['code'] ?? 0), $result['data'] ?? []);
|
||||
}
|
||||
|
||||
$business = $this->businessCode((string)$this->request->param('business', 'evus'));
|
||||
$model = $this->businessModel($business);
|
||||
$invoiceType = $this->businessInvoiceType($business);
|
||||
$orderUrl = trim((string)config('app.get_order_url'));
|
||||
if ($orderUrl === '') {
|
||||
return $this->ok([]);
|
||||
}
|
||||
|
||||
$response = Httpcurl::request($orderUrl, 'post', [
|
||||
'model' => $model,
|
||||
'user_id' => $userId,
|
||||
'invoiceType' => $invoiceType,
|
||||
'pay_status' => $payStatus,
|
||||
]);
|
||||
$result = $this->decodeResponse($response, 'miniapi order list');
|
||||
if (empty($result) || intval($result['code'] ?? 0) !== 1) {
|
||||
return $this->ok([]);
|
||||
}
|
||||
|
||||
$orders = array_map(static function ($item) use ($business) {
|
||||
if (is_array($item)) {
|
||||
$item['business'] = $business;
|
||||
$item['business_text'] = $business === 'esta' ? 'ESTA' : 'EVUS';
|
||||
}
|
||||
return $item;
|
||||
}, (array)($result['data'] ?? []));
|
||||
|
||||
return $this->ok($orders);
|
||||
return $this->ok($result['data'] ?? []);
|
||||
}
|
||||
|
||||
public function detail()
|
||||
@@ -62,13 +37,14 @@ class Order extends Base
|
||||
return $this->fail('参数错误');
|
||||
}
|
||||
|
||||
$business = $this->businessCode((string)$this->request->param('business', 'evus'));
|
||||
$data = $this->fetchOrderDetail($orderSn, $business, $userId);
|
||||
if (isset($data['error'])) {
|
||||
return $this->fail($data['error'], intval($data['code'] ?? 0));
|
||||
$service = new OrderService();
|
||||
$business = $service->businessCode((string)$this->request->param('business', 'evus'));
|
||||
$result = $service->detail($business, $orderSn, $userId);
|
||||
if (intval($result['code'] ?? 0) !== 1) {
|
||||
return $this->fail((string)($result['msg'] ?? '操作失败'), intval($result['code'] ?? 0), $result['data'] ?? []);
|
||||
}
|
||||
|
||||
return $this->ok($data);
|
||||
return $this->ok($result['data'] ?? []);
|
||||
}
|
||||
|
||||
public function pay()
|
||||
@@ -80,18 +56,19 @@ class Order extends Base
|
||||
|
||||
$data = $this->postData();
|
||||
$orderSn = trim((string)($data['order_sn'] ?? ''));
|
||||
$business = $this->businessCode((string)($data['business'] ?? 'evus'));
|
||||
$openid = trim((string)($data['openid'] ?? ''));
|
||||
if ($orderSn === '' || $openid === '') {
|
||||
return $this->fail('参数错误');
|
||||
}
|
||||
|
||||
$detail = $this->fetchOrderDetail($orderSn, $business, $userId);
|
||||
if (isset($detail['error'])) {
|
||||
return $this->fail($detail['error'], intval($detail['code'] ?? 0));
|
||||
$service = new OrderService();
|
||||
$business = $service->businessCode((string)($data['business'] ?? 'evus'));
|
||||
$detail = $service->detail($business, $orderSn, $userId);
|
||||
if (intval($detail['code'] ?? 0) !== 1) {
|
||||
return $this->fail((string)($detail['msg'] ?? '操作失败'), intval($detail['code'] ?? 0), $detail['data'] ?? []);
|
||||
}
|
||||
|
||||
$money = $this->payMoney($detail);
|
||||
$money = $service->payMoney((array)($detail['data'] ?? []));
|
||||
if ($money === '') {
|
||||
return $this->fail('订单金额异常');
|
||||
}
|
||||
@@ -121,173 +98,13 @@ class Order extends Base
|
||||
}
|
||||
|
||||
$data = $this->postData();
|
||||
$business = $this->businessCode((string)($data['business'] ?? 'evus'));
|
||||
$orderSn = trim((string)($data['order_sn'] ?? ''));
|
||||
if ($orderSn === '') {
|
||||
return $this->fail('参数错误');
|
||||
}
|
||||
if ($error = $this->validateInvoiceData($data)) {
|
||||
return $error;
|
||||
}
|
||||
|
||||
$detail = $this->fetchOrderDetail($orderSn, $business, $userId);
|
||||
if (isset($detail['error'])) {
|
||||
return $this->fail($detail['error'], intval($detail['code'] ?? 0));
|
||||
}
|
||||
|
||||
$payload = $this->invoicePayload($data, $business, $userId);
|
||||
$orderUrl = trim((string)config('app.order_url'));
|
||||
if ($orderUrl === '') {
|
||||
return $this->fail('发票接口未配置');
|
||||
}
|
||||
|
||||
$response = Httpcurl::request(rtrim($orderUrl, '/') . '/add_invoice', 'post', $payload);
|
||||
$result = $this->decodeResponse($response, 'miniapi add invoice');
|
||||
if (empty($result)) {
|
||||
return $this->fail('操作失败,请重新填写');
|
||||
}
|
||||
$service = new OrderService();
|
||||
$business = $service->businessCode((string)($data['business'] ?? 'evus'));
|
||||
$result = $service->addInvoice($data, $business, $userId);
|
||||
if (intval($result['code'] ?? 0) !== 1) {
|
||||
return $this->fail((string)($result['msg'] ?? '操作失败,请重新填写'));
|
||||
return $this->fail((string)($result['msg'] ?? '操作失败'), intval($result['code'] ?? 0), $result['data'] ?? []);
|
||||
}
|
||||
|
||||
return $this->ok([], (string)($result['msg'] ?? '提交成功'));
|
||||
}
|
||||
|
||||
private function validateInvoiceData(array $data)
|
||||
{
|
||||
$required = [
|
||||
'order_id' => '订单信息',
|
||||
'phone' => '手机号',
|
||||
'title' => '客户名称',
|
||||
'tax_number' => '税号',
|
||||
'link_person' => '联系人',
|
||||
'link_email' => '邮箱',
|
||||
];
|
||||
foreach ($required as $field => $label) {
|
||||
if (trim((string)($data[$field] ?? '')) === '') {
|
||||
if ($field === 'order_id') {
|
||||
return $this->fail('订单信息异常,请刷新后重试');
|
||||
}
|
||||
return $this->fail('请填写' . $label);
|
||||
}
|
||||
}
|
||||
|
||||
$taxLength = strlen(trim((string)$data['tax_number']));
|
||||
if (!in_array($taxLength, [15, 17, 18, 20], true)) {
|
||||
return $this->fail('购方税号仅支持15、17、18、20位');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function fetchOrderDetail(string $orderSn, string $business, int $userId): array
|
||||
{
|
||||
$model = $this->businessModel($business);
|
||||
$detailUrl = $business === 'esta' ? config('app.get_order_detail_url') : config('app.get_evus_detail_url');
|
||||
if (empty($detailUrl)) {
|
||||
return ['error' => '订单详情接口未配置'];
|
||||
}
|
||||
|
||||
$response = Httpcurl::request($detailUrl, 'post', [
|
||||
'order_sn' => $orderSn,
|
||||
'model' => $model,
|
||||
'user_id' => $userId,
|
||||
]);
|
||||
$result = $this->decodeResponse($response, 'miniapi order detail');
|
||||
if (empty($result) || intval($result['code'] ?? 0) !== 1) {
|
||||
return ['error' => $result['msg'] ?? '订单不存在'];
|
||||
}
|
||||
|
||||
$data = $result['data'] ?? [];
|
||||
$orderUserId = intval($data['user_id'] ?? $data['order']['user_id'] ?? 0);
|
||||
if ($orderUserId > 0 && $orderUserId !== $userId) {
|
||||
return ['error' => '无权访问该订单', 'code' => 403];
|
||||
}
|
||||
|
||||
if (is_array($data)) {
|
||||
$data['business'] = $business;
|
||||
$data['business_text'] = $business === 'esta' ? 'ESTA' : 'EVUS';
|
||||
}
|
||||
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
private function businessCode(string $business): string
|
||||
{
|
||||
$business = strtolower(trim($business));
|
||||
return $business === 'esta' ? 'esta' : 'evus';
|
||||
}
|
||||
|
||||
private function businessModel(string $business): string
|
||||
{
|
||||
return $business === 'esta' ? ESTA_MODEL : MODEL;
|
||||
}
|
||||
|
||||
private function businessInvoiceType(string $business): string
|
||||
{
|
||||
return $business === 'esta' ? ESTA_INCOICETYPE : INCOICETYPE;
|
||||
}
|
||||
|
||||
private function payMoney(array $detail): string
|
||||
{
|
||||
$scopes = [$detail];
|
||||
if (isset($detail['order']) && is_array($detail['order'])) {
|
||||
$scopes[] = $detail['order'];
|
||||
}
|
||||
|
||||
foreach ($scopes as $scope) {
|
||||
foreach (['total_price', 'money', 'pay_money', 'order_amount', 'amount', 'product_value', 'origin_price'] as $field) {
|
||||
$money = $this->cleanMoney($scope[$field] ?? '');
|
||||
if ($money !== '') {
|
||||
return $money;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function cleanMoney($value): string
|
||||
{
|
||||
if (is_int($value) || is_float($value)) {
|
||||
return (string)$value;
|
||||
}
|
||||
|
||||
$value = trim((string)$value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = str_replace([',', '¥', '¥', '元', ' '], '', $value);
|
||||
return preg_match('/^\d+(\.\d+)?$/', $value) ? $value : '';
|
||||
}
|
||||
|
||||
private function invoicePayload(array $data, string $business, int $userId): array
|
||||
{
|
||||
$fields = [
|
||||
'phone',
|
||||
'title',
|
||||
'tax_number',
|
||||
'link_person',
|
||||
'link_email',
|
||||
'address',
|
||||
'mobile',
|
||||
'bank',
|
||||
'account',
|
||||
'order_id',
|
||||
'order_sn',
|
||||
];
|
||||
$payload = [];
|
||||
foreach ($fields as $field) {
|
||||
$payload[$field] = trim((string)($data[$field] ?? ''));
|
||||
}
|
||||
|
||||
$site = (array)config('app.miniapi_site', []);
|
||||
$payload['invoiceType'] = $this->businessInvoiceType($business);
|
||||
$payload['model'] = $this->businessModel($business);
|
||||
$payload['principals'] = (string)($site['principals'] ?? '');
|
||||
$payload['uid'] = $userId;
|
||||
|
||||
return $payload;
|
||||
return $this->ok($result['data'] ?? [], (string)($result['msg'] ?? '提交成功'));
|
||||
}
|
||||
}
|
||||
|
||||
240
app/miniapi/service/Order/OrderService.php
Normal file
240
app/miniapi/service/Order/OrderService.php
Normal file
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\miniapi\service\Order;
|
||||
|
||||
use api\Httpcurl;
|
||||
use think\facade\Log;
|
||||
|
||||
class OrderService
|
||||
{
|
||||
public function businessCode(string $business): string
|
||||
{
|
||||
$business = strtolower(trim($business));
|
||||
return $business === 'esta' ? 'esta' : 'evus';
|
||||
}
|
||||
|
||||
public function lists(string $business, string $payStatus, int $userId): array
|
||||
{
|
||||
if (!in_array($payStatus, ['pay', 'nopay'], true)) {
|
||||
return ['code' => 0, 'msg' => '参数错误', 'data' => []];
|
||||
}
|
||||
|
||||
$orderUrl = trim((string)config('app.get_order_url'));
|
||||
if ($orderUrl === '') {
|
||||
return ['code' => 1, 'msg' => 'ok', 'data' => []];
|
||||
}
|
||||
|
||||
$response = Httpcurl::request($orderUrl, 'post', [
|
||||
'model' => $this->businessModel($business),
|
||||
'user_id' => $userId,
|
||||
'invoiceType' => $this->businessInvoiceType($business),
|
||||
'pay_status' => $payStatus,
|
||||
]);
|
||||
$result = $this->decodeResponse($response, 'miniapi order list');
|
||||
if (empty($result) || intval($result['code'] ?? 0) !== 1) {
|
||||
return ['code' => 1, 'msg' => 'ok', 'data' => []];
|
||||
}
|
||||
|
||||
$orders = array_map(static function ($item) use ($business) {
|
||||
if (is_array($item)) {
|
||||
$item['business'] = $business;
|
||||
$item['business_text'] = $business === 'esta' ? 'ESTA' : 'EVUS';
|
||||
}
|
||||
return $item;
|
||||
}, $this->orderListItems((array)($result['data'] ?? [])));
|
||||
|
||||
return ['code' => 1, 'msg' => 'ok', 'data' => $orders];
|
||||
}
|
||||
|
||||
public function detail(string $business, string $orderSn, int $userId): array
|
||||
{
|
||||
$detailUrl = $business === 'esta' ? config('app.get_order_detail_url') : config('app.get_evus_detail_url');
|
||||
if (empty($detailUrl)) {
|
||||
return ['code' => 0, 'msg' => '订单详情接口未配置', 'data' => []];
|
||||
}
|
||||
|
||||
$response = Httpcurl::request($detailUrl, 'post', [
|
||||
'order_sn' => $orderSn,
|
||||
'model' => $this->businessModel($business),
|
||||
'user_id' => $userId,
|
||||
]);
|
||||
$result = $this->decodeResponse($response, 'miniapi order detail');
|
||||
if (empty($result) || intval($result['code'] ?? 0) !== 1) {
|
||||
return ['code' => 0, 'msg' => $result['msg'] ?? '订单不存在', 'data' => []];
|
||||
}
|
||||
|
||||
$data = $result['data'] ?? [];
|
||||
if (!is_array($data)) {
|
||||
return ['code' => 0, 'msg' => '订单不存在', 'data' => []];
|
||||
}
|
||||
|
||||
$orderUserId = intval($data['user_id'] ?? $data['order']['user_id'] ?? 0);
|
||||
if ($orderUserId > 0 && $orderUserId !== $userId) {
|
||||
return ['code' => 403, 'msg' => '无权访问该订单', 'data' => []];
|
||||
}
|
||||
|
||||
$data['business'] = $business;
|
||||
$data['business_text'] = $business === 'esta' ? 'ESTA' : 'EVUS';
|
||||
|
||||
return ['code' => 1, 'msg' => 'ok', 'data' => $data];
|
||||
}
|
||||
|
||||
public function payMoney(array $detail): string
|
||||
{
|
||||
$scopes = [$detail];
|
||||
if (isset($detail['order']) && is_array($detail['order'])) {
|
||||
$scopes[] = $detail['order'];
|
||||
}
|
||||
|
||||
foreach ($scopes as $scope) {
|
||||
foreach (['total_price', 'money', 'pay_money', 'order_amount', 'amount', 'product_value', 'origin_price'] as $field) {
|
||||
$money = $this->cleanMoney($scope[$field] ?? '');
|
||||
if ($money !== '') {
|
||||
return $money;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public function addInvoice(array $data, string $business, int $userId): array
|
||||
{
|
||||
$orderSn = trim((string)($data['order_sn'] ?? ''));
|
||||
if ($orderSn === '') {
|
||||
return ['code' => 0, 'msg' => '参数错误', 'data' => []];
|
||||
}
|
||||
|
||||
$error = $this->validateInvoiceData($data);
|
||||
if ($error !== '') {
|
||||
return ['code' => 0, 'msg' => $error, 'data' => []];
|
||||
}
|
||||
|
||||
$detail = $this->detail($business, $orderSn, $userId);
|
||||
if (intval($detail['code'] ?? 0) !== 1) {
|
||||
return $detail;
|
||||
}
|
||||
|
||||
$orderUrl = trim((string)config('app.order_url'));
|
||||
if ($orderUrl === '') {
|
||||
return ['code' => 0, 'msg' => '发票接口未配置', 'data' => []];
|
||||
}
|
||||
|
||||
$response = Httpcurl::request(rtrim($orderUrl, '/') . '/add_invoice', 'post', $this->invoicePayload($data, $business, $userId));
|
||||
$result = $this->decodeResponse($response, 'miniapi add invoice');
|
||||
if (empty($result)) {
|
||||
return ['code' => 0, 'msg' => '操作失败,请重新填写', 'data' => []];
|
||||
}
|
||||
if (intval($result['code'] ?? 0) !== 1) {
|
||||
return ['code' => 0, 'msg' => (string)($result['msg'] ?? '操作失败,请重新填写'), 'data' => []];
|
||||
}
|
||||
|
||||
return ['code' => 1, 'msg' => (string)($result['msg'] ?? '提交成功'), 'data' => []];
|
||||
}
|
||||
|
||||
private function businessModel(string $business): string
|
||||
{
|
||||
return $business === 'esta' ? ESTA_MODEL : MODEL;
|
||||
}
|
||||
|
||||
private function businessInvoiceType(string $business): string
|
||||
{
|
||||
return $business === 'esta' ? ESTA_INCOICETYPE : INCOICETYPE;
|
||||
}
|
||||
|
||||
private function orderListItems(array $data): array
|
||||
{
|
||||
foreach (['list', 'rows', 'orders', 'items', 'data'] as $field) {
|
||||
if (isset($data[$field]) && is_array($data[$field])) {
|
||||
return $data[$field];
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($data) === $data ? $data : [];
|
||||
}
|
||||
|
||||
private function validateInvoiceData(array $data): string
|
||||
{
|
||||
$required = [
|
||||
'order_id' => '订单信息',
|
||||
'phone' => '手机号',
|
||||
'title' => '客户名称',
|
||||
'tax_number' => '税号',
|
||||
'link_person' => '联系人',
|
||||
'link_email' => '邮箱',
|
||||
];
|
||||
foreach ($required as $field => $label) {
|
||||
if (trim((string)($data[$field] ?? '')) === '') {
|
||||
return $field === 'order_id' ? '订单信息异常,请刷新后重试' : '请填写' . $label;
|
||||
}
|
||||
}
|
||||
|
||||
$taxLength = strlen(trim((string)$data['tax_number']));
|
||||
if (!in_array($taxLength, [15, 17, 18, 20], true)) {
|
||||
return '购方税号仅支持15、17、18、20位';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function invoicePayload(array $data, string $business, int $userId): array
|
||||
{
|
||||
$fields = [
|
||||
'phone',
|
||||
'title',
|
||||
'tax_number',
|
||||
'link_person',
|
||||
'link_email',
|
||||
'address',
|
||||
'mobile',
|
||||
'bank',
|
||||
'account',
|
||||
'order_id',
|
||||
'order_sn',
|
||||
];
|
||||
$payload = [];
|
||||
foreach ($fields as $field) {
|
||||
$payload[$field] = trim((string)($data[$field] ?? ''));
|
||||
}
|
||||
|
||||
$site = (array)config('app.miniapi_site', []);
|
||||
$payload['invoiceType'] = $this->businessInvoiceType($business);
|
||||
$payload['model'] = $this->businessModel($business);
|
||||
$payload['principals'] = (string)($site['principals'] ?? '');
|
||||
$payload['uid'] = $userId;
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
private function cleanMoney($value): string
|
||||
{
|
||||
if (is_int($value) || is_float($value)) {
|
||||
return (string)$value;
|
||||
}
|
||||
|
||||
$value = trim((string)$value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = str_replace([',', '¥', '¥', '元', ' '], '', $value);
|
||||
return preg_match('/^\d+(\.\d+)?$/', $value) ? $value : '';
|
||||
}
|
||||
|
||||
private function decodeResponse($response, string $scene): array
|
||||
{
|
||||
if (!is_array($response) || !isset($response[0]) || !empty($response[3])) {
|
||||
Log::error($scene . ' request failed: ' . json_encode($response, JSON_UNESCAPED_UNICODE));
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = json_decode($response[0], true);
|
||||
if (!is_array($result)) {
|
||||
Log::error($scene . ' response decode failed: ' . $response[0]);
|
||||
return [];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user