Vip.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use phpu\facade\ThinkCaptcha;
  6. use Ramsey\Uuid\Uuid;
  7. use think\captcha\facade\Captcha;
  8. //VIP控制器
  9. class Vip extends BaseController
  10. {
  11. protected $noNeedLogin = [];
  12. private $platinumVip = [20, 54, 96, 120]; //铂金VIP价格
  13. private $diamondVip = [30, 81, 144, 180]; //钻石VIP价格
  14. //获取用户VIP信息,一般用于前端显示
  15. public function getUserVip()
  16. {
  17. $user = $this->getUser();
  18. $vip = Db::table('tb_user_vip')
  19. ->where(['user_id' => $user->user_id])
  20. ->find();
  21. if (!$vip) {
  22. $this->fail(500, '当前用户不是VIP');
  23. }
  24. if ($vip['expired_at'] < time()) {
  25. $this->fail(501, 'VIP已到期');
  26. }
  27. $this->success('success', $vip);
  28. }
  29. //获取VIP价格
  30. //铂金VIP,包月20R,包季度54R,包半年96R,包年120R
  31. //钻石VIP,包月30R,包季度81R,包半年144R,包年180R
  32. public function getVipPrice()
  33. {
  34. $type = $this->request->post('type'); //VIP类型 1:铂金 2:钻石
  35. if (!is_numeric($type)) {
  36. $this->fail(500, '参数校验错误');
  37. }
  38. $type = intval($type);
  39. if (!in_array($type, [1, 2])) {
  40. $this->fail(500, '参数校验错误');
  41. }
  42. $user = $this->getUser();
  43. $userVipPrice = Db::table('tb_user_vip_price')
  44. ->where(['user_id' => $user->user_id])
  45. ->where(['type' => $type])
  46. ->count();
  47. $priceList = $type == 1 ? $this->platinumVip : $this->diamondVip;
  48. if ($userVipPrice <= 0) {
  49. //插入月付价格
  50. Db::table('tb_user_vip_price')
  51. ->insert([
  52. 'user_id' => $user->user_id,
  53. 'type' => $type,
  54. 'duration' => 1,
  55. 'price' => $priceList[0],
  56. ]);
  57. //插入季付价格
  58. Db::table('tb_user_vip_price')
  59. ->insert([
  60. 'user_id' => $user->user_id,
  61. 'type' => $type,
  62. 'duration' => 3,
  63. 'price' => $priceList[1],
  64. ]);
  65. //插入半年付价格
  66. Db::table('tb_user_vip_price')
  67. ->insert([
  68. 'user_id' => $user->user_id,
  69. 'type' => $type,
  70. 'duration' => 6,
  71. 'price' => $priceList[2],
  72. ]);
  73. //插入一年付价格
  74. Db::table('tb_user_vip_price')
  75. ->insert([
  76. 'user_id' => $user->user_id,
  77. 'type' => $type,
  78. 'duration' => 12,
  79. 'price' => $priceList[3],
  80. ]);
  81. }
  82. $data = Db::table('tb_user_vip_price')
  83. ->where(['user_id' => $user->user_id])
  84. ->where(['type' => $type])
  85. ->select();
  86. $this->success('success', $data);
  87. }
  88. //购买开通VIP
  89. //铂金VIP,包月20R,包季度54R,包半年96R,包年120R
  90. //钻石VIP,包月30R,包季度81R,包半年144R,包年180R
  91. public function buyVip()
  92. {
  93. $type = $this->request->post('type'); //VIP类型 1:铂金 2:钻石
  94. $duration = $this->request->post('duration'); //周期 按月1,3,6,12
  95. if (!is_numeric($type) || !is_numeric($duration)) {
  96. $this->fail(500, '参数校验错误');
  97. }
  98. $type = intval($type);
  99. $duration = intval($duration);
  100. if (!in_array($type, [1, 2]) || !in_array($duration, [1, 3, 6, 12])) {
  101. $this->fail(500, '参数校验错误');
  102. }
  103. $user = $this->getUser();
  104. $userVipPrice = Db::table('tb_user_vip_price')
  105. ->where(['user_id' => $user->user_id])
  106. ->where(['type' => $type])
  107. ->count();
  108. $priceList = $type == 1 ? $this->platinumVip : $this->diamondVip;
  109. if ($userVipPrice <= 0) {
  110. //插入月付价格
  111. Db::table('tb_user_vip_price')
  112. ->insert([
  113. 'user_id' => $user->user_id,
  114. 'type' => $type,
  115. 'duration' => 1,
  116. 'price' => $priceList[0],
  117. ]);
  118. //插入季付价格
  119. Db::table('tb_user_vip_price')
  120. ->insert([
  121. 'user_id' => $user->user_id,
  122. 'type' => $type,
  123. 'duration' => 3,
  124. 'price' => $priceList[1],
  125. ]);
  126. //插入半年付价格
  127. Db::table('tb_user_vip_price')
  128. ->insert([
  129. 'user_id' => $user->user_id,
  130. 'type' => $type,
  131. 'duration' => 6,
  132. 'price' => $priceList[2],
  133. ]);
  134. //插入一年付价格
  135. Db::table('tb_user_vip_price')
  136. ->insert([
  137. 'user_id' => $user->user_id,
  138. 'type' => $type,
  139. 'duration' => 12,
  140. 'price' => $priceList[3],
  141. ]);
  142. }
  143. $data = Db::table('tb_user_vip_price')
  144. ->where(['user_id' => $user->user_id])
  145. ->where(['type' => $type])
  146. ->where(['duration' => $duration])
  147. ->find();
  148. $vipPrice = $data['price'];
  149. $balanceUser = Db::table('tb_user')
  150. ->where(['id' => $user->user_id])
  151. ->find();
  152. if ($balanceUser['balance'] < $vipPrice) {
  153. $this->fail(501, '余额不足无法开通');
  154. }
  155. //扣除余额
  156. Db::table('tb_user')
  157. ->where(['id' => $user->user_id])
  158. ->update([
  159. 'balance' => $balanceUser['balance'] - $vipPrice,
  160. ]);
  161. //开通VIP
  162. $vip = Db::table('tb_user_vip')
  163. ->where(['user_id' => $user->user_id])
  164. ->find();
  165. $startTime = 0;
  166. $expiredTime = 0;
  167. if (!$vip) {
  168. $startTime = time();
  169. $expiredTime = time() + 60 * 60 * 24 * 30 * $duration;
  170. Db::table('tb_user_vip')
  171. ->insert([
  172. 'user_id' => $user->user_id,
  173. 'type' => $type,
  174. 'created_at' => time(),
  175. 'expired_at' => time() + 60 * 60 * 24 * 30 * $duration,
  176. ]);
  177. } else {
  178. $expired = false;
  179. if ($vip['expired_at'] < time()) {
  180. $expired = true;
  181. }
  182. $startTime = time();
  183. $expiredTime = $expired ? time() + 60 * 60 * 24 * 30 * $duration : $vip['expired_at'] + 60 * 60 * 24 * 30 * $duration;
  184. Db::table('tb_user_vip')
  185. ->where(['user_id' => $user->user_id])
  186. ->update([
  187. 'type' => $type,
  188. 'expired_at' => $expired ? time() + 60 * 60 * 24 * 30 * $duration : $vip['expired_at'] + 60 * 60 * 24 * 30 * $duration,
  189. ]);
  190. }
  191. //写VIP购买记录
  192. Db::table('tb_user_vip_record')
  193. ->insert([
  194. 'user_id' => $user->user_id,
  195. 'price' => $vipPrice,
  196. 'type' => $type,
  197. 'duration' => $duration,
  198. 'start_time' => $startTime,
  199. 'expired_at' => $expiredTime,
  200. 'created_at' => time(),
  201. ]);
  202. $this->success('success', null);
  203. }
  204. }