当前位置:网站首页>bitmap签到
bitmap签到
2022-07-15 12:43:00 【陈卿诺语】
/**
* 用户签到接口
* @return false|string
*/
public function sign()
{
$uid = \request()->get('uid');
if (!is_numeric($uid) || $uid !=(int)$uid || $uid <= 0){
return json_encode(['code' => 500,'msg' => 'uid参数错误','']);
}
//获取当前时间
$now = date('Ym');
$day = date('d'); //04
//拼接bitmap的key
$key = $uid.':'.$now; //"1:202207"
//查看是否签到过
$date = Redis::getbit($key,$day);
if($date){
return json_encode(['code' => 200,'msg' =>'今天签到过了','data' => '']);
}
//获取昨天时间
$lastDay = date("d", strtotime("-1 day")); //"02"
//查看昨天是否签到
$lastDate = Redis::getbit($key,$lastDay);
$user = Users::where('uid',$uid)->first()->toarray();
// Redis::setbit($key,0,1);
//昨天签到(连签)
if($lastDate){
//积分叠加 bitmap存已签到
$integral = $user['integral'] + 10 + ($user['sign_num']*5);
//签到次数
$num = $user['sign_num'] + 1;
$data['integral'] = $integral;
$data['sign_num'] = $num;
$result = Users::where('uid',$uid)->update($data);
if (!$result){
return json_encode(['code' => 500,'msg' =>'签到失败','data' => '']);
}
Redis::setbit($key,$day,1);
//签到记录
$sign = \App\Models\SignIn::where('uid',$uid)->orderby('balance','desc')->first();
//累计所得积分
$integralFen = $sign['balance'] + 10 + ($user['sign_num']*5);
$signData['uid'] = $uid;
$signData['title'] = "用户第$num 次签到";
$signData['number'] = 10 + ($user['sign_num']*5);
$signData['balance'] = $integralFen;
$signData['add_time'] = time();
$signRes = \App\Models\SignIn::insert($signData);
if (!$signRes){
return json_encode(['code' => 500,'msg' =>'签到失败','data' => '']);
}
return json_encode(['code' => 200,'msg' => "您已连续签到$num 天,已得积分$integral 分",'data' => '']);
}
//昨天未签到(断签)
$data['sign_num'] = 1;
$data['integral'] = $user['integral'] + 10;
$result = Users::where('uid',$uid)->update($data);
if (!$result){
return json_encode(['code' => 500,'msg' =>'签到失败','data' => '']);
}
Redis::setbit($key,$day,1);
$signData['uid'] = $uid;
$signData['title'] = '用户第一次签到';
$signData['number'] = 10;
$signData['balance'] = 10;
$signData['add_time'] = time();
$signRes = \App\Models\SignIn::insert($signData);
if (!$signRes){
return json_encode(['code' => 500,'msg' =>'签到失败','data' => '']);
}
return json_encode(['code' => 200,'msg' =>'签到成功','data' => '']);
}
/**
* 补签并重新计算连续签到
* @return false|string
*
*/
public static function retroactive()
{
$uid = \request()->get('uid');
$date = \request()->get('date');
//获取现在的时间
$now = date('Ymd');
if ($date >= $now) {
return json_encode(['code' => 500,'msg' =>'补签的日期不能大于等于今天','data' => '']);
}
//获取要补签的时间
$day = substr($date, 6, 2);
$month = substr($date, 0, 6);
$key = $uid . ':' . $month;
$sign = Redis::getbit($key, $day); //0
if ($sign == 1) {
return json_encode(['code' => 200,'msg' =>'你已经签到过了不需要补签','data' => '']);
}
Redis::setbit($key, $day, 1);
$month = date('Ym');
$day = date('d') - 1;
$signKey = $uid . ':' . $month;
$sign = Redis::getbit($signKey, $day); //
$i = 1;
if ($sign == 1) {
dd(1123);
$min = self::getNum($uid, $month, $day,$i);
Users::where('uid',$uid)->update(['sign_num' => $min]);
}
//查询用户签到信息
$user = Users::where('uid',$uid)->first()->toarray();
$integral = $user['integral'];
$signData['uid'] = $uid;
$signData['title'] = '用户补签';
$signData['number'] = 10;
$signData['balance'] = $integral;
$signData['add_time'] = time();
$signRes = \App\Models\SignIn::insert($signData);
if (!$signRes){
return json_encode(['code' => 500,'msg' =>'签到失败','data' => '']);
}
return json_encode(['code' => 200,'msg' => "您已补签成功,获得积分$integral 分,",'data' => '']);
}
/**
* 获取用户连续签到天数
* @param $uid
* @param $date
* @param $day
* @param $i
*/
public static function getNum($uid, $date, $day,&$i)
{
try {
$key = $uid . ':' . $date;
$sign = Redis::getbit($key, $day);
if ($sign == 1) {
$i ++;
if ($day - 1 > 0) {
self::getNum($uid, $date, $day - 1,$i);
}else{
$time = strtotime($date . '0' . $day) - 86400;
$mytime = date('Ymd', $time);
$day = substr($mytime, 6, 2);
$month = substr($mytime, 0, 6);
self::getNum($uid, $month, $day,$i);
}
}
return $i;
}catch (\Exception $e){
return json_encode(500,$e->getMessage());
}
}
边栏推荐
- Robotframework advanced (III) integrated Jenkins operation automation use case
- 上云精品 | 云商店助力慧科 推动校企联合,共育人才发展
- 騰訊大咖分享 | 騰訊Alluxio(DOP)在金融場景的落地與優化實踐
- 聚簇索引和非聚簇索引
- Online office, how to make collaboration more efficient?
- 世界首款抗量子攻击商用密码芯片 | 沐创
- Once, I sprayed AI customer service for two minutes, and it only replied to my first sentence
- [MySQL] multi table query
- 文件解析_Excel文件解析
- Gates donated another $20billion, Google cloud switched to arm, and twitter employees were warned by CEO musk. Today, more big news is here
猜你喜欢

