436 lines
13 KiB
PHP
436 lines
13 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | 小说系统 [ WE CAN DO IT JUST THINK IT ]
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\admin\controller;
|
||
use app\common\controller\Admin;
|
||
|
||
class Chapter extends Admin {
|
||
|
||
public function _initialize() {
|
||
parent::_initialize();
|
||
$this->setMenu();
|
||
$this->model_id = $model_id = $this->request->param('model_id');
|
||
|
||
$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($id) {
|
||
if (!$id) {
|
||
return $this->error("非法操作!");
|
||
}
|
||
$info = $this->model->find($id);
|
||
|
||
if (!$info) {
|
||
return $this->error($this->model->getError());
|
||
}
|
||
$info['model_id'] = $this->modelInfo['id'];
|
||
|
||
//建议本章生成推广文案
|
||
$suggestid = $info['suggestid']?$info['suggestid']:config('suggestid');
|
||
|
||
/***默认强关章节****/
|
||
$uid = session('user_auth.uid');
|
||
$focus = db('chapter_focus')->where(array('uid'=>$uid,'articleid'=>$id))->value('focuschapter');
|
||
$focuschapter = $focus?$focus:config('agent_guanzhu');
|
||
|
||
//获取授权用户组
|
||
$access = config('cartoon_access');
|
||
//获取当前用户组
|
||
$group = session('user_auth.group');
|
||
|
||
//章节重新排序
|
||
$droplist = db('chapter')->where(array('status'=>1,'articleid'=>$id))->field('id,chapterorder')->order('chapterorder ASC')->select();
|
||
if($droplist){
|
||
foreach ($droplist as $k=>$v){
|
||
$o = $k+1;
|
||
if($o!=$v['chapterorder']){
|
||
db('chapter')->where("id=".$v["id"])->setField('chapterorder', $o);
|
||
}
|
||
}
|
||
}
|
||
|
||
/***章节列表***/
|
||
$chapterlist = db('chapter')->where(array('status'=>1,'articleid'=>$id))->field('id,chaptername,chapterorder,chaptertype,isvip,saleprice')->order('chapterorder ASC')->paginate(config('list_rows'));
|
||
$data = array(
|
||
'articleid' => $id,
|
||
'page' => input('page')?input('page'):1,
|
||
'info' => $info,
|
||
'focuschapter'=> $focuschapter,
|
||
'suggestid'=> $suggestid,
|
||
'chapterlist' => $chapterlist,
|
||
'group' => $group,
|
||
'access' => $access,
|
||
);
|
||
|
||
$this->assign($data);
|
||
$this->setMeta("[".$info['title']."]".$this->modelInfo['title'] ."章节列表");
|
||
return $this->fetch();
|
||
|
||
}
|
||
|
||
/**
|
||
* 内容添加
|
||
* @author netlife <40150501@qq.com>
|
||
*/
|
||
public function add() {
|
||
$articleid = $this->request->param('articleid');
|
||
if (IS_POST) {
|
||
//发表者UID
|
||
$this->param['uid'] = session("user_auth.uid");
|
||
//字数
|
||
$this->param['size'] = get_count_content($this->param['content']);
|
||
|
||
//是否有金币
|
||
if( $this->param['isvip'] &&$this->param['size']){
|
||
$this->param['saleprice'] = round($this->param['size']/1000*config('wordsperegold'));
|
||
}
|
||
//章节序号
|
||
$chapterorder = db('chapter')->where(array('articleid'=>$this->param['articleid']))->count();
|
||
$this->param['chapterorder'] = $chapterorder+1;
|
||
|
||
$result = $this->model->save($this->param);
|
||
if ($result) {
|
||
$chapterId = $this->model->getLastInsID();
|
||
|
||
//更新小说章节内容
|
||
$ocontent['chapterid'] = $chapterId;
|
||
$ocontent['content'] = $this->param['content'];
|
||
|
||
//数据分表开启的时候
|
||
if(config('partition')){
|
||
$rule = [
|
||
'type' => 'mod', // 分表方式
|
||
'num' => 10 // 分表数量
|
||
];
|
||
db("novel_content")->partition(['chapterid' => $chapterId], "chapterid", $rule)->insert($ocontent);
|
||
}else{
|
||
db("novel_content")->insert($ocontent);
|
||
}
|
||
|
||
//更新小说总字数 总章节数
|
||
db('novel')->where(array('id'=>$articleid))->update(array('lastvolumeid'=>$chapterId,'lastvolume'=>$this->param['chaptername'],'chapter'=>$this->param['chapterorder'],'size'=>array('exp', '`size`+'.$this->param['size'])));
|
||
|
||
//记录行为
|
||
action_log('add_chapter', 'chapter',$chapterId, session('user_auth.uid'));
|
||
return $this->success("添加成功!", url('admin/chapter/index', array('model_id' =>1,'id' => $this->param['articleid'])));
|
||
} else {
|
||
return $this->error($this->model->getError(), url('admin/chapter/index', array('model_id' =>1,'id' => $this->param['articleid'])));
|
||
}
|
||
} else {
|
||
|
||
$info = array(
|
||
'model_id' => $this->modelInfo['id'],
|
||
);
|
||
$data = array(
|
||
'articleid' => $articleid,
|
||
'info' => $info,
|
||
'fieldGroup' => $this->getField($this->modelInfo),
|
||
);
|
||
|
||
$this->assign($data);
|
||
$this->setMeta("添加" . $this->modelInfo['title']);
|
||
return $this->fetch('chapter/edit');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 内容修改
|
||
* @author netlife <40150501@qq.com>
|
||
*/
|
||
public function edit($id) {
|
||
$articleid = $this->request->param('articleid');
|
||
$info = db('chapter')->where('id='.$id)->find();
|
||
if (IS_POST) {
|
||
|
||
//字数
|
||
$this->param['size'] = get_count_content($this->param['content']);
|
||
//是否有金币
|
||
if( $this->param['isvip'] && $this->param['size']){
|
||
$this->param['saleprice'] = round($this->param['size']/1000*config('wordsperegold'));
|
||
}
|
||
//更新小说总字数
|
||
if($info["size"] != $this->param['size']){
|
||
db('novel')->where(array('id'=>$articleid))->update(array('size'=>array('exp', '`size`-'.$info['size'])));
|
||
db('novel')->where(array('id'=>$articleid))->update(array('size'=>array('exp', '`size`+'.$this->param['size'])));
|
||
}
|
||
|
||
//数据分表开启的时候
|
||
if(config('partition')){
|
||
$rule = [
|
||
'type' => 'mod', // 分表方式
|
||
'num' => 10 // 分表数量
|
||
];
|
||
db('novel_content')->partition(['chapterid' => $id], "chapterid", $rule)->where('chapterid='.$id)->cache('chaptercontent'.$id)->setField('content', $this->param['content']);
|
||
}else{
|
||
$content = db('novel_content')->where('chapterid='.$id)->find();
|
||
if($content){
|
||
db('novel_content')->where('chapterid='.$id)->cache('chaptercontent'.$id)->setField('content', $this->param['content']);
|
||
}else{
|
||
$ocontent['chapterid'] = $id;
|
||
$ocontent['content'] = $this->param['content'];
|
||
db("novel_content")->insert($ocontent);
|
||
}
|
||
|
||
}
|
||
|
||
unset($this->param['content']);
|
||
|
||
//$result = $this->model->save($this->param, array('id'=> $id));
|
||
$result = db('chapter')->where('id', $id)->cache('chapterinfo'.$id)->update($this->param);
|
||
|
||
if ($result !== false) {
|
||
//记录行为
|
||
action_log('update_chapter', 'chapter', $id, session('user_auth.uid'));
|
||
return $this->success("更新成功!", url('admin/chapter/index', array('model_id' =>1,'id'=>$articleid)));
|
||
} else {
|
||
return $this->error($this->model->getError(), url('admin/chapter/edit', array('model_id' => 1,'id'=>$articleid)));
|
||
}
|
||
} else {
|
||
if (!$id) {
|
||
return $this->error("非法操作!");
|
||
}
|
||
$info = $this->model->find($id);
|
||
//数据分表开启的时候
|
||
if(config('partition')){
|
||
$rule = [
|
||
'type' => 'mod', // 分表方式
|
||
'num' => 10 // 分表数量
|
||
];
|
||
$content = db('novel_content')->partition(['chapterid' => $id], "chapterid", $rule)->where('chapterid='.$id)->value('content');
|
||
}else{
|
||
$content = db('novel_content')->where('chapterid='.$id)->value('content');
|
||
}
|
||
|
||
if (!$info) {
|
||
return $this->error($this->model->getError());
|
||
}
|
||
$info['model_id'] = $this->modelInfo['id'];
|
||
$data = array(
|
||
'articleid' => $articleid,
|
||
'info' => $info,
|
||
'content' => $content,
|
||
'fieldGroup' => $this->getField($this->modelInfo),
|
||
);
|
||
$this->assign($data);
|
||
$this->setMeta("编辑" . $this->modelInfo['title']);
|
||
return $this->fetch();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 内容删除
|
||
* @author netlife <40150501@qq.com>
|
||
*/
|
||
public function del() {
|
||
$id = $this->getArrayParam('id');
|
||
$articleid = $this->request->param('articleid');
|
||
|
||
if (empty($id)) {
|
||
return $this->error("非法操作!");
|
||
}
|
||
|
||
$map['id'] = array('IN', $id);
|
||
//删除字数
|
||
$size = db('chapter')->where($map)->sum('size');
|
||
if($size>0){
|
||
db('novel')->where(array('id'=>$articleid))->update(array('size'=>array('exp', '`size`-'.$size)));
|
||
}else{
|
||
db('novel')->where(array('id'=>$articleid))->update(array('size'=>0));
|
||
}
|
||
|
||
$result = $this->model->where($map)->delete();
|
||
|
||
if (false !== $result) {
|
||
|
||
//更新章节数
|
||
$chapters = db('chapter')->where(array('articleid'=>$articleid,"status"=>1,"chaptertype"=>0))->count();
|
||
db('novel')->where(array('id'=>$articleid))->update(array('chapters'=>$chapters));
|
||
|
||
//记录行为
|
||
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($search_key) {
|
||
$map = array();
|
||
$data = $this->request->param();
|
||
foreach ($data as $key => $value) {
|
||
if ($value) {
|
||
if ($key == 'keyword') {
|
||
$map[$search_key] = 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 api_get_chapter() {
|
||
$chapterid = input('id');
|
||
$chaptername = input('chaptername');
|
||
|
||
//数据分表开启的时候
|
||
if(config('partition')){
|
||
$rule = [
|
||
'type' => 'mod', // 分表方式
|
||
'num' => 10 // 分表数量
|
||
];
|
||
$content = db("novel_content")->partition(['chapterid' => $chapterid], "chapterid", $rule)->value('content');
|
||
}else{
|
||
$content = db('novel_content')->where("chapterid=".$chapterid)->value('content');
|
||
}
|
||
echo json_encode(["title"=>$chaptername,"content"=>$content]);
|
||
}
|
||
|
||
/******章节排序******/
|
||
public function drop() {
|
||
$articleid= input('id');
|
||
|
||
if (IS_POST) {
|
||
$articleid= input('id');
|
||
$sort= $_REQUEST["sort"];
|
||
|
||
if($sort){
|
||
foreach ($sort as $k=>$v){
|
||
db('chapter')->where('id='.$v)->setField('chapterorder', $k+1);
|
||
}
|
||
return $this->success("排序成功!", url('admin/chapter/index', array('model_id' =>1,'id'=>$articleid)));
|
||
}else{
|
||
return $this->error("参数错误", url('admin/chapter/drop', array('model_id' => $this->model_id,'id'=>$articleid)));
|
||
}
|
||
}
|
||
|
||
$chapterlist = db('chapter')->where('articleid = '.$articleid)->order('chapterorder ASC')->select();
|
||
$this->assign('articleid',$articleid);
|
||
$this->assign('chapterlist',$chapterlist);
|
||
$this->setMeta("章节排序");
|
||
return $this->fetch();
|
||
}
|
||
} |