ChapterController.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Modules\Push\Models\QappPushTask;
  4. use App\Modules\SendOrder\Models\SendOrder;
  5. use App\Modules\SendOrder\Services\SendOrderService;
  6. use App\Modules\Statistic\Services\WapVisitStatService;
  7. use App\Modules\User\Models\QappUser;
  8. use App\Modules\User\Services\UserBookCombinationConfigService;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\QuickApp\BaseController;
  11. use Redis;
  12. use App\Modules\Book\Services\ChapterService;
  13. use App\Modules\User\Services\ReadRecordService;
  14. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
  15. use App\Modules\Book\Services\BookConfigService;
  16. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
  17. use App\Jobs\UserRententionJob;
  18. use App\Modules\Book\Services\BookService;
  19. use App\Modules\Subscribe\Services\BookOrderService;
  20. use App\Modules\Subscribe\Services\ChapterOrderService;
  21. use App\Modules\Subscribe\Services\YearOrderService;
  22. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  23. use App\Modules\Subscribe\Services\ChapterReminderService;
  24. use App\Modules\User\Services\UserDeepReadTagService;
  25. use App\Modules\UserTask\Services\BaseTask;
  26. use App\Modules\UserTask\Services\UserTaskService;
  27. use App\Modules\SendOrder\Models\QappSendOrder;
  28. class ChapterController extends BaseController
  29. {
  30. private $book_info;
  31. private $push_id;
  32. public function getCatalog(Request $request, $bid)
  33. {
  34. $this->push_id = $request->header('x-pushid', '');
  35. $bid = BookService::decodeBidStatic($bid);
  36. $lists = ChapterService::getChapterLists($bid);
  37. $book_info = BookConfigService::getBookById($bid);
  38. if (!$book_info) {
  39. return response()->error('PARAM_ERROR');
  40. }
  41. $this->book_info = $book_info;
  42. $lists = $this->getChapterCatalog($bid, $lists, $book_info);
  43. return response()->collection(new ChapterListTransformer, $lists);
  44. }
  45. public function getCatalogPerPage(Request $request, $bid)
  46. {
  47. $this->push_id = $request->header('x-pushid', '');
  48. $bid = BookService::decodeBidStatic($bid);
  49. $book_info = BookConfigService::getBookById($bid);
  50. if (!$book_info) {
  51. return response()->error('PARAM_ERROR');
  52. }
  53. $this->book_info = $book_info;
  54. $page_size = $request->input('page_size', 15);
  55. // if ($page_size >= 100) $page_size = 100;
  56. if ($page_size >= 20) $page_size = 20;
  57. // $page_size = 15;
  58. $res = ChapterService::getChapterListsPage($bid, $page_size);
  59. $lists = $this->getChapterCatalog($bid, $res, $book_info);
  60. return response()->pagination(new ChapterListTransformer, $lists);
  61. }
  62. private function getChapterCatalog(int $bid, $chapters, $book_info)
  63. {
  64. // 查询书籍是否限免
  65. $isFree = BookConfigService::judgeBookIsFree($bid);
  66. //渠道自定义vip章节
  67. //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  68. $vip_sequence = BookService::getVipSequence($bid, $this->distribution_channel_id, $this->send_order_id);
  69. list($is_split, $is_change_chapter_name) = BookService::splitContent($bid);
  70. $change_chapter_name = 0;
  71. if ($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name)) {
  72. $change_chapter_name = 1;
  73. }
  74. $account = $this->getAccount();
  75. $work_wechat_info = $this->getWorkWechatInfo($bid);
  76. switch ($book_info->charge_type) {
  77. case 'BOOK':
  78. $price = $this->getPriceNew($book_info, 0, $account);
  79. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  80. foreach ($chapters as $v) {
  81. $v->next_chapter_status = 0;
  82. $v->next_price = 0;
  83. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  84. $v->price = $price;
  85. if ($work_wechat_info['force_add_work_wechat_type'] > 0 && $v->sequence >= $work_wechat_info['force_add_work_wechat_seq']){
  86. $v->force_add_work_wechat_type = $work_wechat_info['force_add_work_wechat_type'];
  87. $v->force_add_work_wechat_url = $work_wechat_info['force_add_work_wechat_url'];
  88. }else{
  89. $v->force_add_work_wechat_type = 0;
  90. $v->force_add_work_wechat_url = "";
  91. }
  92. // 限免判断
  93. if ($isFree) {
  94. $v->is_need_charge = false;
  95. $v->price = 0;
  96. }
  97. if ($vip_sequence) {
  98. if ($v->sequence >= $vip_sequence) {
  99. $v->is_vip = 1;
  100. } else {
  101. $v->is_vip = 0;
  102. }
  103. }
  104. //拆章
  105. if ($change_chapter_name) {
  106. $v->name = '第' . $v->sequence . '章';
  107. }
  108. }
  109. break;
  110. default:
  111. $chapterIds = $this->getChapterIds($chapters);
  112. $rules = ['check' => 0, 'paycid' => []];
  113. if (!$isFree && !empty($chapterIds)) {
  114. $rules = $this->getHandelData($bid, $chapterIds);
  115. }
  116. foreach ($chapters as $v) {
  117. if ($vip_sequence) {
  118. if ($v->sequence >= $vip_sequence) {
  119. $v->is_vip = 1;
  120. } else {
  121. $v->is_vip = 0;
  122. }
  123. }
  124. if ($work_wechat_info['force_add_work_wechat_type'] > 0 && $v->sequence >= $work_wechat_info['force_add_work_wechat_seq']){
  125. $v->force_add_work_wechat_type = $work_wechat_info['force_add_work_wechat_type'];
  126. $v->force_add_work_wechat_url = $work_wechat_info['force_add_work_wechat_url'];
  127. }else{
  128. $v->force_add_work_wechat_type = 0;
  129. $v->force_add_work_wechat_url = "";
  130. }
  131. // 限免判断
  132. if ($isFree) {
  133. $v->is_need_charge = false;
  134. $v->price = 0;
  135. } else {
  136. $v->price = $v->is_vip ? $this->getPriceNew($book_info, $v->size, $account) : 0;
  137. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedChargeNew($v->id, $rules, $v->price) : false;
  138. }
  139. // //下一章付费信息
  140. $v->next_chapter_status = 0;
  141. $v->next_price = 0;
  142. // if($v->is_vip){
  143. // if(in_array($v->next_price,$chapterIds)){
  144. //
  145. // }else{
  146. // $next_chapter_order_status = $this->nextChapterOrderStatus($bid,$v->next_cid);
  147. // $v->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  148. // $v->next_price = $next_chapter_order_status['next_price'];
  149. // }
  150. //
  151. // }
  152. $v->next_chapter_status = $this->isSubscribe($v->id, $rules);
  153. $v->next_price = $v->price;
  154. //拆章
  155. if ($change_chapter_name) {
  156. $v->name = '第' . $v->sequence . '章';
  157. }
  158. }
  159. break;
  160. }
  161. return $chapters;
  162. }
  163. private function getChapterCatalogOld(int $bid, $chapters, $book_info)
  164. {
  165. // 查询书籍是否限免
  166. $isFree = BookConfigService::judgeBookIsFree($bid);
  167. //渠道自定义vip章节
  168. //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  169. $vip_sequence = BookService::getVipSequence($bid, $this->distribution_channel_id, $this->send_order_id);
  170. list($is_split, $is_change_chapter_name) = BookService::splitContent($bid);
  171. $change_chapter_name = 0;
  172. if ($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name)) {
  173. $change_chapter_name = 1;
  174. }
  175. switch ($book_info->charge_type) {
  176. case 'BOOK':
  177. $price = $this->getPrice($book_info);
  178. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  179. foreach ($chapters as $v) {
  180. $v->next_chapter_status = 0;
  181. $v->next_price = 0;
  182. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  183. $v->price = $price;
  184. // 限免判断
  185. if ($isFree) {
  186. $v->is_need_charge = false;
  187. $v->price = 0;
  188. }
  189. if ($vip_sequence) {
  190. if ($v->sequence >= $vip_sequence) {
  191. $v->is_vip = 1;
  192. } else {
  193. $v->is_vip = 0;
  194. }
  195. }
  196. //拆章
  197. if ($change_chapter_name) {
  198. $v->name = '第' . $v->sequence . '章';
  199. }
  200. }
  201. break;
  202. default:
  203. foreach ($chapters as $v) {
  204. if ($vip_sequence) {
  205. if ($v->sequence >= $vip_sequence) {
  206. $v->is_vip = 1;
  207. } else {
  208. $v->is_vip = 0;
  209. }
  210. }
  211. // 限免判断
  212. if ($isFree) {
  213. $v->is_need_charge = false;
  214. $v->price = 0;
  215. } else {
  216. $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
  217. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
  218. }
  219. //下一章付费信息
  220. $v->next_chapter_status = 0;
  221. $v->next_price = 0;
  222. if ($v->is_vip) {
  223. $next_chapter_order_status = $this->nextChapterOrderStatus($bid, $v->next_cid);
  224. $v->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  225. $v->next_price = $next_chapter_order_status['next_price'];
  226. }
  227. //拆章
  228. if ($change_chapter_name) {
  229. $v->name = '第' . $v->sequence . '章';
  230. }
  231. }
  232. break;
  233. }
  234. return $chapters;
  235. }
  236. public function index(Request $request, $bid, $cid)
  237. {
  238. $this->push_id = $request->header('x-pushid', '');
  239. $send_order_id = $request->header('send_order_id', '');
  240. $package = $request->header('x-package', '');
  241. //每次绑定用户和派单的关系
  242. $this->bindSendOrderId($this->uid, $send_order_id);
  243. $oldbid = $bid;
  244. $bid = BookService::decodeBidStatic($bid);
  245. //获取图书信息
  246. $book_info = BookConfigService::getBookById($bid);
  247. if (empty($book_info))
  248. return response()->error('QAPP_SYS_ERROR');
  249. $this->book_info = $book_info;
  250. $book_auth = BookConfigService::bookCopyright($bid, $this->distribution_channel_id);
  251. if ($book_auth == 2) {
  252. return response()->error('QAPP_OFF_SHELF');
  253. }
  254. //yuyuedu、xinghe 快应用这两个cp的书屏蔽下 wutong,wutong2,wutong3下所有内容都不放快应用
  255. if (in_array($book_info->cp_source, getHiddenCp($package)) && $book_auth != 1) {
  256. return response()->error('QAPP_OFF_SHELF');
  257. }
  258. if ($this->distribution_channel_id == 7477 && $bid == 13765) {
  259. $book_info->is_on_shelf = 2;
  260. }
  261. $special = get_special_bid();
  262. if (in_array($this->distribution_channel_id, [9487, 9390]) && in_array($book_info->bid, $special)) {
  263. $book_info->is_on_shelf = 2;
  264. }
  265. if ($bid == 58886) {
  266. $book_info->is_on_shelf = 0;
  267. }
  268. // 华为整改强制下架
  269. if ($this->distribution_channel_id == 13131 && in_array($bid,[13762,65327,66643])){
  270. $device = QappUser::where(['uid' => $this->uid])->where('created_at','>','2023-02-03')->value("device_info");
  271. $device = json_decode($device,true);
  272. $device = strtolower($device['manufacturer'] ?? "");
  273. if ($device == "huawei"){
  274. return response()->error('QAPP_OFF_SHELF');
  275. }
  276. if ($device == "huawei"){
  277. return response()->error('QAPP_OFF_SHELF');
  278. }
  279. $book_info->is_on_shelf = 4;
  280. }
  281. if (!in_array($book_info->is_on_shelf, [1, 2])) {
  282. if ($book_info->is_on_shelf != 4) {
  283. return response()->error('QAPP_OFF_SHELF');
  284. }
  285. //补充操作:如果该用户未订阅该下架的书籍则删除其阅读记录(书架不予显示)
  286. //判断是否属于包年用户
  287. $year_account = YearOrderService::getRecord($this->uid);
  288. if (!$year_account) {
  289. //获取书籍充值类型
  290. $charge_type = $book_info->charge_type;
  291. if ($charge_type == 'BOOK') {
  292. //是否购买过该书,购买过则不删除
  293. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  294. } elseif ($charge_type == 'CHAPTER') {
  295. if ($this->distribution_channel_id == 13131 && in_array($bid,[13762,65327,66643])){
  296. $res = ReadRecordService::getReadRecord($this->uid);
  297. if ($res){
  298. $res = array_column($res,null,'bid');
  299. $res = $res[$bid] ?? [];
  300. }
  301. if (is_empty($res)){
  302. return response()->error('QAPP_OFF_SHELF');
  303. }
  304. }
  305. //是否购买过该书章节,购买过则不删除
  306. $res = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  307. } else {
  308. $res = false;
  309. }
  310. if (!$res) {
  311. ReadRecordService::delReadRecordStatic($this->uid, [$bid]);
  312. return response()->error('QAPP_OFF_SHELF');
  313. }
  314. }
  315. }
  316. //wutong,wutong2,wutong3下所有内容都不放快应用
  317. // if(in_array($book_info->cp_source,['wutong','wutong2','wutong3','youyan2'])){
  318. // return response()->error('QAPP_OFF_SHELF');
  319. // }
  320. $this->book_info = $book_info;
  321. //获取章节信息
  322. $chapter = ChapterService::getChapterNameById($cid, $bid);
  323. if (!$chapter) {
  324. //短推长
  325. $combination_chapter = $this->chapterNotExists($bid, $cid);
  326. if ($combination_chapter) {
  327. $chapter = $combination_chapter;
  328. $bid = $chapter->bid;
  329. $book_info = BookConfigService::getBookById($bid);
  330. $oldbid = \Hashids::encode($bid);
  331. $this->book_info = $book_info;
  332. } else {
  333. return response()->error('QAPP_SYS_ERROR');
  334. }
  335. }
  336. list($is_split, $is_change_chapter_name) = BookService::splitContent($bid);
  337. if ($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name)) {
  338. $chapter->name = '第' . $chapter->sequence . '章';
  339. }
  340. $is_next_day = date('Y-m-d', strtotime($this->user_info->created_at)) == date('Y-m-d', strtotime('-1 days'));
  341. if ($is_next_day) {
  342. $job = new UserRententionJob($this->uid, now(), $this->user_info->created_at);
  343. dispatch($job)->onConnection('redis_queue')->onQueue('{user_rentention_queue}');
  344. }
  345. //自定义vip章节
  346. //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  347. $vip_sequence = BookService::getVipSequence($bid, $this->distribution_channel_id, $this->send_order_id);
  348. if ($vip_sequence) {
  349. if ($chapter->sequence >= $vip_sequence) {
  350. $chapter->is_vip = 1;
  351. } else {
  352. $chapter->is_vip = 0;
  353. }
  354. }
  355. if ($chapter->is_vip == 0) {
  356. ReadRecordService::addReadLog($this->uid, [
  357. 'distribution_channel_id' => $this->distribution_channel_id,
  358. 'bid' => $bid,
  359. 'cid' => $chapter->id,
  360. 'uid' => $this->uid,
  361. 'send_order_id' => $this->send_order_id,
  362. 'sequence' => $chapter->sequence,
  363. ]);
  364. ReadRecordService::addReadRecord([
  365. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  366. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  367. ]);
  368. return response()->item(new ChapterTransformer, $this->getChapter($bid, $chapter->id, $chapter));
  369. }
  370. // 书籍是否限免
  371. $free = BookConfigService::judgeBookIsFree($bid);
  372. if ($free) {
  373. ReadRecordService::addReadLog($this->uid, [
  374. 'distribution_channel_id' => $this->distribution_channel_id,
  375. 'bid' => $bid,
  376. 'cid' => $chapter->id,
  377. 'uid' => $this->uid,
  378. 'send_order_id' => $this->send_order_id,
  379. 'sequence' => $chapter->sequence,
  380. ]);
  381. ReadRecordService::addReadRecord([
  382. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  383. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  384. ]);
  385. if ($chapter->is_vip == 1) {
  386. $fee = $this->getPrice($book_info, $chapter->size);
  387. $now = date('Y-m-d');
  388. Redis::hincrby('qapp:book:free:virtual:' . $free->id, $now, $fee);
  389. Redis::sadd('qapp:free:virtual' . $now, $free->id);
  390. Redis::sadd('qapp:free:virtual:uids' . $now . $free->id, $this->uid);
  391. Redis::sadd('qapp:free:virtual:uids' . $free->id, $this->uid);
  392. }
  393. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  394. }
  395. //已经付费
  396. if ($this->getOrderRecord($bid, $cid)) {
  397. ReadRecordService::addReadLog($this->uid, [
  398. 'distribution_channel_id' => $this->distribution_channel_id,
  399. 'bid' => $bid,
  400. 'cid' => $chapter->id,
  401. 'uid' => $this->uid,
  402. 'send_order_id' => $this->send_order_id,
  403. 'sequence' => $chapter->sequence,
  404. ]);
  405. ReadRecordService::addReadRecord([
  406. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  407. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  408. ]);
  409. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  410. }
  411. //未付费 要提醒
  412. $user_info = $this->user_info;
  413. //未付费 余额不足
  414. $fee = $this->getPrice($book_info, $chapter->size);
  415. $data = [
  416. 'book_id' => $oldbid,
  417. 'book_name' => $book_info->book_name,
  418. 'chapter_name' => $chapter->name,
  419. 'chapter_id' => $cid,
  420. 'pay_type' => $book_info->charge_type,
  421. 'fee' => $fee,
  422. 'user_balance' => $user_info->balance,
  423. 'product_id' => $book_info->product_id,
  424. 'uid' => $this->uid,
  425. 'distribution_channel_id' => $this->distribution_channel_id,
  426. 'is_discount' => 0,
  427. 'discount_fee' => '',
  428. 'discount' => ''
  429. ];
  430. if ($user_info['balance'] < $fee) {
  431. if ($book_info->charge_type == 'BOOK') {
  432. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  433. } elseif ($book_info->charge_type == 'CHAPTER') {
  434. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  435. } else {
  436. return response()->error('QAPP_SYS_ERROR');
  437. }
  438. }
  439. if (!$this->send_order_id) {
  440. if ($book_info->charge_type == 'BOOK') {
  441. return response()->error('QAPP_BOOK_BUY', $data);
  442. }/*else{
  443. return response()->error('QAPP_CHAPTER_BUY', $data);
  444. }*/
  445. }
  446. //付费 不提醒
  447. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  448. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  449. ReadRecordService::addReadLog($this->uid, [
  450. 'distribution_channel_id' => $this->distribution_channel_id,
  451. 'bid' => $bid,
  452. 'cid' => $chapter->id,
  453. 'uid' => $this->uid,
  454. 'send_order_id' => $this->send_order_id,
  455. 'sequence' => $chapter->sequence,
  456. ]);
  457. ReadRecordService::addReadRecord([
  458. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  459. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  460. ]);
  461. //限免统计
  462. $free_book = BookConfigService::getByBidNoFilter($bid);
  463. if ($free_book) {
  464. if (strtotime($free_book->end_time) + 7 * 86400 >= strtotime(date('Y-m-d'))) {
  465. if (Redis::Sismember('qapp:free:virtual:uids' . $free_book->id, $this->uid)) {
  466. $now = date('Y-m-d');
  467. Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
  468. Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
  469. Redis::sadd('qapp:free:actuality:uids' . $now . $free_book->id, $this->uid);
  470. }
  471. }
  472. }
  473. if ($this->uid == 247081369) {
  474. $item = itemTransform(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  475. myLog('xueqi')->info($item);
  476. }
  477. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  478. } else {
  479. if ($book_info->charge_type == 'BOOK') {
  480. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  481. } elseif ($book_info->charge_type == 'CHAPTER') {
  482. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  483. } else {
  484. return response()->error('QAPP_SYS_ERROR');
  485. }
  486. }
  487. }
  488. public function pay(Request $request, $bid, $cid)
  489. {
  490. $this->push_id = $request->header('x-pushid', '');
  491. $remind = (int)$request->input('remind');
  492. $oldbid = $bid;
  493. $bid = BookService::decodeBidStatic($bid);
  494. $book_info = BookConfigService::getBookById($bid);;
  495. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  496. $this->book_info = $book_info;
  497. if ($this->distribution_channel_id == 7477 && $bid == 13765) {
  498. $book_info->is_on_shelf = 2;
  499. }
  500. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  501. if (!$this->isBookOrdered($bid)) {
  502. response()->error('QAPP_OFF_SHELF');
  503. }
  504. }
  505. //获取章节
  506. $chapter = ChapterService::getChapterNameById($cid, $bid);
  507. if (!$chapter) {
  508. $combination_chapter = $this->chapterNotExists($bid, $cid);
  509. if ($combination_chapter) {
  510. $chapter = $combination_chapter;
  511. $bid = $chapter->bid;
  512. $book_info = BookConfigService::getBookById($bid);
  513. $oldbid = \Hashids::encode($bid);
  514. $this->book_info = $book_info;
  515. } else {
  516. return response()->error('QAPP_SYS_ERROR');
  517. }
  518. }
  519. if ($this->getOrderRecord($bid, $cid)) {
  520. ReadRecordService::addReadLog($this->uid, [
  521. 'distribution_channel_id' => $this->distribution_channel_id,
  522. 'bid' => $bid,
  523. 'cid' => $chapter->id,
  524. 'uid' => $this->uid,
  525. 'send_order_id' => $this->send_order_id,
  526. 'sequence' => $chapter->sequence,
  527. ]);
  528. ReadRecordService::addReadRecord([
  529. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  530. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  531. ]);
  532. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  533. }
  534. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  535. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  536. ReadRecordService::addReadLog($this->uid, [
  537. 'distribution_channel_id' => $this->distribution_channel_id,
  538. 'bid' => $bid,
  539. 'cid' => $chapter->id,
  540. 'uid' => $this->uid,
  541. 'send_order_id' => $this->send_order_id,
  542. 'sequence' => $chapter->sequence,
  543. ]);
  544. ReadRecordService::addReadRecord([
  545. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  546. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  547. ]);
  548. $free_book = BookConfigService::getByBidNoFilter($bid);
  549. if ($free_book) {
  550. if (strtotime($free_book->end_time) + 7 * 86400 >= strtotime(date('Y-m-d'))) {
  551. if (Redis::Sismember('qapp:free:virtual:uids' . $free_book->id, $this->uid)) {
  552. $now = date('Y-m-d');
  553. $fee = $this->getPrice($book_info, $chapter->size);
  554. Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
  555. Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
  556. Redis::sadd('qapp:free:actuality:uids' . $now . $free_book->id, $this->uid);
  557. }
  558. }
  559. }
  560. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  561. } else {
  562. $fee = $this->getPrice($book_info, $chapter->size);
  563. $data = [
  564. 'book_id' => $oldbid,
  565. 'book_name' => $book_info->book_name,
  566. 'chapter_name' => $chapter->name,
  567. 'chapter_id' => $cid,
  568. 'pay_type' => $book_info->charge_type,
  569. 'fee' => $fee,
  570. 'user_balance' => $this->user_info['balance'],
  571. 'product_id' => $book_info->product_id,
  572. 'uid' => $this->uid,
  573. 'distribution_channel_id' => $this->distribution_channel_id,
  574. 'is_discount' => 0,
  575. 'discount_fee' => '',
  576. 'discount' => ''
  577. ];
  578. if ($book_info->charge_type == 'BOOK') {
  579. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  580. } elseif ($book_info->charge_type == 'CHAPTER') {
  581. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  582. } else {
  583. return response()->error('QAPP_SYS_ERROR');
  584. }
  585. }
  586. }
  587. /**
  588. * 余额支付
  589. * @param $book_info
  590. * @param $chapter_id
  591. * @param $chapter_size
  592. * @return bool
  593. */
  594. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  595. {
  596. $fee = $this->getPrice($book_info, $chapter_size);
  597. if ((int)$this->user_info['balance'] >= $fee) {
  598. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  599. return true;
  600. }
  601. return false;
  602. } else {
  603. return false;
  604. }
  605. }
  606. /**
  607. * 获取章节内容
  608. * @param $bid
  609. * @param $cid
  610. * @return bool|mixed
  611. */
  612. protected function getChapter($bid, $cid, $chapter)
  613. {
  614. $chapter_content = ChapterService::getChapter($bid, $cid);
  615. if (!$chapter_content) return false;
  616. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  617. // 格式特殊处理
  618. $qian = array(" ", " ", "\t");
  619. $hou = array("", "", "");
  620. // 去掉所有空格
  621. $chapter->content = str_replace($qian, $hou, $chapter->content);
  622. // 2换行变1换行
  623. $chapter->content = str_replace("\n\n", "\n", $chapter->content);
  624. $chapter->content = str_replace("\r\n\r\n", "\r\n", $chapter->content);
  625. // \Log::info('getChapter_chapter:'.$bid.' cid:'.$cid);
  626. // \Log::info($chapter->content);
  627. //统计点击率
  628. $key = 'book_click_num_bid_' . $bid;
  629. $field = date('Y-m-d');
  630. $old = Redis::hget($key, $field);
  631. if (!$old) $old = 0;
  632. Redis::hset($key, $field, $old + 1);
  633. [$force_add_desk_type,$is_need_query_send_order] = $this->addDesktopType($bid, $chapter->sequence);
  634. $chapter->force_add_desk_type = $force_add_desk_type;
  635. $force_add_work_wechat_type = 0;
  636. $force_add_work_wechat_url = "";
  637. // 派单企微弹出查询
  638. if($is_need_query_send_order){
  639. [$force_add_work_wechat_type, $force_add_work_wechat_url] = $this->addWorkWechatType($chapter->sequence);
  640. }
  641. // push企微弹出查询
  642. if ($this->push_id > 0 && $force_add_work_wechat_type == 0){
  643. [$force_add_work_wechat_type, $force_add_work_wechat_url] = $this->addWorkWechatType($chapter->sequence,2);
  644. }
  645. $chapter->force_add_work_wechat_type = $force_add_work_wechat_type;
  646. $chapter->force_add_work_wechat_url = $force_add_work_wechat_url;
  647. //统计
  648. $this->stats();
  649. $next_chapter_order_status = $this->nextChapterOrderStatus($bid, $chapter->next_cid);
  650. $chapter->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  651. $chapter->next_price = $next_chapter_order_status['next_price'];
  652. $chapter->charge_type = $this->book_info->charge_type;
  653. $this->userBookCombination($chapter);
  654. return $chapter;
  655. }
  656. private function nextChapterOrderStatus($bid, $cid)
  657. {
  658. $chapter = ChapterService::getChapterNameById($cid, $bid);
  659. if (!$chapter || $chapter->is_vip == 0 || $this->book_info->charge_type == 'BOOK') {
  660. return ['next_chapter_status' => 0, 'next_price' => 0];
  661. }
  662. $is_paid = $this->getOrderRecord($bid, $cid);
  663. if ($is_paid) {
  664. return ['next_chapter_status' => 1, 'next_price' => 0];
  665. }
  666. $fee = $this->getPrice($this->book_info, $chapter->size);
  667. return ['next_chapter_status' => 2, 'next_price' => $fee];
  668. }
  669. //短续长
  670. private function userBookCombination($chapter)
  671. {
  672. if ($chapter->prev_cid && $chapter->next_cid) {
  673. return;
  674. }
  675. if ($chapter->next_cid == 0 && $this->book_info->charge_type == 'BOOK') {
  676. $bid = UserBookCombinationConfigService::selectAndSave($this->uid, $this->book_info->bid);
  677. if ($bid) {
  678. $bookInfo = BookConfigService::getBookById($bid);
  679. $chapter->next_cid = $bookInfo->first_cid;
  680. }
  681. }
  682. if ($chapter->prev_cid == 0 && $this->book_info->charge_type == 'CHAPTER') {
  683. $bid = UserBookCombinationConfigService::getShortBookFromLongBid($this->uid, $this->book_info->bid);
  684. if ($bid) {
  685. $bookInfo = BookConfigService::getBookById($bid);
  686. $chapter->prev_cid = $bookInfo->last_cid;
  687. }
  688. }
  689. }
  690. private function chapterNotExists($bid, $cid)
  691. {
  692. //1.全局图书组合配置
  693. $combination = BookConfigService::getCombinationInfo($bid);
  694. if ($combination) {
  695. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  696. if ($chapter) {
  697. return $chapter;
  698. }
  699. return false;
  700. }
  701. //2.用户图书组合配置
  702. if ($this->book_info->charge_type == 'CHAPTER') {
  703. $user_combination = UserBookCombinationConfigService::getShortBookFromLongBook($this->uid, $bid);
  704. } else {
  705. $user_combination = UserBookCombinationConfigService::getLongBookFromShortBook($this->uid, $bid);
  706. }
  707. if ($user_combination) {
  708. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  709. if ($chapter) {
  710. return $chapter;
  711. }
  712. return false;
  713. }
  714. return false;
  715. }
  716. /**
  717. * 添加订购记录
  718. * @param $book_info
  719. * @param $chapter_id
  720. * @param $fee
  721. * @return bool
  722. */
  723. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  724. {
  725. if ($book_info['charge_type'] == 'BOOK') {
  726. $data = [
  727. 'uid' => $this->uid,
  728. 'fee' => $fee,
  729. 'u' => $this->send_order_id,
  730. 'distribution_channel_id' => $this->distribution_channel_id,
  731. 'bid' => $book_info->bid,
  732. 'book_name' => $book_info->book_name,
  733. 'send_order_id' => $this->send_order_id,
  734. ];
  735. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  736. } else {
  737. $data = [
  738. 'uid' => $this->uid,
  739. 'fee' => $fee,
  740. 'cid' => $chapter_id,
  741. 'bid' => $book_info->bid,
  742. 'distribution_channel_id' => $this->distribution_channel_id,
  743. 'book_name' => $book_info->book_name,
  744. 'chapter_name' => $chapter_name,
  745. 'send_order_id' => $this->send_order_id,
  746. 'is_remind' => $is_remind
  747. ];
  748. if ($is_remind) {
  749. $this->addOrderRemind($book_info->bid);
  750. }
  751. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  752. }
  753. }
  754. protected function addOrderRemind($bid)
  755. {
  756. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  757. return true;
  758. } else {
  759. ChapterReminderService::add($this->uid, $bid);
  760. return true;
  761. }
  762. }
  763. /**
  764. * 是否订购提醒
  765. * @param $chapter_id
  766. * @return bool
  767. */
  768. protected function isOrderRemind($bid)
  769. {
  770. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  771. return $is_no_reminder == 0;
  772. }
  773. /**
  774. * 用户是否关注
  775. * @param $uid
  776. * @return bool
  777. */
  778. protected function getSubscribe()
  779. {
  780. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  781. if ($res) return true;
  782. return false;
  783. }
  784. /**
  785. * 获取订购记录
  786. * @param $book_info
  787. * @param $chapter_id
  788. * @return bool
  789. */
  790. protected function getOrderRecord($bid, $chapter_id)
  791. {
  792. //包年记录
  793. $uid = $this->uid;
  794. $res = YearOrderService::getRecord($uid);
  795. if ($res) return true;
  796. $res = null;
  797. //单本订购记录
  798. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  799. if ($res) return true;
  800. $res = null;
  801. //章节订购记录
  802. $chapterOrder = new ChapterOrderService();
  803. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  804. return false;
  805. }
  806. /**
  807. * 计算价格
  808. * @param $book_info
  809. * @param $chapter_size
  810. * @return float
  811. */
  812. private function getPrice($book_info, $chapter_size = 0)
  813. {
  814. if ($book_info->charge_type == 'BOOK') {
  815. if (BookOrderService::isHasBookOrder($this->uid)) {
  816. $this->is_first_book_order = 0;
  817. return 1399;
  818. } else {
  819. $this->is_first_book_order = 1;
  820. return 899;
  821. }
  822. } else {
  823. // 获取投放后台账号,
  824. $account = '';
  825. $account_send_order = $this->getNowSendOrderInfo();
  826. if ($account_send_order) {
  827. $account = isset($account_send_order['account']) ? $account_send_order['account'] : '';
  828. }
  829. $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size, $account);
  830. return $fee;
  831. }
  832. }
  833. /**
  834. * 计算价格
  835. * @param $book_info
  836. * @param $chapter_size
  837. * @return float
  838. */
  839. private function getPriceNew($book_info, $chapter_size = 0, $account = "")
  840. {
  841. if ($book_info->charge_type == 'BOOK') {
  842. if (BookOrderService::isHasBookOrder($this->uid)) {
  843. $this->is_first_book_order = 0;
  844. return 1399;
  845. } else {
  846. $this->is_first_book_order = 1;
  847. return 899;
  848. }
  849. } else {
  850. return BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size, $account);
  851. }
  852. }
  853. /**
  854. * 获取投放后台账号,
  855. * name: getAccount
  856. * @return mixed|string
  857. * date 2022/10/20 11:29
  858. */
  859. protected function getAccount()
  860. {
  861. $account = '';
  862. $account_send_order = $this->getNowSendOrderInfo();
  863. if ($account_send_order) {
  864. $account = isset($account_send_order['account']) ? $account_send_order['account'] : '';
  865. }
  866. return $account;
  867. }
  868. /**
  869. * 用户添加标签
  870. * @param $book_info
  871. */
  872. protected function addTag($book_info)
  873. {
  874. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  875. try {
  876. UserDeepReadTagService::addTag([
  877. 'uid' => $this->uid,
  878. 'bid' => $book_info->bid,
  879. 'book_name' => $book_info->book_name,
  880. 'category_id' => $book_info->category_id,
  881. 'category_name' => $book_info->category_name,
  882. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  883. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  884. 'send_order_id' => $this->send_order_id,
  885. ]);
  886. } catch (\Exception $e) {
  887. }
  888. }
  889. }
  890. protected function isBookOrdered($bid)
  891. {
  892. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  893. if ($chapter_order) return true;
  894. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  895. if ($res) return true;
  896. return false;
  897. }
  898. /**
  899. * 判断是否需要充值
  900. */
  901. private function isBookNeedCharge(int $bid, float $price)
  902. {
  903. $book_order = $this->getOrderRecord($bid, 0);
  904. if ($book_order) {
  905. return false;
  906. } else {
  907. $user_info = $this->user_info;
  908. return $user_info['balance'] < $price;
  909. }
  910. }
  911. /**
  912. * 判断章节是否需要充值
  913. */
  914. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  915. {
  916. $book_order = $this->getOrderRecord($bid, $cid);
  917. if ($book_order) {
  918. return false;
  919. } else {
  920. $user_info = $this->user_info;
  921. return $user_info['balance'] < $price;
  922. }
  923. }
  924. private function stats()
  925. {
  926. //阅读器统计
  927. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  928. }
  929. //加桌类型
  930. private function addDesktopType($bid, $sequence)
  931. {
  932. $deault_force_add_desk_type = 0;
  933. $send_order_id = $this->GetBindSendOrderId($this->uid);
  934. $is_need_query_order = 0;
  935. if ($send_order_id) {
  936. $send_order_info = SendOrderService::getById($send_order_id);
  937. if (!$send_order_info) return [$deault_force_add_desk_type,$is_need_query_order];
  938. if ($send_order_info->book_id == $bid) {
  939. $is_need_query_order = 1;
  940. // 派单书籍和观看书籍一致,并设置了强加桌,判断当前章节和设置的强加桌章节
  941. if ($send_order_info->force_add_desk_type == 1 && $send_order_info->force_add_desk_seq) {
  942. //在设置的强加桌章节前一章开始
  943. if ($sequence >= ($send_order_info->force_add_desk_seq - 1)) {
  944. $force_add_desk_type = $send_order_info->force_add_desk_type;
  945. return [$force_add_desk_type,$is_need_query_order];
  946. }
  947. }
  948. if ($send_order_info->force_add_desk_type == 2) {
  949. if ($sequence >= $this->book_info->force_subscribe_chapter_seq && $sequence <= $this->book_info->force_subscribe_chapter_seq + 3) {
  950. $force_add_desk_type = $send_order_info->force_add_desk_type;
  951. return [$force_add_desk_type,$is_need_query_order];
  952. }
  953. }
  954. if ($send_order_info->force_add_desk_type == 3) {
  955. //弱加桌+强加桌 ,即强加桌章节之前的章节均弱加桌,之后的章节显示强加桌:
  956. $need_sequence = $send_order_info->force_add_desk_seq;
  957. if (!$need_sequence) {
  958. $need_sequence = $this->book_info->force_subscribe_chapter_seq;
  959. }
  960. if ($sequence >= ($need_sequence - 1)) {
  961. //之后的章节显示强加桌
  962. return [1,$is_need_query_order];
  963. } else {
  964. ////强加桌章节之前的章节均弱加桌
  965. return [2,$is_need_query_order];
  966. }
  967. }
  968. return [$deault_force_add_desk_type,$is_need_query_order];
  969. }
  970. }
  971. //无派单,或者派单书籍与 此次书籍不一致,直接使用 原书籍的默认强关章节
  972. $book_info = BookConfigService::getBookById($bid);
  973. if ($book_info && $book_info->force_subscribe_chapter_seq) {
  974. if ($sequence >= ($book_info->force_subscribe_chapter_seq - 1)) {
  975. return [2,$is_need_query_order];
  976. }
  977. }
  978. return [$deault_force_add_desk_type,$is_need_query_order];
  979. }
  980. /**
  981. * 获取章节详情企微弹窗设置
  982. * name: addWorkWechatType
  983. * @param mixed $sequence 章节序号
  984. * @param mixed $type 类型 1 派单 2 push
  985. * @return array
  986. * date 2023/02/13 16:08
  987. */
  988. private function addWorkWechatType($sequence,$type = 1)
  989. {
  990. $default_force_add_desk_type = 0;
  991. $work_wechat_url = "";
  992. if ($type == 1){
  993. // 派单
  994. $send_order_id = $this->GetBindSendOrderId($this->uid);
  995. if ($send_order_id) {
  996. $send_order_info = QappSendOrder::getSendOrderById($send_order_id);
  997. if (!$send_order_info) return [$default_force_add_desk_type, $work_wechat_url];
  998. if($send_order_info['force_add_work_wechat_status'] == 1 && $send_order_info['force_add_work_wechat_seq'] <= ($sequence+1)){
  999. $default_force_add_desk_type =$send_order_info['force_add_work_wechat_type'];
  1000. $work_wechat_url =$send_order_info['force_add_work_wechat_url'];
  1001. }
  1002. }
  1003. }else if ($type == 2){
  1004. // push
  1005. $pushInfo = QappPushTask::where(['id' => $this->push_id,'uid' => $this->distribution_channel_id])
  1006. ->select('id','force_add_work_wechat_status','force_add_work_wechat_type','force_add_work_wechat_seq','force_add_work_wechat_url')->first() ;
  1007. if (!$pushInfo) return [$default_force_add_desk_type, $work_wechat_url];
  1008. if($pushInfo['force_add_work_wechat_status'] == 1 && $pushInfo['force_add_work_wechat_seq'] <= ($sequence+1)){
  1009. $default_force_add_desk_type =$pushInfo['force_add_work_wechat_type'];
  1010. $work_wechat_url = $pushInfo['force_add_work_wechat_url'];
  1011. }
  1012. }
  1013. return [$default_force_add_desk_type, $work_wechat_url];
  1014. }
  1015. /**
  1016. * [bindSendOrderId description]
  1017. * @param [type] $uid [description]
  1018. * @param [type] $send_order_id [description]
  1019. * @return [type] [description]
  1020. */
  1021. public function bindSendOrderId($uid, $send_order_id)
  1022. {
  1023. if ($send_order_id) {
  1024. $res = Redis::hset('book_read_chapter:' . $uid, 'send_order_id', $send_order_id);
  1025. }
  1026. }
  1027. /**
  1028. * [bindSendOrderId description]
  1029. * @param [type] $uid [description]
  1030. * @param [type] $send_order_id [description]
  1031. * @return [type] [description]
  1032. */
  1033. public function GetBindSendOrderId($uid)
  1034. {
  1035. try {
  1036. $send_order_id = Redis::hget('book_read_chapter:' . $uid, 'send_order_id');
  1037. if ($send_order_id)
  1038. return (int)$send_order_id;
  1039. } catch (\Exception $e) {
  1040. }
  1041. }
  1042. private function getChapterIds($chapters): array
  1043. {
  1044. $list = [];
  1045. if ($chapters) {
  1046. foreach ($chapters as $v) {
  1047. $list[] = $v->id;
  1048. }
  1049. }
  1050. return $list;
  1051. }
  1052. private function getHandelData(int $bid, array $chapterIds): array
  1053. {
  1054. $uid = $this->uid;
  1055. $res = YearOrderService::getRecord($uid);
  1056. if ($res) {
  1057. return $handle = ['check' => 0, 'paycid' => []];
  1058. }
  1059. $res = null;
  1060. //单本订购记录
  1061. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  1062. if ($res) {
  1063. return $handle = ['check' => 0, 'paycid' => []];
  1064. }
  1065. //章节订购记录
  1066. $chapterOrder = new ChapterOrderService();
  1067. $res = $chapterOrder->getOrdersByChapterIds($uid, $bid, $chapterIds);
  1068. return $handle = ['check' => 1, 'paycid' => $res];
  1069. }
  1070. /**
  1071. * 判断是否需要充值
  1072. * name: isChapterNeedChargeNew
  1073. * @param $cid
  1074. * @param $rules
  1075. * @return bool
  1076. * date 2022/10/20 16:08
  1077. */
  1078. private function isChapterNeedChargeNew($cid, $rules, $price = 0): bool
  1079. {
  1080. if ($rules['check'] != 1) {
  1081. return false;
  1082. }
  1083. if (empty($rules['paycid']) || !in_array($cid, $rules['paycid'])) {
  1084. $user_info = $this->user_info;
  1085. return $user_info['balance'] < $price;
  1086. } else {
  1087. return false;
  1088. }
  1089. }
  1090. /*****
  1091. * 是否订阅
  1092. * name: isSubscribe
  1093. * @param $cid
  1094. * @param $rules
  1095. * @return int
  1096. * date 2022/11/02 17:19
  1097. */
  1098. private function isSubscribe($cid, $rules): int
  1099. {
  1100. if ($rules['check'] != 1) {
  1101. return 1;
  1102. }
  1103. if (empty($rules['paycid']) || !in_array($cid, $rules['paycid'])) {
  1104. return 2;
  1105. } else {
  1106. return 1;
  1107. }
  1108. }
  1109. /**
  1110. * 获取用户相关的企微测试
  1111. * name: getWorkWechatInfo
  1112. * @param $bid
  1113. * @return array
  1114. * date 2023/02/13 16:58
  1115. */
  1116. private function getWorkWechatInfo($bid)
  1117. {
  1118. $info = ['force_add_work_wechat_type' => 0,'force_add_work_wechat_url' => "",'force_add_work_wechat_seq' => 0];
  1119. $send_order_id = $this->GetBindSendOrderId($this->uid);
  1120. if ($send_order_id) {
  1121. $send_order_info = QappSendOrder::getSendOrderById($send_order_id);
  1122. if (!$send_order_info) return $info;
  1123. $send_order_bid = SendOrder::where(['id' => $send_order_id])->value('book_id');
  1124. if($send_order_info['force_add_work_wechat_status'] == 1 && $send_order_bid == $bid){
  1125. $info['force_add_work_wechat_type'] = $send_order_info['force_add_work_wechat_type'];
  1126. $info['force_add_work_wechat_url'] = $send_order_info['force_add_work_wechat_url'];
  1127. $info['force_add_work_wechat_seq'] = $send_order_info['force_add_work_wechat_seq'];
  1128. }
  1129. }
  1130. if($info['force_add_work_wechat_type'] == 0 && $this->push_id > 0){
  1131. $pushInfo = QappPushTask::where(['id' => $this->push_id,'uid' => $this->distribution_channel_id])
  1132. ->select('id','force_add_work_wechat_status','force_add_work_wechat_type','force_add_work_wechat_seq','force_add_work_wechat_url')->first() ;
  1133. if($pushInfo['force_add_work_wechat_status'] == 1){
  1134. $info['force_add_work_wechat_type'] = $pushInfo['force_add_work_wechat_type'];
  1135. $info['force_add_work_wechat_url'] = $pushInfo['force_add_work_wechat_url'];
  1136. $info['force_add_work_wechat_seq'] =$pushInfo['force_add_work_wechat_seq'];
  1137. }
  1138. }
  1139. return $info;
  1140. }
  1141. }