quanwei
2025-11-21 2d9362ae6f528f57e6133d5d80f0b633c24e8eb6
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
namespace app\api\controller\plus\business;
 
use app\api\controller\Controller;
use app\api\model\plus\business\Business as BusinessModel;
use app\api\model\plus\business\Saving as SavingModel;
use app\api\model\plus\business\Grade as GradeModel;
use app\common\service\business\Poster;
use app\api\model\plus\business\Order as OrderModel;
 
class Business extends Controller
{
    /**
     * 获取名片列表
     */
    public function getList()
    {
        $model = new BusinessModel();
        $params=$this->request->param();
        $list=$model->getList($params);
        $user = $this->getUser();
        $currentUserId = $user ? $user['user_id'] : 0;
        $orderModel = new OrderModel();
        
        foreach ($list as $key => &$value) {
            // 标记是否已购买
            $hasPurchased = $orderModel->checkCardPurchase($currentUserId, $value['business_card_id']);
            $list[$key]['has_purchased'] = $hasPurchased;
            $needDesensitization = $this->needDesensitizeCard($value, $currentUserId);
            $posterType = $needDesensitization ? 'desensitization' : 'business';
            $Qrcode = new Poster($value, $posterType);
            $value['mp'] = $Qrcode->getImage();
            $value['posterType'] = $posterType;
            // 使用封装的方法判断是否需要脱敏
            if ($needDesensitization) {
                $list[$key] = $this->desensitizeBusinessData($value);
            }
        }
        
        return $this->renderSuccess('',$list);
    }
    
    public function mpgl()
    {
        $user = $this->getUser();
        $param['type'] = 20;
        $param['user_id'] = $user['user_id'];
        $saving = (new SavingModel())->order('create_time')->distinct(true)->field('business_card_id')->where($param)->select();
        $business = [];
        foreach ($saving as $v) {
            $business = $v['business_card_id'];
        }
        $param = request()->param();
        $paramr = array_merge(['listRow' => 15], $param);
        $where = [];
        !empty($paramr['name']) && $where['name'] = ['like', '%' . $paramr['name'] . '%'];
        !empty($paramr['search']) && $where['name|duties|unit'] = ['like', '%' . $paramr['search'] . '%'];
        !empty($paramr['user_id']) && $where['user_id'] = $paramr['user_id'];
        !empty($paramr['search']) && $w['name|duties|unit'] = $where['name|duties|unit'];
        $w['business_card_id'] = ['in', $business];
        $model = new BusinessModel();
        $order=['create_time' => 'desc'];
        if (!empty($paramr['sort'])) {
            if ($paramr['sort'] == 'name') {
                $order['name' ] = "asc";
            } else if ($paramr['sort'] == 'time') {
                $order['create_time'] ="asc";
            } else {
                $order['unit'] = "asc";
            }
        }
        $data = $model->order($order)->with(['user', 'image', 'logoImage'])->where($where)->whereOr($w)->paginate($paramr['listRow'], false, ['query' => request()->query()]);
        return $this->renderSuccess('',$data);
    }
 
