InquiryLogic.php 3.9 KB
<?php

namespace App\Http\Logic\Bside;

use App\Helper\Arr;
use App\Helper\FormGlobalsoApi;
use App\Helper\Translate;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\InquirySet;

/**
 * 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)
    {
        $page_size = $export ? 1000 : 20;
        $search = $this->request['search'] ?: '';
        $page = $this->request['page'] ?: 1;
        $project = (new ProjectLogic())->getInfo($this->user['project_id']);
        $domain = $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 $this->success($data);
    }

    public function getInfo($id)
    {
        $project = (new ProjectLogic())->getInfo($this->user['project_id']);
        $domain = $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 delete($ids, $map = [])
    {
        $project = (new ProjectLogic())->getInfo($this->user['project_id']);
        $domain = $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();
    }

    /**
     * 读取设置
     * @return array
     * @author zbj
     * @date 2023/5/17
     */
    public function getSet()
    {
        $set = InquirySet::where('project_id', $this->user['project_id'])->first();
        if ($set) {
            return $this->success(Arr::twoKeepKeys($set->toArray(), ['emails', 'phones']));
        } else {
            return $this->success(['emails' => '', 'phones' => '']);
        }
    }

    /**
     * 保存设置
     * @author zbj
     * @date 2023/5/17
     */
    public function saveSet()
    {
        $project = (new ProjectLogic())->getInfo($this->user['project_id']);

        //同步到接口
        $domain = parse_url($project['deploy_optimize']['domain'])['host'] ?? ['path'];
        $emails = Arr::arrToSet($this->request['emails'], 'trim');
        $phones = Arr::arrToSet($this->request['phones'], 'trim');

        $res = $this->form_globalso_api->setInquiry($domain, $emails, $phones);

        if (!$res) {
            $this->fail('保存失败');
        }
        if ($res['status'] != 200) {
            $this->fail($res['message'] ?? '保存失败');
        }
        //保存
        $set = InquirySet::where('project_id', $this->user['project_id'])->first();
        if (!$set) {
            $set = new InquirySet();
        }
        $set->project_id = $this->user['project_id'];
        $set->emails = $emails;
        $set->phones = $phones;
        $set->save();
    }


}