<?php
|
|
namespace app\api\controller\plus\business;
|
|
use app\api\controller\Controller;
|
use app\api\model\plus\business\Business as BusinessModel;
|
use app\api\model\plus\business\Order as OrderModel;
|
use app\common\model\plus\business\Setting as SettingModel;
|
use app\common\enum\order\OrderTypeEnum;
|
use app\api\service\pay\PayService;
|
use app\common\enum\order\OrderPayTypeEnum;
|
use app\api\service\order\paysuccess\type\BusinessCardPaySuccessService;
|
|
/**
|
* 名片置顶控制器
|
*/
|
class Top extends Controller
|
{
|
/**
|
* 创建名片置顶订单
|
*/
|
public function create()
|
{
|
// 获取用户信息
|
$user = $this->getUser();
|
|
// 获取请求参数
|
$params = $this->request->param();
|
|
// 验证参数
|
if (!isset($params['business_card_id'])) {
|
return $this->renderError('缺少必要参数');
|
}
|
|
// 获取名片信息
|
$businessCard = BusinessModel::detail($params['business_card_id']);
|
if (!$businessCard) {
|
return $this->renderError('名片信息不存在');
|
}
|
|
// 验证是否是名片所有者
|
if ($businessCard['user_id'] != $user['user_id']) {
|
return $this->renderError('只能为自己创建名片置顶订单');
|
}
|
|
// 获取置顶设置
|
$setting = SettingModel::getItem('basic');
|
$topAmount = isset($setting['top_amount']) ? $setting['top_amount'] : 10; // 默认10元
|
$topDays = isset($setting['top_days']) ? $setting['top_days'] : 7; // 默认7天
|
|
// 如果价格为0,直接标记为已支付
|
if ($topAmount == 0) {
|
return $this->renderError('免费置顶,无需支付');
|
}
|
|
// 创建订单模型
|
$orderModel = new OrderModel();
|
$order_no = $orderModel->orderNo();
|
|
// 创建订单数据
|
$orderData = [
|
'user_id' => $user['user_id'],
|
'business_card_id' => $params['business_card_id'],
|
'grade_id' => $businessCard['grade_id'],
|
'order_no' => $order_no,
|
'pay_price' => $topAmount,
|
'order_source' => 20, // 20表示名片置顶
|
'app_id' => $user['app_id']
|
];
|
|
// 保存订单
|
if (!$orderModel->save($orderData)) {
|
return $this->renderError('订单创建失败');
|
}
|
|
$orderModel = $orderModel->where('order_no', $order_no)->find();
|
|
// 构建支付参数
|
$payType = isset($params['pay_type']) ? $params['pay_type'] : OrderPayTypeEnum::WECHAT;
|
|
return $this->renderSuccess('订单创建成功', [
|
'order_id' => $orderModel['order_id'],
|
'order_no' => $orderModel['order_no'],
|
'pay_type' => $payType,
|
'order_type' => OrderTypeEnum::BUSINESS_CARD,
|
'pay_price' => $topAmount,
|
'top_days' => $topDays
|
]);
|
}
|
|
/**
|
* 名片置顶订单支付
|
*/
|
public function pay()
|
{
|
// 获取用户信息
|
$user = $this->getUser();
|
|
// 获取请求参数
|
$params = $this->request->param();
|
$orderId = $params['order_id'];
|
|
// 获取订单详情
|
$orderModel = OrderModel::getDetail($orderId);
|
if (!$orderModel) {
|
return $this->renderError('订单不存在');
|
}
|
|
// 验证订单是否属于当前用户
|
if ($orderModel['user_id'] != $user['user_id']) {
|
return $this->renderError('订单不属于当前用户');
|
}
|
|
// 验证订单是否为置顶订单
|
if ($orderModel['order_source'] != 20) {
|
return $this->renderError('订单类型错误');
|
}
|
|
// 订单已支付
|
if ($orderModel['pay_status'] == 20) {
|
return $this->renderError('订单已支付');
|
}
|
|
if ($this->request->isGet()) {
|
// 支付金额
|
$payPrice = $orderModel['pay_price'];
|
$balance = $this->getUser()['balance'];
|
return $this->renderSuccess('', compact('payPrice', 'balance'));
|
}
|
|
$payType = $params['pay_type'] ?? OrderPayTypeEnum::WECHAT;
|
|
// 构建支付参数
|
$payment = $orderModel->orderPayment($user, $orderModel, $payType, $params);
|
|
return $this->renderSuccess('', [
|
'order_id' => $orderId,
|
'business_card_id' => $orderModel['business_card_id'],
|
'payPrice' => $orderModel['pay_price'],
|
'pay_type' => $params['use_balance'] == 1 ? OrderPayTypeEnum::BALANCE : $payment['payType'], // 支付方式
|
'payment' => $payment['payment'], // 微信支付参数
|
'order_type' => OrderTypeEnum::BUSINESS_CARD, //订单类型
|
'use_balance' => $params['use_balance'] ?? 0, // 是否使用余额
|
]);
|
}
|
}
|