table($table)->where('uid', $uid)->groupBy('bid') ->select('bid', DB::raw("(select max(created_at) from {$table} as a where a.bid = {$table}.bid) as read_time"), DB::raw("(select max(sequence) from {$table} as a where a.bid = {$table}.bid) as sequence"))->get(); $data = []; foreach ($result as $item) { $book_chapter = DB::table('chapters as c')->leftJoin('books as b', 'b.id', 'c.bid') ->where(['c.bid'=>getProp($item, 'bid'), 'c.sequence'=>getProp($item, 'sequence'), 'c.is_check'=>1, 'c.is_deleted'=>0]) ->select('c.bid', 'b.name as book_name', 'c.id as cid', 'c.name as chapter_name', 'c.sequence')->first(); if ($book_chapter) { $book_chapter = (array)$book_chapter; $book_chapter['read_time'] = getProp($item, 'read_time'); } Redis::hset($cacheKey, $book_chapter['bid'], json_encode($book_chapter, JSON_UNESCAPED_UNICODE)); $data[] = $book_chapter; } Redis::expire($cacheKey, BaseConst::ONE_WEEK_SECONDS); } return $data; } /** * 获取最近阅读数据(all) * @param $uid * @return mixed|array */ public static function getRecentBooksAll($uid) { if (empty($uid)) { return false; } $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]); $result = Redis::hgetall($cacheKey); return $result ? $result : []; } /** * 删除最近阅读数据 * @param $uid * @return mixed */ public static function delRecentBooks($uid) { if (empty($uid)) { return false; } $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]); return Redis::del($cacheKey); } /** * 设置当前阅读章节 * @param $uid * @param $data * @return false */ public static function setCurrentChapter($uid, $data) { if (empty($uid)) { return false; } $cacheKey = Utils::getCacheKey('user.current_chapter', [$uid]); return Redis::set($cacheKey, json_encode($data, JSON_UNESCAPED_UNICODE)); } /** * 获取当前阅读章节 * @param $uid * @return array|false|mixed */ public static function getCurrentChapter($uid) { if (empty($uid)) { return false; } $cacheKey = Utils::getCacheKey('user.current_chapter', [$uid]); $result = Redis::get($cacheKey); return $result ? json_decode($result, true) : []; } /** * 删除当前阅读章节 * @param $uid * @return mixed */ public static function delCurrentChapter($uid) { if (empty($uid)) { return false; } $cacheKey = Utils::getCacheKey('user.current_chapter', [$uid]); return Redis::del($cacheKey); } /** * 设置今日好书分享数 * @param $uid * @return bool */ public static function setTodayShareNum($uid) { if (empty($uid)) { return false; } $cacheKey = Utils::getCacheKey('user.share_num', [$uid]); if (Redis::exists($cacheKey)) { Redis::incr($cacheKey); }else { $expire_at = strtotime(date('Y-m-d 23:59:59')) - time(); Redis::setex($cacheKey, $expire_at, 1); } return true; } /** * 获取今日好书分享数 * @param $uid * @return int */ public static function getTodayShareNum($uid) { if (empty($uid)) { return 0; } $cacheKey = Utils::getCacheKey('user.share_num', [$uid]); $share_num = Redis::get($cacheKey); return $share_num ? $share_num : 0; } }