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'=>"操作成功"]);
}
//
}