ReadRecordService.php 14 KB

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