ReadRecordService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 10:42
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\Book\Services\BookService;
  10. use App\Modules\User\Models\ReadRecordFromRedis;
  11. use Redis;
  12. use Hashids;
  13. use App\Modules\Book\Services\BookConfigService;
  14. use App\Modules\Book\Models\Chapter;
  15. use App\Modules\User\Models\ReadLog;
  16. use DB;
  17. class ReadRecordService
  18. {
  19. public static function getBookReadRecordStatic(int $uid, int $bid)
  20. {
  21. $record = Redis::hGet('book_read:' . $uid, $bid);
  22. if ($record) {
  23. $cid = explode('_', $record)[0];
  24. $name = self::cid2ChapterName($cid,0);
  25. return ['record_chapter_id' => $cid, 'record_chapter_name' => $name];
  26. }
  27. return false;
  28. }
  29. /**
  30. * 删除最近阅读记录
  31. * @param int $uid
  32. * @param array $bids
  33. */
  34. public static function delReadRecordStatic(int $uid, array $bids)
  35. {
  36. $key = 'book_read:' . $uid;
  37. $last_record = explode('_', Redis::hGet($key, 'last_read'));
  38. $last_record_bid = $last_record[0];
  39. $is_del_last = false;
  40. foreach ($bids as $bid) {
  41. if ($bid == $last_record_bid) {
  42. $is_del_last = true;
  43. }
  44. Redis::hDel($key, $bid);
  45. }
  46. if ($is_del_last) {
  47. self::reSetLastRecord($uid);
  48. }
  49. }
  50. /**
  51. * 重置用户最近阅读记录
  52. * @param int $uid
  53. */
  54. public static function reSetLastRecord($uid)
  55. {
  56. $key = 'book_read:' . $uid;
  57. $alls = Redis::hGetAll($key);
  58. $has_record = false;
  59. $last_timestamp = 0;
  60. $last_record = '';
  61. foreach ($alls as $k => $v) {
  62. if (is_numeric($k)) {
  63. $has_record = true;
  64. $record = explode('_', $v);
  65. $timestamp = $record[1];
  66. if ($last_timestamp < $timestamp) {
  67. $last_record = $k . '_' . $v;
  68. $last_timestamp = $timestamp;
  69. }
  70. }
  71. }
  72. if ($last_record) {
  73. Redis::hSet($key, 'last_read', $last_record);
  74. }
  75. if (!$has_record) {
  76. Redis::hDel($key, 'last_read');
  77. }
  78. }
  79. //阅读记录数
  80. const RECORD_COUNT = 50;
  81. private static $not_uid_key = ['last_read', 'send_order_id', 'sign_count', 'sign_counts', 'sign_info', 'sign_day', 'smart_push', 'inner_send_order_id', 'gxhp', 'property', 'bind_phone_status', 'ua', 'sign_version', 'new_outer', 'new_inner', 'new_total', 'next_push_hour', 'person_account_id'];
  82. /**
  83. * 获取 升级版
  84. * @param $uid
  85. * @return array
  86. */
  87. public static function getReadRecord($uid)
  88. {
  89. self::delTheLastRecord($uid);
  90. $read_bids = Redis::hgetall('book_read:' . $uid);
  91. $res = [];
  92. $i = 0;
  93. foreach ($read_bids as $key => $v) {
  94. if (in_array($key, self::$not_uid_key)) {
  95. continue;
  96. }
  97. $record = explode('_', $v);
  98. $latest_read_cid = $record[0];
  99. $latest_read_time = $record[count($record) - 1];
  100. $channel_name = '';
  101. $book_name = self::bid2BookName($key,$channel_name);
  102. $change_chapter_name = 0;
  103. list($is_split,$is_change_chapter_name) = BookService::splitContent($key);
  104. if($is_split && ($channel_name == '男频' || $is_change_chapter_name) ){
  105. $change_chapter_name = 1;
  106. }
  107. $chapter_name = self::cid2ChapterName($latest_read_cid,$change_chapter_name);
  108. $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time, 'chapter_name' => $chapter_name];
  109. $i++;
  110. }
  111. usort($res, function ($a, $b) {
  112. if ($a['time'] >= $b['time']) return -1;
  113. return 1;
  114. });
  115. return collect($res)->sortByDesc('time')->values()->all();
  116. }
  117. /**
  118. * 添加阅读记录升级版
  119. * @param $param
  120. * @return bool
  121. * @throws \Exception
  122. */
  123. public static function addReadRecord($param)
  124. {
  125. // 第一章不计入阅读记录
  126. $sequence = (int)getProp($param, 'sequence');
  127. // if ($sequence <= 1) {
  128. // return false;
  129. // }
  130. $uid = $param['uid'];
  131. $bid = $param['bid'];
  132. $cid = $param['cid'];
  133. $book_name = getProp($param, 'book_name');
  134. $chapter_name = getProp($param, 'chapter_name');
  135. $book_key = 'wap:string:book:' . $bid;
  136. $chapter_key = 'wap:string:chapter:' . $cid;
  137. if ($book_name) {
  138. Redis::setex($book_key, 3600, $book_name);
  139. }
  140. if ($chapter_name) {
  141. Redis::setex($chapter_key, 3600, $chapter_name);
  142. }
  143. Redis::hmset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time(), $bid, "{$cid}_" . time(), 'next_push_hour', 8);
  144. $num = random_int(1, 100);
  145. if ($num <= 3) {
  146. self::delTheLastRecord($uid);
  147. }
  148. return true;
  149. }
  150. /**
  151. * 获取最近一条阅读记录(升级版)
  152. * @param $uid
  153. * @return array
  154. */
  155. public static function getFirstReadRecord($uid)
  156. {
  157. self::delBookBase($uid);
  158. $record = Redis::hget('book_read:' . $uid, 'last_read');
  159. if ($record) {
  160. $record_arr = explode('_', $record);
  161. $bid = $record_arr[0];
  162. $bid = Hashids::encode($bid);
  163. $cid = $record_arr[1];
  164. $time = $record_arr[2];
  165. $book_info = BookConfigService::getBookById($bid);
  166. $book_name = isset($book_info->book_name) ? $book_info->book_name : '';
  167. $cover = isset($book_info->cover) ? $book_info->cover : '';
  168. $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '';
  169. $res = [
  170. 'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
  171. 'book_name' => $book_name,
  172. 'cover' => $cover,
  173. 'channel_name' => $channel_name,
  174. 'time' => $time
  175. ];
  176. return $res;
  177. }
  178. return [];
  179. }
  180. /**
  181. * 获取简单阅读记录
  182. * @param $uid
  183. * @return int
  184. */
  185. public static function getSimpleFirstReadRecord($uid)
  186. {
  187. try {
  188. $record = Redis::hget('book_read:' . $uid, 'last_read');
  189. if ($record) {
  190. $record_arr = explode('_', $record);
  191. $bid = $record_arr[0];
  192. return (int)$bid;
  193. }
  194. } catch (\Exception $e) {
  195. }
  196. return 0;
  197. }
  198. /**
  199. * 获取某本书的阅读记录
  200. */
  201. public static function getRecordByUidBid($uid, $bid)
  202. {
  203. return Redis::hget('book_read:' . $uid, $bid);
  204. }
  205. /**
  206. * 根据bid获取书名
  207. * @param $bid
  208. * @return bool|null|string
  209. */
  210. public static function bid2BookName($bid,&$channel_name)
  211. {
  212. $book_key = 'wap:string:book:' . $bid;
  213. $book_name = Redis::get($book_key);
  214. Redis::EXPIRE($book_key, 3600);
  215. if (!$book_name) {
  216. $book_name = '';
  217. $book_info = BookConfigService::getBookById($bid);
  218. if ($book_info && isset($book_info->book_name)) {
  219. $book_name = $book_info->book_name;
  220. $channel_name = $book_info->channel_name;
  221. }
  222. }
  223. return $book_name;
  224. }
  225. /**
  226. * 根据cid获取章节名
  227. * @param $cid
  228. * @return bool|null|string
  229. */
  230. public static function cid2ChapterName($cid,$change_chapter_name)
  231. {
  232. $chapter_key = 'wap:string:chapter:' . $cid;
  233. $chapter_name = Redis::get($chapter_key);
  234. Redis::EXPIRE($chapter_key, 3600);
  235. if (!$chapter_name) {
  236. $chapter_name = '';
  237. $chapter_info = Chapter::getChapterNameById($cid);
  238. if ($chapter_info && isset($chapter_info->name)) {
  239. //$chapter_name = $chapter_info->name;
  240. if($change_chapter_name){
  241. $chapter_name = '第'.$chapter_info->sequence.'章';
  242. }else{
  243. $chapter_name = $chapter_info->name;
  244. }
  245. }
  246. }
  247. return $chapter_name;
  248. }
  249. public static function delBookBase($uid)
  250. {
  251. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  252. if ($base_record) {
  253. Redis::del('book_base:' . $uid);
  254. Redis::hset('book_read:' . $uid, 'last_read', $base_record);
  255. }
  256. }
  257. /**
  258. * 获取简单阅读记录只有bid
  259. * @param int $uid
  260. * @return array
  261. */
  262. public static function getSimpleReadRecord(int $uid): array
  263. {
  264. $read_bids = Redis::hgetall('book_read:' . $uid);
  265. $res = [];
  266. if (!$read_bids) {
  267. return $res;
  268. }
  269. foreach ($read_bids as $key => $v) {
  270. if (in_array($key, self::$not_uid_key)) {
  271. continue;
  272. }
  273. array_push($res, $key);
  274. }
  275. return $res;
  276. }
  277. /**
  278. * 获取当前的send_order_id
  279. * @param int $uid
  280. * @return int
  281. */
  282. public static function getSendOrderId(int $uid)
  283. {
  284. try {
  285. $send_order_id = Redis::hget('book_read:' . $uid, 'send_order_id');
  286. if ($send_order_id)
  287. return (int)$send_order_id;
  288. } catch (\Exception $e) {
  289. }
  290. return 0;
  291. }
  292. /**
  293. * 签到日期
  294. * @param int $uid
  295. * @return mixed
  296. */
  297. public static function getSignDay(int $uid)
  298. {
  299. try {
  300. return Redis::hget('book_read:' . $uid, 'sign_day');
  301. } catch (\Exception $e) {
  302. }
  303. return -1;
  304. }
  305. public static function setSignDay(int $uid)
  306. {
  307. return Redis::hset('book_read:' . $uid, 'sign_day', date('Y-m-d'));
  308. }
  309. /**
  310. * 签到次数和日期
  311. * @param int $uid
  312. */
  313. public static function sign(int $uid, bool $is_incr)
  314. {
  315. try {
  316. if ($is_incr) {
  317. Redis::hincrby('book_read:' . $uid, 'sign_counts', 1);
  318. } else {
  319. self::setSignCount($uid, 1);
  320. }
  321. self::setSignDay($uid);
  322. } catch (\Exception $e) {
  323. \Log::info('sign_ept:' . $e->getMessage());
  324. }
  325. return;
  326. }
  327. /**
  328. * @param int $uid
  329. * @return int
  330. */
  331. public static function getSignCount(int $uid)
  332. {
  333. try {
  334. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  335. if ($count) {
  336. return $count;
  337. }
  338. } catch (\Exception $e) {
  339. }
  340. return 0;
  341. }
  342. /**
  343. * 获取简单签到次数
  344. * @param int $uid
  345. * @return int
  346. */
  347. public static function getSignCountSimple(int $uid)
  348. {
  349. try {
  350. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  351. if ($count) {
  352. return (int)$count;
  353. }
  354. return 0;
  355. } catch (\Exception $e) {
  356. }
  357. return 0;
  358. }
  359. public static function setSignCount(int $uid, int $count)
  360. {
  361. Redis::hset('book_read:' . $uid, 'sign_counts', $count);
  362. }
  363. public static function setSignInfo(int $uid, string $info)
  364. {
  365. Redis::hset('book_read:' . $uid, 'sign_info', $info);
  366. }
  367. public static function getByField(int $uid, $field)
  368. {
  369. try {
  370. return Redis::hget('book_read:' . $uid, $field);
  371. } catch (\Exception $e) {
  372. }
  373. return '';
  374. }
  375. public static function getByMultiField(int $uid, ...$field)
  376. {
  377. try {
  378. return Redis::hmget('book_read:' . $uid, $field);
  379. } catch (\Exception $e) {
  380. }
  381. return '';
  382. }
  383. public static function setByField(int $uid, $field, $value)
  384. {
  385. if (!in_array($field, self::$not_uid_key)) {
  386. return false;
  387. }
  388. try {
  389. return Redis::hset('book_read:' . $uid, $field, $value);
  390. } catch (\Exception $e) {
  391. }
  392. return '';
  393. }
  394. public static function setByMultiField(int $uid, $kv)
  395. {
  396. $keys = array_keys($kv);
  397. foreach ($keys as $field) {
  398. if (!in_array($field, self::$not_uid_key)) {
  399. return false;
  400. }
  401. }
  402. try {
  403. return Redis::hmset('book_read:' . $uid, $kv);
  404. } catch (\Exception $e) {
  405. }
  406. return '';
  407. }
  408. //删除多余的阅读纪律
  409. public static function delTheLastRecord($uid)
  410. {
  411. $length = Redis::hlen('book_read:' . $uid);
  412. if ($length <= self::RECORD_COUNT + count(self::$not_uid_key)) {
  413. return;
  414. }
  415. $read_bids = Redis::hgetall('book_read:' . $uid);
  416. $i = 0;
  417. foreach ($read_bids as $key => $v) {
  418. if (in_array($key, self::$not_uid_key)) {
  419. continue;
  420. }
  421. $record = explode('_', $v);
  422. $latest_read_cid = $record[0];
  423. $latest_read_time = $record[count($record) - 1];
  424. $res[$i++] = ['bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time];
  425. }
  426. usort($res, function ($a, $b) {
  427. if ($a['time'] >= $b['time']) return -1;
  428. return 1;
  429. });
  430. $j = 0;
  431. foreach ($res as $v) {
  432. if ($j++ >= self::RECORD_COUNT) {
  433. Redis::hdel('book_read:' . $uid, $v['bid']);
  434. }
  435. }
  436. }
  437. /**
  438. * 添加用户的阅读记录
  439. */
  440. public static function addReadLog(int $uid, array $data)
  441. {
  442. $log = ReadLog::model($uid);
  443. $log->create($data);
  444. }
  445. }