    /**
     * 列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $param = request()->param();
        return $this->renderSuccess('',(new BusinessModel())->getList($param));
    }
 
    /**
     * 获取指定名片
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function getImage()
    {
        $param = request()->param();
        $dealer = (new BusinessModel())->get($param['business_card_id'], ['image', 'logoImage']);
        $image = '';
        if ($dealer) {
            $Qrcode = new Poster($dealer);
            $image = $Qrcode->getImage();
        }
        return $this->renderSuccess('',$image);
    }
 
    public function add()
    {
        $user = $this->getUser();
        $param = request()->param();
        $param['user_id'] = $user['user_id'];
        $param['unit'] = json_encode($param['unit'], JSON_UNESCAPED_UNICODE);
        $param['duties'] = json_encode($param['duties'], JSON_UNESCAPED_UNICODE);
 
        !empty($param['position'])?$param['position'] = json_encode($param['position'], JSON_UNESCAPED_UNICODE):'';
        $param['position'] = $param['duties'];
        $param['address'] = json_encode($param['address'], JSON_UNESCAPED_UNICODE);
        if(!(new BusinessModel())->where(['user_id'=>$user['user_id'],'is_default'=>1])->find()){
            $param['is_default'] = 1;
        }
        if ((new BusinessModel())->add($param)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError('添加失败');
    }
 
    /**
     * 名片详情
     * @param $business_card_id
     * @return array|\think\response\Json
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail($business_card_id)
    {
        $data = (new BusinessModel())::detail($business_card_id);
        if ($data) {
            // 获取当前用户信息
            $currentUser = $this->getUser();
            $currentUserId = $currentUser ? $currentUser['user_id'] : 0;
 
            // 使用封装的方法判断是否需要脱敏
            $needDesensitization = $this->needDesensitizeCard($data, $currentUserId);
 
            // 生成海报图
            $posterType = $needDesensitization ? 'desensitization' : 'business';
            $Qrcode = new Poster($data, $posterType);
            $data['mp'] = $Qrcode->getImage();
            $imageInfo = getimagesize($data['mp']);
            $data['posterType'] = $posterType;
 
            // 如果需要脱敏,对返回数据也进行脱敏处理
            if ($needDesensitization) {
                $data = $this->desensitizeBusinessData($data);
            }
        }
        return $this->renderSuccess('',$data);
    }
 
    /**
     * 获取默认名片
     * @param $user_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetail($user_id)
    {
        $model = new BusinessModel();
        $data = $model->getDefault($user_id);
        if(!$data){
            if($model->where(['user_id' => $user_id])->count()){
                $data = $model->order('create_time', 'desc')->where(['user_id' => $user_id])->find();
                $model->editDefault($data['business_card_id']);
                $data = $model->getDefault($user_id);
            }
        }
        
        if ($data) {
            // 获取当前用户信息
            $currentUser = $this->getUser();
            $currentUserId = $currentUser ? $currentUser['user_id'] : 0;
            // 使用封装的方法判断是否需要脱敏
            $needDesensitization = $this->needDesensitizeCard($data, $currentUserId);
            // 生成海报图
            $posterType = $needDesensitization ? 'desensitization' : 'business';
            $Qrcode = new Poster($data, $posterType);
            $data['mp'] = $Qrcode->getImage();
            $imageInfo = getimagesize($data['mp']);
            $data['height'] = (355/$imageInfo[0])*$imageInfo[1];
            
            // 如果需要脱敏,对返回数据也进行脱敏处理
            if ($needDesensitization) {
                $data = $this->desensitizeBusinessData($data);
            }
        }
        return $this->renderSuccess('',$data);
    }
    
    
 
    /**
     * 设置默认名片
     * @param $business_card_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function editDefault($business_card_id)
    {
        if ((new BusinessModel())->editDefault($business_card_id)) {
            return $this->renderSuccess('切换成功');
        }
        return $this->renderError('切换失败');
    }
 
    /**
     * 获取数量
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getStatistics()
    {
        $user = $this->getUser();
        //我看过的
        $lists['browseUser'] = (new SavingModel)->getQuantity(['type'=>10,'user_id'=>$user['user_id']]);
        //我的访客
        $lists['browse'] = (new SavingModel)->getQuantity(['type'=>10,'affiliation_id'=>$user['user_id']]);
        //我收下的
        $lists['accept'] = (new SavingModel)->getQuantity(['type'=>20,'user_id'=>$user['user_id']]);
        //我的名片
        $lists['card'] = (new BusinessModel())->getQuantity($user['user_id']);
        //今日访问量
        $lists['today'] = (new SavingModel)->getQuantity(['type'=>10,'affiliation_id'=>$user['user_id'],'today'=>1]);
        return $this->renderSuccess('',$lists);
    }
 
    public function edit()
    {
        $param = request()->param();
        $model = (new BusinessModel())->where('business_card_id', $param['business_card_id'])->find();
        if($model){
            $Qrcode = new Poster($model);
            $image = $Qrcode->getPosterPath('business');
            //删除图片
            if (file_exists($image)) {
                unlink($image);
            }
            $image = $Qrcode->getPosterPath('desensitization');
            //删除图片
            if (file_exists($image)) {
                unlink($image);
            }
        }else{
            $model = new BusinessModel();
            $user = $this->getUser();
            $param['user_id'] = $user['user_id'];
        }
 
        $param['unit'] = json_encode($param['unit'], JSON_UNESCAPED_UNICODE);
        $param['duties'] = json_encode($param['duties'], JSON_UNESCAPED_UNICODE);
        $param['address'] = json_encode($param['address'], JSON_UNESCAPED_UNICODE);
        !empty($param['position'])?$param['position'] = json_encode($param['position'], JSON_UNESCAPED_UNICODE):'';
        $param['position'] = $param['duties'];
        if ($model->add($param)) {
            return $this->renderSuccess('编辑成功');
        }
        return $this->renderError('编辑失败');
    }
 
    /**
     * 删除名片
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = request()->param();
        if((new BusinessModel())->where(['business_card_id'=>$param['business_card_id'],'is_default'=>1])->find()){
            return $this->renderError('当前名片为默认名片,无法删除,可切换默认名片后进行删除');
        }
        $model = (new BusinessModel())->where('business_card_id', $param['business_card_id'])->find();
        if ((new BusinessModel())->where('business_card_id', $param['business_card_id'])->delete()) {
            $Qrcode = new Poster($model);
            $image = $Qrcode->getPosterPath('business');
            //删除图片
            if (file_exists($image)) {
                unlink($image);
            }
            $image = $Qrcode->getPosterPath('desensitization');
            //删除图片
            if (file_exists($image)) {
                unlink($image);
            }
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError('删除失败');
    }
    public function getIlk(){
        return $this->renderSuccess('',[10=>'个人名片',20=>'企业名片']);
    }
    /**
     * 获取访客列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function getVisitors()
    {
        $user = $this->getUser();
        $param = request()->param();
        $param['affiliation_id'] = $user['user_id'];
        $param['type'] = 10; // 10表示浏览记录
        $list = (new SavingModel())->lists($param);
        return $this->renderSuccess('',$list);
    }
    
    /**
     * 获取概览数据
     */
    public function getOverview()
    {
        $user = $this->getUser();
        $param = request()->param();
        $businessId = $param['business_card_id'] ?? '';
        
        $savingModel = new SavingModel();
        
        $where = ['affiliation_id' => $user['user_id'], 'type' => 10];
        if ($businessId) {
            $where['business_id'] = $businessId;
        }
        
        return $this->renderSuccess('',[
            'views' => $savingModel->where($where)->count(),
            'saves' => $savingModel->where(['affiliation_id' => $user['user_id'], 'type' => 20])->count(),
            'shares' => 0 // 暂不实现分享统计
        ]);
    }
    
