ReadRecordController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $user = $this->_user_info;
  65. $is_check_from_db = (time()-strtotime($user->created_at) >5*30*86400);
  66. $res = ReadRecordService::getReadRecord($this->uid,$is_check_from_db);
  67. if($res){
  68. $id_arr = [];
  69. foreach ($res as $key => $value) {
  70. $id_arr[] = $value['bid'];
  71. }
  72. $book = BookConfigService::getBooksByIds($id_arr);
  73. foreach ($res as $key => &$value) {
  74. $value['cover'] = '';
  75. $value['last_chapter'] = 0;
  76. foreach ($book as $val) {
  77. if($value['bid'] == $val->bid){
  78. $value['cover'] = $val->cover;
  79. $value['last_chapter'] = $val->last_chapter;
  80. break;
  81. }
  82. }
  83. }
  84. $shelf = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  85. foreach ($res as &$v){
  86. $v['is_on_user_shelf'] = 0;
  87. foreach ($shelf as $val){
  88. if($v['bid'] == $val->bid){
  89. $v['is_on_user_shelf'] = 1;
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. usort($res,function($a,$b){
  96. if($a['time'] >= $b['time']) return -1;
  97. return 1;
  98. });
  99. $res = json_encode($res);
  100. $res = json_decode($res);
  101. foreach ($res as $key=>&$each){
  102. $rec = ShareUsersService::getUnlocked($this->uid,$each->bid);// ($this->uid,$each->bid);
  103. if($rec) {
  104. $each->is_advertise_sub=1;
  105. }else {
  106. $each->is_advertise_sub=0;
  107. }
  108. }
  109. return response()->collection(new ReadRecordTransformer(),$res);
  110. }
  111. /**
  112. * @apiVersion 1.0.0
  113. * @apiDescription 添加阅读记录
  114. * @api {post} readrecord 添加阅读记录
  115. * @apiGroup ReadRecord
  116. * @apiName addReadRecord
  117. *
  118. * @apiParam {Int} book_name 书名
  119. * @apiParam {String} chapter_name 章节名
  120. * @apiParam {Int} bid bid
  121. * @apiParam {Int} cid 章节id
  122. * @apiSuccess {int} code 状态码
  123. * @apiSuccess {String} msg 信息
  124. * @apiSuccess {object} data 结果集
  125. * @apiSuccessExample {json} Success-Response:
  126. * HTTP/1.1 200 OK
  127. * {
  128. * code: 0,
  129. * msg: "",
  130. * data:{}
  131. *
  132. */
  133. public function addReadRecord(Request $request){
  134. if(!$this->checkUid()){
  135. return response()->error('NOT_LOGIN');
  136. }
  137. $param = $request->except('_url');
  138. if(checkParam($param,['bid','cid','chapter_name'])){
  139. return response()->error('LACK_PARAM');
  140. }
  141. //Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  142. $param['uid'] = $this->uid;
  143. $param['bid'] = Hashids::decode($param['bid'])[0];
  144. $record_info = Redis::hget('book_read:'.$this->uid,$param['bid']);
  145. $param['book_name'] = '';
  146. if($record_info){
  147. //$param['book_name'] = explode('_',$record_info)[1];
  148. }
  149. ReadRecordService::addReadRecord($param);
  150. return response()->success();
  151. }
  152. /**
  153. * @apiVersion 1.0.0
  154. * @apiDescription 删除阅读记录
  155. * @api {get} readrecord/delete 删除阅读记录
  156. * @apiGroup ReadRecord
  157. * @apiName delReadRecord
  158. * @apiParam {Int} bid bid
  159. * @apiSuccess {int} code 状态码
  160. * @apiSuccess {String} msg 信息
  161. * @apiSuccess {object} data 结果集
  162. * @apiSuccessExample {json} Success-Response:
  163. * HTTP/1.1 200 OK
  164. * {
  165. * code: 0,
  166. * msg: "",
  167. * data:{}
  168. *
  169. */
  170. public function delReadRecord(Request $request){
  171. if(!$this->checkUid()){
  172. return response()->error('NOT_LOGIN');
  173. }
  174. $param = $request->except('_url');
  175. if(checkParam($param,['bid'])){
  176. return response()->error('LACK_PARAM');
  177. }
  178. $param['bid'] = Hashids::decode($param['bid'])[0];
  179. ReadRecordService::delReadRecord($this->uid,$param['bid']);
  180. return response()->success();
  181. }
  182. //继续阅读
  183. public function latestRead(Request $request){
  184. $this->pageRecord('continue');
  185. $from = $request->input('from');
  186. if($from){
  187. Cookie::queue('fromcustomermsgenter',$from);
  188. $key = "fromcustomermsgenter:distribution_channel_id:".$this->distribution_channel_id.'from:'.$from;
  189. Redis::hincrby($key,date('Y-m-d'),1);
  190. }
  191. $send_order_id = Cookie::get('send_order_id');
  192. $continue_cookie = Cookie::get('send_order_continue');
  193. if($send_order_id && !$continue_cookie){
  194. Redis::hincrby("send_order:continue:{$send_order_id}",date('Y-m-d'),1);
  195. Redis::hincrby("send_order:continue:{$send_order_id}",'total',1);
  196. Cookie::queue('send_order_continue',$send_order_id, env('U_COOKIE_EXPIRE'), null, null, false, false);
  197. }
  198. $last = Redis::hget('book_read:'.$this->uid, 'last_read');
  199. if($last){
  200. $last_arr = explode('_',$last);
  201. if(isset($last_arr[0]) && $last_arr[0] && isset($last_arr[1]) && $last_arr[1]){
  202. $bid = Hashids::encode($last_arr[0]);
  203. $url = parse_url(secure_url('/'))['scheme'].'://'._domain().'/reader?bid='.$bid.'&cid='.$last_arr[1];
  204. Log::info('redis-------------'.$url);
  205. return redirect()->to($url);
  206. }
  207. }
  208. Log::info('default-------------'.parse_url(secure_url('/'))['scheme'].'://'._domain());
  209. return redirect()->to(parse_url(secure_url('/'))['scheme'].'://'._domain());
  210. }
  211. private function pageRecord(string $from){
  212. $is_open_page_record = env('IS_OPEN_PAGE_RECORD');
  213. if($is_open_page_record && $this->distribution_channel_id == 123){
  214. ReadRecordService::ReadRecordStatistical($this->uid,$this->distribution_channel_id,$from);
  215. }
  216. }
  217. }