ReadRecordController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Http\Controllers\Wap\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Wap\BaseController;
  5. use App\Modules\Book\Services\BookConfigService;
  6. use App\Modules\Book\Services\UserShelfBooksService;
  7. use App\Http\Controllers\Wap\User\Transformers\ReadRecordTransformer;
  8. use App\Modules\User\Services\ReadRecordService;
  9. use Hashids;
  10. use Cookie;
  11. use Redis;
  12. use Log;
  13. class ReadRecordController extends BaseController
  14. {
  15. /**
  16. * @apiDefine ReadRecord 阅读记录
  17. */
  18. /**
  19. * @apiVersion 1.0.0
  20. * @apiDescription 获取阅读记录
  21. * @api {get} readrecord 获取阅读记录
  22. * @apiGroup ReadRecord
  23. * @apiName index
  24. * @apiSuccess {int} code 状态码
  25. * @apiSuccess {String} msg 信息
  26. * @apiSuccess {object} data 结果集
  27. * @apiSuccess {Int} data.book_name 书名
  28. * @apiSuccess {String} data.chapter_name 章节名
  29. * @apiSuccess {Int} data.bid bid
  30. * @apiSuccess {Int} data.time 时间
  31. * @apiSuccess {Int} data.cid 章节id
  32. * @apiSuccess {Int} data.chapter_name 章节名
  33. * @apiSuccess {Int} data.cover 封面
  34. * @apiSuccess {Int} data.last_chapter 最后一张
  35. * @apiSuccessExample {json} Success-Response:
  36. * HTTP/1.1 200 OK
  37. * {
  38. * code: 0,
  39. * msg: "",
  40. * data:[
  41. * {
  42. * book_name: "我来好好爱你",
  43. * bid: 2,
  44. * cid: 10402,
  45. * time: 1511783120,
  46. * chapter_name: "你言重了"
  47. * },
  48. * {
  49. * book_name: "京华烟云",
  50. * bid: 1,
  51. * cid: 4,
  52. * time: 1511783068,
  53. * chapter_name: "背水一战"
  54. * }
  55. * ]
  56. */
  57. public function index(Request $request){
  58. if(!$this->checkUid()){
  59. return response()->error('NOT_LOGIN');
  60. }
  61. $this->pageRecord('recent');
  62. $res = ReadRecordService::getReadRecord($this->uid);
  63. if($res){
  64. $id_arr = [];
  65. foreach ($res as $key => $value) {
  66. $id_arr[] = $value['bid'];
  67. }
  68. $book = BookConfigService::getBooksByIds($id_arr);
  69. foreach ($res as $key => &$value) {
  70. $value['cover'] = '';
  71. $value['last_chapter'] = 0;
  72. foreach ($book as $val) {
  73. if($value['bid'] == $val->bid){
  74. $value['cover'] = $val->cover;
  75. $value['last_chapter'] = $val->last_chapter;
  76. break;
  77. }
  78. }
  79. }
  80. $shelf = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  81. foreach ($res as &$v){
  82. $v['is_on_user_shelf'] = 0;
  83. foreach ($shelf as $val){
  84. if($v['bid'] == $val->bid){
  85. $v['is_on_user_shelf'] = 1;
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. usort($res,function($a,$b){
  92. if($a['time'] >= $b['time']) return -1;
  93. return 1;
  94. });
  95. $res = json_encode($res);
  96. $res = json_decode($res);
  97. return response()->collection(new ReadRecordTransformer(),$res);
  98. }
  99. /**
  100. * @apiVersion 1.0.0
  101. * @apiDescription 添加阅读记录
  102. * @api {post} readrecord 添加阅读记录
  103. * @apiGroup ReadRecord
  104. * @apiName addReadRecord
  105. *
  106. * @apiParam {Int} book_name 书名
  107. * @apiParam {String} chapter_name 章节名
  108. * @apiParam {Int} bid bid
  109. * @apiParam {Int} cid 章节id
  110. * @apiSuccess {int} code 状态码
  111. * @apiSuccess {String} msg 信息
  112. * @apiSuccess {object} data 结果集
  113. * @apiSuccessExample {json} Success-Response:
  114. * HTTP/1.1 200 OK
  115. * {
  116. * code: 0,
  117. * msg: "",
  118. * data:{}
  119. *
  120. */
  121. public function addReadRecord(Request $request){
  122. if(!$this->checkUid()){
  123. return response()->error('NOT_LOGIN');
  124. }
  125. $param = $request->except('_url');
  126. if(checkParam($param,['bid','cid','chapter_name'])){
  127. return response()->error('LACK_PARAM');
  128. }
  129. //Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  130. $param['uid'] = $this->uid;
  131. $param['bid'] = Hashids::decode($param['bid'])[0];
  132. $record_info = Redis::hget('book_read:'.$this->uid,$param['bid']);
  133. $param['book_name'] = '';
  134. if($record_info){
  135. //$param['book_name'] = explode('_',$record_info)[1];
  136. }
  137. ReadRecordService::addReadRecord($param);
  138. return response()->success();
  139. }
  140. /**
  141. * @apiVersion 1.0.0
  142. * @apiDescription 删除阅读记录
  143. * @api {get} readrecord/delete 删除阅读记录
  144. * @apiGroup ReadRecord
  145. * @apiName delReadRecord
  146. * @apiParam {Int} bid bid
  147. * @apiSuccess {int} code 状态码
  148. * @apiSuccess {String} msg 信息
  149. * @apiSuccess {object} data 结果集
  150. * @apiSuccessExample {json} Success-Response:
  151. * HTTP/1.1 200 OK
  152. * {
  153. * code: 0,
  154. * msg: "",
  155. * data:{}
  156. *
  157. */
  158. public function delReadRecord(Request $request){
  159. if(!$this->checkUid()){
  160. return response()->error('NOT_LOGIN');
  161. }
  162. $param = $request->except('_url');
  163. if(checkParam($param,['bid'])){
  164. return response()->error('LACK_PARAM');
  165. }
  166. $param['bid'] = Hashids::decode($param['bid'])[0];
  167. ReadRecordService::delReadRecord($this->uid,$param['bid']);
  168. return response()->success();
  169. }
  170. //继续阅读
  171. public function latestRead(Request $request){
  172. $this->pageRecord('continue');
  173. $from = $request->input('from');
  174. if($from){
  175. Cookie::queue('fromcustomermsgenter',$from);
  176. $key = "fromcustomermsgenter:distribution_channel_id:".$this->distribution_channel_id.'from:'.$from;
  177. Redis::hincrby($key,date('Y-m-d'),1);
  178. }
  179. $send_order_id = Cookie::get('send_order_id');
  180. $continue_cookie = Cookie::get('send_order_continue');
  181. if($send_order_id && !$continue_cookie){
  182. Redis::hincrby("send_order:continue:{$send_order_id}",date('Y-m-d'),1);
  183. Redis::hincrby("send_order:continue:{$send_order_id}",'total',1);
  184. Cookie::queue('send_order_continue',$send_order_id, env('U_COOKIE_EXPIRE'), null, null, false, false);
  185. }
  186. $last = Redis::hget('book_read:'.$this->uid, 'last_read');
  187. if($last){
  188. $last_arr = explode('_',$last);
  189. if(isset($last_arr[0]) && $last_arr[0] && isset($last_arr[1]) && $last_arr[1]){
  190. $bid = Hashids::encode($last_arr[0]);
  191. $url = parse_url(secure_url('/'))['scheme'].'://'._domain().'/reader?bid='.$bid.'&cid='.$last_arr[1];
  192. Log::info('redis-------------'.$url);
  193. return redirect()->to($url);
  194. }
  195. }
  196. Log::info('default-------------'.parse_url(secure_url('/'))['scheme'].'://'._domain());
  197. return redirect()->to(parse_url(secure_url('/'))['scheme'].'://'._domain());
  198. }
  199. private function pageRecord(string $from){
  200. $is_open_page_record = env('IS_OPEN_PAGE_RECORD');
  201. if($is_open_page_record && $this->distribution_channel_id == 123){
  202. ReadRecordService::ReadRecordStatistical($this->uid,$this->distribution_channel_id,$from);
  203. }
  204. }
  205. }