__handlePropertyHandler(__CLASS__); } /** * 兑换码信息 */ 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); } }