WebSettingLogic.php 4.0 KB
<?php

namespace App\Http\Logic\Bside\Setting;

use App\Helper\Common as CommonHelper;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Com\UpdateNotify;
use App\Models\Com\UpdateProgress;
use App\Models\RouteMap\RouteMap;
use App\Models\WebSetting\WebSetting;

class WebSettingLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new WebSetting();
        $this->param = $this->requestAll;
    }

    /**
     * @name   :(获取首页设置)setting_read
     * @author :lyh
     * @method :post
     * @time   :2023/4/28 15:14
     */
    public function setting_read(){
        $info = CommonHelper::get_user_cache($this->model->getTable(),$this->user['project_id']);
        if(empty($info)){
            $info = $this->model->read(['project_id'=>$this->user['project_id']]);
            if($info === false){
                $info = [];
            }
            CommonHelper::set_user_cache($info,$this->model->getTable(),$this->user['project_id']);
        }
        return $this->success($info);
    }

    /**
     * @name   :(添加或编辑)setting_save
     * @author :lyh
     * @method :post
     * @time   :2023/4/28 15:18
     */
    public function setting_save(){
        //查看数据是否存在
        $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('error');
        }
        CommonHelper::del_user_cache($this->model->getTable(),$this->user['project_id']);
        return $this->success();
    }

    /**
     * 通知c端
     * @param string $type
     * @return array
     */
    public function sendNotifyMessage($type,$page){
        $updateProgressModel = new UpdateProgress();
        $progressInfo = $updateProgressModel->formatQuery(['project_id'=>$this->user['project_id'],'type'=>$type])->orderBy('id','desc')->first()->toArray();
        if((!empty($progressInfo)) && ($progressInfo['total_num'] > $progressInfo['current_num'])){
            return $this->success($progressInfo);
        }
        $updateNotifyModel = new UpdateNotify();
        $field = ($type == UpdateNotify::TYPE_MINOR) ? 'minor_languages_status' : 'status';
        $count = $updateNotifyModel->formatQuery(['project_id' => $this->user['project_id'], $field => 0])->count();
        if($page == UpdateNotify::PAGE_ALL){
            //如果是更新所有
            $routeMapModel = new RouteMap();
            $count = $routeMapModel->formatQuery(['project_id'=>$this->user['project_id'],'source'=>['!=','product_keyword']])->count();
        }
        $this->addProgress($count);
        $updateNotifyModel->edit([$field => 1], ['project_id' => $this->user['project_id'], $field => 0]);
        $urlStr = $this->getString();
        http_get($urlStr,["charset = UTF-8"]);
        return $this->success();
    }

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

    /**
     * @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;
    }
}