TicketService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Services\Pay;
  3. use App\Services\Service;
  4. use Illuminate\Support\Facades\DB;
  5. class TicketService extends Service
  6. {
  7. public const TICKET = array(
  8. 1 => [
  9. 'name' => '5折购买券',
  10. 'discount' => 0.5,
  11. 'description' => '购买卡片时5折消费',
  12. 'validity' => 86400
  13. ],
  14. 2 => [
  15. 'name' => '5折购买券',
  16. 'discount' => 0.5,
  17. 'description' => '购买卡片时5折消费',
  18. 'validity' => 86400
  19. ],
  20. 3 => [
  21. 'name' => '72小时活动入场券',
  22. 'discount' => 0,
  23. 'description' => '72小时活动入场券',
  24. 'validity' => 86400 * 31
  25. ],
  26. 4 => [
  27. 'name' => '免费解锁券',
  28. 'discount' => 0,
  29. 'description' => '免费解锁卡片一次',
  30. 'validity' => 86400 * 365 * 10
  31. ],
  32. 5 => [
  33. 'name' => '免费解锁券',
  34. 'discount' => 0,
  35. 'description' => '免费解锁卡片一次',
  36. 'validity' => 86400 * 365 * 10
  37. ]
  38. );
  39. /**
  40. * 创建卡片[存在uid则领取]
  41. * @param int $type 类型
  42. * @param int $uid 用户
  43. * @param int $cnt 数量
  44. * @return mixed 参数cnt为1时返回卡券id;否则返回true
  45. */
  46. public function create(int $type = 1, int $uid = 0, int $cnt = 1)
  47. {
  48. $data = array();
  49. for ($i = 0; $i < $cnt; $i++) {
  50. $add = array(
  51. 'name' => self::TICKET[$type]['name'],
  52. 'description' => self::TICKET[$type]['description'],
  53. 'code' => bin2hex(\openssl_random_pseudo_bytes(4)),
  54. 'type' => $type,
  55. 'create_time' => time()
  56. );
  57. if ($uid) {
  58. $add['uid'] = $uid;
  59. $add['get_time'] = time();
  60. $add['validity_time'] = time() + self::TICKET[$type]['validity'];
  61. }
  62. array_push($data, $add);
  63. }
  64. if (1 == $cnt) {
  65. return DB::table("kdgx_partner_charge_user_ticket")->insertGetId($data[0]);
  66. } else {
  67. DB::table("kdgx_partner_charge_user_ticket")->insert($data);
  68. return true;
  69. }
  70. }
  71. }