2023-01-29 10:26:52 +08:00

483 lines
14 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
同步模型
**/
namespace app\admin\controller;
use app\common\controller\Admin;
use think\Request;
use think\Session;
class Sync extends Admin {
public function _initialize() {
parent::_initialize();
$this->getContentMenu();
//$this->model_id = $model_id = $this->request->param('model_id');
$this->model_id = $model_id = 60;
$list = db('Model')->column('*', 'id');
if (empty($list[$model_id])) {
return $this->error("无此模型!");
} else {
$this->modelInfo = $list[$model_id];
$this->model = M($this->modelInfo['name']);
}
$this->assign('model_id', $model_id);
$this->assign('model_list', $list);
}
/**
* 内容列表
* @return [html] [页面内容]
* @author netlife <40150501@qq.com>
*/
public function index() {
if ($this->modelInfo['list_grid'] == '') {
return $this->error("列表定义不正确!", url('admin/model/edit', array('id' => $this->modelInfo['id'])));
}
$grid_list = get_grid_list($this->modelInfo['list_grid']);
//$order = "id desc";
$map = $this->buildMap();
$field = array_filter($grid_list['fields']);
$list = $this->model->where($map)->paginate($this->modelInfo['list_row']);
$data = array(
'grid' => $grid_list,
'list' => $list,
'page' => $list->render()
);
if ($this->modelInfo['template_list']) {
$template = 'content/' . $this->modelInfo['template_list'];
} else {
$template = 'content/index';
}
$this->assign($data);
$this->setMeta($this->modelInfo['title'] . "列表");
return $this->fetch($template);
}
/**
* 内容添加
* @author netlife <40150501@qq.com>
*/
public function add() {
if (IS_POST) {
$result = $this->model->save($this->param);
if ($result) {
//记录行为
action_log('add_content', 'content', $result, session('user_auth.uid'));
return $this->success("添加成功!", url('admin/sync/index', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/sync/add', array('model_id' => $this->modelInfo['id'])));
}
} else {
$info = array(
'model_id' => $this->modelInfo['id'],
);
$data = array(
'info' => $info,
'fieldGroup' => $this->getField($this->modelInfo),
);
if ($this->modelInfo['template_add']) {
$template = 'content/' . $this->modelInfo['template_add'];
} else {
$template = 'public/nostyleedit';
}
$this->assign($data);
$this->setMeta("添加" . $this->modelInfo['title']);
return $this->fetch($template);
}
}
/**
* 内容修改
* @author netlife <40150501@qq.com>
*/
public function edit($id) {
if (IS_POST) {
$result = $this->model->save($this->param, array('id'=> $id));
if ($result !== false) {
//记录行为
action_log('update_content', 'content', $result, session('user_auth.uid'));
return $this->success("更新成功!", url('admin/sync/index', array('model_id' => $this->modelInfo['id'])));
} else {
return $this->error($this->model->getError(), url('admin/sync/edit', array('model_id' => $this->modelInfo['id'], 'id' => $id)));
}
} else {
if (!$id) {
return $this->error("非法操作!");
}
$info = $this->model->find($id);
if (!$info) {
return $this->error($this->model->getError());
}
$info['model_id'] = $this->modelInfo['id'];
$data = array(
'info' => $info,
'fieldGroup' => $this->getField($this->modelInfo),
);
if ($this->modelInfo['template_edit']) {
$template = 'content/' . $this->modelInfo['template_edit'];
} else {
$template = 'public/nostyleedit';
}
$this->assign($data);
$this->setMeta("编辑" . $this->modelInfo['title']);
return $this->fetch($template);
}
}
/**
* 内容删除
* @author netlife <40150501@qq.com>
*/
public function del() {
$id = $this->getArrayParam('id');
if (empty($id)) {
return $this->error("非法操作!");
}
$map['id'] = array('IN', $id);
$result = $this->model->where($map)->delete();
$list = db('chapter')->where(['articleid'=>array('IN', $id)])->column('id');
foreach ($list as $v){
db('chapter')->where(['id'=>$v])->delete();
db('novel_content')->where(['chapterid'=>$v])->delete();
}
if (false !== $result) {
//记录行为
action_log('delete_content', 'content', $result, session('user_auth.uid'));
return $this->success("删除成功!");
} else {
return $this->error("删除失败!");
}
}
/**
* 设置状态
* @author netlife <40150501@qq.com>
*/
public function status($id, $status) {
$map['id'] = $id;
$result = $this->model->where($map)->setField('status', $status);
if (false !== $result) {
return $this->success("操作成功!");
} else {
return $this->error("操作失败!!");
}
}
/**
* 设置置顶
* @author netlife <40150501@qq.com>
*/
public function settop($id, $is_top) {
$map['id'] = $id;
$result = $this->model->where($map)->setField('is_top', $is_top);
if (false !== $result) {
return $this->success("操作成功!");
} else {
return $this->error("操作失败!!");
}
}
/**
* 获取字段信息
* @return array 字段数组
* @author netlife <40150501@qq.com>
*/
protected function getField() {
$field_group = parse_config_attr($this->modelInfo['attribute_group']);
$map['model_id'] = $this->modelInfo['id'];
if ($this->request->action() == 'add') {
$map['is_show'] = array('in', array('1', '2'));
} elseif ($this->request->action() == 'edit') {
$map['is_show'] = array('in', array('1', '3'));
}
//获得数组的第一条数组
$rows = model('Attribute')->getFieldlist($map, 'id');
if (!empty($rows)) {
foreach ($rows as $key => $value) {
$list[$value['group_id']][] = $value;
}
foreach ($field_group as $key => $value) {
$fields[$value] = isset($list[$key]) ? $list[$key] : array();
}
}else{
$fields = array();
}
return $fields;
}
/**
* 创建搜索
* @return [array] [查询条件]
*/
protected function buildMap() {
$map = array();
$data = $this->request->param();
foreach ($data as $key => $value) {
if ($value) {
if ($key == 'keyword') {
$map['title'] = array("LIKE", "%$value%");
} elseif ($key == 'category') {
$map['category_id'] = $value;
} elseif ($key == 'create_time') {
$map['create_time'] = array('BETWEEN', array(strtotime($value[0]), strtotime($value[1])));
} else {
$map[$key] = $value;
}
}
}
if (isset($map['page'])) {
unset($map['page']);
}
if (isset($map['model_id'])) {
unset($map['model_id']);
}
$this->assign($data);
return $map;
}
/**
* 检测需要动态判断的文档类目有关的权限
*
* @return boolean|null
* 返回true则表示当前访问有权限
* 返回false则表示当前访问无权限
* 返回null则会进入checkRule根据节点授权判断权限
*
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
protected function checkDynamic() {
$model_id = $this->request->param('model_id');
if (IS_ROOT) {
return true; //管理员允许访问任何页面
}
$models = model('AuthGroup')->getAuthModels(session('user_auth.uid'));
if (!$model_id) {
return false;
} elseif (in_array($model_id, $models)) {
//返回null继续判断操作权限
return null;
} else {
return false; //无权限
}
return false;
}
public function sync() {
$this->assign('id',input('id'));
$this->setMeta("同步中");
return $this->fetch();
}
public function spiderlist()
{
if (Request::instance()->isPost()) {
$id = Request::instance()->param('id');
if (!empty($id)) {
$tpm = [];
$webInfo = db('novel_synclist')->where('id', $id)->find();
Session::set('webInfo', $webInfo);
$list = $this->curl_get($webInfo['listurl']);
if ($list) {
$tpm['data'] = $list;
$tpm['code'] = 0;
return json($tpm);
}
return json(['code' => 1]);
}
}
}
//详情
public function articlelist()
{
$webInfo = Session::get('webInfo');
$categorys = [];
if($webInfo["category"]){
$cat = explode('|',$webInfo["category"]);
if($cat){
foreach ($cat as $k=>$v){
$cate = explode('=>',$v);
if(strpos($v,"@")!==false){
foreach ($cate as $vv){
if(strpos($vv,"@")!==false){
$c = explode('@',$vv);
$categorys[$k][$c[0]] =$c[1];
}else{
$categorys[$k]['catename'] =$vv;
}
}
}else{
$categorys[$k]=$cate;
}
}
}
}
if($categorys){
foreach ($categorys as $k=>$v){
if(isset($v['catename'])){
$category[$v['catename']] = ['id'=>$v['id'],'channel'=>$v['channel']];
}else{
$category[$v['0']]=$v['1'];
}
}
}
/**
$category = [
"玄幻奇幻"=>['id'=>23,'channel'=>1],
"武侠仙侠"=>['id'=>24,'channel'=>1],
"都市校园"=>['id'=>25,'channel'=>1],
"历史军事"=>['id'=>26,'channel'=>1],
"网游竞技"=>['id'=>27,'channel'=>1],
"科幻灵异"=>['id'=>28,'channel'=>1],
"总裁豪门"=>['id'=>29,'channel'=>2],
"古代言情"=>['id'=>30,'channel'=>2],
"青春校园"=>['id'=>31,'channel'=>2],
"女频灵异"=>['id'=>32,'channel'=>2],
];
***/
$infourl = str_replace("{0}", Request::instance()->param('articleid'),$webInfo['infourl']);
//获取小说详情
$info = $this->curl_get($infourl);
if($info['sort'] &&$info['articlename']){
//小说存在
$articleinfo = db('novel')->where('title', 'like', '%' . $info["articlename"] . '%')->value('id');
if ($articleinfo) {
$articleid = $articleinfo;
} else {//不存在
$data['create_time'] = strtotime($info['postdate']);
$data['update_time'] = strtotime($info['lastupdate']);
$data['title'] = $info['articlename'];
$data['keywords'] = $info['keywords'];
$data['author'] = $info['author'];
$data['intro'] = $info['intro'];
$data['fullflag'] = $info['fullflag'];
$data['articletype'] = 1;
if($info['sort']){
$data['channel'] = $category[$info['sort']]['channel'];
$data['category_id'] = $category[$info['sort']]['id'];
}else{
$data['channel'] = 0;
$data['category_id'] = 0;
}
$save_dir = "uploads/novelhumb/".date('Ymd')."/";
if($info["cover"]){
$data['cover'] = file_get_img($info["cover"],$save_dir,1);
}
$articleid = db("novel")->insertGetId($data);
}
$arr = [];
$arr['code'] = 0;
$arr['articleid'] = $articleid;
$arr['url'] = $infourl;
$arr['data'] = $info;
}else{
$arr = [];
$arr['code'] = 1;
$arr['msg'] = "小说名称或者分类为空".$infourl;
}
return json($arr);
}
//章节
public function chapterlist()
{
$webInfo = Session::get('webInfo');
$aid = Request::instance()->param('aid');
$volumeurl = str_replace("{0}", $aid, $webInfo['chapterurl']);
$volume = $this->curl_get($volumeurl);
if ($volume) {
$arr = [];
$arr['code'] = 0;
$arr['url'] = $volumeurl;
$arr['data'] = $volume;
return json($arr);
}
}
//内容
public function chapter()
{
$request = Request::instance();
$params = $request->param();
$arr = [];
$webInfo = Session::get('webInfo');
$aid = $request->param('aid');
$articlename = $request->param('articlename');
$articleid = $request->param('articleid');
$chapterOrder = $request->param('chapterOrder');
$articleInfo = $params['articleInfo'];
//查询最新章节
$chapters = db('novel')->where('id', $articleid)->value('chapters');
//判断是否有最新需要同步的章节
if ($chapterOrder >= $chapters) {
$size = round($articleInfo["words"]/2);
//更新章节
$chapter['articleid'] = $articleid; //文章id
$chapter['articlename'] = $articlename; //章节名称
$chapter['uid'] = session('user_auth.uid'); //发表者UID
$chapter['chaptername'] = $articleInfo["chaptername"]; //章节名
$chapter['size'] = $size; //字数
$chapter['isvip'] = $articleInfo["isvip"];
$chapter['saleprice'] = $articleInfo["saleprice"]; //销售价格,单位为 分
$chapter['create_time'] = strtotime($articleInfo["postdate"]) ? strtotime($articleInfo["postdate"]) : time(); //开始发表时间
$chapter['update_time'] = strtotime($articleInfo["lastupdate"]) ? strtotime($articleInfo["lastupdate"]) : time(); //最后更新时间
$chapter['chapterorder'] = $chapterOrder + 1; //章节序号
$chapterid = db("chapter")->insertGetId($chapter);
if ($chapterid) {
// 更新最新信息至小说表
$xidb = db("novel")->where('id', $articleid)->update(['lastvolume' => $articleInfo["chaptername"], 'lastvolumeid' => $chapterid, 'chapters' => ['exp', 'chapters+1'], 'size' => ['exp', 'size+'.$size], 'update_time' => time()]);
}
if (!$xidb) {
return json($arr['code'] = 1);
}
$contenturl = str_replace(array("{0}", "{1}"), array($aid, $articleInfo['chapterid']), $webInfo['contenturl']);
//获取小说内容
$content = $this->curl_get($contenturl);
$ocontent = [];
$ocontent['chapterid'] = $chapterid;
$ocontent['content'] = "<p>" . str_replace(array("\r\n", "\r", "\n"), '</p><p>', $content["content"]) . "</p>";
$contentDb = db("novel_content")->insert($ocontent);
if (!$contentDb) {
return json($arr['code'] = 2);
}
$arr['code'] = 0;
$arr['data'] = $content;
$arr['data']['chaptername'] = $articleInfo["chaptername"];
return json($arr);
} else {
$arr['code'] = 0;
$arr['success'] = 'ok';
$arr['chapters'] = $chapters;
return json($arr);
}
}
}