
判断用户是否过期(如果未过期则重新登录):

获取用户信息:

获取用户的信息并在前台显示:

主要实现两个功能:
①判断登录是否过期,如果过期则就重新登录,如果没过期就提示未过期
②获取用户的信息,并在前台显示
index.wxml
button bindtap="login"登录/buttonbutton bindtap="checksession"登录是否过期/buttonbutton open-type="getUserInfo" bindgetuserinfo="info"点击授权/buttontext{{city}}/texttext{{country}}/texttext{{nickName}}/texttext{{province}}/textindex.js
const app = getApp() Page({ data: { city:'', country:'', nickName:'', province:'' }, login:function(){ wx.login({ success:function(res){ console.log(res.code) wx.request({ url: '自己的域名', data: { code:res.code }, header: { 'content-type': 'application/json' }, success(res) { console.log(res) } }) } }) }, checksession:function(){ wx.checkSession({ success:function(res){ console.log(res,'登录未过期') wx.showToast({ title: '登录未过期啊', }) }, fail:function(res){ console.log(res,'登录过期了') wx.showModal({ title: '提示', content: '你的登录信息过期了,请重新登录', }) wx.login({ success: function (res) { console.log(res.code) wx.request({ url: '自己的域名', data: { code: res.code }, header: { 'content-type': 'application/json' }, success(res) { console.log(res) } }) } }) } }) }, info:function(){ var that=this wx.getUserInfo({ success:function(res){ console.log(res.userInfo) var city = res.userInfo.city var country = res.userInfo.country var nickName = res.userInfo.nickName var province = res.userInfo.province that.setData({ city:city, country:country, nickName:nickName, province:province }) } }) }})index.php
?php$code=$_GET['code']; $appid="xxxxxxxxxxx"; $secret="xxxxxxxxxxxx"; $api="https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; function httpGet($url){$curl=curl_init();curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_TIMEOUT, 500);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);curl_setopt($curl, CURLOPT_URL, $url);$res= curl_exec($curl);curl_close($curl);return $res;} $str=httpGet($api); echo $str;?关于这个php文件的说明:
①获取appid和secret:

②当你点击登录的时候,出现这些东西就说明php文件调用成功

③登录凭证校检地址(该里面的参数即可):

④域名要合法
在小程序平台上:

在web开发者工具里:

TAG标签:
微信小程序token过期
http://www.xiaochengxub.com/read-102610.html