InquiryController.php 3.1 KB
<?php

namespace App\Http\Controllers\Bside\Inquiry;


use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Inquiry\InquiryLogic;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

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

    public function index(InquiryLogic $logic)
    {
        if(($this->param['type']??'') == 'other'){
            $data = $logic->getOtherList();
        }else{
            $data = $logic->getApiList();
        }
        return $this->success($data);
    }

    public function info(Request $request, InquiryLogic $logic){
        $request->validate([
            'id' => 'required',
        ],[
            'id.required' => 'ID不能为空'
        ]);
        if(($this->param['type']??'') == 'other'){
            $data = $logic->getOtherInfo($this->param['id']);
        }else{
            $data = $logic->getInfo($this->param['id']);
        }

        return $this->success($data);
    }

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

        return $this->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(($this->param['type']??'') == 'other'){
            $data = $logic->getOtherList(true);
            $map = [
                'submit_time' => '询盘发送时间',
                'email' => '邮箱',
                'ip_address' => '访问国家/地区(IP)',
                'referer' => '发送页面',
            ];
        }else{
            $data = $logic->getApiList(true);
            $map = [
                'submit_time' => '询盘发送时间',
                'name' => '姓名',
                'email' => '邮箱',
                'phone' => '电话',
                'ip_address' => '访问国家/地区(IP)',
                'refer' => '发送页面',
                'message' => '询盘内容',
            ];
        }

        $data = $data['list'] ?? [];
        foreach ($data as &$item){
            $item['ip_address'] = "{$item['country']}({$item['ip']})";
        }

        //生成文件,发送到客户端
        $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); //直接下载
        return $this->success(['url' => $fileurl]);
    }
}