ProofreadingController.php 3.5 KB
<?php

namespace App\Http\Controllers\Bside\Setting;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Setting\ProofreadingLogic;

class ProofreadingController extends BaseController
{
    const LANGUAGE_ID = 1;//默认语言英语
    const TYPE_IMAGE = 2;//校队图片
    /**
     * @name   :lists
     * @author :lyh
     * @method :post
     * @time   :2023/6/12 10:52
     */
    public function lists(ProofreadingLogic $proofreadingLogic){
        //默认显示语言为英语
        if(!isset($this->map['language_id']) || empty($this->map['language_id'])){
            $this->map['language_id'] = $this::LANGUAGE_ID;
        }
        $lists = $proofreadingLogic->proofreadingList($this->map,$this->page,$this->row);
        if(!empty($lists['list']) && ($this->param['type'] == $this::TYPE_IMAGE)){
            foreach ($lists['list'] as $k => $v){
                $lists['list'][$k]['image_link'] = getImageUrl($v['translate']);
            }
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @name   :(新增/更新多语言)save
     * @author :lyh
     * @method :post
     * @time   :2023/6/12 10:52
     */
    public function save(ProofreadingLogic $proofreadingLogic){
        $proofreadingLogic->proofreadingSave();
        $this->response('success');
    }

    /**
     * @name   :(当前项目选中的语言列表)languageList
     * @author :lyh
     * @method :post
     * @time   :2023/6/12 15:52
     */
    public function languageList(ProofreadingLogic $proofreadingLogic){
        $list = $proofreadingLogic->countryLanguageList($this->map,$this->order);
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * @remark :获取Url内容
     * @name   :getUrlRead
     * @author :lyh
     * @method :post
     * @time   :2023/11/22 10:02
     */
    public function getUrlRead($url){
        $contextOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ];
        $context = stream_context_create($contextOptions);
        $sourceCode = file_get_contents($url, false, $context);
        $pattern = '/<style\b[^>]*>(.*?)<\/style>/s'; // 定义匹配`<style>`标签及其内容的正则表达式
        $strippedContent = preg_replace($pattern, '', $sourceCode); // 删除`<style>`标签及其内容
        $pattern = '/<script\b[^>]*>(.*?)<\/script>/s'; // 定义匹配`<script>`标签及其内容的正则表达式
        $strippedContent = preg_replace($pattern, '', $strippedContent); // 删除`<script>`标签及其内容
        $pattern = '/<link\b[^>]*>/'; // 定义匹配 `<link>` 标签的正则表达式
        $strippedContent = preg_replace($pattern, '', $strippedContent); // 删除 `<link>` 标签
        $pattern = '/<footer\b[^>]*>(.*?)<\/footer>/s'; // 定义匹配`<script>`标签及其内容的正则表达式
        $strippedContent = preg_replace($pattern, '', $strippedContent); // 删除`<script>`标签及其内容
        $pattern = '/>([^<]+)</'; // 定义匹配中间内容不是标签的正则表达式
        $matches = array();
        preg_match_all($pattern, $strippedContent, $matches);
        $textContentArray = array_filter($matches[1], function($item) {
            return !empty(trim($item));
        });
        $textContentArray = array_values($textContentArray);
        $uniqueArray = array_unique($textContentArray);
        return $this->success($uniqueArray);
    }
}