ReadRecordController.php 8.0 KB

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