    /**
     * 判断是否需要对名片进行脱敏处理
     */
    private function needDesensitizeCard($cardData, $currentUserId)
    {
        // 如果是名片所有者,不需要脱敏
        if ($currentUserId == $cardData['user_id']) {
            return false;
        }
        
        // 获取名片等级信息
        $gradeModel = new GradeModel();
        $gradeInfo = $gradeModel->detail($cardData['grade_id']);
        
        // 检查等级费用是否大于0且用户未购买
        if ($gradeInfo && $gradeInfo['price'] > 0) {
            // 检查用户是否已购买该名片
            $hasPurchased = (new OrderModel())->checkCardPurchase($currentUserId, $cardData['business_card_id']);
            // 如果未购买,则需要脱敏
            if (!$hasPurchased) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * 对名片数据进行脱敏处理
     */
    private function desensitizeBusinessData($detail)
    {
        // 手机号脱敏
        if (!empty($detail['mobile'])) {
            $detail['mobile'] = $this->maskPhoneNumber($detail['mobile']);
        }
        if (!empty($detail['mobile_phone'])) {
            $detail['mobile_phone'] = $this->maskPhoneNumber($detail['mobile_phone']);
        }
        
        // 微信脱敏
        if (!empty($detail['wechat'])) {
            $len = strlen($detail['wechat']);
            if ($len > 4) {
                $detail['wechat'] = substr($detail['wechat'], 0, 2) . str_repeat('*', $len - 4) . substr($detail['wechat'], -2);
            }
        }
        
        // 邮箱脱敏
        if (!empty($detail['mailbox'])) {
            $parts = explode('@', $detail['mailbox']);
            if (count($parts) == 2) {
                $username = $parts[0];
                $domain = $parts[1];
                $len = strlen($username);
                if ($len > 2) {
                    $detail['mailbox'] = substr($username, 0, 2) . str_repeat('*', $len - 2) . '@' . $domain;
                }
            }
        }
        
        // 电话脱敏
        if (!empty($detail['phone'])) {
            $detail['phone'] = $this->maskPhoneNumber($detail['phone']);
        }
        
        return $detail;
    }
    
    /**
     * 手机号/座机号脱敏处理
     */
    private function maskPhoneNumber($phone)
    {
        $len = strlen($phone);
        if ($len <= 7) return $phone;
        
        // 检查是否是带区号的座机号
        if (strpos($phone, '-') !== false || strpos($phone, ' ') !== false) {
            $parts = preg_split('/[-\s]/', $phone);
            if (count($parts) >= 2) {
                $areaCode = $parts[0];
                $number = end($parts);
                $separator = strpos($phone, '-') !== false ? '-' : ' ';
                
                $numLen = strlen($number);
                if ($numLen <= 4) return $phone;
                
                return $areaCode . $separator . str_repeat('*', $numLen - 4) . substr($number, -4);
            }
        }
        
        // 普通手机号脱敏
        return substr($phone, 0, 3) . str_repeat('*', $len - 7) . substr($phone, -4);
    }
}