model = model('Book');
$this->bookCategoryModel = model('BookCategory');
$this->view->assign("stateList", $this->model->getStateList());
$this->view->assign("sexList", $this->model->getSexList());
$this->view->assign("billingTypeList", $this->model->getBillingTypeList());
$this->view->assign("isFinishList", $this->model->getIsFinishList());
}
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
//赛选标签条件
//Log::write($_GET,'cctest');
$whereTip = [];
$whereSearch = false;
$wordWhere = false;
if(!empty($_GET)){
if(array_key_exists('name',$_GET) && $_GET['name']){
$name = $_GET['name'];
$whereSearch = function ($query) use ($name) {
$query->where('id', $name)
->whereOr('name', 'like', '%' . $name . '%')
->whereOr('keywords', 'like', '%' . $name . '%');
};
}
if ($_GET) {
foreach ($_GET as $k => $v) {
if ($v === '') {
continue;
}
if ($k == 'corner_mark') {
if ($v == 'free') {
$whereTip['free_stime'] = ['<=', time()];
$whereTip['free_etime'] = ['>=', time()];
} else {
$whereTip['corner_mark'] = (string)$_GET[$k];
}
continue;
} elseif ($k == 'state') {
if ($v !== '') {
$whereTip[$k] = $v;
} else {
$whereTip[$k] = ['neq', '0'];
}
continue;
} elseif (in_array($k, ['is_finish', 'len', 'sex', 'book_category_id', 'state'])) {
$whereTip[$k] = (string)$_GET[$k];
} elseif ($k == 'idx') {
$whereTip[$k] = ['between', $v];
} elseif ($k == 'word_count') {
$p = explode(',', $v);
$wordWhere = "cast($k as UNSIGNED) between $p[0] and $p[1]";
}
}
}
}
//Log::write($whereTip,'cctest');
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('pkey_name')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$map = [];
if($this->group>2) {
$map['cansee'] = 1;
}
//获取独家书籍
ExclusiveService::instance()->getExclusiveNotidsWithWhere($this->group, $this->auth->id,$map);
$total = $this->model
->where($where)
->where($whereTip)
->where($whereSearch)
->where($wordWhere)
->where($map)
->order('book.idx desc,book.idxx desc')
->count();
$list = $this->model
->where($where)
->where($whereTip)
->where($whereSearch)
->where($wordWhere)
->where($map)
->order('book.idx desc,book.idxx desc')
->limit($offset, $limit)
->select();
if ($list) {
foreach ($list as $k => $item) {
$list[$k]['book_tags_text'] = $this->getBookTags($item['book_tags']);
}
}
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$this->assignconfig('site',config('site'));
$this->assign('filter', $this->searchFilter());
$this->assignconfig('filter', $this->searchFilter());
//dump($this->searchFilter());die;
return $this->view->fetch();
}
public function getBookTags($book_tags_ids)
{
$html = '';
if ($book_tags_ids) {
$tag_arr = explode(',', $book_tags_ids);
if ($tag_arr) {
foreach ($tag_arr as $k => $item) {
$tag_obj = model('BookTags')->getBookTagById($item);
if ($tag_obj && $tag_obj['status'] == 'normal') {
$html .= '' . $tag_obj['name'] . '';
}
}
}
}
return $html;
}
private function searchFilter()
{
$sex = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '男频'], ['id' => 2, 'name' => '女频']];
$state = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '上架'], ['id' => -1, 'name' => '入库']];
$isFinish = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '完结'], ['id' => 0, 'name' => '连载']];
$idx = [['id' => '', 'name' => '全部','active'=>true], ['id' => '91,100', 'name' => '100-91'], ['id' => '81,90', 'name' => '90-81'], ['id' => '61,80', 'name' => '80-61'], ['id' => '0,60', 'name' => '60以下']];
$len = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '长篇'], ['id' => 2, 'name' => '短篇']];
$property = [['id' => '', 'name' => '全部','active'=>true], ['id' => 'exclusive', 'name' => '独家'], ['id' => 'hot', 'name' => '火热在推'], ['id' => 'new', 'name' => '新书'], ['id' => 'free', 'name' => '免费']];
$word_count = [['id' => '', 'name' => '全部','active'=>true], ['id' => '0,300000', 'name' => '30万以下'], ['id' => '300000,500000', 'name' => '30万-50万'], ['id' => '500000,1000000', 'name' => '50万-100万'], ['id' => '1000000,5000000', 'name' => '100万-500万'], ['id' => '5000000,100000000', 'name' => '500万以上']];
// 'hot', 'exclusive', 'new', ''
$categoryAll = collection($this->bookCategoryModel
->where(['status' => 'normal'])
->order('sex', 'asc')
->order('weigh', 'desc')
->select())->toArray();
$categoryGirl = collection($this->bookCategoryModel
->where(['status' => 'normal','sex'=>'2'])
->order('weigh', 'desc')
->select())->toArray();
$categoryBoy = collection($this->bookCategoryModel
->where(['status' => 'normal','sex'=>'1'])
->order('weigh', 'desc')
->select())->toArray();
array_unshift($categoryAll,['id'=>'','name'=>'全部','active'=>true,'sex'=>'-1']);
array_unshift($categoryGirl,['id'=>'','name'=>'全部','active'=>true,'sex'=>'2']);
array_unshift($categoryBoy,['id'=>'','name'=>'全部','active'=>true,'sex'=>'1']);
$filter = ['sex' => $sex, 'is_finish' => $isFinish, 'category_all' => $categoryAll, 'category_boy' => $categoryBoy, 'category_girl' => $categoryGirl, 'idx' => $idx, 'len' => $len, 'property' => $property, 'state' => $state, 'word_count'=>$word_count];
return $filter;
}
private function categorylist()
{
$list = $this->bookCategoryModel
->where(['status' => 'normal'])
->order('sex', 'asc')
->order('weigh', 'desc')
->select();
return $list;
}
/**
* 频道下拉列表
*/
public function sexList()
{
$result = $this->model->getSexList();
$searchlist = [];
foreach ($result as $key => $value) {
$searchlist[] = ['id' => $key, 'name' => $value];
}
$data = ['searchlist' => $searchlist];
$this->success('', null, $data);
}
/**
* 书籍信息
*/
public function info()
{
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
try {
$filter = $this->request->param('filter');
$filter = json_decode($filter, true);
if (is_array($filter) && isset($filter['id'])) {
$id = $filter['id'];
} else {
$this->error('ajax参数错误,缺少id!');
}
$book = model('Book')::get($id);
if($book['cansee']==0 && $this->group > 2)
$this->error('数据错误');
$offset = $this->request->get('offset', 0);
$limit = $this->request->get('limit', 10);
$page_num = ($offset/$limit)+1;
$search = '';
if (array_key_exists('chapter_name', $filter)) {
$search = $filter['chapter_name'];
}
$data = model('Book')::searchChapterByName($id, $page_num, $limit, $search);
if($data['code']){
$this->error($data['msg']);
}
$chapters = $data['data']['data'];
$chapter_ids = array_column($chapters, 'id');
if ($chapter_ids) {
$chapterList = model('chapter_uv_rate')
->where('channel_id', $this->auth->id)
->where('book_id', $id)
->whereIn('chapter_id', $chapter_ids)
->column('chapter_id,uv,uv_rate');
if ($chapterList) {
foreach ($chapters as $index => $chapter) {
if (array_key_exists($chapter['id'], $chapterList)) {
$chapters[$index]['uv'] = $chapterList[$chapter['id']]['uv'];
$chapters[$index]['uv_rate'] = round($chapterList[$chapter['id']]['uv_rate'] * 100, 2);
}
}
}
}
$result = array("total" => $data['data']['totalNum'], "rows" => $chapters, 'list'=>$chapters);
return json($result);
} catch (Exception $e) {
$this->error('接口数据异常!');
}
}
if($this->auth->agent_id || $this->group == 3){
if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
$this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
}
}
$id = input('id');
$book = model('Book')::get($id);
$price = $book['price']?$book['price']:Config::get("site.book_chapter_price");
$this->assignconfig("price",$price);
if($book['cansee']==0 && $this->group > 2)
$this->error('数据错误');
$book['category_name'] = model('Book')->getCategoryName($book['book_category_id']);
// $config= $this->getConfig();
$this->assignconfig('site',config('site'));
$this->assignconfig('agent_distribute',false);
if($this->group == 3 || $this->auth->agent_id ){
$this->assignconfig('agent_distribute',true);
}
$this->assign('book', $book);
$this->assignconfig('bookinfo',$book);
$this->assignconfig('book_id', $id);
$guide_switch = false;
$guideResult = model('Guide')->where(['admin_id' => $this->auth->id, 'book_id' => $id])->find();
if (isset($guideResult['chapter_idx'])) {
$guide_chapter_idx = $guideResult['chapter_idx'];
} else {
$adminConfigResult = model('AdminConfig')->where(['admin_id' => $this->auth->channel_id])->find();
if (isset($adminConfigResult['book_guide_chapter_idx'])) {
$guide_chapter_idx = $adminConfigResult['book_guide_chapter_idx'];
} else {
$guide_chapter_idx = config('site.book_guide_chapter_idx');
}
}
if($this->auth->agent_id){
$channel_id = $this->auth->agent_id;
}else{
$channel_id = $this->auth->channel_id;
}
$adminConfigResult = model('AdminConfig')->where(['admin_id' => $channel_id])->find();
if(isset($adminConfigResult['guide_domain']) && $adminConfigResult['guide_domain']){
$guide_switch = true;
}
$showAllSelect = 1;
if (in_array($this->group, [7,8])) {
$showAllSelect = 0;
}
$not_ban = 0;
//独家书籍
if (in_array($this->group, [3, 4])) {
$exclusive_books = model('Book')->getExclusiveBookIds($channel_id);
if (in_array($id, $exclusive_books)) {
$not_ban = 1;
}
}
if(in_array($this->group, [7, 8])){
$bind_channel_ids = model('Exclusive')->getAllBindChannelIds();
if(!empty($bind_channel_ids)) {
$adminIds = model("VipAdminBind")->getChannelIds($this->auth->id);//获取服务号id
if(empty(array_intersect($bind_channel_ids,$adminIds)) && empty(array_intersect($adminIds,$bind_channel_ids))){
$exclusive = model('Book')->getExclusiveBookIds($this->auth->id);
if(!empty($exclusive) && in_array($id,$exclusive)){
$not_ban = 1;
}
}else{
$exclusive = model('Book')->getVipExclusiveBookIds($adminIds);
if(!empty($exclusive) && in_array($id,$exclusive)){
$not_ban = 1;
}
}
}
}
$this->assignconfig('not_ban',$not_ban);
$this->assignconfig('guide_switch',$guide_switch);
$this->assignconfig('show_all_select',$showAllSelect);
$this->assignconfig('guide_chapter_idx', $guide_chapter_idx); //关注章节序
$this->assignconfig('free_chapter_num', $book['free_chapter_num'] >0 ? $book['free_chapter_num'] : config('site.book_free_chapter_num')); //关注章节序
return $this->view->fetch();
}
/**
* 获取章节内容
*/
public function chapter()
{
$book_id = input('book_id');
if ($this->group != AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN && $this->group != AdminConstants::ADMIN_GROUP_ID_OPERATOR) {
$bind_channel_ids = model('Exclusive')->getAllBindChannelIds();
if(!empty($bind_channel_ids)) {
if($this->group == AdminConstants::ADMIN_GROUP_ID_VIP || $this->group == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR){
$adminIds = model("VipAdminBind")->getChannelIds($this->auth->id);//获取服务号id
if(empty(array_intersect($bind_channel_ids,$adminIds)) && empty(array_intersect($adminIds,$bind_channel_ids))){
$exclusive = model('Book')->getExclusiveBookIds($this->auth->id);
if(!empty($exclusive) && in_array($book_id,$exclusive)){
$this->error('此书籍为渠道独家书籍');
}
}else{
$exclusive = model('Book')->getVipExclusiveBookIds($adminIds);
if(!empty($exclusive) && in_array($book_id,$exclusive)){
$this->error('此书籍为渠道独家书籍');
}
}
}else{
$exclusive = model('Book')->getExclusiveBookIds($this->auth->id);
if(!empty($exclusive) && in_array($book_id,$exclusive)){
$this->error('此书籍为渠道独家书籍');
}
}
}
}
$chapter_id = input('chapter_id');
$data = model('Book')::getChapterInfo($book_id,$chapter_id);
if($data['code']){
$this->error($data['msg']);
}
if ($this->request->isAjax()) {
return json($data);
}
$this->assign('data', $data);
return $this->view->fetch();
}
public function createQRCode(){
$params = $this->request->param();
$book_id = $this->request->get('book_id');
$chapter_id = $this->request->get('chapter_id');
if(isset($params['essay_id'])){
/**
* 图片序号的生成格式
*/
$imgSize = [
1 => [
'size' => 143, //二维码宽度
'x' => 376, //水印x
'y' => 35 //水印y
],
2 => [
'size' => 147, //二维码宽度
'x' => 374, //水印x
'y' => 33 //水印y
],
3 => [
'size' => 135, //二维码宽度
'x' => 372, //水印x
'y' => 46 //水印y
],
4 => [
'size' => 179, //二维码宽度
'x' => 356, //水印x
'y' => 15 //水印y
],
5 => [
'size' => 179, //二维码宽度
'x' => 356, //水印x
'y' => 15 //水印y
],
6 => [
'size' => 179, //二维码宽度
'x' => 356, //水印x
'y' => 15 //水印y
]
];
$qrcodeRes = model('CustomQrcode')->where(['admin_id'=>$this->auth->id,'book_id'=>$params['book_id'],'chapter_id'=>$params['chapter_id']])->select();
$imgList = array();
if($qrcodeRes){
foreach($qrcodeRes as $val){
if(strpos($val['url'], 'mp.weixin.qq.com') === false){
$tmp = explode('_',basename($val['url'],".png"));
$imgList[$tmp[2]] = $val['url'];
}else{
$imgList[0] = $val['url'];
}
}
}
if(!isset($imgList[$params['essay_id']])){
//获取微信永久二维码
$data['index'] = 1;
$last_index = model('CustomQrcode')->field('index')->where(['admin_id'=>$this->auth->id])->order('`index` desc')->find();
if($last_index){
if($last_index['index'] >= 100000){
$this->error(__('WeChat QRCode Max 100000'));
}
$data['index'] = ($last_index['index'] + 1);
}
$channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
$adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
$wechat = new WeChatObject($adminConfig);
$officialAccount = $wechat->getOfficialAccount();
$result = $officialAccount->qrcode->forever($data['index']);
if(empty($result) || isset($result['errcode'])){
$this->error(__('Get WeChat QRCode Error'));
}
$data['url'] = $officialAccount->qrcode->url($result['ticket']);
$res = null;
if($params['essay_id'] != 0){
$image_1 = imagecreatefrompng(ROOT_PATH . "public/assets/img/essay/qrcode_{$params['essay_id']}.png"); //底图
// $image_2 = imagecreatefromjpeg($data['url']); //二维码图片
$image_2 = $this->createThumbnail($data['url'],$imgSize[$params['essay_id']]['size'],$imgSize[$params['essay_id']]['size']);
imagecopymerge($image_1, $image_2, $imgSize[$params['essay_id']]['x'], $imgSize[$params['essay_id']]['y'], 0, 0, imagesx($image_2),
imagesy($image_2), 100);
$imgPath = ROOT_PATH . 'public/uploads/qrcode/'; //图片保存路径
if (!file_exists($imgPath)) { //创建文件夹
mkdir($imgPath, 0700, true);
clearstatcache();
}
$imgPath = $imgPath .$book_id.'_'.$chapter_id .'_' . $params['essay_id'] .'_'.$this->auth->id.'_'.time().'.png';
$imgUrl = cdnurl("/uploads/qrcode/" . $book_id .'_'.$chapter_id . '_' . $params['essay_id'] .'_'.$this->auth->id.'_'.time().'.png');
$res = imagepng($image_1, $imgPath);
$data['url'] = $res ? $imgUrl : $data['url'];
imagedestroy($image_1);
imagedestroy($image_2);
}
$data['type'] = 2;
$data['title'] = $params['book_name'].'-'.$params['chapter_name'].'-模板'.$params['essay_id'];
$data['admin_id'] = $this->auth->id;
$data['book_id'] = $params['book_id'];
$data['book_name'] = $params['book_name'];
$data['chapter_id'] = $params['chapter_id'];
if(model('CustomQrcode')->allowField(true)->save($data)){
$this->success("关注二维码生成成功");
}else{
$this->error("生成失败");
}
}else{
$this->success("关注二维码生成成功");
}
}else{
if(!$book_id || !$chapter_id){
$this->error('参数错误!');
}
$qrcodeRes = model('CustomQrcode')->where(['admin_id'=>$this->auth->id,'book_id'=>$params['book_id'],'chapter_id'=>$params['chapter_id']])->select();
$imgList = array();
if($qrcodeRes){
foreach($qrcodeRes as $val){
if(strpos($val['url'], 'mp.weixin.qq.com') === false){
$tmp = explode('_',basename($val['url'],".png"));
$imgList[$tmp[2]] = $val['url'];
}else{
$imgList[0] = $val['url'];
}
}
}
$this->assign('imgList',$imgList);
$this->assign('book_info', $params); //关注章节序
return $this->view->fetch();
}
}
/**
* 生成保持原图纵横比的缩略图,支持.png .jpg .gif
* 缩略图类型统一为.png格式
* $srcFile 原图像文件名称
* $toW 缩略图宽
* $toH 缩略图高
* @return bool
*/
private function createThumbnail($srcFile, $toW, $toH)
{
$toW += 20;
$toH += 20;
$info = "";
//返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。
//失败返回false并产生警告。
$data = getimagesize($srcFile, $info);
if (!$data)
return false;
//将文件载入到资源变量im中
switch ($data[2]) //1-GIF,2-JPG,3-PNG
{
case 1:
if(!function_exists("imagecreatefromgif"))
{
echo "the GD can't support .gif, please use .jpeg or .png! back";
exit();
}
$im = imagecreatefromgif($srcFile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg"))
{
echo "the GD can't support .jpeg, please use other picture! back";
exit();
}
$im = imagecreatefromjpeg($srcFile);
break;
case 3:
$im = imagecreatefrompng($srcFile);
break;
}
//计算缩略图的宽高
$srcW = imagesx($im);
$srcH = imagesy($im);
$toWH = $toW / $toH;
$srcWH = $srcW / $srcH;
if ($toWH <= $srcWH)
{
$ftoW = $toW;
$ftoH = (int)($ftoW * ($srcH / $srcW));
}
else
{
$ftoH = $toH;
$ftoW = (int)($ftoH * ($srcW / $srcH));
}
if (function_exists("imagecreatetruecolor"))
{
$ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像
if ($ni)
{
//重采样拷贝部分图像并调整大小 可保持较好的清晰度
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
else
{
//拷贝部分图像并调整大小
$ni = imagecreate($ftoW, $ftoH);
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
}
else
{
$ni = imagecreate($ftoW, $ftoH);
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
ImageDestroy($im);
return $ni;
}
/**
* 设置关注章节
*/
public function setGuideChapterIdx()
{
$book_id = $this->request->get('book_id');
$chapter_idx = $this->request->get('chapter_idx');
if (!$book_id || !$chapter_idx) {
$this->error('参数错误!');
}
$where = [
'book_id' => $book_id,
'admin_id' => $this->auth->id
];
$guideModel = model('Guide');
$row = $guideModel::get($where);
if ($row) {
$row->chapter_idx = $chapter_idx;
$res = $row->save();
} else {
$data = array_merge($where, ['chapter_idx' => $chapter_idx]);
$res = model('Guide')->save($data);
}
if ($res) {
$redis = Redis::instance();
$key = 'GUIDE:' . $this->auth->id;
$redis->del($key);
$this->success('操作成功!', '', ['guide_chapter_idx' => $chapter_idx]);
} else {
$this->error('保存失败!');
}
}
}