huangsijun
2025-12-11 097a5f9e524acd965fa2abcfd18db30fc3f00ddb
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
<?php
 
namespace app\api\controller\branch;
 
use app\api\controller\Controller;
use app\api\model\branch\Activity as ActivityModel;
use app\api\model\branch\Category as CategoryModel;
use app\api\model\branch\ActivityUser as ActivityUserModel; // 报名会员
use app\api\model\branch\Member as MemberModel; // 会员
use app\api\model\branch\ActivityFile as ActivityFileModel; // 活动文件
use app\api\model\branch\ActivityCart as ActivityCartModel; // 活动购物车
// use app\api\model\user\BalanceOrder as BalanceOrderModel;
// use app\common\enum\order\OrderPayTypeEnum;
use app\common\enum\order\OrderTypeEnum;
use app\api\model\settings\Setting as SettingModel;
use app\common\service\qrcode\BranchActivityService;
use app\common\model\branch\Setting as BranchSettingModel;
use xin\helper\Func;
 
/**
 * 活动控制器
 */
class Activity extends Controller
{
    /**
     *获取分类
     */
    public function category()
    {
        // 分类
        $category = CategoryModel::getAll();
        return $this->renderSuccess('', compact('category'));
    }
 
    /**
     * 活动列表
     */
    public function index($category_id = 0)
    {
        $model = new ActivityModel;
        $list = $model->getList($category_id, $this->postData(), $this->getUser());
        $words = BranchSettingModel::getItem('words');
        return $this->renderSuccess('', compact('list', 'words'));
    }
 
    /**
     *活动详情
     */
    public function detail($activity_id)
    {
        $detail = ActivityModel::detail($activity_id);
        $user = $this->getUser();
        $detail["balance"] = $user["balance"]; // 余额
        $detail["points"] = $user["points"]; // 积分
        // 用户是否已报名
        $detail['is_reg'] = ActivityUserModel::isReg($user['user_id'], $activity_id);
        // 用户是否已加入分会
        $detail['is_member'] = MemberModel::isMember($user['user_id']);
        // 用户是否已签到
        $detail['is_verify'] = ActivityUserModel::isVerify($user['user_id'], $activity_id);
        $store = SettingModel::getItem('store');
        $detail['store_name'] = $store['name']; // 平台名称
        $points = SettingModel::getItem('points');
        $detail['points_ratio'] = $points['discount']['discount_ratio']; // 积分抵扣比例
        // 获取报名用户列表
        $userList = (new ActivityUserModel())->getListForActivity(['activity_id' => $activity_id], false, 8);
        return $this->renderSuccess('', compact('detail', 'userList'));
    }
 
    /**
     *提交活动报名
     */
    public function submit()
    {
        $params = json_decode($this->postData()['params'], true);
        // 用户信息
        $user = $this->getUser();
        // 生成报名用户
        $model = new ActivityUserModel();
        if (!$model->addUser($user, $params)) {
            return $this->renderError($model->getError() ?: '报名失败');
        }
        $order_id = $model['order_id'];
        // 如果需要在线支付
        if ($model['online_money'] > 0) {
            // 处理微信支付
            $payment = ActivityUserModel::onOrderPayment($user, $model['order_no'], $params['pay_type'], $params['pay_source'], $model['online_money']);
            // 返回结算信息
            return $this->renderSuccess(['success' => '支付成功', 'error' => '未支付'], [
                'order_id' => $order_id,   // 订单id
                'online_money' => $model['online_money'],
                'pay_type' =>  $params['pay_type'],  // 支付方式
                'payment' => $payment,               // 微信支付参数
                'order_type' => OrderTypeEnum::BRANCHACTIVITY, //订单类型
            ]);
        } else {
            return $this->renderSuccess('', compact('order_id'));
        }
    }
    public function getRegistrationInformation($user_id = 0)
    {
        // 生成报名用户
        $model = new ActivityUserModel();
        $userId = $user_id!=0?$user_id:$this->getUser()['user_id'];
        $registrationInformation = $model->getRegistrationInformation($userId);
        return $this->renderSuccess('', compact('registrationInformation'));
    }
    /**
     * 生成活动海报
     */
    public function poster($activity_id, $source)
    {
        // 活动详情
        $detail = ActivityModel::detail($activity_id);
        $Qrcode = new BranchActivityService($detail, $this->getUser(false), $source);
        return $this->renderSuccess('', [
            'qrcode' => $Qrcode->getImage(),
        ]);
    }
 
    /**
     * 获取活动相册
     */
    public function fileLists($activity_id)
    {
        $data = $this->postData();
        $model = new ActivityFileModel;
        $list = $model->getList($activity_id, $data);
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 检查用户是否有足够积分下载活动文件
     */
    public function checkUserPoints($points = 0)
    {
        $is_enough = false;
        $user = $this->getUser();
        if ($user['points'] >= $points) {
            $is_enough = true;
        }
        return $this->renderSuccess('', compact('is_enough'));
    }
 
    /**
     * 获取活动相关积分设置
     */
    public function pointsSetting()
    {
        $setting = BranchSettingModel::getItem('basic');
        return $this->renderSuccess('', compact('setting'));
    }
 
    /**
     * 获取文件路径
     */
    public function getFilePath($file_id, $is_original = false)
    {
        $detail = ActivityFileModel::detail(['file_id' => $file_id], ['original']);
        $file_path = $is_original ? $detail['original_path'] : $detail['file_path'];
        return $this->renderSuccess('', compact('file_path'));
    }
 
    /**
     * 扣除积分
     */
    public function deductPoints($points)
    {
        $user = $this->getUser();
        $describe = "下载活动文件消费";
        $user->setIncPoints(-$points, $describe);
        return $this->renderSuccess('', '扣除成功');
    }
 
    /**
     * 活动商品列表
    **/
    public function productLists($activity_id)
    {
        $model = new ActivityModel();
        $activityDetail = $model->detail(['activity_id'=>$activity_id],[]);
 
        if (!$activityDetail) {
            return $this->renderError('活动不存在');
        }
        if ($activityDetail['status_text']['status'] != 1) {
            return $this->renderError('活动未开始');
        }
        $data = $this->postData();
        $user = $this->getUser();
        $check_result = $model->checkUserCanSome($activityDetail,$user,$data);
        if(!$check_result){
            return $this->renderError($model->getError());
        }
        $list = $model->getGoodsList($activity_id);
        $cart_total_num = ( new ActivityCartModel())->where(['activity_id'=>$activity_id,'user_id'=>$user['user_id']])->count();
        return $this->renderSuccess('', compact('list','cart_total_num', 'activityDetail'));
    }
}