<?php
?>
<center>
<table border="1">
<tr>
<th>连续签到天数</th>
<th>获得星星数量</th>
<th>获得的积分</th>
<th>最后一次签到时间</th>
</tr>
<tr align="center">
<td class="sign_num"><?=$data['sign_num']?></td>
<td class="start_num"><?=$data['start_num']?><span class="img">
<?php if($data['start_num']>=1){?><img src="public/start.png"><?php }?>
</span></td>
<td class="integral"><?=$data['integral']?></td>
<td class="time"><?=$data['sign_time']?></td>
</tr>
</table>
<button class="btn">签到</button>
<hr>
<table border="1">
<tr>
<th>编号</th>
<th>商品名称</th>
<th>所需积分</th>
<th>操作</th>
</tr>
<?php foreach($goods as $k=>$v){?>
<tr>
<td><?=$v['id']?></td>
<td><?=$v['name']?></td>
<td><?=$v['integral']?></td>
<td><a href="javascript:void (0)" onclick="conversion(<?=$v['integral']?>)">兑换</a></td>
</tr>
<?php }?>
</table>
</center>
<script src="public/jq.js"></script>
<script>
function conversion(integral){
var user_id=1;
$.ajax({
type: "POST",
url: '<?=\Yii::$app->urlManager->createUrl('sign/integral');?>',
data:{
integral:integral,
id:user_id
},
dataType: "json",
async:false,
success: function(msg){
if(msg==1){
var integrals=$('.integral').text();
integrals=parseInt(integrals)-integral;
$('.integral').html(integrals);
alert('恭喜您!商品兑换成功!')
}else if(msg==2){
alert('很遗憾!您的积分不足!')
}else{
alert('对不起!商品兑换失败!')
}
}
});
}
$('.btn').click(function(){
var user_id=1;
$.ajax({
type: "POST",
url: '<?=\Yii::$app->urlManager->createUrl('sign/ajax');?>',
data:{
id:user_id
} ,
dataType: "json",
async:false,
success: function(html){
if(html==3){
alert('今天已经签过到了!')
}else if(html==1){
alert('签到成功!');
getChange(html);
}else if(html==2){
alert('签到成功!但是续签失败!')
getChange(html);
}else{
alert('签到失败!')
}
}
});
});
function getChange(html){
var date=new Date();
var year=date.getFullYear();
var month=parseInt(date.getMonth())+1;
if(month<10){
month="0"+month;
}
var day=date.getDate();
if(day<10){
day="0"+day;
}
var hours=date.getHours();
if(hours<10){
hours="0"+hours;
}
var minutes=date.getMinutes();
if(minutes<10){
minutes="0"+minutes
}
var seconds=date.getSeconds();
if(seconds<10){
seconds="0"+seconds;
}
var sign_num=1;
var start_num=0;
if($('.sign_num').text()!=''){
sign_num=parseInt($('.sign_num').text())+1;
}else{
$('.start_num').html(start_num);
}
if(html==1){
if(sign_num>4){
$('.sign_num').html(0);
start_num=parseInt($('.start_num').text())+1;
$('.start_num').html(start_num+'<img src="public/start.png">')
}else{
$('.sign_num').html(sign_num);
}
}else if(html==2){
$('.sign_num').html(1);
}
var time=year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds;
$('.time').html(time);
var integral=10;
if($('.integral').text()!=''){
integral=parseInt($('.integral').text())+10;
}
$('.integral').html(integral);
}
</script>
<?php
namespace frontend\models;
use yii\base\Model;
use common\models\User;
/**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
return $user->save() ? $user : null;
}
}
<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use app\models\Sign;
use app\models\Goods;
/**
* Site controller
*/
class SignController extends Controller
{
public function actionIndex(){
$sign=new Sign();
$good=new Goods();
$goods=$good->_get();
$data=$sign->_getWhere('user_id=1');
return $this->render('index',['data'=>$data,'goods'=>$goods]);
}
public function actionIntegral(){
$id=\Yii::$app->request->post('id');
$integral=\Yii::$app->request->post('integral');
$sign=new Sign();
$integralOld=$sign->_getWhere('user_id = '.$id);
if(intval($integral) > intval($integralOld['integral'])){
$result=2;
}else{
$result=$sign->_deduct($integral,'user_id = '.$id);
}
return $result;
}
public function actionAjax(){
$id=\Yii::$app->request->post('id');
$sign=new Sign();
$data=$sign->_getWhere('user_id='.$id);
if(!$data){
$result=$sign->_set($id);
}else{
if($data['sign_num']>=4){
$sign_num=0;
$start=$data['start_num']+1;
}else{
$sign_num=$data['sign_num']+1;
$start=0;
}
$integral=(intval($data['integral'])+10);
$signDay=$this->getTime($data['sign_time']);
$thisDay=$this->getTime(date('Y-m-d H:i:s',time()));
if($thisDay-$signDay==24*3600){
$result=$sign->_save('user_id='.$id,$sign_num,$start,$integral);
}else if($thisDay-$signDay > 24*3600){
$result=$sign->_save('user_id='.$id,1,$data['start_num'],$integral);
if($result){
$result=2;
}
}else{
$result=3;
}
}
echo json_encode($result);
}
public function getTime($data){
$str=strtotime(date('Ymd',strtotime($data)));
return $str;
}
}
<?php
namespace app\models;
use Yii;
class Sign extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'signIn';
}
/**
* @inheritdoc
*/
public function _get()
{
$connection = \Yii::$app->db;
$command = $connection->createCommand('SELECT * FROM '.$this->tableName());
$data = $command->queryAll();
return $data;
}
/**
* @inheritdoc
*/
public function _getWhere($where)
{
$connection = \Yii::$app->db;
$command = $connection->createCommand('SELECT * FROM '.$this->tableName().' where '.$where);
$data = $command->queryOne();
return $data;
}
/**
* @inheritdoc
*/
public function _set($user_id,$sign=1,$start=0)
{
$connection = \Yii::$app->db;
$command = $connection->createCommand('insert into '.$this->tableName().' values(\'\','.$sign.','.$start.',"'.date('Y-m-d H:i:s',time()).'", '.$user_id.',10)');
$data = $command->execute();
return $data;
}
/**
* @inheritdoc
*/
public function _deduct($integral,$where)
{
$connection = \Yii::$app->db;
$command = $connection->createCommand('update '.$this->tableName().' set integral=integral-'.$integral.' where '.$where);
$result = $command->execute();
return $result;
}
/**
* @inheritdoc
*/
public function _save($where,$sign=1,$start=0,$integral=10)
{
$connection = \Yii::$app->db;
$command=$connection->createCommand('update '.$this->tableName().' set sign_num='.$sign.',start_num='.$start.',sign_time="'.date('Y-m-d H:i:s',time()).'",integral='.$integral.' where '.$where);
if($start==0){
$command = $connection->createCommand('update '.$this->tableName().' set sign_num='.$sign.',sign_time="'.date('Y-m-d H:i:s',time()).'",integral='.$integral.' where '.$where);
}
$data = $command->execute();
return $data;
}
}