OrdersController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. if ($bid) {
  186. $bid = Hashids::decode($bid)[0];
  187. \Log::info('order:$bid:' . $bid);
  188. $book = BookService::getBookById($bid);
  189. \Log::info('order:$$book:' . json_encode($book));
  190. if ($book->size < 200000) {
  191. $template_id = 1;
  192. }
  193. } else {
  194. $template_id = 1;
  195. }
  196. }
  197. if($temp){
  198. $temp_bid = 0;
  199. try{
  200. $temp_bid = Hashids::decode($temp)[0];
  201. }catch (\Exception $e){}
  202. $t = $this->recordFirstIntoPayPage($temp_bid);
  203. if ($t) {
  204. $template_id = $t;
  205. }
  206. }
  207. \Log::info('recordFirstIntoPayPage:$template_id:' . $template_id);
  208. $res = ProductService::getChargeProduct($template_id);
  209. if (!$res) {
  210. return response()->error('WAP_SYS_ERROR');
  211. }
  212. /*if ($this->send_order_id) {
  213. try {
  214. Redis::sadd('pay_page_uv' . $this->send_order_id, $this->uid);
  215. Redis::sadd('pay_page_uv_send_order_ids', $this->send_order_id);
  216. } catch (\Exception $e) {
  217. }
  218. }*/
  219. //TODO 长篇小数才有模板2
  220. //$user = $this->_user_info;
  221. $uid = $this->uid;
  222. $is_first_recharge = OrderService::judgeUserFirstRecharge($uid);
  223. //yqLog('user')->info('user is ',['user'=>$user]);
  224. //yqLog('user')->info('user is ',['charge_count'=>$user->charge_count]);
  225. $data = [];
  226. foreach ($res as $v) {
  227. if ($v->type == 'NEW_USER' && $is_first_recharge) {
  228. if (env('NO_NEW_USER_CHARGE') &&
  229. in_array(
  230. $this->distribution_channel_id,
  231. explode(',', env('NO_NEW_USER_CHARGE'))
  232. )
  233. ) {
  234. continue;
  235. }
  236. $temp = [
  237. 'price' => (int)$v->price . '元',
  238. 'is_year_order' => 0,
  239. 'text' => sprintf('%s+%s书币', $v->price * 100, $v->given),
  240. 'first_charge' => true,
  241. 'today_special' => false,
  242. 'save_text' => round($v->given / 100, 1) . '元',
  243. 'product_id' => $v->id
  244. ];
  245. $data[] = $temp;
  246. } elseif ($v->type == 'YEAR_ORDER') {
  247. if ($v->type == 'NEW_USER') {
  248. continue;
  249. }
  250. $save_text = '年费vip会员';
  251. $text = '每天1元,全年免费看';
  252. $temp = [
  253. 'price' => (int)$v->price . '元',
  254. 'is_year_order' => 1,
  255. 'text' => $text,
  256. 'first_charge' => false,
  257. 'today_special' => false,
  258. 'save_text' => $save_text,
  259. 'product_id' => $v->id
  260. ];
  261. $data[] = $temp;
  262. } else {
  263. if ($v->type == 'NEW_USER') {
  264. continue;
  265. }
  266. $save_text = '';
  267. if ($v->given) {
  268. $save_text = round($v->given / 100, 1) . '元';
  269. $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
  270. } else {
  271. $text = sprintf('%s书币', $v->price * 100);
  272. }
  273. $temp = [
  274. 'price' => (int)$v->price . '元',
  275. 'is_year_order' => 0,
  276. 'text' => $text,
  277. 'first_charge' => false,
  278. 'today_special' => $v->is_default == 1 ? true : false,
  279. 'save_text' => $save_text,
  280. 'product_id' => $v->id
  281. ];
  282. $data[] = $temp;
  283. }
  284. }
  285. return response()->success($data);
  286. }
  287. public function substitutePayChargeList(Request $request)
  288. {
  289. $default = $request->get('product_id');
  290. if (!$this->checkUid()) {
  291. return response()->error('WAP_NOT_LOGIN');
  292. }
  293. $su = $request->get('su');
  294. $res = ProductService::getChargeProduct();
  295. if (!$res) {
  296. return response()->error('WAP_SYS_ERROR');
  297. }
  298. $is_can_allow_pay = 1;
  299. if ($su) {
  300. $su_user = UserService::getById($su);
  301. $now_user = $this->_user_info;
  302. if ($now_user && $su_user && $su_user->openid == $now_user->openid) {
  303. $is_can_allow_pay = 0;
  304. }
  305. }
  306. SubstituteOrderService::substitutePageUvPv($this->uid, $this->distribution_channel_id);
  307. $data = [];
  308. foreach ($res as $v) {
  309. if ($v->type == 'YEAR_ORDER') {
  310. $temp = [
  311. 'price' => (int)$v->price . '元',
  312. 'text' => '年费VIP会员',
  313. 'first_charge' => false,
  314. 'today_special' => $v->is_default == 1 ? true : false,
  315. 'save_text' => '全年免费',
  316. 'product_id' => $v->id,
  317. 'default' => false,
  318. 'is_vip' => 1,
  319. 'is_can_allow_pay' => $is_can_allow_pay
  320. ];
  321. if ($default) {
  322. if ($default == $v->id) {
  323. $temp['default'] = true;
  324. }
  325. } else {
  326. $temp['default'] = $v->is_default == 1 ? true : false;
  327. }
  328. $data[] = $temp;
  329. } else {
  330. $save_text = '';
  331. if ($v->given) {
  332. $save_text = round($v->given / 100, 1) . '元';
  333. $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
  334. } else {
  335. $text = sprintf('%s书币', $v->price * 100);
  336. }
  337. $temp = [
  338. 'price' => (int)$v->price . '元',
  339. 'text' => $text,
  340. 'first_charge' => false,
  341. 'today_special' => $v->is_default == 1 ? true : false,
  342. 'save_text' => $save_text,
  343. 'product_id' => $v->id,
  344. 'default' => false,
  345. 'is_vip' => 0,
  346. 'is_can_allow_pay' => $is_can_allow_pay
  347. ];
  348. if ($default) {
  349. if ($default == $v->id) {
  350. $temp['default'] = true;
  351. }
  352. } else {
  353. $temp['default'] = $v->is_default == 1 ? true : false;
  354. }
  355. $data[] = $temp;
  356. }
  357. }
  358. return response()->success($data);
  359. }
  360. /**
  361. * @apiVersion 1.0.0
  362. * @apiDescription 单本消费记录
  363. * @api {get} order/bookOrderList 单本消费记录
  364. * @apiGroup Order
  365. * @apiName bookOrderList
  366. * @apiSuccess {int} code 状态码
  367. * @apiSuccess {String} msg 信息
  368. * @apiSuccess {object} data 结果集
  369. * @apiSuccess {Int} uid uid
  370. * @apiSuccess {Int} bid bid
  371. * @apiSuccess {Int} book_name 书名
  372. * @apiSuccess {Int} fee 钱
  373. * @apiSuccess {String} created_at 时间
  374. * @apiSuccessExample {json} Success-Response:
  375. * HTTP/1.1 200 OK
  376. * {
  377. * code: 0,
  378. * msg: "",
  379. * data: list:[
  380. * {
  381. * uid: 4,
  382. * bid: 1,
  383. * book_name: "dfsedfertrwet",
  384. * fee: 100,
  385. * created_at: "2017-12-02 16:24:54"
  386. * }
  387. * ]
  388. * meta: {
  389. * total: 1,
  390. * per_page: 15,
  391. * current_page: 1,
  392. * last_page: 1,
  393. * next_page_url: "",
  394. * prev_page_url: ""
  395. * }
  396. * }
  397. */
  398. public function bookOrderList(Request $request)
  399. {
  400. if (!$this->checkUid()) {
  401. return response()->error('WAP_NOT_LOGIN');
  402. }
  403. $page_size = $request->input('page_size', 15);
  404. $book_order = BookOrderService::getRecord($this->uid, $page_size);
  405. //$book_order = BookOrder::where('uid', $this->uid)->select('bid', 'uid', 'book_name', 'created_at', 'fee')->paginate($page_size);
  406. return response()->pagination(new BookOrderTransformer(), $book_order);
  407. }
  408. /**
  409. * @apiVersion 1.0.0
  410. * @apiDescription 章节消费记录
  411. * @api {get} order/chapterOrderList 章节消费记录
  412. * @apiGroup Order
  413. * @apiName chapterOrderList
  414. * @apiSuccess {int} code 状态码
  415. * @apiSuccess {String} msg 信息
  416. * @apiSuccess {object} data 结果集
  417. * @apiSuccess {Int} uid uid
  418. * @apiSuccess {Int} bid bid
  419. * @apiSuccess {Int} cid cid
  420. * @apiSuccess {Int} chapter_name 章节名
  421. * @apiSuccess {Int} book_name 书名
  422. * @apiSuccess {Int} fee 钱
  423. * @apiSuccess {String} created_at 时间
  424. * @apiSuccessExample {json} Success-Response:
  425. * HTTP/1.1 200 OK
  426. * {
  427. * code: 0,
  428. * msg: "",
  429. * data: list:[
  430. * {
  431. * uid: 4,
  432. * bid: 1,
  433. * cid: 1,
  434. * chapter_name: "sdfsd",
  435. * book_name: "dfsedfertrwet",
  436. * fee: 100,
  437. * created_at: "2017-12-02 16:24:54"
  438. * }
  439. * ]
  440. * meta: {
  441. * total: 1,
  442. * per_page: 15,
  443. * current_page: 1,
  444. * last_page: 1,
  445. * next_page_url: "",
  446. * prev_page_url: ""
  447. * }
  448. * }
  449. */
  450. public function chapterOrderList(Request $request)
  451. {
  452. if (!$this->checkUid()) {
  453. return response()->error('WAP_NOT_LOGIN');
  454. }
  455. $chapter_model = new ChapterOrderService();
  456. $page_size = $request->input('page_size', 15);
  457. $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
  458. foreach ($chapter_order as $item){
  459. if($item->fee == 0){
  460. $result = AdVisitStatService::getInfo($this->uid,$item->cid,'UNLOCK');
  461. if($result) $item->fee = '解锁';
  462. }
  463. }
  464. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  465. }
  466. /**
  467. * @apiVersion 1.0.0
  468. * @apiDescription 充值记录
  469. * @api {get} order/chargeRecordLists 充值记录
  470. * @apiGroup Order
  471. * @apiName chargeRecordLists
  472. * @apiSuccess {int} code 状态码
  473. * @apiSuccess {String} msg 信息
  474. * @apiSuccess {object} data 结果集
  475. * @apiSuccess {String} data.price 价格
  476. * @apiSuccess {String} data.status 状态
  477. * @apiSuccess {String} data.trade_no 订单号
  478. * @apiSuccess {String} data.created_at 时间
  479. * @apiSuccessExample {json} Success-Response:
  480. * HTTP/1.1 200 OK
  481. * {
  482. * code: 0,
  483. * msg: "",
  484. * data: {
  485. * list: [
  486. * {
  487. * id: 134,
  488. * price: "1.00",
  489. * status: "PAID",
  490. * trade_no: "201712021915481585670623626232",
  491. * created_at: "2017-12-02 19:15:56"
  492. * }
  493. * ],
  494. * meta: {
  495. * total: 1,
  496. * per_page: 15,
  497. * current_page: 1,
  498. * last_page: 1,
  499. * next_page_url: "",
  500. * prev_page_url: ""
  501. * }
  502. * }
  503. * }
  504. */
  505. public function chargeRecordLists(Request $request)
  506. {
  507. $page_size = $request->input('page_size', 15);
  508. $res = OrderService::getOrderList($this->uid, $page_size);
  509. return response()->pagination(new ChargeListTransformer(), $res);
  510. }
  511. //订单是否成功
  512. public function isSuccess(Request $request)
  513. {
  514. $order = $request->input('order');
  515. $order_info = OrderService::getByTradeNo($order);
  516. if ($order_info && $order_info->status == 'PAID') {
  517. return response()->success();
  518. }
  519. return response()->error('WAP_SYS_ERROR');
  520. }
  521. public function substitutePay(Request $request)
  522. {
  523. if (!$this->checkUid()) {
  524. return response()->error('WAP_NOT_LOGIN');
  525. }
  526. $product_id = $request->get('product_id');
  527. SubstituteOrderService::substituteButtonUvPv($this->uid, $this->distribution_channel_id);
  528. $user = $this->_user_info;
  529. $data = [
  530. 'head_img' => '',
  531. 'nickname' => $user->nickname
  532. ];
  533. $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  534. $url_format = '%s://site%s.%s.com/helppay?%s';
  535. $param = [];
  536. $param['su'] = $this->uid;
  537. if ($product_id) {
  538. $param['product_id'] = $product_id;
  539. }
  540. $help_pay_page = sprintf(
  541. $url_format,
  542. env('PROTOCOL'),
  543. encodeDistributionChannelId($help_pay_page_channel_id),
  544. env('CUSTOM_HOST'),
  545. http_build_query($param)
  546. );
  547. $data['help_pay_page'] = $help_pay_page;
  548. return response()->success($data);
  549. }
  550. private function recordFirstIntoPayPage($bid)
  551. {
  552. $start_time = env('RECORD_FIRST_VISIT_PAY_PAGE_TIME');
  553. $template_id = 0;
  554. if (in_array($this->distribution_channel_id,[123,211]) && $start_time && $this->uid) {
  555. $user = $this->_user_info;
  556. if (strtotime($user->created_at) > $start_time) {
  557. $old = DB::table('user_first_visit_pay_page')->where('uid', $this->uid)->select('uid', 'template_type')->first();
  558. if ($old) {
  559. $template_id = $old->template_type;
  560. if($template_id) return $template_id;
  561. }
  562. if ($bid && $bid == 1148) {
  563. $template_id = $this->uid % 2 == 0 ? 2 : 1;
  564. if (empty($old)) {
  565. try {
  566. DB::table('user_first_visit_pay_page')->insert([
  567. 'uid' => $this->uid,
  568. 'template_type' => $template_id,
  569. 'created_at' => date('Y-m-d H:i:s'),
  570. 'updated_at' => date('Y-m-d H:i:s')
  571. ]);
  572. } catch (\Exception $e) {
  573. }
  574. }
  575. }
  576. }
  577. }
  578. return $template_id;
  579. }
  580. }