OrdersController.php 22 KB

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