cxhxy/app/api/model/food/SignLog.php
test_service d3170b4d1c 1
2023-12-01 15:43:29 +08:00

1 line
2.9 KiB
PHP

<?php
namespace app\api\model\food;
use app\common\model\food\SignLog as SignLogModel;
use think\facade\Cache;
use think\facade\Db;
/**
* 签到记录模型
*/
class SignLog extends SignLogModel
{
/**
* 添加
*/
public function add(array $data)
{
$new_data[0] = $data;
$new_data[0]['applet_id'] = self::$applet_id;
$sign = Setting::getItem('sign');//获取签到配置
$user = User::get($data['user_id']);//获取用户信息
//判断奖励数值
if($sign['mode'] == 'fixed'){
$new_data[0]['number'] = $sign['number'];//固定值
}else{
$new_data[0]['number'] = rand(1,$sign['number']);//随机值
}
//判断奖励类型
if($sign['type'] == 'score'){
$user->score = ['inc', $new_data[0]['number']];
$new_data[0]['type'] = 'score';
}else{
$user->money = ['inc', $new_data[0]['number']];
$new_data[0]['type'] = 'money';
}
$new_data[0]['remark'] = '每日签到';
//如果有签到任务
if(isset($sign['plan']) and sizeof($sign['plan']) > 0){
$plan = array_sort($sign['plan'], 'days');//按照天数从小到大排序
$days = Cache::get('food_sign_' . $user['user_id'] . '_' . self::$applet_id,0);
$days = $days + 1;
$expire_time = strtotime(date('Y-m-d',strtotime('+1 day'))) - time() + (24*3600);//到期时间第二天24点
Cache::set('food_sign_' . $user['user_id'] . '_' . self::$applet_id,$days,$expire_time);
for($n=0;$n<sizeof($plan);$n++){
if($plan[$n]['days'] == $days){
$new_data[1] = $data;
$new_data[1]['applet_id'] = self::$applet_id;
//判断奖励类型
if($plan[$n]['type'] == 'score'){
$user->score = ['inc', $plan[$n]['number']];
$new_data[1]['type'] = 'score';
}else{
$user->money = ['inc', $plan[$n]['number']];
$new_data[1]['type'] = 'money';
}
$new_data[1]['number'] = $plan[$n]['number'];
$new_data[1]['remark'] = '任务奖励';
if(($n+1) == sizeof($plan)){
Cache::delete('food_sign_' . $user['user_id'] . '_' . self::$applet_id);
}
break;
}
}
}
// 开启事务
Db::startTrans();
try {
// 添加记录
$this->saveAll($new_data);
$user->save();//更新用户信息
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
}
return false;
}
}