123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App\Http\Controllers;
- use App\Exceptions\AlertException;
- use App\Http\Controllers\Core\Auth;
- use App\Models\TicketModel;
- use App\Models\User\UserModel;
- class Ticket extends Controller
- {
- public const TICKET = array(
- 1 => [
- 'name' => '5折购买券',
- 'discount' => 0.5,
- 'description' => '购买卡片时5折消费',
- 'validity' => 86400
- ],
- 2 => [
- 'name' => '5折购买券',
- 'discount' => 0.5,
- 'description' => '购买卡片时5折消费',
- 'validity' => 86400
- ],
- 3 => [
- 'name' => '分配对象补偿券',
- 'discount' => 0,
- 'description' => '抵扣分配对象入场费9.9元',
- 'validity' => 86400 * 14
- ],
- 4 => [
- 'name' => '免费解锁券',
- 'discount' => 0,
- 'description' => '免费解锁卡片一次',
- 'validity' => 86400 * 365 * 10
- ],
- 5 => [
- 'name' => '免费解锁券',
- 'discount' => 0,
- 'description' => '免费解锁卡片一次',
- 'validity' => 86400 * 365 * 10
- ]
- );
- private $ticketModel;
- public function __construct()
- {
- $this->ticketModel = new TicketModel();
- }
- /**
- * 创建卡片[存在uid则领取]
- * @param int $type
- * @param int $uid
- * @return mixed
- * @throws \ApiException
- * @deprecated
- */
- public function create(int $type = 1, int $uid = 0)
- {
- return array(
- 'code' => 404,
- 'message' => '该功能已下线'
- );
- $add = array(
- 'name' => self::TICKET[$type]['name'],
- 'description' => self::TICKET[$type]['description'],
- 'code' => bin2hex(\openssl_random_pseudo_bytes(4)),
- 'type' => $type,
- 'create_time' => time()
- );
- if ($uid) {
- $add['uid'] = $uid;
- $add['get_time'] = time();
- $add['validity_time'] = time() + self::TICKET[$type]['validity'];
- }
- $ticketModel = new TicketModel();
- $ticket = $ticketModel->fill($add);
- if ($ticket->save()) {
- return $ticket->id;
- } else {
- throw new AlertException("数据库异常", 505);
- }
- }
- /**
- * 检验卡劵是否存在
- * @param string $ticket_code
- * @return bool
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function checkTicket(string $ticket_code)
- {
- $uid = Auth::auth();
- $where = array(
- ['code', $ticket_code],
- ['validity_time', '>', time()],
- ['uid', $uid],
- ['type', 5],
- ['state', 0]
- );
- if (TicketModel::where($where)->exists()) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 抽卡
- * @return array
- * @throws \ApiException
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function randTicket()
- {
- return array(
- 'code' => 404,
- 'message' => '该功能已下线'
- );
- $uid = Auth::auth();
- $user = UserModel::find($uid);
- if ($user->ck_count > 0) {
- $rand = rand(1, 10001);
- if ($rand == 1) { # 0.01%
- $cnt = 9;
- }
- if ($rand > 1 && $rand <= 51) { # 0.5%
- $cnt = 8;
- }
- if ($rand > 51 && $rand <= 151) { # 1.0%
- $cnt = 7;
- }
- if ($rand > 151 && $rand <= 301) { # 1.5%
- $cnt = 6;
- }
- if ($rand > 301 && $rand <= 501) { # 2.0%
- $cnt = 5;
- }
- if ($rand > 501 && $rand <= 1001) { # 5.0%
- $cnt = 4;
- }
- if ($rand > 1001 && $rand <= 1501) { # 5.0%
- $cnt = 3;
- }
- if ($rand > 1501 && $rand <= 3001) { # 15%
- $cnt = 2;
- }
- if ($rand > 3001 && $rand <= 10001) { # 70%
- $cnt = 1;
- }
- for ($i = 0; $i < $cnt; $i++) {
- $this->create(5, $uid);
- }
- $user->decrement('ck_count', 1);
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => [
- 'cnt' => $i
- ],
- );
- } else {
- return array(
- 'code' => 101,
- 'message' => '没有抽卡机会'
- );
- }
- }
- }
|