ThinkPHP5 签到功能

本文阅读 2 分钟
首页 Thinkphp笔记 正文
  1. <?php
  2. //签到主页面
  3. public function sign_index(){
  4. // 日历列表
  5. $monthSign = $this->getMonthSign();
  6. $dayList = $this->showDays($monthSign);
  7. // 今天签到
  8. $data = $this->todayData();
  9. if(empty($data)){
  10. $data['is_sign'] =0;
  11. $data['status']=10002;
  12. $data['msg']="今天未签到";
  13. }else{
  14. $data['status']=10001;
  15. $data['msg']="今天已经签到";
  16. }
  17. $this->return_data($data);
  18. }
  19. /**
  20. * 执行当天签到 也就是点击签到调用的接口
  21. * @return json 签到成功返回 {status:1,info:'已签到'}
  22. */
  23. public function sign(){
  24. $todayData = $this->todayData();
  25. if($todayData['is_sign'] == 1){
  26. $this->return_data('已签到');
  27. }else{
  28. $data = $this->getInsertData($this->user_id);
  29. // 无今天数据
  30. if($todayData == NULL){
  31. $data['uid'] = $this->user_id;
  32. $data['atime'] = time();
  33. $id = Db::name('members_sign')->insertGetId($data);
  34. }else{
  35. $save = Db::name('members_sign')->where("id = {$todayData['id']}")->update($data);
  36. }
  37. if($id or $save){
  38. //积分数
  39. $score = $this->getTodayScores($data['days']);
  40. // 为该用户添加积分
  41. // addScore($this->user_id,$score);
  42. $res['status'] = 10001;
  43. $res['msg'] ="签到成功!";
  44. $res['score']=$score;
  45. $res['days']=$data['days'];
  46. $this->return_data($res);
  47. }else{
  48. $res['status'] = 10002;
  49. $res['msg'] ="签到失败,请刷新后重试!";
  50. $this->return_data($res);
  51. }
  52. }
  53. }
  54. /**
  55. * 返回每次签到要插入的数据
  56. *
  57. * @param int $uid 用户id
  58. * @return array(
  59. * 'days' => '天数',
  60. * 'is_sign' => '是否签到,用1表示已经签到',
  61. * 'stime' => '签到时间',
  62. * );
  63. */
  64. protected function getInsertData($uid){
  65. // 昨天的连续签到天数
  66. $start_time = strtotime(date('Y-m-d 0:0:0',time()-86400))-1;
  67. $end_time = strtotime(date('Y-m-d 23:59:59',time()-86400))+1;
  68. $days = Db::name('members_sign')->where("uid = $uid and atime > $start_time and atime < $end_time")->value('days');
  69. if($days){
  70. $days++;
  71. if($days > 30){
  72. $days = 1;
  73. }
  74. }else{
  75. $days = 1;
  76. }
  77. return array(
  78. 'days' => $days,
  79. 'is_sign' => 1,
  80. 'stime' => time()
  81. );
  82. }
  83. /**
  84. * 用户当天签到的数据
  85. * @return array 签到信息 is_sign,stime 等
  86. */
  87. protected function todayData(){
  88. $time = time();
  89. $start_stime = strtotime(date('Y-m-d 0:0:0',$time))-1;
  90. $end_stime = strtotime(date('Y-m-d 23:59:59',$time))+1;
  91. return Db::name('members_sign')->field('atime',true)->where("uid = {$this->user_id} and atime > $start_stime and atime < $end_stime")->find();
  92. }
  93. /**
  94. * 积分规则,返回连续签到的天数对应的积分
  95. *
  96. * @param int $days 当天应该得的分数
  97. * @return int 积分
  98. */
  99. protected function getTodayScores($days){
  100. if($days == 30){
  101. return 50;
  102. }else if($days > 19){
  103. return 8;
  104. }else if($days > 9){
  105. return 5;
  106. }else{
  107. return 3;
  108. }
  109. }
  110. /**
  111. * 显示签到列表
  112. *
  113. * @param array $signDays 某月签到的日期 array(1,2,3,4,5,12,13)
  114. * @param int $year 可选,年份
  115. * @param int $month 可选,月份
  116. * @return string 日期列表<li>1</li>....
  117. */
  118. protected function showDays($signDays,$year='',$month=''){
  119. $time = time();
  120. $year = $year ? $year : date('Y',$time);
  121. $month = $month ? $month : date('m',$time);
  122. $daysTotal = date('t', mktime(0, 0, 0, $month, 1, $year));
  123. $now = date('Y-m-d',$time);
  124. $str = '';
  125. // dump($daysTotal);
  126. //exit;
  127. for ($j = 1; $j <= $daysTotal; $j++) {
  128. $i++;
  129. $someDay = date('Y-m-d',strtotime("$year-$month-$j"));
  130. // dump($someDay);exit;
  131. // 小于今天的日期样式
  132. if ($someDay <= $now){
  133. // 当天日期样式 tdc = todayColor
  134. if($someDay == $now){
  135. // 当天签到过的
  136. if(in_array($j,$signDays)){
  137. $str .= '<li class="current fw tdc">'.$j.'</li>';
  138. }else{
  139. $str .= '<li class="today fw tdc">'.$j.'</li>';
  140. }
  141. }else{
  142. // 签到过的日期样式 current bfc = beforeColor , fw = font-weight
  143. if(in_array($j,$signDays)){
  144. $str .= '<li class="current fw bfc">'.$j.'</li>';
  145. }else{
  146. $str .= '<li class="fw bfc">'.$j.'</li>';
  147. }
  148. }
  149. }else{
  150. $str .= '<li>'.$j.'</li>';
  151. }
  152. }
  153. // dump($str);exit;
  154. return $str;
  155. }
  156. /**
  157. * 获取当月签到的天数,与 $this->showDays() 配合使用
  158. * @return 当月签到日期 array(1,2,3,4,5,12,13)
  159. */
  160. protected function getMonthSign(){
  161. $time = time();
  162. $year = date('Y',$time);
  163. $month = date('m',$time);
  164. $day = date("t",strtotime("$year-$month"));
  165. $start_stime = strtotime("$year-$month-1 0:0:0")-1;
  166. $end_stime = strtotime("$year-$month-$day 23:59:59")+1;
  167. $list = Db::name('members_sign')->field('stime')->where("uid = {$this->user_id} and stime > $start_stime and stime < $end_stime")->order('stime asc')->select();
  168. // dump($list);
  169. // exit;
  170. foreach($list as $key => $value){
  171. // dump($value);exit;
  172. $list[$key] = date("d",$value["stime"]);
  173. }
  174. // dump($list);exit;
  175. return $list;
  176. }

mysql数据库

  1. CREATE TABLE `members_sign` (
  2. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  3. `uid` int(11) unsigned NOT NULL COMMENT '用户id',
  4. `days` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT '连续签到的天数',
  5. `is_share` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否分享过',
  6. `is_sign` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否签到过',
  7. `stime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '签到的时间',
  8. `atime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '添加时间',
  9. PRIMARY KEY (`id`),
  10. KEY `index_uid` (`uid`) USING BTREE
  11. ) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8 COMMENT='签到分享表';
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

thinkphp5.1 关联查询多表查询
« 上一篇 12-01
tp5.1保存搜索条件
下一篇 » 12-03

发表评论