ReadRecordController.php 7.9 KB

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