VipController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * VIP套餐控制器.
  4. * @author zhangjunbao <junbaoz1314@gmail.com>
  5. * @date 2020-01-29 10:13:09
  6. */
  7. declare(strict_types=1);
  8. namespace Modules\Api\Controllers;
  9. use App\Repositories\Service\PaymentService;
  10. use App\Repositories\VipRepository;
  11. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  12. use App\Repositories\UserRepository;
  13. class VipController extends BaseController
  14. {
  15. /**
  16. * VIP套餐信息
  17. * @param VipRepository $vipRepository
  18. * @return mixed
  19. */
  20. public function lists(VipRepository $vipRepository)
  21. {
  22. $userInfo = make(UserRepository::class)->getUserInfoFromCache($this->getUserId());
  23. $result = [
  24. 'plans' => [],
  25. 'ad' => [], // TODO 后期可能会用到.
  26. ];
  27. $packages = $vipRepository->getVipPackage($userInfo['coins'], $userInfo['device_type']);
  28. $result['plans'] = $packages;
  29. return json_success($result);
  30. }
  31. /**
  32. * 购买vip套餐
  33. * @param VipRepository $vipRepository
  34. * @return mixed
  35. */
  36. public function buyvip(VipRepository $vipRepository)
  37. {
  38. $inputData = $this->getJsonData();
  39. $rules = [
  40. 'vip_id' => 'required|integer',
  41. 'pay_id' => 'required|integer',
  42. 'pay_type' => 'required|integer',
  43. ];
  44. $validator = make(ValidatorFactoryInterface::class)
  45. ->make($inputData, $rules);
  46. if ($validator->fails()) {
  47. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  48. return json_fail($validator->errors()->first());
  49. }
  50. $inputData['user_id'] = $this->getUserId();
  51. $inputData['order_type'] = 1;
  52. if ($inputData['pay_type'] == 4) {
  53. $order = $vipRepository->createCoinVipOrder($inputData);
  54. if ($order) {
  55. return json_success('vip购买成功');
  56. }
  57. } else {
  58. $order = $vipRepository->createVipOrder($inputData);
  59. if ($order) {
  60. //去生成支付链接
  61. $payLink = make(PaymentService::class)->getGateway($order, $inputData['pay_id']);
  62. if (!empty($payLink)) {
  63. $data = array(
  64. 'jump'=>'y',
  65. 'link'=>checkPayUrl($payLink),
  66. );
  67. return json_success($data);
  68. }
  69. }
  70. }
  71. return json_fail('亲,请重试');
  72. }
  73. /**
  74. * VIP结构的jdata独立给android的接口
  75. * @return mixed
  76. */
  77. public function vipJdata()
  78. {
  79. $vipRepo = make(VipRepository::class);
  80. $userInfo = make(UserRepository::class)->getUserInfoData($this->getUserId());
  81. $packages = $vipRepo->getVipPackage($userInfo['coins']);
  82. $result = [];
  83. if (!empty($packages)) {
  84. foreach ($packages as $item) {
  85. $result[] = $item['jdata'];
  86. }
  87. }
  88. return json_success($result);
  89. }
  90. }