初始界面:
判斷用戶是否過(guò)期(如果未過(guò)期則重新登錄):
獲取用戶信息:
獲取用戶的信息并在前臺(tái)顯示:
主要實(shí)現(xiàn)兩個(gè)功能:
①判斷登錄是否過(guò)期,如果過(guò)期則就重新登錄,如果沒(méi)過(guò)期就提示未過(guò)期
②獲取用戶的信息,并在前臺(tái)顯示
index.wxml
<button bindtap="login">登錄</button><button bindtap="checksession">登錄是否過(guò)期</button><button open-type="getUserInfo" bindgetuserinfo="info">點(diǎn)擊授權(quán)</button><text>{{city}}</text><text>{{country}}</text><text>{{nickName}}</text><text>{{province}}</text>
index.js
//index.js//獲取應(yīng)用實(shí)例const app = getApp() Page({ data: { city:'', country:'', nickName:'', province:'' }, //發(fā)起http請(qǐng)求 login:function(){ wx.login({ success:function(res){ console.log(res.code) //發(fā)送請(qǐng)求 wx.request({ url: '自己的域名', //僅為示例,并非真實(shí)的接口地址 data: { code:res.code }, header: { 'content-type': 'application/json' // 默認(rèn)值 }, success(res) { console.log(res) } }) } }) }, //驗(yàn)證登錄是否過(guò)期 checksession:function(){ wx.checkSession({ success:function(res){ console.log(res,'登錄未過(guò)期') wx.showToast({ title: '登錄未過(guò)期啊', }) }, fail:function(res){ console.log(res,'登錄過(guò)期了') wx.showModal({ title: '提示', content: '你的登錄信息過(guò)期了,請(qǐng)重新登錄', }) //再次調(diào)用wx.login() wx.login({ success: function (res) { console.log(res.code) //發(fā)送請(qǐng)求 wx.request({ url: '自己的域名', //僅為示例,并非真實(shí)的接口地址 data: { code: res.code }, header: { 'content-type': 'application/json' // 默認(rèn)值 }, 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,用來(lái)接收前臺(tái)傳過(guò)來(lái)的code$code=$_GET['code']; //獲取到appid$appid="xxxxxxxxxxx"; //自己的appid$secret="xxxxxxxxxxxx"; //自己的secret$api="https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; //可去小程序開(kāi)發(fā)文檔中查看這個(gè)鏈接 //發(fā)送的代碼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;?>
關(guān)于這個(gè)php文件的說(shuō)明:
①獲取appid和secret:
②當(dāng)你點(diǎn)擊登錄的時(shí)候,出現(xiàn)這些東西就說(shuō)明php文件調(diào)用成功
③登錄憑證校檢地址(該里面的參數(shù)即可):
④域名要合法
在小程序平臺(tái)上:
在web開(kāi)發(fā)者工具里:
以上所述是小編給大家介紹的微信小程序用戶授權(quán)及判斷登錄是否過(guò)期詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注