huangsijun
2025-09-22 a78c011de350b188afb03beb2f26a73f35f71986
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
<?php
 
namespace app\api\model\plus\agent;
 
use app\api\model\plus\agent\Referee as RefereeModel;
use app\common\model\plus\agent\Apply as ApplyModel;
use app\common\model\plus\agent\Grade as GradeModel;
 
/**
 * 分销商申请模型
 */
class Apply extends ApplyModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'create_time',
        'update_time',
    ];
 
    /**
     * 是否为分销商申请中
     */
    public static function isApplying($user_id)
    {
        $detail = self::detail(['user_id' => $user_id]);
        return $detail ? ((int)$detail['apply_status']['value'] === 10) : false;
    }
 
    /**
     * 提交申请
     */
    public function submit($user, $data)
    {
        // 成为分销商条件
        $config = Setting::getItem('condition');
        // 如果之前有关联分销商,则继续关联之前的分销商
        $has_referee_id = Referee::getRefereeUserId($user['user_id'], 1);
        if($has_referee_id > 0){
            $referee_id = $has_referee_id;
        }else{
            $referee_id = $data['referee_id'] > 0 ?$data['referee_id']:0;
        }
        // 数据整理
        $data = [
            'user_id' => $user['user_id'],
            'real_name' => trim($data['name']),
            'mobile' => trim($data['mobile']),
            'referee_id' => $referee_id,
            'apply_type' => $config['become'],
            'apply_time' => time(),
            'app_id' => self::$app_id,
        ];
        if ($config['become'] == 10) {
            $data['apply_status'] = 10;
        } elseif ($config['become'] == 20) {
            $data['apply_status'] = 20;
        }
        return $this->add($user, $data);
    }
 
    /**
     * 更新分销商申请信息
     */
    private function add($user, $data)
    {
        // 实例化模型
        $model = self::detail(['user_id' => $user['user_id']]) ?: $this;
        // 更新记录
        $this->startTrans();
        try {
            // 保存申请信息
            $model->save($data);
            // 无需审核,自动通过
            if ($data['apply_type'] == 20) {
                // 新增分销商用户记录
                User::add($user['user_id'], [
                    'real_name' => $data['real_name'],
                    'mobile' => $data['mobile'],
                    'referee_id' => $data['referee_id'],
                    'grade_id' => (new GradeModel())->getDefaultGradeId()
                ]);
            }
            // 记录推荐人关系
            if ($data['referee_id'] > 0) {
                RefereeModel::createRelation($user['user_id'], $data['referee_id']);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
}