微信小程序签到

login(){
  let token=wx.getStorageSync('token')
  if(!token){
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
       let userinfo=res.userInfo;
       wx.login({
        success (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'http://www.zb.com/index.php/index',
              data: {
                code: res.code,
                nick_name:userinfo.nickName,
                avatar:userinfo.avatarUrl
              },
              success:ret=>{
                wx.setStorageSync('token', ret.data.token)
              }
            })
          } else {
            console.log('登录失败!' + res.errMsg)
          }
        }
      })
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  }

},

  bindSignIn(e){
       this.login()
       wx.redirectTo({
         url: '/pages/qi/qi',
       })
  },
 onLoad: function (options) {
    let token=wx.getStorageSync('token')
    wx.request({
      url: 'http://www.zb.com/index.php/admin/login/qi?token='+token,
      success:({data})=>{
      this.setData({
        ...data
      })

     
      }
    })

  },
<view>

  <view wx:if="{{clock_status==0}}"> <button bindtap="qiandao">签到</button></view>
 
    <view wx:else> <button bindtap="yiqiandao">已签到</button> </view>





</view>
  qiandao(e){
    let token=wx.getStorageSync('token');
    wx.request({
      url: 'http://www.zb.com/index.php/admin/login/qi?token='+token,
      success:({data})=>{
        this.setData({
          ...data
        })
        
        }
    })
  },
 public function getUrl($url){
            $headerArray =array("Content-type:application/json;","Accept:application/json");
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
            $output = curl_exec($ch);
            curl_close($ch);
            $output = json_decode($output,true);
            return $output;

    }

    public function login()
    {
            $params=input();
        $AppID = 'wxa1f79edf42175e02';//微信公众平台获取
        $AppSecret = '5e7b75c47168b51084e4b9f63ec78495';
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
        $url = sprintf($url,$AppID,$AppSecret,$params['code']);
        $data=$this->getUrl($url);
        $user= User::where('openid',$data['openid'])->find();
       if (empty($user)){
          $user= User::create([
            'nick_name'=>$params['nick_name'],
               'avatar'=>$params['avatar'],
               'openid'=>$data['openid']
           ]);
       }

        $token=md5($user['id'].$user['openid'].rand(1111,99999999));

       cache($token,$user,7200);

//      $res=cache($token);
//
//      print_r($res);die();
       return  json(['token'=>$token,'error_code'=>0,'msg'=>'登录成功']);
//       print_r($data); die();

    }


    public function qi()
    {
        $token=input('get.token');

       //获取用户信息
       $user=cache($token);
//          print_r($user);die();
       $uid=$user['id'];

     $data=Users::where('uid',$uid)->whereTime('clock_date','today')->find();
// print_r($data);die();
     if ($data == null){
         Users::create(['uid'=>$uid,'clock_date'=>time()]);
         return json(['clock_status'=>0,'error_code'=>0,'msg'=>"操作成功"]);

     }else{
         return json(['clock_status'=>1,'error_code'=>0,'msg'=>"操作成功"]);

     }

//



    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值