InquiryController.php 6.6 KB
<?php

namespace App\Http\Controllers\Bside\Inquiry;


use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Inquiry\InquiryLogic;
use App\Models\Inquiry\InquiryForm;
use App\Models\Inquiry\PhoneData;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

/**
 * 精准询盘
 * Class InquiryInfoController
 * @package App\Http\Controllers\Bside
 * @author zbj
 * @date 2023/5/4
 */
class InquiryController extends BaseController
{

    public function form_list(){
        $list = InquiryForm::where('is_default', 0)->orderBy('title', 'desc')->get();
        $data = $list->toArray();
        foreach ($data as &$item){
            $field_text = [];
            foreach ($item['field'] as $v){
                $field_text[$v] = InquiryForm::fieldMap($v);
            }
            $item['field_text'] = $field_text;
        }
        $this->response('success',Code::SUCCESS,$data);
    }

    public function index(InquiryLogic $logic)
    {
        if(!empty($this->param['form_id'])){
            $data = $logic->getFormDataList();
        }elseif(($this->param['type']??'') == 'other'){
            $data = $logic->getOtherList();
        }else{
            $data = $logic->getApiList();
        }
        if(!empty($data) && !empty($data['list'])){
            $phone = [];
            foreach ($data['list'] as $v){
                if(!empty($v['phone'])){
                    $phone[] = $v['phone'];
            }
        }
        if(!empty($phone)){
                $phoneDataModel = new PhoneData();
                $phoneDataList = $phoneDataModel->list(['phone'=>['in',$phone]]);
                foreach ($data['list'] as $key => $value){
                    $value['phone_data'] = [];
                    foreach ($phoneDataList as  $valuePhone){
                        if($value['phone'] == $valuePhone['phone']){
                            $value['phone_data'] = $valuePhone;
                            break;
                        }
                    }
                    $data['list'][$key] = $value;
                }
            }
        }
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :发送请求(获取手机号码对应信息)
     * @name   :sendMobileVerifyData
     * @author :lyh
     * @method :post
     * @time   :2024/9/5 17:44
     */
    public function sendMobileVerifyData(InquiryLogic $logic){
        $this->request->validate([
            'phone' => 'required',
        ],[
            'phone.required' => 'phone不能为空'
        ]);
        $data = $logic->sendMobileVerifyData($this->param['phone']);
        $this->response('success',Code::SUCCESS,$data);
    }

    public function info(Request $request, InquiryLogic $logic){
        $request->validate([
            'id' => 'required',
        ],[
            'id.required' => 'ID不能为空'
        ]);

        if(!empty($this->param['form_id'])){
            $data = $logic->getFormDataInfo($this->param['id'], $this->param['form_id']);
        }elseif(($this->param['type']??'') == 'other'){
            $data = $logic->getOtherInfo($this->param['id']);
        }else{
            $data = $logic->getInfo($this->param['id']);
        }
        $this->response('success',Code::SUCCESS,$data);
    }

    public function delete(Request $request, InquiryLogic $logic)
    {
        $request->validate([
            'ids'=>['required', new Ids()]
        ],[
            'ids.required' => 'ID不能为空'
        ]);
        if(!empty($this->param['form_id'])){
            $logic->deleteFormData($this->param['ids'], ['form_id' => $this->param['form_id']]);
        }elseif(($this->param['type']??'') == 'other'){
            $logic->deleteOther($this->param['ids']);
        }else{
            $logic->delete($this->param['ids']);
        }

        $this->response('success');
    }

    /**
     * 导出
     * @param InquiryLogic $logic
     * @return \Illuminate\Http\JsonResponse
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @author zbj
     * @date 2023/5/8
     */
    public function export(InquiryLogic $logic)
    {
        if(!empty($this->param['form_id'])){
            $data = $logic->getFormDataList(true);
            $field = InquiryForm::getField($this->param['form_id']);
            $map = [
                'submit_time' => '询盘发送时间',
            ];
            foreach ($field as $v) {
                $map[$v] = InquiryForm::fieldMap($v);
            }
            $map['ip_address'] = '访问国家/地区(IP)';
            $map['refer'] = '发送页面';
        }elseif(($this->param['type']??'') == 'other'){
            $data = $logic->getOtherList(true);
            $map = [
                'submit_time' => '询盘发送时间',
                'email' => '邮箱',
                'ip_address' => '访问国家/地区(IP)',
                'refer' => '发送页面',
            ];
        }else{
            $data = $logic->getApiList(true);
            $map = [
                'submit_time' => '询盘发送时间',
                'name' => '姓名',
                'email' => '邮箱',
                'phone' => '电话',
                'ip_address' => '访问国家/地区(IP)',
                'refer' => '发送页面',
                'message' => '询盘内容',
            ];
        }

        $data = $data['list'] ?? [];
        foreach ($data as &$item){
            //非正常登录的
            if($this->param['login_source'] != 2){
                //脱敏
                $item['email'] = email_desensitize($item['email']);
                $item['phone'] = substr($item['phone'], 0, -4) . '****';
            }

            $item['ip_address'] = "{$item['country']}({$item['ip']})";

            if(!empty($this->param['form_id'])){
                $item = array_merge($item, $item['data']);
            }

            foreach ($map as $field => $name) {
                if(is_array($item[$field])){
                    $item[$field] = implode(',',$item[$field]);
                }
                if (Str::startsWith($item[$field], '=')) {
                    $item[$field] = "'" . $item[$field];
                }
            }
        }

        //生成文件,发送到客户端
        $table = new BatchExportService("询盘数据导出");
        $file = $table->head($map)->data($data)->save();
        if (!$file) {
            throw new \Exception('文件生成失败,请重试');
        }
        $fileurl = Storage::disk('runtime')->url($file);
//        return Storage::disk('runtime')->download($file); //直接下载
        $this->response('success',Code::SUCCESS,['url' => $fileurl]);
    }
}