CodeController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * 兑换码控制器.
  4. * @author ningque
  5. * @date 2020-02-19 21:13:09
  6. */
  7. declare(strict_types=1);
  8. namespace Modules\Api\Controllers;
  9. use App\Model\UserModel;
  10. use App\Repositories\Service\QueueService;
  11. use App\Repositories\UserRepository;
  12. use Carbon\Carbon;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  15. class CodeController extends BaseController
  16. {
  17. /**
  18. * 兑换码信息
  19. */
  20. public function info(ValidatorFactoryInterface $validatorFactory)
  21. {
  22. $inputData = $this->getJsonData();
  23. $rules = [
  24. 'code' => 'required|string',
  25. ];
  26. $validator = $validatorFactory->make($inputData, $rules);
  27. if ($validator->fails()) {
  28. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  29. return json_fail($validator->errors()->first());
  30. }
  31. $code = Db::table('vip_code')->where('code', $inputData['code'])->first();
  32. if (!$code) {
  33. return json_fail('无效的兑换码');
  34. }
  35. if ($code->type == 1) {
  36. //活动类
  37. $isExchange = Db::table('vip_code_exchange')
  38. ->where('uid', $this->getUserId())
  39. ->where('cid', $code->id)
  40. ->count();
  41. $code->is_exchange = $isExchange ? 1 : 0;
  42. } else {
  43. $code->is_exchange = !$code->status ? 1 : 0;
  44. }
  45. return json_success($code);
  46. }
  47. /**
  48. * 兑换码兑换
  49. */
  50. public function exchage(ValidatorFactoryInterface $validatorFactory)
  51. {
  52. $inputData = $this->getJsonData();
  53. $rules = [
  54. 'code' => 'required|string',
  55. ];
  56. $validator = $validatorFactory->make($inputData, $rules);
  57. if ($validator->fails()) {
  58. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  59. return json_fail($validator->errors()->first());
  60. }
  61. $code = Db::table('vip_code')
  62. ->where('code', $inputData['code'])
  63. ->where('status', 1)
  64. ->first();
  65. if (!$code) {
  66. return json_fail('无效的兑换码或兑换码已被兑换');
  67. }
  68. if ($code->expire < date('Y-m-d')) {
  69. return json_fail('该兑换码已过期');
  70. }
  71. $user_code = $this->getUserId();
  72. Db::beginTransaction();
  73. if ($code->type == 1) {
  74. //活动类
  75. $limit = Db::table('vip_code_exchange')
  76. ->where('cid', $code->id)
  77. ->count();
  78. if ($limit > $code->limit) {
  79. return json_fail('该兑换码已达上限');
  80. }
  81. $isExchange = Db::table('vip_code_exchange')
  82. ->where('uid', $user_code)
  83. ->where('cid', $code->id)
  84. ->count();
  85. if ($isExchange) {
  86. return json_fail('你已经兑换了该活动码');
  87. }
  88. $insert = Db::table('vip_code_exchange')
  89. ->insert(
  90. [
  91. 'uid' => $user_code,
  92. 'cid' => $code->id,
  93. 'created_at' => Carbon::now()->toDateTimeString()
  94. ]
  95. );
  96. $code->is_exchange = 1;
  97. } else {
  98. Db::table('vip_code')
  99. ->where('code', $inputData['code'])
  100. ->update(['status' => '0']);
  101. $code->is_exchange = 1;
  102. }
  103. $userInfo = make(UserRepository::class)
  104. ->getUserInfoFromCache($user_code);
  105. if ($userInfo['is_vip'] == 'y') {
  106. $vipTime = Carbon::parse($userInfo['vip_expired'])
  107. ->addDays($code->day)
  108. ->toDateTimeString();
  109. } else {
  110. $vipTime = Carbon::now()
  111. ->addDays($code->day)
  112. ->toDateTimeString();
  113. }
  114. $result = UserModel::query()
  115. ->where('code', $user_code)
  116. ->update(['vip_expired' => $vipTime]);
  117. if ($result) {
  118. // 加mongodb兑换日志
  119. make(QueueService::class)->addExchangeCodeJob(
  120. [
  121. 'nick' => $userInfo['nick'],
  122. 'user_id' => $userInfo['id'],
  123. 'user_code' => $userInfo['code'],
  124. 'code' => $code->code,
  125. 'title' => $code->title,
  126. 'day' => $code->day,
  127. ]
  128. );
  129. Db::commit();
  130. } else {
  131. Db::rollBack();
  132. return json_fail('兑换失败');
  133. }
  134. make(UserRepository::class)->delUserCache($user_code);
  135. return json_success($code);
  136. }
  137. }