当前位置:网站首页>Laravel8 implementation of sign in function

Laravel8 implementation of sign in function

2022-06-11 16:07:00 Chenqing Nuo language

This article is a case

First come database


// User table 
CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '[email protected]',
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `days` int(11) DEFAULT NULL,
  `scores` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
// Integral table 
CREATE TABLE `sign_detailed` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) DEFAULT NULL COMMENT ' user id',
  `type` tinyint(4) DEFAULT NULL COMMENT ' The type of points obtained ',
  `fid` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ' Point source ',
  `score` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ' Points obtained ',
  `time` datetime DEFAULT NULL COMMENT ' Time ',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
// Sign in form 
CREATE TABLE `sign_records` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `userid` int(10) NOT NULL COMMENT ' user id',
  `ymd` date NOT NULL COMMENT ' Check in time ',
  `updated_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL COMMENT ' Check in time ',
  PRIMARY KEY (`id`,`userid`,`ymd`),
  UNIQUE KEY ` only ` (`userid`,`ymd`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
// integral 
CREATE TABLE `sign_rule` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `day` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ' Check in days ',
  `number` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ' integral ',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
// Note that the model should write    protected $guarded = [];

1.wxml

<view class='signIn'>
    <view class='sign-com'>
        <view class='thead'>
            <view class='tt'> Continuously signed in </view>
            <view class='mm'><label class='n'>{
   {signNum}}</label> God </view>
            <view class='pp'> Your score is {
   { score}}</view>
        </view>
        <view class='modle'>
            <view class='mol'>
                <view class='mol-line'></view>
                <view class='mol-ites'>
                    <view class="ite {
   {signNum>=min?'hover':''}}" data-n='{
   {min}}'>
                        <label class='n'>+{
   {min<7?1:3}}</label>
                    </view>
                    <view class="ite {
   {signNum>=min+1?'hover':''}}" data-n='{
   {min+1}}'>
                        <label class='n'>+{
   {min+1<7?1:3}}</label>
                    </view>
                    <view class="ite {
   {signNum>=min+2?'hover':''}}" data-n='{
   {min+2}}'>
                        <label class='n'>+{
   {min+2<7?1:3}}</label>
                    </view>
                    <view class="ite {
   {signNum>=min+3?'hover':''}}" data-n='{
   {min+3}}'>
                        <label class='n'>+{
   {min+3<7?1:3}}</label>
                    </view>
                    <view class="ite {
   {signNum>=min+4?'hover':''}}" data-n='{
   {min+4}}'>
                        <label class='n'>+{
   {min+4<7?1:3}}</label>
                    </view>
                    <view class="ite {
   {signNum>=min+5?'hover':''}}" data-n='{
   {min+5}}'>
                        <label class='n'>+{
   {min+5<7?1:3}}</label>
                    </view>
                    <view class="ite {
   {signNum>=min+6?'hover':''}}" data-n='{
   {max}}'>
                        <label class='n'>+{
   {min+6<7?1:3}}</label>
                    </view>
                </view>
            </view>
            <view class='moday'>
                <label class='dd'>{
   {min}} God </label>
                <label class='dd'>{
   {min+1}} God </label>
                <label class='dd'>{
   {min+2}} God </label>
                <label class='dd'>{
   {min+3}} God </label>
                <label class='dd'>{
   {min+4}} God </label>
                <label class='dd'>{
   {min+5}} God </label>
                <label class='dd'>{
   {max}} God </label>
            </view>
        </view>
        <view class='the-btn'>
            <button type='button' class='btn' bindtap='bindSignIn' data-num="{
   {signNum}}" disabled='{
   {signState}}' data-min="{
   {min}}" data-max="{
   {max}}" data-be="{
   {be}}"> Sign in </button>
        </view>
    </view>
</view>
<view class='explax'>
    <view class=''> The date starts :{
   {min}} </view>
    <view class=''> Date end :{
   {max}} </view>
    <view class=''> Check in number :{
   {signNum}} God </view>
    <view class=''> Your points are :{
   {score}}</view>
</view>

 2.wxss

.signIn {
    width: 100%;
    height: auto;
}
 
.sign-com {
    width: 100%;
    height: auto;
    padding: 0 30rpx;
    box-sizing: border-box;
    overflow: hidden;
}
 
.sign-com .thead {
    width: 100%;
    text-align: center;
    padding: 50rpx 0 35rpx;
}
 
.sign-com .thead .tt {
    font-size: 24rpx;
}
 
.sign-com .thead .mm {
    margin-top: 10rpx;
    font-size: 24rpx;
}
 
.sign-com .thead .mm .n {
    font-size: 66rpx;
    margin-right: 25rpx;
}
 
.sign-com .thead .pp {
    color: #999;
    font-size: 24rpx;
    margin-top: 10rpx;
}
 
.sign-com .modle {
    width: 100%;
    height: 100rpx;
    margin-top: 10rpx;
}
 
.sign-com .modle .mol {
    width: 100%;
    height: 52rpx;
    position: relative;
}
 
.sign-com .mol-line {
    width: 100%;
    height: 4rpx;
    background-color: #e6e6e6;
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
}
 
.sign-com .mol-ites {
    width: 100%;
    height: 100%;
    position: absolute;
}
 
.mol-ites .ite {
    width: 52rpx;
    height: 52rpx;
    border-radius: 50%;
    border: 1px solid #f5f5f5;
    background-color: #fff;
    box-sizing: border-box;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
}
 
.mol-ites .ite .n {
    width: 44rpx;
    height: 44rpx;
    line-height: 44rpx;
    text-align: center;
    border-radius: 50%;
    background-color: #f5f5f5;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-size: 22rpx;
}
 
.mol-ites .ite::after {
    content: "";
    width: 80rpx;
    height: 4rpx;
    background-color: transparent;
    position: absolute;
    left: 52rpx;
    top: 50%;
    margin-top: -2rpx;
    z-index: 2;
}
 
.mol-ites .ite:last-of-type::after {
    width: 0;
}
 
.mol-ites .ite:nth-of-type(2) {
    left: 107rpx;
}
 
.mol-ites .ite:nth-of-type(3) {
    left: 214rpx;
}
 
.mol-ites .ite:nth-of-type(4) {
    left: 321rpx;
}
 
.mol-ites .ite:nth-of-type(5) {
    left: 428rpx;
}
 
.mol-ites .ite:nth-of-type(6) {
    left: 535rpx;
}
 
.mol-ites .ite:nth-of-type(7) {
    left: 642rpx;
}
 
.mol-ites .ite.hover {
    border-color: #ff614a;
}
 
.mol-ites .ite.hover .n {
    background-color: #ff614a;
    color: #fff;
}
 
.mol-ites .ite.hover::after {
    background-color: #ff614a;
}
 
.moday {
    width: 100%;
    height: 40rpx;
    overflow: hidden;
    position: relative;
    margin-top: 20rpx;
}
 
.moday .dd {
    width: 52rpx;
    height: 40rpx;
    line-height: 1;
    text-align: center;
    font-size: 22rpx;
    position: absolute;
    left: 0;
    bottom: 0;
}
 
.moday .dd:nth-of-type(2) {
    left: 107rpx;
}
 
.moday .dd:nth-of-type(3) {
    left: 214rpx;
}
 
.moday .dd:nth-of-type(4) {
    left: 321rpx;
}
 
.moday .dd:nth-of-type(5) {
    left: 428rpx;
}
 
.moday .dd:nth-of-type(6) {
    left: 535rpx;
}
 
.moday .dd:nth-of-type(7) {
    left: 642rpx;
}
 
.the-btn {
    margin: 50rpx 0;
}
 
.the-btn .btn {
    background-color: #ff614a;
    color: #fff;
}
 
.the-btn.signed .btn {
    background-color: rgba(153, 153, 153, 0.61);
}
 
.explax {
    padding: 0 30rpx;
    font-size: 28rpx;
    color: #666;
}

3.js


const app = getApp();
 
Page({
 
  /**
   *  Initial data of the page 
   */
  data: {
    // Check in module 
    signNum: 0,  // Check in number 
    signState: false, // Check in status 
    integral: '',
    min: 1,  // The default value is the first day of the date 1
    max: 7,  // The default value is the last day of the date 7
    score:"0"
  },
 
  // Sign in 
  bindSignIn(e) {
    //   obtain token
    var token = wx.getStorageSync('token');
    let that = this;
    //   user id
    wx.request({
      url: '*******************',// Request path 
      data: {},
      header:{"Authorization":"Bearer "+token},
      success(res) {
        //   console.log(res.data.data);
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          that.setData({
            signNum:res.data.data.days,
            score:res.data.data.score,
            signState:true
          })
        }
        if (res.data.code == 500) {
          wx.showToast({
            title: ' Network anomalies ',
          })
        }
        if (res.data.code == 501) {
          wx.showToast({
            title: ' Check in failed ',
          })
        }
      }
    })
  },
  /**
   *  Life cycle function -- Monitor page loading 
   */
  onLoad: function (options) {
    var token = wx.getStorageSync('token');
    let that = this;
    //   user id
    wx.request({
      url: '*******************', // Just for the sample , Not a real interface address 
      data: {},
      header:{"Authorization":"Bearer "+token},
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            signNum:res.data.data.days,
            score:res.data.data.score,
          })
          
        }
      }
    })
  },
})

4. Back end code snippet

public function signin(){
        $userId =1;
        $user = \App\Models\Login::where('id',$userId)->first();
        $day = $user->days;
        // Gets the current time 
        $yearMonthDay = date('Y-m-d',time());
        $has =  Records::where('userid',$userId)->where('ymd',$yearMonthDay)->first();
        if($has)
        {
            return ['code'=>200,'msg'=>' Signed in ','data'=>[]];
        }
        // Open transaction 
        DB::beginTransaction();
        try {
            // Will the user id  and   The current check-in days are added to the table 
            $sign = Records::create(['userid'=>$userId,'ymd'=>$yearMonthDay]);
            $signId = $sign->id;
            // Get the last sign in time of the user 
            $lastSignDayObj = Records::select('ymd')->where('userid',$userId)->where('id','<',$signId)->orderBy('id','desc')->limit(1)->first();
            // According to the last check-in time, it can be judged as   Severance  , Or continuous check-in  , This is the first time to sign in 
            if (empty($lastSignDayObj)){
                // non-existent   Indicates the first check-in 
                $days = 1;
                $number = Rule::select('number')->where('day',$days)->first();
                if ($number){
                    $score = $number->number;
                    // There is 
                }else{
                    // non-existent 
                    $score = 7;
                }
                $status = ' First sign in , Get integral '.$score;
            }else{
                $lastSignDay = $lastSignDayObj->ymd;
                // There is    Timestamp the current number of days   and   Compare the timestamp of the last check-in 
                $time = strtotime($yearMonthDay) - strtotime($lastSignDay);
                if ($time >= 24*3600 && $time < 48*3600){
                    // Indicates continuous check-in   Sign in days plus one 
                    $days = $day +1;
                    $number = Rule::select('number')->where('day',$days)->first();
                    if ($number){
                        $score = $number->number;
                    }else{
                        // non-existent 
                        $score = 7;
                    }
                    $status = ' Continuous sign in '.$days.' God , Get integral '.$score;
                }else if ($time >= 48*3600){
                    // Indicates disconnection   and   The first check-in is the same 
                    $days = 1;
                    $number = Rule::select('number')->where('day',$days)->first();
                    if ($number){
                        $score = $number->number;
                        // There is 
                    }else{
                        // non-existent 
                        $score = 7;
                    }
                    $status = ' Sign in for the first time after signing off , Get integral '.$score;
                }else{
                    return ['code'=>500,'msg'=>' Network error '];
                }
            }
            // Record the user's points details 
            Detailed::create(['userid'=>$userId,'score'=>$score,'type'=>1,'fid'=>$signId.' The order number ']);
            // Calculate the user's credit balance 
            $scores = Detailed::where('userid',$userId)->sum('score');
//            dd($days,$scores);
            // Change the number of consecutive sign in days for the user   and   integral 
            \App\Models\Login::where('id',$userId)->update(['days'=>$days,'scores'=>$scores]);
            // Transaction submission 
            DB::commit();
            return ['code'=>200,'msg'=>' Sign in successfully ','data'=>
                ['score'=>$scores,'status'=>$status,'days'=>$days]];
        }catch (\Exception $e){
            // Transaction rollback 
            DB::rollBack();
            return ['code'=>501,'msg'=>' Check in failed '];
        }
    }

        // Page initialization 
        public function signinin()
        {
            // user ID
            $uid =1;
            $data = \App\Models\Login::where('id',$uid)->first();
            return ['code'=>200,'msg'=>' Sign in successfully ','data'=>$data];
        }

原网站

版权声明
本文为[Chenqing Nuo language]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111554494221.html