manghe/app/common/model/UserSign.php
2025-03-21 18:43:39 +08:00

122 lines
3.4 KiB
PHP

<?php
namespace app\common\model;
use app\common\model\Base;
use app\common\model\User;
use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\OrderList;
use think\Model;
class UserSign extends Base
{
// 设置当前模型对应的完整数据表名称
protected $table = 'user_sign';
/**
* 获取单条数据
*/
public static function getInfo($where = [], $field = '*')
{
$data = self::where($where)
->field($field)
->find();
return $data;
}
/**
* 签到
*/
public static function add($user_id)
{
//签到日期
$sign_date = date('Y-m-d', time());
//获取连续签到天数
$days = self::getDays($user_id, $sign_date);
if ($days == '-1') {
return "今日您已签到";
}
$config = getConfig('sign');
if ($days > 7) {
$days = 1;
}
if ($days == 1) {
$num = $config['one_num'];
} elseif ($days == 2) {
$num = $config['two_num'];
} elseif ($days == 3) {
$num = $config['three_num'];
} elseif ($days == 4) {
$num = $config['four_num'];
} elseif ($days == 5) {
$num = $config['five_num'];
} elseif ($days == 6) {
$num = $config['six_num'];
} elseif ($days == 7) {
$num = $config['seven_num'];
}
$data = [
'user_id' => $user_id,
'sign_date' => date('Y-m-d', time()),
'sign_day' => intval(date('d', time())),
'days' => $days,
'num' => $num,
'create_time' => time(),
'update_time' => time(),
];
self::insert($data);
User::changeIntegral($user_id, $num, 2,'签到');
return ['msg' => "签到成功,获得{$num}个积分", 'data' => ['num' => $num]];
}
/**
* 获取签到信息
*/
public static function getDays($user_id, $sign_date)
{
$row = self::where('user_id', '=', $user_id)->order(['create_time' => 'desc'])->find();
if (empty($row)) {
return 1;
}
$dif = (strtotime($sign_date) - strtotime($row['create_time'])) / (24 * 60 * 60);
if (strtotime($row['sign_date']) == strtotime($sign_date)) {
return -1; //今日已签到
}
if ($dif > 1) {
return 1;
}
if ($dif < 1) {
return $row['days'] + 1;
}
return false;
}
public static function getListByUserId($user_id)
{
$str = date('Y-m-d', time());
$arr = explode('-', $str);
$beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //查询昨天到今天 的数据看今日是否签到和连续签到天数
$list = self::where('user_id', '=', $user_id)
->whereTime('create_time', '>=', $beginYesterday)
->order(['create_time' => 'desc'])->select()->toArray();
$res = array_column($list, 'sign_day');
$len = count($list);
if ($len == 0) {
$days = 0;
$is_sign = 0;
} else {
$days = $list[0]['days']; //连续签到天数
$is_sign = ($list[0]['sign_date'] == date('Y-m-d', time())) ? 1 : 0; //今日是否签到
}
return [$days, $is_sign];
}
}