ReadRecordController.php 8.1 KB

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