quanwei
2025-10-29 76ed09d116f484b261d44219de300b79eb2013b3
admin/app/api/controller/plus/business/Business.php
@@ -3,6 +3,10 @@
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
{
@@ -12,6 +16,404 @@
    public function getList()
    {
        $model = new BusinessModel();
        return $this->renderSuccess('',$model->getLists());
        $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);
            $data['mp'] = $Qrcode->getImage();
            // 使用封装的方法判断是否需要脱敏
            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
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail($business_card_id)
    {
        $data = (new BusinessModel())->where(['business_card_id'=>$business_card_id])->with(['image', 'logoImage'])->find();
        if ($data) {
            $Qrcode = new Poster($data);
            $data['mp'] = $Qrcode->getImage();
        }
        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->get(['user_id' => $user_id])){
                $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())->get(['business_card_id'=>$param['business_card_id'],'is_default'=>1])){
            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('删除失败');
    }
    /**
     * 获取访客列表
     * @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);
    }
}