CNoticeController.php 8.8 KB
<?php
/**
 * @remark :
 * @name   :CNoticeController.php
 * @author :lyh
 * @method :post
 * @time   :2023/9/12 10:04
 */

namespace App\Http\Controllers\Bside\BCom;

use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Models\Com\Notify;
use App\Models\Com\UpdateNotify;
use App\Models\Com\UpdateProgress;
use App\Models\Devops\ServerConfig;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Country as CountryModel;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\WebSetting\WebLanguage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


/**
 * @remark :通知C端
 * @name   :CNoticeController
 * @author :lyh
 * @method :post
 * @time   :2023/9/12 10:38
 */
class CNoticeController extends BaseController
{
    /**
     * 更新通知C端
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendNotify(Request $request)
    {
        $project_id = $this->user['project_id'];
        $type = intval($request->input('type', 1));
        $route = intval($request->input('page', 1));
        $url = $request->input('url', []);
        $language = $request->input('language', []);
        $is_sitemap = intval($request->input('is_sitemap', 0));

        //获取项目所在服务器
        $project_model = new Project();
        $project_info = $project_model->read(['id'=>$project_id],['serve_id']);

        if($project_info && $project_info['serve_id'] == ServerConfig::SELF_SITE_ID){
            //自建站服务器:如果项目已经上线,不请求C端接口,数据直接入库
            $domain_model = new DomainInfo();
            $domain_info = $domain_model->read(['project_id'=>$this->user['project_id']],['domain']);
            if($domain_info){
                //判断是否已有更新进行中
                $notify_model = new Notify();
                $data = [
                    'project_id' => $project_id,
                    'type' => $type,
                    'route' => $route,
                    'server_id' => ServerConfig::SELF_SITE_ID,
                    'status' => ['<',Notify::STATUS_FINISH_PAGE]
                ];
                $notify = $notify_model->read($data,['id']);

                if(!$notify){
                    $domain = $domain_info['domain'];
                    if($type == Notify::TYPE_AMP){
                        $domain_array = parse_url($domain);
                        $host = $domain_array['host'] ?? $domain_array['path'];
                        $host_array = explode('.',$host);
                        if(count($host_array) <= 2){
                            array_unshift($host_array,'m');
                        }else{
                            $host_array[0] = 'm';
                        }
                        $domain = implode('.',$host_array);
                    }

                    $data['data'] = Arr::a2s(['domain'=>$domain,'url'=>$url,'language'=>$language]);
                    $data['status'] = $is_sitemap == 1 ? Notify::STATUS_FINISH_PAGE : Notify::STATUS_INIT;
                    $notify_model->add($data);
                }
            }
        }else{
            //其他服务器:请求对应C端接口
            $c_url = $this->user['domain'].'api/update_page/';
            $param = [
                'project_id' => $this->user['project_id'],
                'type' => $type,
                'route' => $route,
                'url' => $url,
                'language'=> $language,
                'is_sitemap' => $is_sitemap
            ];
            http_post($c_url, json_encode($param));
        }

        $this->response('更新中请稍后, 更新完成将会发送站内信通知更新结果!');
    }

    /**
     * @remark :主站通知
     * @name   :updateMaster
     * @author :lyh
     * @method :post
     * @time   :2023/9/12 14:46
     */
    public function updateMaster(){
        try {
            $updateNotifyModel = new UpdateNotify();
            if($this->param['page'] == UpdateNotify::PAGE_ALL){
                //如果是更新所有
                $routeMapModel = new RouteMap();
                $count = $routeMapModel->formatQuery(['project_id'=>$this->user['project_id']])->count();
                $updateNotifyModel->edit(['status' => 1], ['project_id' => $this->user['project_id'], 'status' => 0]);
                $this->addProgress($count,$this->param['type'],$this->param['page']);
            }elseif($this->param['page'] == UpdateNotify::PAGE_SINGLE){
                //更新所有已修改的更新
                $count = $updateNotifyModel->formatQuery(['project_id' => $this->user['project_id'], 'status' => 0])->count();
                $updateNotifyModel->edit(['status' => 1], ['project_id' => $this->user['project_id'], 'status' => 0]);
                $this->addProgress($count,$this->param['type'],$this->param['page']);
            }else{
                //根据传递的参数更新
                $count = count($this->param['url']);
                $extent = json_encode(['url'=>$this->param['url']]);
                $this->addProgress($count,$this->param['type'],$this->param['page'],$extent);
            }
        }catch (\Exception $e){
            $this->response('error',Code::USER_ERROR);
        }
        return true;
    }

