laravel8实现微信小程序签到

小程序:

sign.wxml:

<!-- 签到顶部背景信息-->

<view class='signBkImg'>

  <image src='https://img2.baidu.com/it/u=3558912797,2093608853&fm=26&fmt=auto'></image>

  <view class='sign-con'>

    <view class='headNews-left'>

      <image src='{{data.img}}'></image>

      <view class='nikeNme'>

        <view>{{data.nickname}}</view>

        <view>已签到 {{data.days}} 天</view>

      </view>

    </view>

    <navigator url='/pages/mine/integral/integral'>兑换商品</navigator>

  </view>

</view>

<!-- 积分,积分明细 -->

<view class='points-con'>

  <view>

    <text>我的积分:</text>

    <text class='points-num'>{{data.scores}}</text>

  </view>

  <navigator url='/pages/score/score'>

    积分明细 >

  </navigator>

</view>

<!-- 日历 -->

<view class="calendar">

  <view class='preNext-path'>

    <!-- <image src='/img/mine/preIcon.png' bindtap='sign_prev'></image> -->

    <text>{{data.created_at}}月</text>

    <!-- <image src='/img/mine/nextIcon.png' bindtap='sign_next'></image> -->

  </view>
 

  <view class="weekName">

    <view class="sunday">日</view>

    <view class="monday">一</view>

    <view class="tuesday">二</view>

    <view class="wednesday">三</view>

    <view class="thursday">四</view>

    <view class="friday">五</view>

    <view class="saturday">六</view>

  </view>

  <view class="week">

    <!--填补空格-->

    <view wx:for="{{nbsp}}">\n</view>

  </view>

  <view class="calendarSign">

    <button class='signInBtn' bindtap="sign">签到</button>

  </view>

</view>

sign.js:

Page({

/**

   * 页面的初始数据

   */

  data: {

     data:""

  },

/**

   * 展示用户签到的天数、积分

   * 生命周期函数--监听页面加载

   */

onLoad: function (options) {

    var that = this

    wx.request({

      url: 'http://www.week3.com/api/show', 

      data: {

      },

      header: {

        token:wx.getStorageSync('token')

      },

      success (res) {

        console.log(res.data)

        that.setData({

          data:res.data.data

        })

      }

    })

  },

 /**

   * 实现签到功能

   */

sign(e){

     var that = this

     wx.request({

      url: 'http://www.week3.com/api/sign', 

      data: {

      },

      header: {

        token:wx.getStorageSync('token')

      },

      method:"POST",

      success (res) {

        console.log(res.data)

        if(res.data.code==200){

          wx.showToast({

            title: '首次签到成功',

          })

        }

        if(res.data.code==100){

          wx.showToast({

            title: '今天已经签到过了',

          })

        }

        if(res.data.code==300){

          wx.showToast({

            title: '今天已断签',

          })

        }

        if(res.data.code==400){

          wx.showToast({

            title: '连续签到成功',

          })

        }

      }

    })

  },

})

sign:json

{

  "navigationBarTitleText": "签到",

  "usingComponents": {

    "l-button":"/miniprogram_npm/lin-ui/button"

  }

}

sign.wxss

/* 签到顶部背景信息 */

.signBkImg{

  width: 100%;

  height: 241rpx;

  position: relative;

}

.signBkImg>image{

  width: 100%;

  height: 100%;

}

.sign-con{

  width: 92%;

  height: 213rpx;

  padding:  0 30rpx;

  display: flex;

  justify-content: space-between;

  align-items: center;

  position: absolute;

  top: 0;

}

.sign-con>navigator{

  width: 167rpx;

  height: 67rpx;

  line-height: 67rpx;

  text-align: center;

  border-radius: 34rpx;

  color: #33998C;

  background-color: #fff;

  font-size: 30rpx;

}

.headNews-left{

  display: flex;

  align-items: center;

  flex-wrap: nowrap;

}

.headNews-left>image{

  width: 133rpx;

  height: 133rpx;

  border: 6rpx solid rgb(255,255,255);

  border-radius: 50%;

}

