[抛砖引玉]暂时停用失效源并停用对应的小说

xiaoshuo88   ·   发表于 2022-5-24   ·   技术交流

我又来了,今天某个源突然宕机了,一大堆小说采集失败,作者做的删除源关联小说虽说方便,但万一明天源恢复了那不是得重新采集么?所以需求来了:禁用某个源,并且停用对应源的小说。

代码如下


application/admin/view/collect/index.html

// 添加一列叫做源开启的
...
<col >
...
<th>源开启</th>
...
<td><input type="checkbox" name="status" value="{$vo.status}" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" lay-url="{:url('collectstatus',['id'=>$vo['id']])}" {eq name="vo.status" value="1"}checked{/eq}></td>
...

application/admin/controller/Collect.php

...
// 修改源状态的方法
    public function collectstatus(){
        $id = $this->request->param('id');
        $Collect=model('collect');
        $info = $Collect->where(["id"=>$id])->find();
        if($info){
            $info = $info->toArray();
        }else{
            return $this->error("请添加数据");
        }
        if($info['status']==1){
            return $this->forbid('collect');
        }else{
            return $this->resume('collect');
        }
    }

application/common/model/Api.php

...
public function novel_detail($id){
	$cache_name=__FUNCTION__."_".implode('_',func_get_args());
	if(isset($this->cache_data[$cache_name])){
		return $this->cache_data[$cache_name];
	}
	$info = Db::name("novel")->where(['id'=>$id,'status'=>1])->find();
	if(!$info){
		$this->error = '小说被禁用或已删除!';
		return false;
	}
        // 检查源状态
        $novelchapter = NovelChapter::where(['novel_id' => $id])->find();
        if ($novelchapter) {
            $collection = Collect::where(['id' => $novelchapter['collect_id'], 'status' => 1])->find();
            if (!$collection) {
                $this->error = '小说被禁用或已删除!';
                return false;
            }
        }
	$this->cache_set=true;
	$this->cache_data[$cache_name]=$this->data_change($info,'novel');
	return $this->cache_data[$cache_name];
}
...


注意:

对已抓取缓存的部分不生效


效果:

打开源被停用的书籍时提示


缺点:

列表还会显示那个书籍,毕竟临时禁用,就没处理列表了,而且列表处理连表太多了


0 条回复   |  直到 2022-5-24 | 283 次浏览
登录后才可发表内容