228 lines
6.6 KiB
PHP
228 lines
6.6 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 app\common\model\SignConfig;
|
||
use app\common\model\SignConfigReward;
|
||
use app\common\service\RewardService;
|
||
use think\Model;
|
||
use app\common\helper\ConfigHelper;
|
||
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());
|
||
$current_month = date('m', time());
|
||
$current_year = date('Y', time());
|
||
|
||
//检查今日是否已签到
|
||
$today_sign = self::where('user_id', $user_id)
|
||
->where('sign_date', $sign_date)
|
||
->find();
|
||
|
||
if (!empty($today_sign)) {
|
||
return ["status" => false, "msg" => "今日您已签到"];
|
||
}
|
||
|
||
//获取连续签到天数
|
||
$days = self::getDays($user_id, $sign_date, $current_month, $current_year);
|
||
|
||
//签到数据
|
||
$data = [
|
||
'user_id' => $user_id,
|
||
'sign_date' => date('Y-m-d', time()),
|
||
'sign_day' => intval(date('d', time())),
|
||
'days' => $days,
|
||
'month' => $current_month,
|
||
'year' => $current_year,
|
||
'create_time' => time(),
|
||
'update_time' => time(),
|
||
];
|
||
|
||
// 执行签到并记录
|
||
self::insert($data);
|
||
|
||
$day_count = self::getMonthSignDays($user_id);
|
||
$res[] = [];
|
||
//处理每日签到奖励
|
||
$res1 = self::processSignRewards($user_id, 1, $day_count);
|
||
$msg = '';
|
||
if ($res1) {
|
||
|
||
foreach ($res1 as $item) {
|
||
$msg = $msg . '每日签到获得:' . $item['msg'] . ',';
|
||
}
|
||
}
|
||
// 处理累计签到奖励
|
||
$res2 = self::processSignRewards($user_id, 2, $day_count);
|
||
if ($res2) {
|
||
$res[] = $res2;
|
||
foreach ($res2 as $item) {
|
||
$msg = $msg . '累计签到获得:' . $item['msg'] . ',';
|
||
}
|
||
}
|
||
if ($msg) {
|
||
$msg = substr($msg, 0, -1);
|
||
}
|
||
|
||
return ['status' => true, 'msg' => $msg, 'data' => $day_count];
|
||
}
|
||
|
||
/**
|
||
* 处理签到奖励
|
||
* @param int $user_id 用户ID
|
||
* @param int $type 配置类型 1-每日签到 ,2-累计签到,
|
||
* @param int $day 天数或星期几
|
||
*/
|
||
protected static function processSignRewards($user_id, $type, $day)
|
||
{
|
||
$sign_multiple=1;
|
||
// $sign_multiple = ConfigHelper::getSystemTestKey("sign_multiple");
|
||
// if (!$sign_multiple) {
|
||
// $sign_multiple = 0;
|
||
// } else {
|
||
// $sign_multiple = intval($sign_multiple);
|
||
// }
|
||
if ($sign_multiple <= 0) {
|
||
$sign_multiple = 1;
|
||
}
|
||
// 查找符合条件的配置
|
||
$reward = SignConfig::where('type', '=', $type)
|
||
->where('status', '=', 1)
|
||
->where('day', '=', $day)
|
||
->field('reward_id')->find();
|
||
$res = [];
|
||
if ($reward) {
|
||
$res = RewardService::sendRewardsMultiple($user_id, $reward['reward_id'], $sign_multiple,$type == 1 ? "签到奖励" : "累计签到奖励");
|
||
if ($res && $res['status']) {
|
||
return $res['data'];
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 获取签到信息 - 计算连续签到天数
|
||
* @param int $user_id 用户ID
|
||
* @param string $sign_date 签到日期
|
||
* @param int $current_month 当前月份
|
||
* @param int $current_year 当前年份
|
||
* @return int 连续签到天数
|
||
*/
|
||
public static function getDays($user_id, $sign_date, $current_month = null, $current_year = null)
|
||
{
|
||
if ($current_month === null) {
|
||
$current_month = date('m', time());
|
||
}
|
||
|
||
if ($current_year === null) {
|
||
$current_year = date('Y', time());
|
||
}
|
||
|
||
// 查询该用户最近一次签到记录
|
||
$row = self::where('user_id', '=', $user_id)
|
||
->order(['create_time' => 'desc'])
|
||
->find();
|
||
|
||
if (empty($row)) {
|
||
return 1; // 第一次签到
|
||
}
|
||
|
||
// 如果是新的一个月,重置签到天数
|
||
if ($row['month'] != $current_month || $row['year'] != $current_year) {
|
||
return 1;
|
||
}
|
||
|
||
// 判断是否是今天已经签到
|
||
if (strtotime($row['sign_date']) == strtotime($sign_date)) {
|
||
return -1; // 今日已签到
|
||
}
|
||
|
||
// 计算两次签到的时间差(天数)
|
||
$dif = (strtotime($sign_date) - strtotime($row['sign_date'])) / (24 * 60 * 60);
|
||
|
||
if ($dif > 1) {
|
||
return 1; // 不是连续签到,重置为1
|
||
} else {
|
||
return $row['days'] + 1; // 连续签到
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户签到信息
|
||
* @param int $user_id 用户ID
|
||
* @param int $sign_type 签到类型 0-每日签到 1-累计签到
|
||
* @return array [连续签到天数, 今日是否签到]
|
||
*/
|
||
public static function getListByUserId($user_id, $sign_type = 0)
|
||
{
|
||
$current_date = date('Y-m-d', time());
|
||
$current_month = date('m', time());
|
||
$current_year = date('Y', time());
|
||
|
||
// 查询本月签到记录
|
||
$list = self::where('user_id', '=', $user_id)
|
||
->where('month', '=', $current_month)
|
||
->where('year', '=', $current_year)
|
||
->where('sign_type', '=', $sign_type)
|
||
->order(['create_time' => 'desc'])
|
||
->select()
|
||
->toArray();
|
||
|
||
$days = 0;
|
||
$is_sign = 0;
|
||
|
||
if (!empty($list)) {
|
||
$days = $list[0]['days']; // 连续签到天数
|
||
$is_sign = ($list[0]['sign_date'] == $current_date) ? 1 : 0; // 今日是否签到
|
||
}
|
||
|
||
return [$days, $is_sign];
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 获取用户本月签到记录
|
||
* @param int $user_id 用户ID
|
||
* @return int 本月已签到的数量
|
||
*/
|
||
public static function getMonthSignDays($user_id): int
|
||
{
|
||
$current_month = date('m', time());
|
||
$current_year = date('Y', time());
|
||
|
||
$sign_days = self::where('user_id', '=', $user_id)
|
||
->where('month', '=', $current_month)
|
||
->where('year', '=', $current_year)
|
||
->where('sign_type', '=', '0')
|
||
->count();
|
||
|
||
return $sign_days;
|
||
}
|
||
} |