| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- /**
- * 兑换码控制器.
- * @author ningque
- * @date 2020-02-19 21:13:09
- */
- declare(strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Model\UserModel;
- use App\Repositories\Service\QueueService;
- use App\Repositories\UserRepository;
- use Carbon\Carbon;
- use Hyperf\DbConnection\Db;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- class CodeController extends BaseController
- {
- /**
- * 兑换码信息
- */
- public function info(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'code' => 'required|string',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $code = Db::table('vip_code')->where('code', $inputData['code'])->first();
- if (!$code) {
- return json_fail('无效的兑换码');
- }
- if ($code->type == 1) {
- //活动类
- $isExchange = Db::table('vip_code_exchange')
- ->where('uid', $this->getUserId())
- ->where('cid', $code->id)
- ->count();
- $code->is_exchange = $isExchange ? 1 : 0;
- } else {
- $code->is_exchange = !$code->status ? 1 : 0;
- }
- return json_success($code);
- }
- /**
- * 兑换码兑换
- */
- public function exchage(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'code' => 'required|string',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $code = Db::table('vip_code')
- ->where('code', $inputData['code'])
- ->where('status', 1)
- ->first();
- if (!$code) {
- return json_fail('无效的兑换码或兑换码已被兑换');
- }
- if ($code->expire < date('Y-m-d')) {
- return json_fail('该兑换码已过期');
- }
- $user_code = $this->getUserId();
- Db::beginTransaction();
- if ($code->type == 1) {
- //活动类
- $limit = Db::table('vip_code_exchange')
- ->where('cid', $code->id)
- ->count();
- if ($limit > $code->limit) {
- return json_fail('该兑换码已达上限');
- }
- $isExchange = Db::table('vip_code_exchange')
- ->where('uid', $user_code)
- ->where('cid', $code->id)
- ->count();
- if ($isExchange) {
- return json_fail('你已经兑换了该活动码');
- }
- $insert = Db::table('vip_code_exchange')
- ->insert(
- [
- 'uid' => $user_code,
- 'cid' => $code->id,
- 'created_at' => Carbon::now()->toDateTimeString()
- ]
- );
- $code->is_exchange = 1;
- } else {
- Db::table('vip_code')
- ->where('code', $inputData['code'])
- ->update(['status' => '0']);
- $code->is_exchange = 1;
- }
- $userInfo = make(UserRepository::class)
- ->getUserInfoFromCache($user_code);
- if ($userInfo['is_vip'] == 'y') {
- $vipTime = Carbon::parse($userInfo['vip_expired'])
- ->addDays($code->day)
- ->toDateTimeString();
- } else {
- $vipTime = Carbon::now()
- ->addDays($code->day)
- ->toDateTimeString();
- }
- $result = UserModel::query()
- ->where('code', $user_code)
- ->update(['vip_expired' => $vipTime]);
- if ($result) {
- // 加mongodb兑换日志
- make(QueueService::class)->addExchangeCodeJob(
- [
- 'nick' => $userInfo['nick'],
- 'user_id' => $userInfo['id'],
- 'user_code' => $userInfo['code'],
- 'code' => $code->code,
- 'title' => $code->title,
- 'day' => $code->day,
- ]
- );
- Db::commit();
- } else {
- Db::rollBack();
- return json_fail('兑换失败');
- }
- make(UserRepository::class)->delUserCache($user_code);
- return json_success($code);
- }
- }
|