Commit 5d5627ee by 杨树贤

修改签到部分的逻辑

parent 2e21ca92
......@@ -14,7 +14,6 @@ use Illuminate\Support\Facades\DB;
class CheckInController extends Controller
{
public function index(Request $request, CheckIn $checkIn, CheckInFilter $filter)
{
$page = $request->has('page') ? $request->page : self::DEFAULT_PAGE;
......@@ -25,13 +24,13 @@ class CheckInController extends Controller
return $this->Export(0, 'ok', $checkIns);
}
public function store(Request $request, CheckIn $checkIn)
public function store(Request $request, CheckIn $checkIn, Integral $integral)
{
$data = [
'user_id' => $request->user_id,
'add_time' => time(),
];
$canCheckIn = $checkIn->canCheckInFromRedis($data['user_id']);
$canCheckIn = $integral->checkIntegralLimit($data['user_id'], CheckIn::INTEGRAL_TYPE_CHECK_IN);
if (!$canCheckIn) {
return $this->Export(ErrorCode(1, 1), '今天已签到,无法再进行签到');
} else {
......
......@@ -18,7 +18,7 @@ class CheckIn extends Model
protected $table = 'check_in';
public $timestamps = false;
const INTEGRAL_TYPE_CHECK_IN = 4;
const INTEGRAL_TYPE_CHECK_IN = 1;
public function scopePage($query, $page = 1, $pageSize = 10)
{
......@@ -57,10 +57,10 @@ class CheckIn extends Model
}
//添加流水成功后,要把签到记录放到redis
$date = Carbon::now()->toDateString();
$hashKey = 'ic_welfare_check_in_' . $date;
$hashKey = 'ic_welfare_integral_limit_' . self::INTEGRAL_TYPE_CHECK_IN;
$userId = $data['user_id'];
$redis = new RedisModel();
$redis->hset($hashKey, $userId, $data['add_time']);
$redis->hincrby($hashKey, $userId, 1);
return true;
});
......
......@@ -5,9 +5,11 @@ namespace App\Models;
use App\Http\Filters\QueryFilter;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Common\Model\RedisModel;
use Illuminate\Support\Facades\Log;
//活动红包模型
class Integral extends Model
......@@ -207,4 +209,37 @@ class Integral extends Model
return $res;
}
//检查当前红包添加的操作是否已经达到限额,比如每个用户每日只能签到一次
public function checkIntegralLimit($userId, $integralId)
{
//先去redis取出对应的判断类型,比如我要判断签到是否达到每日限额
//就要去查找ic_integral_limit_{$integralId},里面存储了每个用户每天使用的次数
$key = 'ic_welfare_integral_limit_' . $integralId;
$redis = new RedisModel();
//先去判断对应项存不存在,不存在的话要先去添加对应限制项的redis哈希
$exist = $redis->exists($key);
if ($exist === 0) {
$redis->hset($key, $userId, 1);
//同时给予过期时间(今天晚上12点)
$endTimeOfToday = Carbon::now()->endOfDay()->timestamp;
$redis->expireAt($key, $endTimeOfToday);
//因为连最基本的key都不存在,所以今天肯定没有人对这个类型操作过,所以可以直接返回可操作
return true;
}
$count = $redis->hget($key, $userId);
$limit = 0;
//获取到次数之后,还要去对比该红包id对应的每日限制次数
$integral = $redis->hget('ic_welfare_integrals', $integralId);
if ($integral) {
$integral = json_decode($integral, true);
$limit = array_get($integral, 'daily_limit');
} else {
Log::Error('查询不存在的类型限制');
}
//如果当前用户每日使用没有超过限制次数,那就可以进行操作
return $count < $limit ? true : false;
}
}
\ No newline at end of file
517
\ No newline at end of file
3352
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment