InquiryLogic.php 6.9 KB
<?php

namespace App\Http\Logic\Bside\Inquiry;

use App\Helper\Arr;
use App\Helper\FormGlobalsoApi;
use App\Helper\Translate;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryForm;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Inquiry\InquiryOther;
use App\Models\Inquiry\PhoneData;
use Illuminate\Support\Facades\DB;

/**
 * Class InquiryLogic
 * @package App\Http\Logic\Bside
 * @author zbj
 * @date 2023/5/4
 */
class InquiryLogic extends BaseLogic
{
    protected $form_globalso_api;

    public function __construct()
    {
        parent::__construct();

        $this->form_globalso_api = new FormGlobalsoApi();
    }

    public function getApiList($export = false)
    {
        if(isset($this->request['row'])){
            $page_size = $this->request['row'];
        }else{
            $page_size = $export ? 1000 : 20;
        }
        $search = $this->request['search'] ?: '';
        $page = $this->request['page'] ?: 1;
        $project = (new ProjectLogic())->getProjectInfo($this->user['project_id']);
        $domain = (!empty(
            $project['deploy_optimize']['domain']) ? ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) :  '');
        $list = $this->form_globalso_api->getInquiryList($domain, $search, $page, $page_size);
        //处理格式 免得前端又改
        $data = [
            "list" => [],
            "total" => 0,
            "page" => $page,
            "total_page" => 1,
            "size" => $page_size
        ];
        if (!empty($list['status']) && $list['status'] == 200) {
            foreach ($list['data']['data'] as $item) {
                $data['list'][] = $item;
            }
            $data['total'] = $list['data']['total'];
            $data['total_page'] = $list['data']['last_page'];
        }
        return $data;
    }

    public function getOtherList($export = false){
        $page_size = $export ? 1000 : 20;
        $search = $this->request['search'] ?: '';
        $page = $this->request['page'] ?: 1;
        $map = [];
        if($search){
            $map['email'] = ['like','%'.$search.'%'];
        }

        $data = (new InquiryOther())->lists($map,$page,$page_size,'id',
            ['id', 'email', 'ip', 'country', 'domain', DB::raw('referer as refer'), DB::raw('status as read_status'), 'submit_time']
    );

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

    public function getFormDataList($export = false){
        $page_size = $export ? 1000 : 20;
        $search = $this->request['search'] ?: '';
        $page = $this->request['page'] ?: 1;
        $form_id = $this->request['form_id'] ?? '';
        $field = InquiryForm::getField($form_id);
        $fields = ['id', 'data', 'ip', 'country', 'domain', DB::raw('referer as refer'), DB::raw('status as read_status'), DB::raw('submit_at as submit_time')];
        $lists = InquiryFormData::where('form_id', $form_id)
            ->where(function ($query) use ($search, $field){
                if($search){
                    foreach ($field as $v){
                        $query->orWhere('data->' . $v, $search);
                    }
                }
            })
            ->select($fields)->orderBy('id', 'desc')->paginate($page_size, ['*'], 'page', $page);
        $data = $lists->toArray();
        foreach ($data['list'] as &$v){
            foreach ($v['data'] as &$field){
                if(is_array($field) && !empty($field['path'])){
                    $field['path'] = getImageUrl($field['path'],$this->user['storage_type'],$this->user['project_location']);
                }
            }
            $v = array_merge($v, $v['data']);
        }
        return $this->success($data);
    }

    public function getInfo($id)
    {
        $project = (new ProjectLogic())->getProjectInfo($this->user['project_id']);
        $domain = (!empty($project['deploy_optimize']['domain']) ? ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) :  '');
        //修改状态为已读
        if($this->request['read_status']){
            $this->form_globalso_api->saveInquiryRead($domain, $id);
        }
        //翻译
        $trans_message = '';
        if($this->request['message']){
            $trans_message = Translate::tran($this->request['message'], 'zh');
        }
        return $this->success(['trans_message' => $trans_message]);
    }

    public function getFormDataInfo($id, $form_id){
        //修改状态为已读
        if($this->request['read_status']){
            (new InquiryFormData())->edit(['status' => 1], ['id' => $id, 'form_id' => $form_id]);
        }
        //翻译
        $trans_message = '';
        if($this->request['message']){
            $trans_message = Translate::tran($this->request['message'], 'zh');
        }
        return $this->success(['trans_message' => $trans_message]);
    }

    public function getOtherInfo($id){
        //修改状态为已读
        if($this->request['read_status']){
            (new InquiryOther())->edit(['status' => 1], ['id' => $id]);
        }
        return $this->success(['trans_message' => '']);
    }

    public function delete($ids, $map = [])
    {
        $project = (new ProjectLogic())->getProjectInfo($this->user['project_id']);
        $domain = (!empty($project['deploy_optimize']['domain']) ? ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) :  '');
        $ids = array_filter(Arr::splitFilterToArray($ids), 'intval');
        if(!$ids){
            $this->fail('ID不能为空');
        }
        $this->form_globalso_api->delInquiry($domain, $ids);
        return $this->success();
    }

    public function deleteOther($ids, $map = [])
    {
        $ids = array_filter(Arr::splitFilterToArray($ids), 'intval');
        if(!$ids){
            $this->fail('ID不能为空');
        }
        (new InquiryOther())->del(['id'=>['in',$ids]]);
        return $this->success();
    }

    public function deleteFormData($ids, $map = [])
    {
        $ids = array_filter(Arr::splitFilterToArray($ids), 'intval');
        if(!$ids){
            $this->fail('ID不能为空');
        }
        $map['id'] = ['in',$ids];
        (new InquiryFormData())->del($map);
        return $this->success();
    }

    /**
     * @remark :获取手机号码下是否拥有wa
     * @name   :sendMobileVerifyData
     * @author :lyh
     * @method :post
     * @time   :2024/9/5 17:40
     */
    public function sendMobileVerifyData($phone){
        $phoneDataModel = new PhoneData();
        $info = $phoneDataModel->read(['phone'=>$phone]);
        if($info === false){
            $url = 'https://fob.ai.cc/api/mobile_verify_data/'.$phone;
            $data = http_get($url);
            $param = [
                'phone'=>$phone,
                'data'=>json_encode($data,true)
            ];
            (new PhoneData())->addReturnId($param);
        }else{
            $data = json_decode($info['data']);
        }
        return $this->success(['data'=>$data]);
    }

}