Ticket.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Models\TicketModel;
  6. use App\Models\User\UserModel;
  7. class Ticket extends Controller
  8. {
  9. public const TICKET = array(
  10. 1 => [
  11. 'name' => '5折购买券',
  12. 'discount' => 0.5,
  13. 'description' => '购买卡片时5折消费',
  14. 'validity' => 86400
  15. ],
  16. 2 => [
  17. 'name' => '5折购买券',
  18. 'discount' => 0.5,
  19. 'description' => '购买卡片时5折消费',
  20. 'validity' => 86400
  21. ],
  22. 3 => [
  23. 'name' => '分配对象补偿券',
  24. 'discount' => 0,
  25. 'description' => '抵扣分配对象入场费9.9元',
  26. 'validity' => 86400 * 14
  27. ],
  28. 4 => [
  29. 'name' => '免费解锁券',
  30. 'discount' => 0,
  31. 'description' => '免费解锁卡片一次',
  32. 'validity' => 86400 * 365 * 10
  33. ],
  34. 5 => [
  35. 'name' => '免费解锁券',
  36. 'discount' => 0,
  37. 'description' => '免费解锁卡片一次',
  38. 'validity' => 86400 * 365 * 10
  39. ]
  40. );
  41. private $ticketModel;
  42. public function __construct()
  43. {
  44. $this->ticketModel = new TicketModel();
  45. }
  46. /**
  47. * 创建卡片[存在uid则领取]
  48. * @param int $type
  49. * @param int $uid
  50. * @return mixed
  51. * @throws \ApiException
  52. * @deprecated
  53. */
  54. public function create(int $type = 1, int $uid = 0)
  55. {
  56. return array(
  57. 'code' => 404,
  58. 'message' => '该功能已下线'
  59. );
  60. $add = array(
  61. 'name' => self::TICKET[$type]['name'],
  62. 'description' => self::TICKET[$type]['description'],
  63. 'code' => bin2hex(\openssl_random_pseudo_bytes(4)),
  64. 'type' => $type,
  65. 'create_time' => time()
  66. );
  67. if ($uid) {
  68. $add['uid'] = $uid;
  69. $add['get_time'] = time();
  70. $add['validity_time'] = time() + self::TICKET[$type]['validity'];
  71. }
  72. $ticketModel = new TicketModel();
  73. $ticket = $ticketModel->fill($add);
  74. if ($ticket->save()) {
  75. return $ticket->id;
  76. } else {
  77. throw new AlertException("数据库异常", 505);
  78. }
  79. }
  80. /**
  81. * 检验卡劵是否存在
  82. * @param string $ticket_code
  83. * @return bool
  84. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  85. */
  86. public function checkTicket(string $ticket_code)
  87. {
  88. $uid = Auth::auth();
  89. $where = array(
  90. ['code', $ticket_code],
  91. ['validity_time', '>', time()],
  92. ['uid', $uid],
  93. ['type', 5],
  94. ['state', 0]
  95. );
  96. if (TicketModel::where($where)->exists()) {
  97. return true;
  98. } else {
  99. return false;
  100. }
  101. }
  102. /**
  103. * 抽卡
  104. * @return array
  105. * @throws \ApiException
  106. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  107. */
  108. public function randTicket()
  109. {
  110. return array(
  111. 'code' => 404,
  112. 'message' => '该功能已下线'
  113. );
  114. $uid = Auth::auth();
  115. $user = UserModel::find($uid);
  116. if ($user->ck_count > 0) {
  117. $rand = rand(1, 10001);
  118. if ($rand == 1) { # 0.01%
  119. $cnt = 9;
  120. }
  121. if ($rand > 1 && $rand <= 51) { # 0.5%
  122. $cnt = 8;
  123. }
  124. if ($rand > 51 && $rand <= 151) { # 1.0%
  125. $cnt = 7;
  126. }
  127. if ($rand > 151 && $rand <= 301) { # 1.5%
  128. $cnt = 6;
  129. }
  130. if ($rand > 301 && $rand <= 501) { # 2.0%
  131. $cnt = 5;
  132. }
  133. if ($rand > 501 && $rand <= 1001) { # 5.0%
  134. $cnt = 4;
  135. }
  136. if ($rand > 1001 && $rand <= 1501) { # 5.0%
  137. $cnt = 3;
  138. }
  139. if ($rand > 1501 && $rand <= 3001) { # 15%
  140. $cnt = 2;
  141. }
  142. if ($rand > 3001 && $rand <= 10001) { # 70%
  143. $cnt = 1;
  144. }
  145. for ($i = 0; $i < $cnt; $i++) {
  146. $this->create(5, $uid);
  147. }
  148. $user->decrement('ck_count', 1);
  149. return array(
  150. 'code' => 200,
  151. 'message' => 'success',
  152. 'data' => [
  153. 'cnt' => $i
  154. ],
  155. );
  156. } else {
  157. return array(
  158. 'code' => 101,
  159. 'message' => '没有抽卡机会'
  160. );
  161. }
  162. }
  163. }