VipController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Controllers\V1\User;
  3. use App\Helper\SerialNumber;
  4. use App\Http\Controllers\V1\Controller;
  5. use App\Models\Pay;
  6. use App\Models\Setting;
  7. use App\Models\UserConsumeRecord;
  8. use App\Models\UserEpisodesRecord;
  9. use App\Models\UserRechargeRecord;
  10. use App\Models\UserVipRecord;
  11. use App\Models\UserWatchRecord;
  12. use App\Models\VipCombo;
  13. use Carbon\Carbon;
  14. use Dingo\Api\Http\Request;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Database\QueryException;
  17. class VipController extends Controller
  18. {
  19. public function setting()
  20. {
  21. $lists = VipCombo::select(['id','name','price','valid_day','desc'])->where('status', 1)->get();
  22. return $this->success($lists);
  23. }
  24. public function createOrder()
  25. {
  26. try {
  27. /* @var Setting $setting*/
  28. $setting = Setting::first();
  29. if($setting->is_review) {
  30. throw new \Exception('暂不支持,请联系管理员');
  31. }
  32. \DB::beginTransaction();
  33. $comboId = \request()->input('id');
  34. $app = $this->getUniFactory(\user()->info->platform);
  35. $combo = VipCombo::find($comboId);
  36. // 避免重复创建订单 一个小时内有未支付的订单直接使用
  37. $vip = UserVipRecord::where('user_id', \user()->id)
  38. ->where('combo_id', $combo->id)
  39. ->where('status', 0)
  40. ->where('created_at','>=', Carbon::now()->subHour()->toDateTimeString())
  41. ->first();
  42. if($vip){
  43. $pay = Pay::find($vip->pay_id);
  44. if(\user()->info->platform == 3){
  45. $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
  46. $res = json_decode($res, true);
  47. }else{
  48. $res = [
  49. 'order_id' => $pay->prepay_id,
  50. 'order_token' => $pay->token,
  51. ];
  52. }
  53. }else{
  54. $res = app(Pay::class)->create($app, $combo->price,Pay::SOURCE_BUY_VIP);
  55. $vip = new UserVipRecord();
  56. $vip->user_id = \user()->id;
  57. $vip->combo_id = $combo->id;
  58. $vip->valid_day = $combo->valid_day;
  59. $vip->pay_id = $res['pay_id'];
  60. $vip->status = 0;
  61. $vip->save();
  62. if(\user()->info->platform == 3){
  63. $pay = Pay::find($res['pay_id']);
  64. $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
  65. $res = json_decode($res, true);
  66. }
  67. }
  68. $res['pay_id'] = $vip->pay_id;
  69. \DB::commit();
  70. return $this->success($res);
  71. }catch (QueryException $e){
  72. \DB::rollBack();
  73. return $this->error('下单失败');
  74. }catch (\Exception $e){
  75. \DB::rollBack();
  76. return $this->error($e->getMessage());
  77. }
  78. }
  79. }