.nikeNme{

    color: rgb(254,254,254);

    margin-left: 26rpx;

}

.nikeNme>view:nth-child(1){

  font-size: 40rpx;

  font-weight: bold;

}

.nikeNme>view:nth-child(2){

  font-size: 26rpx;

  margin-top: 12rpx;

}

/* 积分,积分明细 */

.points-con{

  margin: 50rpx 0;

  display: flex;

}

.points-con>view,.points-con>navigator{

  width: 50%;

  height: 61rpx;

  line-height: 61rpx;

  text-align: center;

  color: rgb(119,119,119);

  font-size: 30rpx;

}

.points-con>view{

  border-right: 2rpx solid rgb(235,235,235);

}

.points-num{

  font-size: 38rpx;

  color: #33998C;

}


 

/* 日历签到 */

/**index.wxss**/

.t_red{

  color: red;

}

.t_blue{

  color: royalblue;

}

.calendar{

  background-color: white;

  box-shadow: 2px 2px 5px 2px #e6e6e6;

  padding:30rpx;

  width: 85%;

  margin-left: 4%;

  border-radius: 50rpx;

  display: block

}

.time{

  padding: 16rpx 20rpx;

  background-color: wheat;

  display: flex;

}

.time{

  font-family: '黑体';

  font-size: 40rpx;

  color: white;

  text-align: center;

  padding: 16rpx 20rpx;

  height:80rpx;

  line-height:80rpx;

  border-radius:12rpx;

  background-color: #f9634b;

  display: flex;

  margin-bottom: 29rpx;

}

.time view{

  flex: 1;

  font-size: 30rpx;

}

.time view text{

  font-size: 38rpx;

}

.weekName{

  font-size: 36rpx;

  color: #333;

  width: 100%;

  display: flex;

  padding: 16rpx 0;

}

.weekName view{

  flex: 1;

  text-align: center;

}

.sunday,.saturday{

  color: #33998C;

}

.week{

  width: 100%;

}

.week view{

  width: 14.2%;

  height: 90rpx;

  line-height: 80rpx;

  display: inline-block;

  margin: 10rpx 0;

  font-size: 34rpx;

  text-align: center;

  

}

.week view text{

  width: 100%;

  height: 100%;

  display: inline-block;

}

.calendarSign{

  margin: 40rpx auto;

}

.selDate{

  font-size: 28rpx;

   border-bottom: 1px solid #33998C; 

}

.selDateItem{

   color:#33998C;

   font-weight: bold;

   font-size:22rpx; 

   border-radius: 50%;

}

/* 前后时间 */

.preNext-path{

  margin: 28rpx 0;

  display: flex;

  align-items: center;

  justify-content: center;

}

.preNext-path>image{

    width: 11rpx;

    height: 21rpx;

}

.preNext-path>text{

   color: #33998C;

   font-size: 28rpx;

   margin: 0 54rpx;

}

/* 签到按钮 */

.signInBtn{

  width: 235rpx;

  height: 77rpx;

  line-height: 77rpx;

  text-align: center;

  border-radius: 10rpx;

  font-size: 34rpx;

  color: #fff;

  background:#33998C;

}

后端php

<?php

namespace App\Http\Controllers;

