quanwei
2026-01-17 e1e2fe5710a5b5cd9c19bd3aa99c998a1a613ca8
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
 
namespace app\api\controller\user;
 
use app\api\controller\Controller;
use app\api\model\settings\Express as ExpressModel;
use app\api\model\order\OrderProduct as OrderProductModel;
use app\api\model\order\OrderRefund as OrderRefundModel;
use app\api\model\settings\Message as MessageModel;
use app\common\model\settings\ReturnAddress as ReturnAddressModel;
 
/**
 * 订单售后服务
 */
class Refund extends Controller
{
    // $user
    private $user;
 
    /**
     * 构造方法
     */
    public function initialize()
    {
        $this->user = $this->getUser();   // 用户信息
    }
 
    /**
     * 用户售后单列表
     */
    public function lists($state = -1)
    {
        $model = new OrderRefundModel;
        $list = $model->getList($this->user['user_id'], $state, $this->postData());
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 申请售后
     */
    public function apply($order_product_id, $platform = 'wx')
    {
        // 订单商品详情
        $detail = OrderProductModel::detail($order_product_id);
        if (isset($product['refund']) && !empty($detail['refund'])) {
            return $this->renderError('当前商品已申请售后');
        }
        if ($detail['orderM']['order_source'] == 70 && $detail['orderM']['advance']['money_return'] == 1) {
            $detail['total_pay_price'] = round($detail['total_pay_price'] + $detail['orderM']['advance']['pay_price'], 2);
        }
        if ($this->request->isGet()) {
            // 如果来源是小程序, 则获取小程序订阅消息id.获取售后通知.
            $template_arr = MessageModel::getMessageByNameArr($platform, ['order_refund_user']);
            return $this->renderSuccess('', compact('detail', 'template_arr'));
        }
        // 新增售后单记录
        $model = new OrderRefundModel;
        if ($model->apply($this->user, $detail, $this->request->post())) {
            return $this->renderSuccess('提交成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }
 
    /**
     * 申请平台介入售后
     */
    public function plateapply($order_refund_id)
    {
        // 订单详情
        $detail = OrderRefundModel::detail($order_refund_id);
        if (!in_array($detail['status']['value'], [0, 10]) || $detail['plate_status']['value'] != 0) {
            return $this->renderError('当前状态不允许申请');
        }
        // 新增记录
        $model = new OrderRefundModel;
        if ($model->plateapply($order_refund_id)) {
            return $this->renderSuccess('提交成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }
 
    /**
     * 售后单详情
     */
    public function detail($order_refund_id, $platform = '')
    {
        // 售后单详情
        $detail = OrderRefundModel::detail([
            'user_id' => $this->user['user_id'],
            'order_refund_id' => $order_refund_id
        ]);
        if (empty($detail)) {
            return $this->renderError('售后单不存在');
        }
        $detail['orderproduct']['max_refund_money'] = $detail['orderproduct']['total_pay_price'];
        if ($detail['order_master']['order_source'] == 70) {
            $detail['orderproduct']['total_pay_price'] = round($detail['orderproduct']['total_pay_price'] + $detail['order_master']['advance']['pay_price'], 2);
            if ($detail['order_master']['advance']['money_return'] == 1) {
                $detail['orderproduct']['max_refund_money'] = round($detail['orderproduct']['max_refund_money'] + $detail['order_master']['advance']['pay_price'], 2);
            }
        }
        // 物流公司列表
        $model = new ExpressModel();
        $expressList = $model->getAll();
        // 如果来源是小程序, 则获取小程序订阅消息id.获取售后通知.
        $template_arr = MessageModel::getMessageByNameArr($platform, ['order_refund_user']);
        // 退货地址
        $address = (new ReturnAddressModel)->getAll($detail['shop_supplier_id']);
        return $this->renderSuccess('', compact('address', 'detail', 'expressList', 'template_arr'));
    }
 
    /**
     * 用户发货
     */
    public function delivery($order_refund_id)
    {
        // 售后单详情
        $model = OrderRefundModel::detail([
            'user_id' => $this->user['user_id'],
            'order_refund_id' => $order_refund_id
        ]);
        if ($model->delivery($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }
 
    /**
     * 商家审核
     */
    public function audit($order_refund_id)
    {
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->audit($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }
 
    /**
     * 确认收货并退款
     */
    public function receipt($order_refund_id)
    {
        if (!$this->request->isPost()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->receipt($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }
 
    /**
     * 获取物流信息
     */
    public function express($order_refund_id)
    {
        // 订单信息
        $model = OrderRefundModel::detail($order_refund_id);
        if (!$model['send_express_no']) {
            return $this->renderError('没有物流信息');
        }
        // 获取物流信息
        $model = $model['sendexpress'];
        $express = $model->dynamic($model['express_name'], $model['express_code'], $model['send_express_no']);
        if ($express === false) {
            return $this->renderError($model->getError());
        }
        return $this->renderSuccess('', compact('express'));
    }
 
}