ThinkPHP implements the sign-in function, and thinkphp implements sign-in.

Source: Internet
Author: User
Tags add time

ThinkPHP implements the sign-in function, and thinkphp implements sign-in.

This article describes how thinkPHP implements the sign-in function. We will share this with you for your reference. The details are as follows:

Data Table:

Create table 'members _ sign' ('id' int (11) unsigned not null AUTO_INCREMENT, 'uid' int (11) unsigned not null comment 'user id ', 'Days 'tinyint (2) unsigned not null default '0' comment' consecutive sign-In days ', 'is _ share' tinyint (1) unsigned not null default '0' comment' whether shared ', 'is _ sign' tinyint (1) unsigned not null default '0' comment' whether checked in ', 'stime' int (11) unsigned not null default '0' comment' check-in time', 'atime' int (11) unsigned not null default '0' comment' add time ', primary key ('id'), KEY 'index _ uid' ('uid') using btree) ENGINE = InnoDB AUTO_INCREMENT = 162 default charset = utf8 COMMENT = 'sign in to the sharing table ';

Controller:

<? Phpnamespace Member \ Controller; use Member \ Controller \ MController; class IndexController extends MController {/*** user center * @ param */public function index () {$ pre = C ('db _ prefix'); // calendar list $ monthSign = $ this-> getMonthSign (); $ dayList = $ this-> showDays ($ monthSign); // sign in today $ data = $ this-> todayData (); if ($ data ['is _ sign'] = 1) {$ this-> assign ('issign', true) ;}$ this-> display ();} /*** sign in on the Execution day * @ return Return {status: 1, info: 'signed in '} */public function sign () {$ todayData = $ this-> todayData (); if ($ todayData ['is _ sign'] = 1) {$ this-> successMsg ('signed in ');} else {$ data = $ this-> getInsertData ($ this-> uid); // if ($ todayData = NULL) {$ data ['uid'] = $ this-> uid; $ data ['atime '] = time (); $ id = M ('members _ sign ') -> add ($ data);} else {$ save = M ('members _ sign')-> where ("id = {$ todayData ['id']}") -> save ($ da Ta);} if ($ id or $ save) {$ score = $ this-> getTodayScores ($ data ['days']); // Add the point addScore ($ this-> uid, $ score) for this user; $ this-> successMsg ('checked in ', array ('score' => $ score, 'Days '=> $ data ['days']);} else {$ this-> errorMsg ('sign-in failed, Please refresh and try again! ') ;}}/*** Return the data to be inserted in each sign-In ** @ param int $ uid user ID * @ return array (* 'days' => 'days ', * 'is _ sign' => 'indicates whether to sign in. Use 1 to indicate that you have already signed in. * 'stime' => 'sign in time ',*); */protected function getInsertData ($ uid) {// consecutive days of signing in yesterday $ start_time = strtotime (date ('Y-m-d 0: 0 ', time ()-86400 )) -1; $ end_time = strtotime (date ('Y-m-d 23:59:59 ', time ()-86400) + 1; $ days = M ('members _ sign ') -> where ("uid = $ uid and atime> $ start_time and Ime <$ end_time ")-> getField ('days'); if ($ days) {$ days ++; if ($ days> 30) {$ days = 1 ;}} else {$ days = 1;} return array ('days' => $ days, 'is _ sign' => 1, 'stime '=> time ();}/*** data the user signs in on the current day * @ return array sign-in information is_sign, stime, etc. */protected function todayData () {$ time = time (); $ start_stime = strtotime (date ('Y-m-d 0: 0 ', $ time)-1; $ end_stime = strtotime (date ('Y-m-d 23:59:59 ', $ time) + 1; return M ('Members _ sign')-> field ('atime ', true) -> where ("uid = {$ this-> uid} and atime> $ start_stime and atime <$ end_stime")-> find ();}/*** credit rule, returns the credits corresponding to the consecutive days of sign-In ** @ param int $ days the expected score for the current day * @ return int credits */protected function getTodayScores ($ days) {if ($ days = 30) {return 50;} else if ($ days> 19) {return 8;} else if ($ days> 9) {return 5 ;} else {return 3 ;}/ ** display the sign-in list ** @ param array $ signDays Date array (1, 2, 3, 4, 5, 12, 13) * @ param int $ year (optional), year * @ param int $ month (optional, month * @ return string date list <li> 1 </li> .... */protected function showDays ($ signDays, $ year, $ month) {$ time = time (); $ year = $ year? $ Year: date ('y', $ time); $ month = $ month? $ Month: date ('M', $ time); $ daysTotal = date ('T', mktime (0, 0, 0, $ month, 1, $ year )); $ now = date ('Y-m-d', $ time); $ str = ''; for ($ j = 1; $ j <= $ daysTotal; $ j ++) {$ I ++; $ someDay = date ('Y-m-d', strtotime ("$ year-$ month-$ j ")); // less than today's date style if ($ someDay <= $ now) {// today's date style tdc = todayColor if ($ someDay = $ now) {// if (in_array ($ j, $ signDays) checked in on the current day) {$ str. = '<li class = "current fw tdc"> '. $ j. '</li>';} else {$ str. = '<li class = "today fw tdc"> '. $ j. '</li>' ;}} else {// The date style checked in. current bfc = beforeColor, fw = font-weight if (in_array ($ j, $ signDays )) {$ str. = '<li class = "current fw bfc"> '. $ j. '</li>';} else {$ str. = '<li class = "fw bfc"> '. $ j. '</li>' ;}} else {$ str. = '<li> '. $ j. '</li>';} return $ str;}/*** get the number of days of sign-in for the current month, and $ this-> showDays () use array (, 13) */protected function getMonthSign () {$ time = time (); $ year = date ('y', $ time); $ month = date ('M', $ time); $ day = date ("t ", strtotime ("$ year-$ month"); $ start_stime = strtotime ("$ year-$ month-1 0: 0")-1; $ end_stime = strtotime ("$ year-$ month-$ day 23:59:59") + 1; $ list = M ('members _ sign ') -> where ("uid = {$ this-> uid} and stime> $ start_stime and stime <$ end_stime")-> order ('stime asc ') -> getField ('stime', true); foreach ($ list as $ key => $ value) {$ list [$ key] = date ('J ', $ value) ;}return $ list ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.