Vip.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\index\controller;
  3. use addons\vip\library\Service;
  4. use addons\vip\model\Order;
  5. use addons\vip\model\Record;
  6. use app\common\controller\Frontend;
  7. use think\Exception;
  8. /**
  9. * 购买VIP
  10. */
  11. class Vip extends Frontend
  12. {
  13. protected $layout = 'default';
  14. protected $noNeedLogin = ['pay', 'epay'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * VIP列表
  18. * @return string
  19. */
  20. public function viplist()
  21. {
  22. $config = get_addon_config('vip');
  23. $vipList = [];
  24. $vipList = \addons\vip\model\Vip::where('status', '=', 'normal')->field('sales', true)->order('level', 'asc')->select();
  25. $paytypeList = [];
  26. foreach (explode(',', $config['paytypelist']) as $index => $item) {
  27. $paytypeList[] = ['value' => $item, 'image' => '/assets/addons/vip/img/' . $item . '.png', 'default' => $item === $config['defaultpaytype']];
  28. }
  29. $vipInfo = Service::getVipInfo();
  30. $this->view->assign('addonConfig', $config);
  31. $this->view->assign('vipList', $vipList);
  32. $this->view->assign('vipInfo', $vipInfo);
  33. $this->view->assign('paytypeList', $paytypeList);
  34. $this->view->assign('title', __('VIP列表'));
  35. return $this->view->fetch();
  36. }
  37. /**
  38. * VIP日志
  39. * @return string
  40. */
  41. public function record()
  42. {
  43. $recordList = Record::with(['vip'])->where('user_id', $this->auth->id)
  44. ->where('status', '<>', 'created')
  45. ->order('id', 'desc')
  46. ->paginate();
  47. $vipInfo = Service::getVipInfo();
  48. $this->view->assign('title', "VIP日志");
  49. $this->view->assign('recordList', $recordList);
  50. $this->view->assign('vipInfo', $vipInfo);
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 创建订单并发起支付请求
  55. */
  56. public function submit()
  57. {
  58. $level = $this->request->param('level/d');
  59. $days = $this->request->param('days/d');
  60. $paytype = $this->request->param('paytype', '');
  61. $vipInfo = \addons\vip\model\Vip::getByLevel($level);
  62. if (!$vipInfo) {
  63. $this->error('未找到VIP相关信息');
  64. }
  65. if ($this->auth->vip > $vipInfo['level']) {
  66. $this->error('当前VIP等级已高于购买的VIP等级');
  67. }
  68. $lastRecordInfo = Record::getLastRecord();
  69. $recordInfo = Record::where('user_id', $this->auth->id)
  70. ->where('status', 'created')
  71. ->where('level', $level)->where('days', $days)
  72. ->whereTime('createtime', '-30 minutes')
  73. ->find();
  74. if (!$recordInfo) {
  75. $amount = $vipInfo->getPriceByDays($days);
  76. $insert = [
  77. 'user_id' => $this->auth->id,
  78. 'vip_id' => $vipInfo->id,
  79. 'level' => $vipInfo->level,
  80. 'days' => $days,
  81. 'amount' => $amount,
  82. 'status' => 'created',
  83. ];
  84. $recordInfo = Record::create($insert);
  85. }
  86. try {
  87. $response = \addons\vip\library\Order::submit($vipInfo->id, $recordInfo->id, $recordInfo->amount, $paytype);
  88. } catch (\Exception $e) {
  89. $this->error($e->getMessage());
  90. }
  91. return $response;
  92. }
  93. /**
  94. * 企业支付通知和回调
  95. */
  96. public function epay()
  97. {
  98. $type = $this->request->param('type');
  99. $paytype = $this->request->param('paytype');
  100. if ($type == 'notify') {
  101. $pay = \addons\epay\library\Service::checkNotify($paytype);
  102. if (!$pay) {
  103. echo '签名错误';
  104. return;
  105. }
  106. $data = $pay->verify();
  107. try {
  108. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  109. \addons\vip\library\Order::settle($data['out_trade_no'], $payamount);
  110. } catch (Exception $e) {
  111. }
  112. echo $pay->success();
  113. } else {
  114. $pay = \addons\epay\library\Service::checkReturn($paytype);
  115. if (!$pay) {
  116. $this->error('签名错误');
  117. }
  118. //微信支付没有返回链接
  119. if ($pay === true) {
  120. $this->success("请返回网站查看支付状态!", "index/vip/viplist");
  121. }
  122. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  123. $this->success("恭喜你!支付成功!", url("index/vip/viplist"));
  124. }
  125. return;
  126. }
  127. }