OrdersController.php 24 KB

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