Book.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. namespace app\admin\controller\referral;
  3. use app\admin\service\ExclusiveService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use app\common\library\WeChatObject;
  7. use think\Config;
  8. use think\Exception;
  9. use app\main\constants\AdminConstants;
  10. /**
  11. * 小说中心
  12. * @icon fa fa-book
  13. */
  14. class Book extends Backend
  15. {
  16. /**
  17. * @var \app\common\model\Book
  18. */
  19. protected $model = null;
  20. /**
  21. * @var \app\common\model\BookCategory
  22. */
  23. protected $bookCategoryModel = null;
  24. protected $relationSearch = true;
  25. protected $searchFields = 'id,name';
  26. //Multi方法可批量修改的字段
  27. protected $multiFields = 'state';
  28. protected $modelValidate = true;
  29. protected $modelSceneValidate = true;
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. $this->model = model('Book');
  34. $this->bookCategoryModel = model('BookCategory');
  35. $this->view->assign("stateList", $this->model->getStateList());
  36. $this->view->assign("sexList", $this->model->getSexList());
  37. $this->view->assign("billingTypeList", $this->model->getBillingTypeList());
  38. $this->view->assign("isFinishList", $this->model->getIsFinishList());
  39. }
  40. public function index()
  41. {
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags']);
  44. if ($this->request->isAjax()) {
  45. //赛选标签条件
  46. //Log::write($_GET,'cctest');
  47. $whereTip = [];
  48. $whereSearch = false;
  49. $wordWhere = false;
  50. if(!empty($_GET)){
  51. if(array_key_exists('name',$_GET) && $_GET['name']){
  52. $name = $_GET['name'];
  53. $whereSearch = function ($query) use ($name) {
  54. $query->where('id', $name)
  55. ->whereOr('name', 'like', '%' . $name . '%')
  56. ->whereOr('keywords', 'like', '%' . $name . '%');
  57. };
  58. }
  59. if ($_GET) {
  60. foreach ($_GET as $k => $v) {
  61. if ($v === '') {
  62. continue;
  63. }
  64. if ($k == 'corner_mark') {
  65. if ($v == 'free') {
  66. $whereTip['free_stime'] = ['<=', time()];
  67. $whereTip['free_etime'] = ['>=', time()];
  68. } else {
  69. $whereTip['corner_mark'] = (string)$_GET[$k];
  70. }
  71. continue;
  72. } elseif ($k == 'state') {
  73. if ($v !== '') {
  74. $whereTip[$k] = $v;
  75. } else {
  76. $whereTip[$k] = ['neq', '0'];
  77. }
  78. continue;
  79. } elseif (in_array($k, ['is_finish', 'len', 'sex', 'book_category_id', 'state'])) {
  80. $whereTip[$k] = (string)$_GET[$k];
  81. } elseif ($k == 'idx') {
  82. $whereTip[$k] = ['between', $v];
  83. } elseif ($k == 'word_count') {
  84. $p = explode(',', $v);
  85. $wordWhere = "cast($k as UNSIGNED) between $p[0] and $p[1]";
  86. }
  87. }
  88. }
  89. }
  90. //Log::write($whereTip,'cctest');
  91. //如果发送的来源是Selectpage,则转发到Selectpage
  92. if ($this->request->request('pkey_name')) {
  93. return $this->selectpage();
  94. }
  95. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  96. $map = [];
  97. if($this->group>2) {
  98. $map['cansee'] = 1;
  99. }
  100. //获取独家书籍
  101. ExclusiveService::instance()->getExclusiveNotidsWithWhere($this->group, $this->auth->id,$map);
  102. $total = $this->model
  103. ->where($where)
  104. ->where($whereTip)
  105. ->where($whereSearch)
  106. ->where($wordWhere)
  107. ->where($map)
  108. ->order('book.idx desc,book.idxx desc')
  109. ->count();
  110. $list = $this->model
  111. ->where($where)
  112. ->where($whereTip)
  113. ->where($whereSearch)
  114. ->where($wordWhere)
  115. ->where($map)
  116. ->order('book.idx desc,book.idxx desc')
  117. ->limit($offset, $limit)
  118. ->select();
  119. if ($list) {
  120. foreach ($list as $k => $item) {
  121. $list[$k]['book_tags_text'] = $this->getBookTags($item['book_tags']);
  122. }
  123. }
  124. $result = array("total" => $total, "rows" => $list);
  125. return json($result);
  126. }
  127. $this->assignconfig('site',config('site'));
  128. $this->assign('filter', $this->searchFilter());
  129. $this->assignconfig('filter', $this->searchFilter());
  130. //dump($this->searchFilter());die;
  131. return $this->view->fetch();
  132. }
  133. public function getBookTags($book_tags_ids)
  134. {
  135. $html = '';
  136. if ($book_tags_ids) {
  137. $tag_arr = explode(',', $book_tags_ids);
  138. if ($tag_arr) {
  139. foreach ($tag_arr as $k => $item) {
  140. $tag_obj = model('BookTags')->getBookTagById($item);
  141. if ($tag_obj && $tag_obj['status'] == 'normal') {
  142. $html .= '<span class="tag_rb" style="background: ' . $tag_obj['color'] . '">' . $tag_obj['name'] . '</span>';
  143. }
  144. }
  145. }
  146. }
  147. return $html;
  148. }
  149. private function searchFilter()
  150. {
  151. $sex = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '男频'], ['id' => 2, 'name' => '女频']];
  152. $state = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '上架'], ['id' => -1, 'name' => '入库']];
  153. $isFinish = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '完结'], ['id' => 0, 'name' => '连载']];
  154. $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以下']];
  155. $len = [['id' => '', 'name' => '全部','active'=>true], ['id' => 1, 'name' => '长篇'], ['id' => 2, 'name' => '短篇']];
  156. $property = [['id' => '', 'name' => '全部','active'=>true], ['id' => 'exclusive', 'name' => '独家'], ['id' => 'hot', 'name' => '火热在推'], ['id' => 'new', 'name' => '新书'], ['id' => 'free', 'name' => '免费']];
  157. $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万以上']];
  158. // 'hot', 'exclusive', 'new', ''
  159. $categoryAll = collection($this->bookCategoryModel
  160. ->where(['status' => 'normal'])
  161. ->order('sex', 'asc')
  162. ->order('weigh', 'desc')
  163. ->select())->toArray();
  164. $categoryGirl = collection($this->bookCategoryModel
  165. ->where(['status' => 'normal','sex'=>'2'])
  166. ->order('weigh', 'desc')
  167. ->select())->toArray();
  168. $categoryBoy = collection($this->bookCategoryModel
  169. ->where(['status' => 'normal','sex'=>'1'])
  170. ->order('weigh', 'desc')
  171. ->select())->toArray();
  172. array_unshift($categoryAll,['id'=>'','name'=>'全部','active'=>true,'sex'=>'-1']);
  173. array_unshift($categoryGirl,['id'=>'','name'=>'全部','active'=>true,'sex'=>'2']);
  174. array_unshift($categoryBoy,['id'=>'','name'=>'全部','active'=>true,'sex'=>'1']);
  175. $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];
  176. return $filter;
  177. }
  178. private function categorylist()
  179. {
  180. $list = $this->bookCategoryModel
  181. ->where(['status' => 'normal'])
  182. ->order('sex', 'asc')
  183. ->order('weigh', 'desc')
  184. ->select();
  185. return $list;
  186. }
  187. /**
  188. * 频道下拉列表
  189. */
  190. public function sexList()
  191. {
  192. $result = $this->model->getSexList();
  193. $searchlist = [];
  194. foreach ($result as $key => $value) {
  195. $searchlist[] = ['id' => $key, 'name' => $value];
  196. }
  197. $data = ['searchlist' => $searchlist];
  198. $this->success('', null, $data);
  199. }
  200. /**
  201. * 书籍信息
  202. */
  203. public function info()
  204. {
  205. $this->request->filter(['strip_tags']);
  206. if ($this->request->isAjax()) {
  207. try {
  208. $filter = $this->request->param('filter');
  209. $filter = json_decode($filter, true);
  210. if (is_array($filter) && isset($filter['id'])) {
  211. $id = $filter['id'];
  212. } else {
  213. $this->error('ajax参数错误,缺少id!');
  214. }
  215. $book = model('Book')::get($id);
  216. if($book['cansee']==0 && $this->group > 2)
  217. $this->error('数据错误');
  218. $offset = $this->request->get('offset', 0);
  219. $limit = $this->request->get('limit', 10);
  220. $page_num = ($offset/$limit)+1;
  221. $search = '';
  222. if (array_key_exists('chapter_name', $filter)) {
  223. $search = $filter['chapter_name'];
  224. }
  225. $data = model('Book')::searchChapterByName($id, $page_num, $limit, $search);
  226. if($data['code']){
  227. $this->error($data['msg']);
  228. }
  229. $chapters = $data['data']['data'];
  230. $chapter_ids = array_column($chapters, 'id');
  231. if ($chapter_ids) {
  232. $chapterList = model('chapter_uv_rate')
  233. ->where('channel_id', $this->auth->id)
  234. ->where('book_id', $id)
  235. ->whereIn('chapter_id', $chapter_ids)
  236. ->column('chapter_id,uv,uv_rate');
  237. if ($chapterList) {
  238. foreach ($chapters as $index => $chapter) {
  239. if (array_key_exists($chapter['id'], $chapterList)) {
  240. $chapters[$index]['uv'] = $chapterList[$chapter['id']]['uv'];
  241. $chapters[$index]['uv_rate'] = round($chapterList[$chapter['id']]['uv_rate'] * 100, 2);
  242. }
  243. }
  244. }
  245. }
  246. $result = array("total" => $data['data']['totalNum'], "rows" => $chapters, 'list'=>$chapters);
  247. return json($result);
  248. } catch (Exception $e) {
  249. $this->error('接口数据异常!');
  250. }
  251. }
  252. if($this->auth->agent_id || $this->group == 3){
  253. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  254. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  255. }
  256. }
  257. $id = input('id');
  258. $book = model('Book')::get($id);
  259. $price = $book['price']?$book['price']:Config::get("site.book_chapter_price");
  260. $this->assignconfig("price",$price);
  261. if($book['cansee']==0 && $this->group > 2)
  262. $this->error('数据错误');
  263. $book['category_name'] = model('Book')->getCategoryName($book['book_category_id']);
  264. // $config= $this->getConfig();
  265. $this->assignconfig('site',config('site'));
  266. $this->assignconfig('agent_distribute',false);
  267. if($this->group == 3 || $this->auth->agent_id ){
  268. $this->assignconfig('agent_distribute',true);
  269. }
  270. $this->assign('book', $book);
  271. $this->assignconfig('bookinfo',$book);
  272. $this->assignconfig('book_id', $id);
  273. $guide_switch = false;
  274. $guideResult = model('Guide')->where(['admin_id' => $this->auth->id, 'book_id' => $id])->find();
  275. if (isset($guideResult['chapter_idx'])) {
  276. $guide_chapter_idx = $guideResult['chapter_idx'];
  277. } else {
  278. $adminConfigResult = model('AdminConfig')->where(['admin_id' => $this->auth->channel_id])->find();
  279. if (isset($adminConfigResult['book_guide_chapter_idx'])) {
  280. $guide_chapter_idx = $adminConfigResult['book_guide_chapter_idx'];
  281. } else {
  282. $guide_chapter_idx = config('site.book_guide_chapter_idx');
  283. }
  284. }
  285. if($this->auth->agent_id){
  286. $channel_id = $this->auth->agent_id;
  287. }else{
  288. $channel_id = $this->auth->channel_id;
  289. }
  290. $adminConfigResult = model('AdminConfig')->where(['admin_id' => $channel_id])->find();
  291. if(isset($adminConfigResult['guide_domain']) && $adminConfigResult['guide_domain']){
  292. $guide_switch = true;
  293. }
  294. $showAllSelect = 1;
  295. if (in_array($this->group, [7,8])) {
  296. $showAllSelect = 0;
  297. }
  298. $not_ban = 0;
  299. //独家书籍
  300. if (in_array($this->group, [3, 4])) {
  301. $exclusive_books = model('Book')->getExclusiveBookIds($channel_id);
  302. if (in_array($id, $exclusive_books)) {
  303. $not_ban = 1;
  304. }
  305. }
  306. if(in_array($this->group, [7, 8])){
  307. $bind_channel_ids = model('Exclusive')->getAllBindChannelIds();
  308. if(!empty($bind_channel_ids)) {
  309. $adminIds = model("VipAdminBind")->getChannelIds($this->auth->id);//获取服务号id
  310. if(empty(array_intersect($bind_channel_ids,$adminIds)) && empty(array_intersect($adminIds,$bind_channel_ids))){
  311. $exclusive = model('Book')->getExclusiveBookIds($this->auth->id);
  312. if(!empty($exclusive) && in_array($id,$exclusive)){
  313. $not_ban = 1;
  314. }
  315. }else{
  316. $exclusive = model('Book')->getVipExclusiveBookIds($adminIds);
  317. if(!empty($exclusive) && in_array($id,$exclusive)){
  318. $not_ban = 1;
  319. }
  320. }
  321. }
  322. }
  323. $this->assignconfig('not_ban',$not_ban);
  324. $this->assignconfig('guide_switch',$guide_switch);
  325. $this->assignconfig('show_all_select',$showAllSelect);
  326. $this->assignconfig('guide_chapter_idx', $guide_chapter_idx); //关注章节序
  327. $this->assignconfig('free_chapter_num', $book['free_chapter_num'] >0 ? $book['free_chapter_num'] : config('site.book_free_chapter_num')); //关注章节序
  328. return $this->view->fetch();
  329. }
  330. /**
  331. * 获取章节内容
  332. */
  333. public function chapter()
  334. {
  335. $book_id = input('book_id');
  336. if ($this->group != AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN && $this->group != AdminConstants::ADMIN_GROUP_ID_OPERATOR) {
  337. $bind_channel_ids = model('Exclusive')->getAllBindChannelIds();
  338. if(!empty($bind_channel_ids)) {
  339. if($this->group == AdminConstants::ADMIN_GROUP_ID_VIP || $this->group == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR){
  340. $adminIds = model("VipAdminBind")->getChannelIds($this->auth->id);//获取服务号id
  341. if(empty(array_intersect($bind_channel_ids,$adminIds)) && empty(array_intersect($adminIds,$bind_channel_ids))){
  342. $exclusive = model('Book')->getExclusiveBookIds($this->auth->id);
  343. if(!empty($exclusive) && in_array($book_id,$exclusive)){
  344. $this->error('此书籍为渠道独家书籍');
  345. }
  346. }else{
  347. $exclusive = model('Book')->getVipExclusiveBookIds($adminIds);
  348. if(!empty($exclusive) && in_array($book_id,$exclusive)){
  349. $this->error('此书籍为渠道独家书籍');
  350. }
  351. }
  352. }else{
  353. $exclusive = model('Book')->getExclusiveBookIds($this->auth->id);
  354. if(!empty($exclusive) && in_array($book_id,$exclusive)){
  355. $this->error('此书籍为渠道独家书籍');
  356. }
  357. }
  358. }
  359. }
  360. $chapter_id = input('chapter_id');
  361. $data = model('Book')::getChapterInfo($book_id,$chapter_id);
  362. if($data['code']){
  363. $this->error($data['msg']);
  364. }
  365. if ($this->request->isAjax()) {
  366. return json($data);
  367. }
  368. $this->assign('data', $data);
  369. return $this->view->fetch();
  370. }
  371. public function createQRCode(){
  372. $params = $this->request->param();
  373. $book_id = $this->request->get('book_id');
  374. $chapter_id = $this->request->get('chapter_id');
  375. if(isset($params['essay_id'])){
  376. /**
  377. * 图片序号的生成格式
  378. */
  379. $imgSize = [
  380. 1 => [
  381. 'size' => 143, //二维码宽度
  382. 'x' => 376, //水印x
  383. 'y' => 35 //水印y
  384. ],
  385. 2 => [
  386. 'size' => 147, //二维码宽度
  387. 'x' => 374, //水印x
  388. 'y' => 33 //水印y
  389. ],
  390. 3 => [
  391. 'size' => 135, //二维码宽度
  392. 'x' => 372, //水印x
  393. 'y' => 46 //水印y
  394. ],
  395. 4 => [
  396. 'size' => 179, //二维码宽度
  397. 'x' => 356, //水印x
  398. 'y' => 15 //水印y
  399. ],
  400. 5 => [
  401. 'size' => 179, //二维码宽度
  402. 'x' => 356, //水印x
  403. 'y' => 15 //水印y
  404. ],
  405. 6 => [
  406. 'size' => 179, //二维码宽度
  407. 'x' => 356, //水印x
  408. 'y' => 15 //水印y
  409. ]
  410. ];
  411. $qrcodeRes = model('CustomQrcode')->where(['admin_id'=>$this->auth->id,'book_id'=>$params['book_id'],'chapter_id'=>$params['chapter_id']])->select();
  412. $imgList = array();
  413. if($qrcodeRes){
  414. foreach($qrcodeRes as $val){
  415. if(strpos($val['url'], 'mp.weixin.qq.com') === false){
  416. $tmp = explode('_',basename($val['url'],".png"));
  417. $imgList[$tmp[2]] = $val['url'];
  418. }else{
  419. $imgList[0] = $val['url'];
  420. }
  421. }
  422. }
  423. if(!isset($imgList[$params['essay_id']])){
  424. //获取微信永久二维码
  425. $data['index'] = 1;
  426. $last_index = model('CustomQrcode')->field('index')->where(['admin_id'=>$this->auth->id])->order('`index` desc')->find();
  427. if($last_index){
  428. if($last_index['index'] >= 100000){
  429. $this->error(__('WeChat QRCode Max 100000'));
  430. }
  431. $data['index'] = ($last_index['index'] + 1);
  432. }
  433. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  434. $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
  435. $wechat = new WeChatObject($adminConfig);
  436. $officialAccount = $wechat->getOfficialAccount();
  437. $result = $officialAccount->qrcode->forever($data['index']);
  438. if(empty($result) || isset($result['errcode'])){
  439. $this->error(__('Get WeChat QRCode Error'));
  440. }
  441. $data['url'] = $officialAccount->qrcode->url($result['ticket']);
  442. $res = null;
  443. if($params['essay_id'] != 0){
  444. $image_1 = imagecreatefrompng(ROOT_PATH . "public/assets/img/essay/qrcode_{$params['essay_id']}.png"); //底图
  445. // $image_2 = imagecreatefromjpeg($data['url']); //二维码图片
  446. $image_2 = $this->createThumbnail($data['url'],$imgSize[$params['essay_id']]['size'],$imgSize[$params['essay_id']]['size']);
  447. imagecopymerge($image_1, $image_2, $imgSize[$params['essay_id']]['x'], $imgSize[$params['essay_id']]['y'], 0, 0, imagesx($image_2),
  448. imagesy($image_2), 100);
  449. $imgPath = ROOT_PATH . 'public/uploads/qrcode/'; //图片保存路径
  450. if (!file_exists($imgPath)) { //创建文件夹
  451. mkdir($imgPath, 0700, true);
  452. clearstatcache();
  453. }
  454. $imgPath = $imgPath .$book_id.'_'.$chapter_id .'_' . $params['essay_id'] .'_'.$this->auth->id.'_'.time().'.png';
  455. $imgUrl = cdnurl("/uploads/qrcode/" . $book_id .'_'.$chapter_id . '_' . $params['essay_id'] .'_'.$this->auth->id.'_'.time().'.png');
  456. $res = imagepng($image_1, $imgPath);
  457. $data['url'] = $res ? $imgUrl : $data['url'];
  458. imagedestroy($image_1);
  459. imagedestroy($image_2);
  460. }
  461. $data['type'] = 2;
  462. $data['title'] = $params['book_name'].'-'.$params['chapter_name'].'-模板'.$params['essay_id'];
  463. $data['admin_id'] = $this->auth->id;
  464. $data['book_id'] = $params['book_id'];
  465. $data['book_name'] = $params['book_name'];
  466. $data['chapter_id'] = $params['chapter_id'];
  467. if(model('CustomQrcode')->allowField(true)->save($data)){
  468. $this->success("关注二维码生成成功");
  469. }else{
  470. $this->error("生成失败");
  471. }
  472. }else{
  473. $this->success("关注二维码生成成功");
  474. }
  475. }else{
  476. if(!$book_id || !$chapter_id){
  477. $this->error('参数错误!');
  478. }
  479. $qrcodeRes = model('CustomQrcode')->where(['admin_id'=>$this->auth->id,'book_id'=>$params['book_id'],'chapter_id'=>$params['chapter_id']])->select();
  480. $imgList = array();
  481. if($qrcodeRes){
  482. foreach($qrcodeRes as $val){
  483. if(strpos($val['url'], 'mp.weixin.qq.com') === false){
  484. $tmp = explode('_',basename($val['url'],".png"));
  485. $imgList[$tmp[2]] = $val['url'];
  486. }else{
  487. $imgList[0] = $val['url'];
  488. }
  489. }
  490. }
  491. $this->assign('imgList',$imgList);
  492. $this->assign('book_info', $params); //关注章节序
  493. return $this->view->fetch();
  494. }
  495. }
  496. /**
  497. * 生成保持原图纵横比的缩略图,支持.png .jpg .gif
  498. * 缩略图类型统一为.png格式
  499. * $srcFile 原图像文件名称
  500. * $toW 缩略图宽
  501. * $toH 缩略图高
  502. * @return bool
  503. */
  504. private function createThumbnail($srcFile, $toW, $toH)
  505. {
  506. $toW += 20;
  507. $toH += 20;
  508. $info = "";
  509. //返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。
  510. //失败返回false并产生警告。
  511. $data = getimagesize($srcFile, $info);
  512. if (!$data)
  513. return false;
  514. //将文件载入到资源变量im中
  515. switch ($data[2]) //1-GIF,2-JPG,3-PNG
  516. {
  517. case 1:
  518. if(!function_exists("imagecreatefromgif"))
  519. {
  520. echo "the GD can't support .gif, please use .jpeg or .png! <a href='javascript:history.back();'>back</a>";
  521. exit();
  522. }
  523. $im = imagecreatefromgif($srcFile);
  524. break;
  525. case 2:
  526. if(!function_exists("imagecreatefromjpeg"))
  527. {
  528. echo "the GD can't support .jpeg, please use other picture! <a href='javascript:history.back();'>back</a>";
  529. exit();
  530. }
  531. $im = imagecreatefromjpeg($srcFile);
  532. break;
  533. case 3:
  534. $im = imagecreatefrompng($srcFile);
  535. break;
  536. }
  537. //计算缩略图的宽高
  538. $srcW = imagesx($im);
  539. $srcH = imagesy($im);
  540. $toWH = $toW / $toH;
  541. $srcWH = $srcW / $srcH;
  542. if ($toWH <= $srcWH)
  543. {
  544. $ftoW = $toW;
  545. $ftoH = (int)($ftoW * ($srcH / $srcW));
  546. }
  547. else
  548. {
  549. $ftoH = $toH;
  550. $ftoW = (int)($ftoH * ($srcW / $srcH));
  551. }
  552. if (function_exists("imagecreatetruecolor"))
  553. {
  554. $ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像
  555. if ($ni)
  556. {
  557. //重采样拷贝部分图像并调整大小 可保持较好的清晰度
  558. imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  559. }
  560. else
  561. {
  562. //拷贝部分图像并调整大小
  563. $ni = imagecreate($ftoW, $ftoH);
  564. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  565. }
  566. }
  567. else
  568. {
  569. $ni = imagecreate($ftoW, $ftoH);
  570. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  571. }
  572. ImageDestroy($im);
  573. return $ni;
  574. }
  575. /**
  576. * 设置关注章节
  577. */
  578. public function setGuideChapterIdx()
  579. {
  580. $book_id = $this->request->get('book_id');
  581. $chapter_idx = $this->request->get('chapter_idx');
  582. if (!$book_id || !$chapter_idx) {
  583. $this->error('参数错误!');
  584. }
  585. $where = [
  586. 'book_id' => $book_id,
  587. 'admin_id' => $this->auth->id
  588. ];
  589. $guideModel = model('Guide');
  590. $row = $guideModel::get($where);
  591. if ($row) {
  592. $row->chapter_idx = $chapter_idx;
  593. $res = $row->save();
  594. } else {
  595. $data = array_merge($where, ['chapter_idx' => $chapter_idx]);
  596. $res = model('Guide')->save($data);
  597. }
  598. if ($res) {
  599. $redis = Redis::instance();
  600. $key = 'GUIDE:' . $this->auth->id;
  601. $redis->del($key);
  602. $this->success('操作成功!', '', ['guide_chapter_idx' => $chapter_idx]);
  603. } else {
  604. $this->error('保存失败!');
  605. }
  606. }
  607. }