Event preview | Apache Doris x Apache seatunnel joint meetup to start registration!

Where is the IBD file of MySQL in win11 virtual machine

Will the expired data of redis be deleted immediately? Great mystery

Clustered index and non clustered index
![[live review] openharmony knowledge empowerment phase 6 lesson 3 - control panel function implementation of openharmony smart home project](/img/3d/23700d282053ff994f49e8a04e6be2.png)
[live review] openharmony knowledge empowerment phase 6 lesson 3 - control panel function implementation of openharmony smart home project

活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!

Tikv & tiflash accelerates complex business queries
![leetcode:300. Longest increasing subsequence [LIS board + greedy dichotomy + nlogn]](/img/1c/cfbeb0ef8b0adea03b901653c0d7ed.png)
leetcode:300. Longest increasing subsequence [LIS board + greedy dichotomy + nlogn]

Carte écologique numérique des ressources humaines en Chine - marché flexible de l'emploi

中国人力资源数字化生态图谱-灵活用工市场
随机推荐
Thesis learning (I) -- MWP bert: numerical augmented pre training for math wordproblem solving
lnmp架构php安装
Indexedstack in shutter
DataArts Studio数据架构——基于模型驱动的智能自动化流水线建设案例
Quickly deploy mqtt clusters on Alibaba cloud using terraform
Slow SQL analysis and optimization
DEVKIT-mpc5744p配置rtos
Issue 106: hree Application scenarios and basic concepts of JS
Infrared remote control atmosphere lamp touch chip-dlt8sa15a-jericho
MySQL multi table query, detailed to this extent, haven't you learned yet?, Including display inner connection, implicit inner connection, left connection, right connection and sub query in inner and
Why did musk stop acquiring twitter?
网站漏洞修复服务商讲解越权漏洞的原因
Musk suspends twitter acquisition, with a breakup fee of $1billion
中国人力资源数字化生态图谱-灵活用工市场
盖茨再捐200亿美元,谷歌云转投ARM,推特员工因马斯克遭CEO警告,今日更多大新闻在此...
5 款内部系统搭建低代码平台评测
论文学习(一)——MWP-BERT: Numeracy-Augmented Pre-training for Math WordProblem Solving
同花顺开户选哪个证券公司好?网上开户安全吗?
"Xiaobaizui" white yam is the characteristic agricultural product of which county? Ant new village July 15 answer
JS to realize the function of electronic signature