ProjectCountryLogic.php 4.3 KB
<?php

namespace App\Http\Logic\Bside\Setting;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Project\Country as CountryModel;
use App\Models\Project\CountryCustom;
use App\Models\WebSetting\WebLanguage;
class ProjectCountryLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new CountryModel();
        $this->param = $this->requestAll;
    }
    /**
     * @name   :(获取当前项目关联的多语言)get_country_info
     * @author :lyh
     * @method :post
     * @time   :2023/4/28 17:03
     */
    public function country_info(){
        $lists = $this->model->read(['project_id'=>$this->user['project_id']]);
        if (empty($lists)){
            $lists['country_lists'] = '';
            $lists['country_sort'] = '';
        }
        $lists['country_lists'] = $this->countryListsFormat($lists['country_lists']);
        $lists['country_sort'] = $this->countrySortFormat($lists['country_sort']);
        return $this->success($lists);
    }

    /**
     * @name   :(更新多语言设置)country_edit
     * @author :lyh
     * @method :post
     * @time   :2023/4/28 17:42
     */
    public function country_save(){
        //处理数据
        if(!isset($this->param['country_lists']) || empty($this->param['country_lists'])){
            $this->param['country_lists'] = '';
        }
        $this->param['country_lists'] = $this->countryListsFormat($this->param['country_lists']);

        if(!isset($this->param['country_sort']) || empty($this->param['country_sort'])){
            $this->param['country_sort'] = '';
        }
        $this->param['country_sort'] = $this->countrySortFormat($this->param['country_sort']);

        $info = $this->model->read(['project_id'=>$this->user['project_id']]);
        if($info === false){
            $this->param['project_id'] = $this->user['project_id'];
            $rs = $this->model->add($this->param);
        }else{
            $rs = $this->model->edit($this->param,['project_id'=>$this->user['project_id']]);
        }
        if($rs === false){
            $this->fail('当前数据不存在');
        }

        $custom_model = new CountryCustom();
        //将未勾选的设置了自定义跳转的语种,置为不可用
        $custom_model->edit(['status'=>0],['project_id'=>$this->user['project_id'],'language_id'=>['not in',explode(',',$this->param['country_lists'])]]);
        //将勾选的设置了自定义跳转的语种,置为可用
        $custom_model->edit(['status'=>1],['project_id'=>$this->user['project_id'],'language_id'=>['in',explode(',',$this->param['country_lists'])]]);

        return $this->success();
    }

    /**
     * 删除语种自定义跳转
     * @return array
     * @author Akun
     * @date 2024/03/05 10:18
     */
    public function country_custom_del(){
        $custom_model = new CountryCustom();
        $rs = $custom_model->del(['project_id'=>$this->user['project_id'],'language_id'=>$this->param['language_id']]);
        if($rs === false){
            $this->fail('error');
        }

        return $this->success();
    }

    /**
     * 获取语种自定义跳转详情
     * @return array|bool
     * @author Akun
     * @date 2024/03/06 14:38
     */
    public function country_custom_info(){
        $custom_model = new CountryCustom();
        $info = $custom_model->read(['project_id'=>$this->user['project_id'],'language_id'=>$this->param['language_id']],['language_id','custom_domain','is_create','type','private_key','private_cert']);

        return $this->success($info?:[]);
    }

    protected function countryListsFormat($country_lists)
    {
        if(empty($country_lists)){
            $country_lists = [];
        }else{
            //默认选中主语种
            $country_lists = explode(',', $country_lists);
        }
        $main_lang_id = $this->project['main_lang_id'] ?? 1;
        if (!in_array($main_lang_id, $country_lists)) {
            $country_lists[] = $main_lang_id;
        }
        return implode(',', $country_lists);
    }

    protected function countrySortFormat($country_sort){
        $webLanguageModel = new WebLanguage();
        $idArr = $webLanguageModel->selectField(['id'=>['not in',explode(',',$country_sort)]],'id');
        if(!empty($idArr)){
            $country_sort = $country_sort.','.implode(',',$idArr);
        }
        return $country_sort;
    }
}