use App\Models\Login;
use App\Models\Score;
use App\Models\Sign;
use App\Models\User_Sign;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SignController extends Controller
{

    //签到记录
    public function getSign(request $request)
    {

        DB::beginTransaction(); //开启事务
        //获取到用户id
        $openid = $request['uid'];

        $login = Login::where('openid',$openid)->first();

        $uid = $login['id'];

        //查询是否签到过
        $data = Sign::where('uid',$uid)->first();

        if (empty($data)){
            //用户没有签到过
            $day = 1;
            //查询第一天的签到所得积分
            $score = Score::where('day',$day)->first();
            //添加签到记录

            $arr = [
              'uid'=>$uid,
              'mouth_week_day'=>date('Y-m-d 00:00:00'),
            ];
            $userSgin =[
                'uid'=>$uid,
                'days'=>$score['day'],
                'scores'=>$score['score']
            ];
            $user = Login::where('id',$uid)->first();

            $sign = Sign::create($arr,true);//添加签到记录

            $score_sign = User_Sign::create($userSgin,true);//添加签到积分

            $upuser = Login::where('id',$uid)->update(['days'=>$score['day'],'scores'=>$user['scores']+$score['score']]);

            if ($sign && $score_sign && $upuser!==false){
                DB::commit();
                return ['code'=>200,'msg'=>'签到成功','data'=>$user];
            }else{
                DB::rollBack();
                return ['code'=>500,'msg'=>'签到失败','data'=>null];
            }

        }
        //如果用户签到过
        if (!empty($data)){
            //查询用户最近的签到记录
            $signTime = Sign::where('uid',$uid)->orderBy('mouth_week_day','desc')->first();

            $time = $signTime['mouth_week_day'];

            $prevTime = strtotime($time);//获取最近一天的签到时间

            $nowTime = time();//当前时间
            //判断用户今日是否签到过
            if ($nowTime-$prevTime<24*3600){
                return ['code'=>100,'msg'=>'您今天已经签到过了','data'=>null];
            }
            //判断用户是否断签
            if ($nowTime-$prevTime>48*3600){
                //如果断签了还是从第一天算起
                $day = 1;  //断签从第一天开始算起
                $score = Score::where('day',$day)->first();
                //添加签到记录
                $arr = [
                    'uid'=>$uid,
                    'mouth_week_day'=>date('Y-m-d 00:00:00'),
                ];
                $userSgin =[
                    'uid'=>$uid,
                    'days'=>$score['day'],
                    'scores'=>$score['score']
                ];
                $user = Login::where('id',$uid)->first();

                $sign = Sign::create($arr,true);//添加签到记录

                $score_sign = User_Sign::create($userSgin,true);//添加签到积分

                $upuser = Login::where('id',$uid)->update(['days'=>$score['day'],'scores'=>$user['scores']+$score['score']]);

                if ($sign && $score_sign && $upuser!==false){
                    DB::commit();
                    return ['code'=>300,'msg'=>'今日已断签','data'=>$user];
                }else{
                    DB::rollBack();
                    return ['code'=>500,'msg'=>'签到失败','data'=>null];
                }

            }
            //判断用户今天是否签到
            if (24*3600<$nowTime-$prevTime && $nowTime-$prevTime<48*3600){
                $user = Login::where('id',$uid)->first();//获取出连续签到天数

                //查询出连续签到所得积分
                $score = Score::where('day',$user['days']+1)->first();

                //添加签到记录
                $arr = [
                    'uid'=>$uid,
                    'mouth_week_day'=>date('Y-m-d 00:00:00'),
                ];
                $userSgin =[
                    'uid'=>$uid,
                    'days'=>$score['day'],
                    'scores'=>$score['score']
                ];
                $user = Login::where('id',$uid)->first();

                $sign = Sign::create($arr,true);//添加签到记录

                $score_sign = User_Sign::create($userSgin,true);//添加签到积分

                $upuser = Login::where('id',$uid)->update(['days'=>$score['day'],'scores'=>$user['scores']+$score['score']]);

                if ($sign && $score_sign && $upuser!==false){
                    DB::commit();
                    return ['code'=>400,'msg'=>'连续签到成功','data'=>$user];
                }else{
                    DB::rollBack();
                    return ['code'=>500,'msg'=>'签到失败','data'=>null];
                }

            }


        }

    }

    /**
     * 用户签到展示
     * @return array
     */
    public function showSign(request $request)
    {
        $openid = $request['uid'];
        $login = Login::where('openid',$openid)->first();

        $uid = $login['id'];
        $data = Login::where('id',$uid)->first();
        return ['code'=>200,'msg'=>'用户签到展示成功','data'=>$data];
    }

    
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值