先上效果:
代码思想其实也很简单,在
public function save()
{
$register = new Register();
if (!$this->validate()) {
return $this->errorResponse;
}
//未到时间签到修复
if(date('Ymd')<date('Ymd',strtotime($this->register_time))){
return [
'code' => 1,
'msg' => '日期无效'
];
}
$first_day = Register::find()->where(['store_id' => $this->store_id, 'user_id' => $this->user_id, 'type' => 1])->orderBy('addtime DESC')->asArray()->one();
$setting = IntegralSetting::find()->where(['store_id' => $this->store_id])->asArray()->one();
$user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
if (!$setting) {
return [
'code' => 1,
'msg' => '签到未设置'
];
}
$day1 = $first_day['continuation'];
$first_day['register_time'] = strtotime(date('Ymd', strtotime($first_day['register_time'])));
// $first_day['register_time'] = explode("/",$first_day['register_time']);
// $first_day['register_time'] = implode("", $first_day['register_time']);
$date = strtotime(date('Ymd', time()));
$day = $date - $first_day['register_time'];
if ($date == $first_day['register_time']) {
return [
'code' => 1,
'msg' => '已签到'
];
}
$day = ($date - $first_day['register_time']) / 86400;
if ($day >= 1 && $day < 2) {
$day1++;
} else {
$day1 = 1;
}
$score = $setting['register_integral'];
if ($day1 >= $setting['register_continuation']) {
$score = $setting['register_reward'] + $setting['register_integral'];
} else {
$score = $setting['register_integral'];
}
$register->store_id = $this->store_id;
$register->user_id = $this->user_id;
$register->register_time = $this->register_time;
$register->addtime = time();
$register->continuation = $day1;
$register->integral = $score;
$register->type = 1;
$user->integral += $score;
if ($user->save()) {
if ($register->save()) {
return [
'code' => 0,
'msg' => '签到成功',
'data' => [
'continuation' => $register->continuation
]
];
} else {
return $this->getErrorResponse($register);
}
} else {
return $this->getErrorResponse($user);
}
}
public function actionRegister()
{
$today = date('Y/n/j'); //年月日,如08月是8
$form = new RegisterForm();
$form->store_id = $this->store->id;
$form->user_id = \Yii::$app->user->id;
$form->register_time = $today;
return new \app\opening\BaseApiResponse($form->save());
}
总结经验:因为出现了客户未到期疯狂签到的大bug,后来发现是前端小程序使用的是获取手机系统的原因,然后直接传递到后端,后端也没有做任何限制直接接收前端的参数;因此任何代码都要谨慎约束前后端配合!