quanwei
2025-11-21 1db9a4130699636cabe7e0c9f7f15d004aadada0
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
 
namespace app\api\controller\takeout;
 
use app\api\controller\Controller;
 
use app\common\model\store\Order as StoreOrderModel;
use app\api\model\order\Order as OrderModel;
 
use app\common\exception\BaseException;
use app\api\model\takeout\Commander as CommanderModel;
use app\api\model\takeout\Deliveryman as DeliverymanModel;
use app\common\model\takeout\CommanderCash as CashModel;
use app\api\model\user\UserAuth;
 
/**
 * 门店列表
 */
class Commander extends Controller
{
    /**
     * 团长详情
     */
    public function detail()
    {
        $user = $this->getUser();
        $detail=CommanderModel::getDetail($user['user_id']);
        if (empty($detail)) {
            return $this->renderError('您还不是团长');
        }
        $settlement = $this->getSet();
        $payType=$settlement['pay_type'];
        return $this->renderSuccess('', compact('detail','payType'));
    }
    //数据
    public function index()
    {
        $user = $this->getUser();
        $commander =CommanderModel::getDetail($user['user_id']);
        if (empty($commander)) {
            return $this->renderError('您还不是团长');
        }
        return $this->renderSuccess('', compact('commander'));
    }
    /**
     * 我的订单列表
     */
    public function order($dataType)
    {
        $data = $this->postData();
        $model = new OrderModel;
        $user = $this->getUser();
        
        $list = $model->getList($user['user_id'], $dataType, $data);
        return $this->renderSuccess('', compact('list'));
    }
    /**
     * 提交申请
     */
    public function cashsubmit($data)
    {
        $user = $this->getUser();
        $detail =CommanderModel::getDetail($user['user_id']);
        if (empty($detail)) {
            return $this->renderError('您还不是团长');
        }
        
        $formData = json_decode(htmlspecialchars_decode($data), true);
         
        // 数据验证
        $this->validation($detail, $formData);
        
        // 新增申请记录
        $CashModel=new CashModel;
        $CashModel->save(array_merge($formData, [
            'user_id' => $user['user_id'],
            'commander_id' => $detail['commander_id'],
            'school_id' => $detail['school_id'],
            'apply_status' => 10,
            'app_id' => $user['app_id'],
        ]));
        // 冻结用户资金
        if ( $detail->freezeMoney($formData['money'])) {
            return $this->renderSuccess('成功');
        }
        return $this->renderError($detail->getError() ?: '提交失败');
    }
    private function getSet()
    {
        //提现方式 10微信 20支付宝 30银行卡
        $payType=['10','20','30'];
        $settlement=['pay_type'=>$payType,'min_money'=>0.01];
        return $settlement;
    }
    /**
     * 数据验证
     */
    private function validation($detail, $data)
    {
        // 结算设置
        $settlement = $this->getSet();
        // 最低提现佣金
        if ($data['money'] <= 0) {
            throw new BaseException(['msg' => '提现金额不正确']);
        }
        if ($detail['money'] <= 0) {
            throw new BaseException(['msg' => '当前用户没有可提现佣金']);
        }
        if ($data['money'] > $detail['money']) {
            throw new BaseException(['msg' => '提现金额不能大于可提现佣金']);
        }
        if ($data['money'] < $settlement['min_money']) {
            throw new BaseException(['msg' => '最低提现金额为' . $settlement['min_money']]);
        }
        if (!in_array($data['pay_type'], $settlement['pay_type'])) {
            throw new BaseException(['msg' => '提现方式不正确']);
        }
        if ($data['pay_type'] == '20') {
            if (empty($data['alipay_name']) || empty($data['alipay_account'])) {
                throw new BaseException(['msg' => '请补全提现信息']);
            }
        } elseif ($data['pay_type'] == '30') {
            if (empty($data['bank_name']) || empty($data['bank_account']) || empty($data['bank_card'])) {
                throw new BaseException(['msg' => '请补全提现信息']);
            }
        }elseif ($data['pay_type'] == '10') {
            //微信支付需要实名认证
            $auth = UserAuth::detail($detail['user_id']);
            if(empty($auth)){
                throw new BaseException(['msg' => '请先到个人中心->设置->实名认证']);
            }elseif(!empty($auth) && $auth["auth_status"] != 1){
                throw new BaseException(['msg' => '您的实名认证还未审核通过']);
            }
        }
    }
    /**
     * 提现明细
     */
    public function cashlists($status = -1)
    {
        $postData = $this->postData();
        $user = $this->getUser();
        $user_id=$user['user_id'];
        
        $model = new CashModel;
        $status > -1 && $model = $model->where('apply_status', '=', $status);
        $list=$model->where('user_id', '=', $user_id)->order(['create_time' => 'desc'])
            ->paginate($postData);
            
        return $this->renderSuccess('', [
            // 提现明细列表
            'list' => $list
        ]);
    }
    /**
     * 结算订单列表
     */
    public function orderlists()
    {
        $user = $this->getUser();
        $detail=CommanderModel::getDetail($user['user_id']);
        if (empty($detail)) {
            return $this->renderError('您还不是团长');
        }
        $model = new StoreOrderModel;
        return $this->renderSuccess('', [
            // 提现明细列表
            'list' => $model->getSettled()
        ]);
    }
    /**
     * 我的外卖配送员列表
     */
    public function teamlists()
    {
        $user = $this->getUser();
        $commander =CommanderModel::getDetail($user['user_id']);
        if (empty($commander)) {
            return $this->renderError('您还不是团长');
        }
        
        $postData = $this->postData();
        $postData["commander_id"] = $commander["commander_id"];
        $model = new DeliverymanModel();
        return $this->renderSuccess('', [
            // 团长用户信息
            'commander' =>$commander,
            // 我的团队列表
            'list' => $model->getList($postData),
        ]);
    }
    /**
     * 添加配送员
     */
    public function add()
    {
        $postData = $this->postData();
        if(empty($postData["user_id"])){
            return $this->renderError('请输入用户小程序id');
        }
        if(empty($postData["real_name"]) || empty($postData["mobile"])){
            return $this->renderError('请填写姓名和手机号');
        }
        $is_exsit = DeliverymanModel::isDeliveryman($postData["user_id"]);
        if($is_exsit){
            return $this->renderError('该会员已成为配送员');
        }
        $user = $this->getUser();
        $commander =CommanderModel::getDetail($user['user_id']);
        if (empty($commander)) {
            return $this->renderError('您还不是团长');
        }
        $postData["school_id"] = $commander["school_id"];
        $postData["commander_id"] = $commander["commander_id"];
        $model = new DeliverymanModel;
        // 新增记录
        if ($model->add($postData["user_id"],$postData)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
}