    /**
     * @remark :更新小语种
     * @name   :updateMinorLanguages
     * @author :lyh
     * @method :post
     * @time   :2023/9/12 14:48
     */
    public function updateMinorLanguages(){
        $updateNotifyModel = new UpdateNotify();
        try {
            if($this->param['page'] == UpdateNotify::PAGE_ALL){
                //如果是更新所有
                $routeMapModel = new RouteMap();
                $count = $routeMapModel->formatQuery(['project_id'=>$this->user['project_id']])->count();
                $updateNotifyModel->edit(['minor_languages_status' => 1], ['project_id' => $this->user['project_id'], 'minor_languages_status' => 0]);
                $extent = json_encode(['language'=>$this->param['language']]);
                $this->addProgress($count,$this->param['type'],$this->param['page'],$extent);
            }else{
                //根据传递的参数更新
                $count = count($this->param['url']);
                $extent = json_encode(['url'=>$this->param['url'],'language'=>$this->param['language']]);
                $this->addProgress($count,$this->param['type'],$this->param['page'],$extent);
            }
            //更新小语种,同步更新项目表已翻译小语种
            if(isset($this->user['is_domain']) && ($this->user['is_domain'] != 0)){
                $projectModel = new Project();
                $projectModel->edit(['is_language'=>$this->user['is_domain']],['id'=>$this->user['project_id']]);
            }
        }catch (\Exception $e){
            $this->response('error',Code::USER_ERROR);
        }
        return true;
    }

    /**
     * @remark :生成一条更新记录
     * @name   :addProgress
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 17:01
     */
    public function addProgress($count,$type,$page,$extend = ''){
        $data = [
            'total_num'=>$count,
            'current_num'=>0,
            'type'=>$type,
            'page'=>$page,
            'extends'=>!empty($extend) ? $extend : json_encode([]),
            'project_id'=>$this->user['project_id'],
            'created_at'=>date('Y-m-d H;i:s')
        ];
        $updateProgressModel = new UpdateProgress();
        $updateProgressModel->insert($data);
        return true;
    }

    /**
     * @remark :通知参数处理
     * @name   :getString
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 17:03
     */
    public function getString($type,$page){
        $param = [
            'project_id' => $this->user['project_id'],
            'type' => $type,
            'route' => $page
        ];
        $string = http_build_query($param);
        return $this->user['domain'].'api/updateHtmlNotify/?' . $string;
    }

    /**
     * @remark :获取当前项目选中的语种
     * @name   :getCountry
     * @author :lyh
     * @method :post
     * @time   :2023/9/12 15:20
     */
    public function getCountry(){
        $countryModel = new CountryModel();
        $info = $countryModel->read(['project_id'=>$this->user['project_id']],['id','country_lists']);
        $ids = [];
        if($info !== false){
            $ids = explode(',',$info['country_lists']);
        }
        $languageModel = new WebLanguage();
        //根据排序查询选中的小语种
        $lists = $languageModel->whereIn('id', $ids)->orderByRaw(DB::raw("FIND_IN_SET(id,'" . implode(',', $ids) . "'" . ')'))->get()->toArray();
//        $lists = $languageModel->list(['id'=>['in',$ids]]);
        $this->response('success',Code::SUCCESS,$lists);
    }

}