最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序用戶授權(quán),以及判斷登錄是否過期的方法

 更新時(shí)間:2019年05月10日 10:24:39   作者:光頭強(qiáng)兒  
這篇文章主要介紹了微信小程序用戶授權(quán)及判斷登錄是否過期,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

初始界面:

判斷用戶是否過期(如果未過期則重新登錄):

獲取用戶信息:

獲取用戶的信息并在前臺(tái)顯示:

 

主要實(shí)現(xiàn)兩個(gè)功能:

①判斷登錄是否過期,如果過期則就重新登錄,如果沒過期就提示未過期

②獲取用戶的信息,并在前臺(tái)顯示

index.wxml

<button bindtap="login">登錄</button>
<button bindtap="checksession">登錄是否過期</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)證登錄是否過期
 checksession:function(){
  wx.checkSession({
   success:function(res){
    console.log(res,'登錄未過期')
    wx.showToast({
     title: '登錄未過期啊',
    })
   },
   fail:function(res){
    console.log(res,'登錄過期了')
    wx.showModal({
     title: '提示',
     content: '你的登錄信息過期了,請(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,用來接收前臺(tá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";  //可去小程序開發(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文件的說明:

①獲取appid和secret:

②當(dāng)你點(diǎn)擊登錄的時(shí)候,出現(xiàn)這些東西就說明php文件調(diào)用成功

③登錄憑證校檢地址(該里面的參數(shù)即可):

④域名要合法

在小程序平臺(tái)上:

在web開發(fā)者工具里:

以上所述是小編給大家介紹的微信小程序用戶授權(quán)及判斷登錄是否過期詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

宁国市| 嫩江县| 务川| 广安市| 赣州市| 临城县| 泾川县| 德惠市| 鄂州市| 通山县| 安仁县| 阳高县| 甘南县| 绥滨县| 公安县| 蒙自县| 济阳县| 金溪县| 闸北区| 房产| 沂水县| 喀喇沁旗| 酒泉市| 当涂县| 鹿泉市| 滨海县| 诸暨市| 沅陵县| 隆子县| 高淳县| 河津市| 贵定县| 历史| 阳东县| 石阡县| 绥棱县| 满洲里市| 德钦县| 孙吴县| 肇源县| 厦门市|