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

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

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

初始界面:

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

獲取用戶信息:

獲取用戶的信息并在前臺顯示:

 

主要實現(xiàn)兩個功能:

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

②獲取用戶的信息,并在前臺顯示

index.wxml

<button bindtap="login">登錄</button>
<button bindtap="checksession">登錄是否過期</button>
<button open-type="getUserInfo" bindgetuserinfo="info">點擊授權(quán)</button>
<text>{{city}}</text>
<text>{{country}}</text>
<text>{{nickName}}</text>
<text>{{province}}</text>

index.js

//index.js
//獲取應(yīng)用實例
const app = getApp()
 
Page({
 data: {
  city:'',
  country:'',
  nickName:'',
  province:''
 },
 //發(fā)起http請求
 login:function(){
  wx.login({
   success:function(res){
    console.log(res.code)
    //發(fā)送請求
    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: '你的登錄信息過期了,請重新登錄',
    })
    //再次調(diào)用wx.login()
    wx.login({
     success: function (res) {
      console.log(res.code)
      //發(fā)送請求
      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,用來接收前臺傳過來的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ā)文檔中查看這個鏈接
 
//發(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)于這個php文件的說明:

①獲取appid和secret:

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

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

④域名要合法

在小程序平臺上:

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

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

相關(guān)文章

最新評論

交城县| 达拉特旗| 叶城县| 梧州市| 潼关县| 全椒县| 高州市| 本溪| 禹州市| 昌邑市| 左贡县| 宝坻区| 寿阳县| 永善县| 澜沧| 绥棱县| 西乌珠穆沁旗| 灵丘县| 观塘区| 探索| 荣昌县| 连城县| 棋牌| 修水县| 海安县| 民勤县| 伊川县| 石楼县| 宁远县| 会宁县| 长治县| 济宁市| 西华县| 儋州市| 子洲县| 汝州市| 茂名市| 米易县| 秦安县| 开江县| 报价|