quanwei
2025-11-28 3ea53e61cc23fdb3ddf8b38a199ca60a6da8c407
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?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);
        if (!$payment) {
            return $this->renderError($orderModel->getError() ?: '订单支付失败');
        }
        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, // 是否使用余额
        ]);
    }
}