OrdersController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <?php
  2. namespace App\Http\Controllers\Wap\Order;
  3. use App\Http\Controllers\Wap\BaseController;
  4. use App\Modules\Statistic\Services\AdVisitStatService;
  5. use App\Modules\Statistic\Services\DataAnalysisSelectUserService;
  6. use App\Modules\User\Services\UserService;
  7. use App\Modules\Book\Services\BookService;
  8. use App\Modules\Channel\Services\PayTemplateService;
  9. use Illuminate\Http\Request;
  10. use App\Modules\Subscribe\Services\BookOrderService;
  11. use App\Modules\Subscribe\Services\ChapterOrderService;
  12. use App\Modules\Subscribe\Services\OrderService;
  13. use App\Http\Controllers\Wap\Order\Transformers\BookOrderTransformer;
  14. use App\Http\Controllers\Wap\Order\Transformers\ChapterOrderTransformer;
  15. use App\Http\Controllers\Wap\Order\Transformers\ChargeListTransformer;
  16. use Redis;
  17. use App\Modules\Product\Services\ProductService;
  18. use App\Modules\Subscribe\Services\SubstituteOrderService;
  19. use Hashids;
  20. use DB;
  21. class OrdersController extends BaseController
  22. {
  23. /**
  24. * @apiDefine Order 订单
  25. */
  26. /**
  27. * @apiVersion 1.0.0
  28. * @apiDescription 充值列表
  29. * @api {get} order/chargeList 充值列表
  30. * @apiGroup Order
  31. * @apiName chargeList
  32. * @apiSuccess {int} code 状态码
  33. * @apiSuccess {String} msg 信息
  34. * @apiSuccess {object} data 结果集
  35. * @apiSuccessExample {json} Success-Response:
  36. * HTTP/1.1 200 OK
  37. * {
  38. * code: 0,
  39. * msg: "",
  40. * data: [
  41. * {
  42. * product_id: 1,
  43. * price: "30.00元",
  44. * vip: 0,
  45. * intro: [
  46. * {
  47. * label: 3000,
  48. * important: false
  49. * },
  50. * {
  51. * label: "书币",
  52. * important: true
  53. * }
  54. * ]
  55. * },
  56. * {
  57. * product_id: 2,
  58. * price: "50.00元",
  59. * vip: 1,
  60. * intro: [
  61. * {
  62. * label: 5000,
  63. * important: false
  64. * },
  65. * {
  66. * label: "1000+",
  67. * important: true
  68. * },
  69. * {
  70. * label: "书币",
  71. * important: false
  72. * }
  73. * ]
  74. * },
  75. * {
  76. * product_id: 5,
  77. * price: "365.00元",
  78. * vip: 0,
  79. * intro: [
  80. * {
  81. * label: "年费VIP会员",
  82. * important: true
  83. * }
  84. * ]
  85. * }
  86. * ]
  87. * }
  88. */
  89. public function chargeList_(Request $request)
  90. {
  91. if (!$this->checkUid()) {
  92. return response()->error('WAP_NOT_LOGIN');
  93. }
  94. $res = ProductService::getChargeProduct();
  95. if (!$res->isEmpty()) {
  96. $data = [];
  97. /*if ($this->send_order_id) {
  98. try {
  99. Redis::sadd('pay_page_uv' . $this->send_order_id, $this->uid);
  100. Redis::sadd('pay_page_uv_send_order_ids', $this->send_order_id);
  101. } catch (\Exception $e) {
  102. }
  103. }*/
  104. foreach ($res as $v) {
  105. $intro = [];
  106. if ($v->given > 0 && $v->type == 'TICKET_RECHARGE') {
  107. $intro = [
  108. [
  109. 'label' => ($v->price * 100) . "+",
  110. 'important' => false,
  111. ],
  112. [
  113. 'label' => $v->given,
  114. 'important' => true,
  115. ],
  116. [
  117. 'label' => '书币',
  118. 'important' => false,
  119. ]
  120. ];
  121. $intro2 = [
  122. ['label' => '多送', 'important' => false],
  123. ['label' => (int)($v->given / 100), 'important' => true],
  124. ['label' => '元', 'important' => false],
  125. ];
  126. $v->vip = 0;
  127. }
  128. if ($v->given == 0 && $v->type == 'TICKET_RECHARGE') {
  129. $intro = [
  130. [
  131. 'label' => $v->price * 100,
  132. 'important' => false,
  133. ],
  134. [
  135. 'label' => '书币',
  136. 'important' => false,
  137. ]
  138. ];
  139. $v->vip = 0;
  140. $intro2 = [];
  141. }
  142. if ($v->given == 0 && $v->type == 'YEAR_ORDER') {
  143. $intro = [
  144. [
  145. 'label' => '年费VIP会员',
  146. 'important' => true,
  147. ]
  148. ];
  149. $v->vip = 1;
  150. $intro2 = [
  151. ['label' => '每天1元,全年免费看', 'important' => false],
  152. ];
  153. }
  154. $data[] = [
  155. 'product_id' => $v->id,
  156. 'price' => (int)$v->price . '元',
  157. 'vip' => $v->vip,
  158. 'intro' => $intro,
  159. 'intro2' => $intro2,
  160. 'is_default' => $v->is_default,
  161. ];
  162. }
  163. return response()->success($data);
  164. } else {
  165. return response()->error('WAP_SYS_ERROR');
  166. }
  167. }
  168. public function chargeList(Request $request)
  169. {
  170. if (!$this->checkUid()) {
  171. return response()->error('WAP_NOT_LOGIN');
  172. }
  173. /*if (env('OTHER_PAY_TEMPLATE') &&
  174. in_array($this->distribution_channel_id,
  175. explode(',', env('OTHER_PAY_TEMPLATE')))
  176. ) {
  177. $template_id = 3;
  178. } else {*/
  179. $bid = $request->input('bid', '');
  180. $temp = $bid;
  181. $template_id = PayTemplateService::getPayTemplate($this->distribution_channel_id);
  182. //}
  183. \Log::info('order:$template_id:' . $template_id);
  184. \Log::info('order:$distribution_channel_id:' . ($this->distribution_channel_id));
  185. if ($template_id == 2) { //模板2只有在长篇小说过来的用户才显示
  186. //部分渠道需要2元模板不管哪个入口进来都展示
  187. $exclude_channels = explode(',', env('PRICE_TWO_SHOW_ALL_CHANNEL'));
  188. if(!in_array($this->distribution_channel_id,$exclude_channels))
  189. {
  190. if ($bid) {
  191. $bid = Hashids::decode($bid)[0];
  192. \Log::info('order:$bid:' . $bid);
  193. $book = BookService::getBookById($bid);
  194. \Log::info('order:$$book:' . json_encode($book));
  195. if ($book->size < 200000) {
  196. $template_id = 1;
  197. }
  198. } else {
  199. $template_id = 1;
  200. }
  201. }
  202. }
  203. if($temp){
  204. $temp_bid = 0;
  205. try{
  206. $temp_bid = Hashids::decode($temp)[0];
  207. }catch (\Exception $e){}
  208. $t = $this->recordFirstIntoPayPage($temp_bid);
  209. if ($t) {
  210. $template_id = $t;
  211. }
  212. }
  213. if( ($compare_id = $this->templateCompare()) ){
  214. $template_id = $compare_id;
  215. }
  216. \Log::info('recordFirstIntoPayPage:$template_id:' . $template_id);
  217. $res = ProductService::getChargeProduct($template_id);
  218. if (!$res) {
  219. return response()->error('WAP_SYS_ERROR');
  220. }
  221. /*if ($this->send_order_id) {
  222. try {
  223. Redis::sadd('pay_page_uv' . $this->send_order_id, $this->uid);
  224. Redis::sadd('pay_page_uv_send_order_ids', $this->send_order_id);
  225. } catch (\Exception $e) {
  226. }
  227. }*/
  228. //TODO 长篇小数才有模板2
  229. //$user = $this->_user_info;
  230. $uid = $this->uid;
  231. $is_first_recharge = OrderService::judgeUserFirstRecharge($uid);
  232. //yqLog('user')->info('user is ',['user'=>$user]);
  233. //yqLog('user')->info('user is ',['charge_count'=>$user->charge_count]);
  234. $data = [];
  235. foreach ($res as $v) {
  236. if ($v->type == 'NEW_USER' && $is_first_recharge) {
  237. if (env('NO_NEW_USER_CHARGE') &&
  238. in_array(
  239. $this->distribution_channel_id,
  240. explode(',', env('NO_NEW_USER_CHARGE'))
  241. )
  242. ) {
  243. continue;
  244. }
  245. $temp = [
  246. 'price' => (float)$v->price . '元',
  247. 'is_year_order' => 0,
  248. 'text' => sprintf('%s+%s书币', $v->price * 100, $v->given),
  249. 'first_charge' => true,
  250. 'today_special' => false,
  251. 'save_text' => round($v->given / 100, 1) . '元',
  252. 'product_id' => $v->id
  253. ];
  254. $data[] = $temp;
  255. } elseif ($v->type == 'YEAR_ORDER') {
  256. if ($v->type == 'NEW_USER') {
  257. continue;
  258. }
  259. $save_text = '年费vip会员';
  260. $text = '每天1元,全年免费看';
  261. $temp = [
  262. 'price' => (int)$v->price . '元',
  263. 'is_year_order' => 1,
  264. 'text' => $text,
  265. 'first_charge' => false,
  266. 'today_special' => false,
  267. 'save_text' => $save_text,
  268. 'product_id' => $v->id
  269. ];
  270. $data[] = $temp;
  271. } else {
  272. if ($v->type == 'NEW_USER') {
  273. continue;
  274. }
  275. $save_text = '';
  276. if ($v->given) {
  277. $save_text = round($v->given / 100, 1) . '元';
  278. $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
  279. } else {
  280. $text = sprintf('%s书币', $v->price * 100);
  281. }
  282. $temp = [
  283. 'price' => (float)$v->price . '元',
  284. 'is_year_order' => 0,
  285. 'text' => $text,
  286. 'first_charge' => false,
  287. 'today_special' => $v->is_default == 1 ? true : false,
  288. 'save_text' => $save_text,
  289. 'product_id' => $v->id
  290. ];
  291. $data[] = $temp;
  292. }
  293. }
  294. return response()->success($data);
  295. }
  296. public function substitutePayChargeList(Request $request)
  297. {
  298. $default = $request->get('product_id');
  299. if (!$this->checkUid()) {
  300. return response()->error('WAP_NOT_LOGIN');
  301. }
  302. $su = $request->get('su');
  303. $res = ProductService::getChargeProduct();
  304. if (!$res) {
  305. return response()->error('WAP_SYS_ERROR');
  306. }
  307. $is_can_allow_pay = 1;
  308. if ($su) {
  309. $su_user = UserService::getById($su);
  310. $now_user = $this->_user_info;
  311. if ($now_user && $su_user && $su_user->openid == $now_user->openid) {
  312. $is_can_allow_pay = 0;
  313. }
  314. }
  315. SubstituteOrderService::substitutePageUvPv($this->uid, $this->distribution_channel_id);
  316. $data = [];
  317. foreach ($res as $v) {
  318. if ($v->type == 'YEAR_ORDER') {
  319. $temp = [
  320. 'price' => (int)$v->price . '元',
  321. 'text' => '年费VIP会员',
  322. 'first_charge' => false,
  323. 'today_special' => $v->is_default == 1 ? true : false,
  324. 'save_text' => '全年免费',
  325. 'product_id' => $v->id,
  326. 'default' => false,
  327. 'is_vip' => 1,
  328. 'is_can_allow_pay' => $is_can_allow_pay
  329. ];
  330. if ($default) {
  331. if ($default == $v->id) {
  332. $temp['default'] = true;
  333. }
  334. } else {
  335. $temp['default'] = $v->is_default == 1 ? true : false;
  336. }
  337. $data[] = $temp;
  338. } else {
  339. $save_text = '';
  340. if ($v->given) {
  341. $save_text = round($v->given / 100, 1) . '元';
  342. $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
  343. } else {
  344. $text = sprintf('%s书币', $v->price * 100);
  345. }
  346. $temp = [
  347. 'price' => (int)$v->price . '元',
  348. 'text' => $text,
  349. 'first_charge' => false,
  350. 'today_special' => $v->is_default == 1 ? true : false,
  351. 'save_text' => $save_text,
  352. 'product_id' => $v->id,
  353. 'default' => false,
  354. 'is_vip' => 0,
  355. 'is_can_allow_pay' => $is_can_allow_pay
  356. ];
  357. if ($default) {
  358. if ($default == $v->id) {
  359. $temp['default'] = true;
  360. }
  361. } else {
  362. $temp['default'] = $v->is_default == 1 ? true : false;
  363. }
  364. $data[] = $temp;
  365. }
  366. }
  367. return response()->success($data);
  368. }
  369. /**
  370. * @apiVersion 1.0.0
  371. * @apiDescription 单本消费记录
  372. * @api {get} order/bookOrderList 单本消费记录
  373. * @apiGroup Order
  374. * @apiName bookOrderList
  375. * @apiSuccess {int} code 状态码
  376. * @apiSuccess {String} msg 信息
  377. * @apiSuccess {object} data 结果集
  378. * @apiSuccess {Int} uid uid
  379. * @apiSuccess {Int} bid bid
  380. * @apiSuccess {Int} book_name 书名
  381. * @apiSuccess {Int} fee 钱
  382. * @apiSuccess {String} created_at 时间
  383. * @apiSuccessExample {json} Success-Response:
  384. * HTTP/1.1 200 OK
  385. * {
  386. * code: 0,
  387. * msg: "",
  388. * data: list:[
  389. * {
  390. * uid: 4,
  391. * bid: 1,
  392. * book_name: "dfsedfertrwet",
  393. * fee: 100,
  394. * created_at: "2017-12-02 16:24:54"
  395. * }
  396. * ]
  397. * meta: {
  398. * total: 1,
  399. * per_page: 15,
  400. * current_page: 1,
  401. * last_page: 1,
  402. * next_page_url: "",
  403. * prev_page_url: ""
  404. * }
  405. * }
  406. */
  407. public function bookOrderList(Request $request)
  408. {
  409. if (!$this->checkUid()) {
  410. return response()->error('WAP_NOT_LOGIN');
  411. }
  412. $page_size = $request->input('page_size', 15);
  413. $book_order = BookOrderService::getRecord($this->uid, $page_size);
  414. //$book_order = BookOrder::where('uid', $this->uid)->select('bid', 'uid', 'book_name', 'created_at', 'fee')->paginate($page_size);
  415. return response()->pagination(new BookOrderTransformer(), $book_order);
  416. }
  417. /**
  418. * @apiVersion 1.0.0
  419. * @apiDescription 章节消费记录
  420. * @api {get} order/chapterOrderList 章节消费记录
  421. * @apiGroup Order
  422. * @apiName chapterOrderList
  423. * @apiSuccess {int} code 状态码
  424. * @apiSuccess {String} msg 信息
  425. * @apiSuccess {object} data 结果集
  426. * @apiSuccess {Int} uid uid
  427. * @apiSuccess {Int} bid bid
  428. * @apiSuccess {Int} cid cid
  429. * @apiSuccess {Int} chapter_name 章节名
  430. * @apiSuccess {Int} book_name 书名
  431. * @apiSuccess {Int} fee 钱
  432. * @apiSuccess {String} created_at 时间
  433. * @apiSuccessExample {json} Success-Response:
  434. * HTTP/1.1 200 OK
  435. * {
  436. * code: 0,
  437. * msg: "",
  438. * data: list:[
  439. * {
  440. * uid: 4,
  441. * bid: 1,
  442. * cid: 1,
  443. * chapter_name: "sdfsd",
  444. * book_name: "dfsedfertrwet",
  445. * fee: 100,
  446. * created_at: "2017-12-02 16:24:54"
  447. * }
  448. * ]
  449. * meta: {
  450. * total: 1,
  451. * per_page: 15,
  452. * current_page: 1,
  453. * last_page: 1,
  454. * next_page_url: "",
  455. * prev_page_url: ""
  456. * }
  457. * }
  458. */
  459. public function chapterOrderList(Request $request)
  460. {
  461. if (!$this->checkUid()) {
  462. return response()->error('WAP_NOT_LOGIN');
  463. }
  464. $chapter_model = new ChapterOrderService();
  465. $page_size = $request->input('page_size', 15);
  466. $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
  467. foreach ($chapter_order as $item){
  468. if($item->fee == 0){
  469. $result = AdVisitStatService::getInfoV2($this->uid,$item->cid,['UNLOCK','UNLOCK_2']);
  470. if($result) $item->fee = '解锁';
  471. }
  472. }
  473. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  474. }
  475. /**
  476. * @apiVersion 1.0.0
  477. * @apiDescription 充值记录
  478. * @api {get} order/chargeRecordLists 充值记录
  479. * @apiGroup Order
  480. * @apiName chargeRecordLists
  481. * @apiSuccess {int} code 状态码
  482. * @apiSuccess {String} msg 信息
  483. * @apiSuccess {object} data 结果集
  484. * @apiSuccess {String} data.price 价格
  485. * @apiSuccess {String} data.status 状态
  486. * @apiSuccess {String} data.trade_no 订单号
  487. * @apiSuccess {String} data.created_at 时间
  488. * @apiSuccessExample {json} Success-Response:
  489. * HTTP/1.1 200 OK
  490. * {
  491. * code: 0,
  492. * msg: "",
  493. * data: {
  494. * list: [
  495. * {
  496. * id: 134,
  497. * price: "1.00",
  498. * status: "PAID",
  499. * trade_no: "201712021915481585670623626232",
  500. * created_at: "2017-12-02 19:15:56"
  501. * }
  502. * ],
  503. * meta: {
  504. * total: 1,
  505. * per_page: 15,
  506. * current_page: 1,
  507. * last_page: 1,
  508. * next_page_url: "",
  509. * prev_page_url: ""
  510. * }
  511. * }
  512. * }
  513. */
  514. public function chargeRecordLists(Request $request)
  515. {
  516. $page_size = $request->input('page_size', 15);
  517. $res = OrderService::getOrderList($this->uid, $page_size);
  518. return response()->pagination(new ChargeListTransformer(), $res);
  519. }
  520. //订单是否成功
  521. public function isSuccess(Request $request)
  522. {
  523. $order = $request->input('order');
  524. $order_info = OrderService::getByTradeNo($order);
  525. if ($order_info && $order_info->status == 'PAID') {
  526. return response()->success();
  527. }
  528. return response()->error('WAP_SYS_ERROR');
  529. }
  530. public function substitutePay(Request $request)
  531. {
  532. if (!$this->checkUid()) {
  533. return response()->error('WAP_NOT_LOGIN');
  534. }
  535. $product_id = $request->get('product_id');
  536. SubstituteOrderService::substituteButtonUvPv($this->uid, $this->distribution_channel_id);
  537. $user = $this->_user_info;
  538. $data = [
  539. 'head_img' => '',
  540. 'nickname' => $user->nickname
  541. ];
  542. $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  543. $url_format = '%s://site%s.%s.com/helppay?%s';
  544. $param = [];
  545. $param['su'] = $this->uid;
  546. if ($product_id) {
  547. $param['product_id'] = $product_id;
  548. }
  549. $help_pay_page = sprintf(
  550. $url_format,
  551. env('PROTOCOL'),
  552. encodeDistributionChannelId($help_pay_page_channel_id),
  553. env('CUSTOM_HOST'),
  554. http_build_query($param)
  555. );
  556. $data['help_pay_page'] = $help_pay_page;
  557. return response()->success($data);
  558. }
  559. private function recordFirstIntoPayPage($bid)
  560. {
  561. $start_time = env('RECORD_FIRST_VISIT_PAY_PAGE_TIME');
  562. $template_id = 0;
  563. if (in_array($this->distribution_channel_id,[123,211]) && $start_time && $this->uid) {
  564. $user = $this->_user_info;
  565. if (strtotime($user->created_at) > $start_time) {
  566. $old = DB::table('user_first_visit_pay_page')->where('uid', $this->uid)->select('uid', 'template_type')->first();
  567. if ($old) {
  568. $template_id = $old->template_type;
  569. if($template_id) return $template_id;
  570. }
  571. if ($bid && $bid == 1148) {
  572. $template_id = $this->uid % 2 == 0 ? 2 : 1;
  573. if (empty($old)) {
  574. try {
  575. DB::table('user_first_visit_pay_page')->insert([
  576. 'uid' => $this->uid,
  577. 'template_type' => $template_id,
  578. 'created_at' => date('Y-m-d H:i:s'),
  579. 'updated_at' => date('Y-m-d H:i:s')
  580. ]);
  581. } catch (\Exception $e) {
  582. }
  583. }
  584. }
  585. }
  586. }
  587. return $template_id;
  588. }
  589. private function templateCompare(){
  590. $start_time = env('TEMPLATE_COMPARE_START_TIME',0);
  591. if(!in_array($this->distribution_channel_id,[2,14,211])){
  592. return 0;
  593. }
  594. $user = $this->_user_info;
  595. if(strtotime($user->created_at) < $start_time){
  596. return 0;
  597. }
  598. $user = DataAnalysisSelectUserService::getByUidAndType($this->uid,'TEMPLATE_COMPARE');
  599. if($user){
  600. $template_id = $user->attach;
  601. }else{
  602. $template_id = random_int(2,4);
  603. DataAnalysisSelectUserService::create(
  604. $this->uid,
  605. $this->distribution_channel_id,
  606. 'TEMPLATE_COMPARE',
  607. -1, $template_id);
  608. }
  609. //BookOrderService
  610. if($template_id == 3 && !OrderService::isPaidUser($this->uid)){
  611. $template_id = 2;
  612. }
  613. return $template_id;
  614. }
  615. }