quanwei
2025-12-09 ca425b889f3c1b5847ffc26a0229307f7f8ef43e
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
<?php
 
namespace app\common\model\plus\vip;
 
use app\common\model\BaseModel;
use app\common\model\plus\agent\Referee as agentReferee;
use app\common\model\plus\agent\User as agentUser;
use app\common\model\user\User as UserModel;
use app\common\model\plus\agent\Referee as AgentRefereeModel;
 
/**
 * VIP专区申请模型
 */
class Apply extends BaseModel
{
    protected $name = 'vip_area_apply';
    protected $pk = 'apply_id';
 
    /**
     * 打款方式
     * @var array
     */
    public $payType = [
        10 => '微信',
        20 => '支付宝',
        30 => '银行卡',
    ];
 
    /**
     * 申请状态
     * @var array
     */
    public $applyStatus = [
        10 => '待审核',
        20 => '审核通过',
        30 => '驳回',
    ];
 
    /**
     * 关联用户表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\user\User');
    }
 
    /**
     * 关联VIP专区用户表
     * @return \think\model\relation\BelongsTo
     */
    public function vipUser()
    {
        return $this->belongsTo('User');
    }
 
    /**
     * 获取详情
     */
    public static function detail($apply_id)
    {
        return (new static())->find($apply_id);
    }
 
    /**
     * 购买指定商品成为VIP用户
     * @param $userId
     * @param $productIds
     * @param $appId
     * @param $order
     * @return bool
     */
    public function becomeVipUser($userId, $productIds, $appId, $order)
    {
        // 验证是否设置
        $config = Setting::getItem('basic', $appId);
        if (empty($config['become__buy_product_ids'])) {
            return false;
        }
        // 判断商品是否在设置范围内
        $intersect = array_intersect($productIds, $config['become__buy_product_ids']);
        if (empty($intersect)) {
            return false;
        }
 
        // 检查用户是否已经是VIP用户
        if(User::isVipUser($userId)) {
            return false;
        }
        //同时成为分销商
        $referee_id = agentReferee::getRefereeUserId($userId, 1);
 
        agentUser::add($userId, [
            'referee_id' => $referee_id,
            'app_id' => $appId,
        ]);
        // 新增VIP用户
        User::add($userId, [
            'referee_id' => AgentRefereeModel::getRefereeUserId($userId, 1),
            'app_id' => $appId,
        ]);
        return true;
    }
 
    /**
     * 审核状态
     * @param $value
     * @return array
     */
    public function getApplyStatusAttr($value)
    {
        $method = [10 => '待审核', 20 => '审核通过', 30 => '驳回'];
        return ['text' => $method[$value], 'value' => $value];
    }
 
    /**
     * 打款方式
     * @param $value
     * @return array
     */
    public function getPayTypeAttr($value)
    {
        $method = [10 => '微信', 20 => '支付宝', 30 => '银行卡'];
        return ['text' => $method[$value], 'value' => $value];
    }
 
    /**
     * 获取申请列表
     */
    public function getList($search)
    {
        $model = $this->with(['user', 'vipUser'])
            ->order(['create_time' => 'desc']);
            
        if (!empty($search['nick_name'])) {
            $model = $model->where('user.nickName', 'like', '%' . $search['nick_name'] . '%');
        }
 
        if (isset($search['apply_status']) && $search['apply_status'] > -1) {
            $model = $model->where('apply_status', '=', $search['apply_status']);
        }
 
        return $model->paginate($search['list_rows']);
    }
}