| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
本文將與您分享微信小程序實現授權登錄并獲取用戶信息和手機號碼的方法。

相關學習推薦:小程序開發教程
綜上所述,增加了獲取用戶手機號碼的功能,該功能使用了獲取用戶信息和用戶手機號碼的功能。當我第一次寫它時,我發現我在一個登錄頁面上寫了兩個按鈕。獲取手機號的邏輯是用戶點擊授權時跳轉到首頁,用戶點擊拒絕時彈出提示,我最后發現可能是微信的限制。模擬器調試拒絕有提示,真機也能跳。沒有辦法寫一套關于用戶進入某個詳情頁判斷手機號授權的問題。!!

1.全球判斷
app({ on launch:function()){//調用API從本地緩存獲取數據//顯示本地存儲容量var logs = wx . getstoragesync(&;#39;日志& amp#39;)| |【】logs . un shift(date . now())wx . setstoragesync(& amp;#39;日志& amp#39;日志)that = this//獲取用戶是否首次在本地輸入新版本。Varversions = wx。getStorageSync(&;#39;版本和名稱。#39;);console . log(amp;#39;版本:& amp#39;+版本);//判斷是否if(versions = = &;#39;1 & amp#39;){//已授權,可以直接調用getUserInfo獲取頭像昵稱。無法播放box wx . get userinfo({ success:function(RES)){ that . global data . userinfo = RES . userinfo console . log(that . global data . userinfo)},fail:function(){ wx . redirectto({ URL:& amp;#39;../吳銳大廈/pages/log in/index & amp;#39;,})})}否則{//未授權,跳轉到登錄頁面wx . redirecto({ URL:&;#39;../吳銳大廈/pages/log in/index & amp;#39;,})} },on show:function(){//console . log(getCurrentPages())},on hide:function(){//console . log(getCurrentPages())},on error:function(msg){//console . log(msg)},util:require(& amp#39;we7/resource/js/util . js & amp;#39;),tabBar:{ & quot;顏色& quot:& quot# 123 & quot,& quot選定的顏色& quot:& quot# 1ba9ba & quot,& quot邊框樣式& quot:& quot# 1ba9ba & quot,& quot背景色& quot:& quot# fff & quot,& quot列表& quot:【】},getLocationInfo:function(CB){ var that = this;if(this . global data . location info){ CB(this . global data . location info)} else { wx . get location({ type:& amp;#39;gcj02 & amp#39;,//默認情況下,wgs84返回gps坐標,gcj02返回可用于wx.openLocation success的坐標:function(RES){ that . global data . location info = RES;CB(that . global data . location info)},fail:function(){//fail },complete:function(){//complete } }),globalData: { userInfo: null,appid:& quot;"app secret:& quot;",},siteInfo:require(& amp;#39;siteinfo.js & amp#39;)});2 .登錄登錄頁面判斷
index.wxml
& ltview wx:if = & quot;{ { isHide } } & quot& gt& ltview wx:if = & quot;{ { canIUse } } & quot& gt& lt視圖& gt& ltimage src = & amp#39;../../../we7/resource/images/log in . png & amp;#39;& gt& lt/image & gt;& lt/view & gt;& lt視圖& gt& lt文本& gt請依次允許訪問您的公共信息和手機號碼
。頁眉{ margin:90 rpx 0 90 rpx 50 rpx;邊框-底部:1px實心# ccctext-align:居中;寬度:650rpx高度:300rpxline -高度:450rpx}.標題圖像{ width:200 rpx;高度:200rpx}.內容{ margin -left:50 rpx;margin -bottom:90 rpx;}.內容文本{ display:block;顏色:# 9d9d9dmargin -top:40 rpx;} /* .operBtn { border -radius:80 rpx;邊距:70rpx 50rpxfont -大小:35rpx}.operBtns{ background: #eef0ed!重要;邊框-半徑:80rpx邊距:70rpx 50rpxfont -大小:35rpx顏色:#000300!重要;} */.隱藏{ border -radius:80 rpx;邊距:70rpx 50rpxfont -大小:35rpx顯示:無;}.show { display:block;/*背景:#eef0ed!重要;*/border -radius:80 rpx;邊距:70rpx 50rpxfont -大小:35rpx/* color: #000300!重要;*/}(3)index . js
const app = getApp();page({ data:{//判斷是否有API、回調、參數、組件等。小程序的當前版本可用。cani use:wx . cani use(& amp;#39;button . open -type . get userinfo & amp;#39;),isHide: false,authorized log in:& amp;#39;授權登錄&;#39;,用戶電話:#39;手機號碼授權&;#39;,李:",flag: true },onLoad:function(){ var that = this;//檢查是否授權//獲取用戶是否第一次進入新版本。var versions = wx . getstoragesync(&;#39;版本和名稱。#39;);if(versions = = & amp;#39;1 & amp#39;){ wx . get setting({ success:function(RES){ if(RES . auth setting【& amp;#39;scope.userInfo'】){//調用常用登錄方法app . util . get userinfo(function(userinfo){ that . setdata({ userinfo:userinfo })});} else {//用戶無權//更改isHide的值,并顯示授權頁面that . setdata({ isHide:true });} } });} else {//用戶無權//更改isHide的值,并顯示授權頁面that . setdata({ isHide:true });}},bindingetuserinfo:function(e){ if(e . detail . userinfo){//用戶按下了允許授權按鈕var that = thislet user = e . detail . userinfo;//獲取用戶信息,在控制臺上打印并查看console . log(& quot;用戶的信息如下:“);console.log(用戶);//授權成功后,通過更改isHide的值顯示實現頁面,隱藏授權頁面。#39;& amp#39;){ wx . show toast({ icon:& quot;無& quot,標題:& amp#39;請繼續點擊獲取手機號碼&;#39;,})、that . setdata({ isHide:true,flag:(!that.data.flag)、lee:true })that . wxlogin();} else if(!that . data . lee){ wx . switch tab({ URL:& quot;/吳銳房子/pages/index/index & quot;})} } else {//用戶按下了拒絕按鈕wx . show modal({ title:&;#39;警告&;#39;,內容:& amp#39;如果單擊“拒絕授權”,您將無法進入小程序。請授權后再進入!!!& amp#39;,showCancel: false,confirm text:& amp;#39;退貨授權&;#39;,success:function(RES){//用戶未授權成功,無需更改is hide { console . log(&;#39;用戶單擊了“返回授權”&;#39;);} } });} },wxlogin:function(){//獲取用戶的openID var that = this//調用常用登錄方法app . util . get userinfo(function(userinfo){ that . setdata({ userinfo:userinfo })});},獲取電話號碼:函數(e){//單擊獲取電話號碼按鈕var that = thisthat . data . lee if(that . data . lee = = & amp;#39;& amp#39;){ wx . show toast({ icon:& quot;無& quot,標題:& amp#39;請先點擊獲取用戶信息&;#39;,})return } else { wx . check session({ success:function(RES){ console . log(e . detail . errmsg)console . log(e . detail . iv)console . log(e . detail . encrypted data)var ency = e . detail . encrypted data;var iv = e . detail . iv;var session k = that . data . session key;if(e . detail . errmsg = = & amp;#39;用戶拒絕失敗。#39;){ wx . show modal({ title:& amp;#39;警告&;#39;,內容:& amp#39;您點擊了拒絕授權,部分功能無法使用!!!& amp#39;,showCancel: false,confirm text:& amp;#39;退貨授權&;#39;,success:function(RES){//用戶未授權成功,無需更改isHide的值if(RES . confirm){ wx . setstoragesync(&;#39;enws & amp#39;,& amp#39;1 & amp#39;);wx . switch tab({ URL:& quot;/吳銳房子/pages/index/index & quot;})console . log(& amp;#39;用戶單擊了“返回授權”&;#39;);};} })、that . setdata({ modal status:true,});} else { that . setdata({ lee:false,});wx . switch tab({ URL:& quot;/吳銳房子/pages/index/index & quot;})//同意授權VaruserInfo = wx。getStorageSync(&;#39;userInfo & amp#39;);app . util . request({ & amp;#39;url & amp#39;:& amp#39;entry/wxapp/saveusermobile&。#39;,data:{ session id:userinfo . session id,uid: userinfo.memberInfo.uid,iv: iv,encrypted data:encrypted },success:function(RES){ if(RES . data . data . error = = 0){ console . log(& amp;#39;成功& amp#39;+RES . data . data);//用戶輸入了新版本,可以更新本地數據wx . setstoragesync(&;#39;版本和名稱。#39;,& amp#39;1 & amp#39;);wx . setstoragesync(& amp;#39;enws & amp#39;,& amp#39;2 & amp#39;);} else {//用戶保存手機號碼失敗,將繼續授權手機號碼wx . setstoragesync(&;#39;enws & amp#39;,& amp#39;1 & amp#39;);console . log(amp;#39;失敗與失敗。#39;+RES . data . data);} },fail:function(RES){ console . log(& amp;#39;失敗與失敗。#39;+RES);} });} },fail:function(){ console . log(& quot;Session_key已過期,需要重新執行登錄過程”);that . wxlogin();//再次登錄} });} }})2.詳情頁上手機號碼的授權判斷。
所使用的掩模層寫入方法
①index.html
& lt代碼& gt& lt!--授權框提醒--》;& lt視圖class = & quot容器"& gt& lt視圖class = & quot浮動& quot隱藏= & amp#39;{ { viewShowed } } & amp#39;& gt& lt查看class = & amp#39;浮動內容和。#39;& gt& lt文本& gt允許授權獲取手機號碼
/*手機號碼授權*/。浮動{高度:100%;寬度:100%;位置:固定;background -color:rgba(0,0,0,0.5);z-指數:2;top:0;左:0;}.float content {/* padding:20 rpx 0;*/寬度:80%;背景:# fff利潤率:40%自動;邊框-半徑:20rpx顯示器:flexflex -方向:列;justify -內容:space -環繞;align -項目:居中;位置:相對;高度:255rpx}.float content text { color:# 000;font -大小:40rpx顯示:塊;邊距:0自動;位置:絕對;頂部:50rpx/* text -align:center;*//* line -高度:60rpx*/border -radius:30 rpx;}.float text { width:100%;高度:100rpx顯示器:flexjustify -內容:flex -啟動;位置:絕對;底部:0;}。BTN -cancell { line -height:100 rpx;flex:1;邊距:0;填充:0;邊框-半徑:無;}(3)index . js
Data: {view displayed: true,//控制是否可以顯示許可證},Cancle:function(){ wx . setstoragesync(&;#39;enws & amp#39;,& amp#39;2 & amp#39;);this . setdata({ view showed:true })},/* *生命周期函數--監控頁面顯示*/on show:Function(){ Varenws = wx . getstoragesync(&;#39;enws & amp#39;);console . log(& quot;enws:,+enws);var that = this如果(enws!= & amp#39;2 & amp#39;){//判斷。setdata({ view displayed:false,})console。log(&;#39;判斷是否授權&;#39;);} else {},獲取手機號碼:function(e){//單擊獲取手機號碼按鈕var that = thiswx . check session({ success:function(RES){ console . log(e . detail . errmsg)console . log(e . detail . iv)console . log(e . detail . encrypted data)var ency = e . detail . encrypted data;var iv = e . detail . iv;var session k = that . data . session key;that . setdata({ view showed:true,})wx . setstoragesync(& amp;#39;enws & amp#39;,& amp#39;2 & amp#39;);if(e . detail . errmsg = = & amp;#39;用戶拒絕失敗。#39;){} else {//同意授權var userinfo = wx . getstoragesync(&;#39;userInfo & amp#39;);app . util . request({ & amp;#39;url & amp#39;:& amp#39;entry/wxapp/saveusermobile&。#39;,data:{ session id:userinfo . session id,uid: userinfo.memberInfo.uid,iv: iv,encrypted data:encrypted },success:function(RES){ console . log(& amp;#39;成功& amp#39;+RES);if(RES . data . data . error = = 0){ console . log(& amp;#39;成功& amp#39;+RES . data . data);//用戶輸入了新版本,可以更新本地數據wx . setstoragesync(&;#39;版本和名稱。#39;,& amp#39;1 & amp#39;);} else {//用戶保存手機號碼失敗,下次將繼續授權手機號碼}},fail:function(RES){ console . log(&;#39;失敗與失敗。#39;+RES);} });} } });},更多編程相關知識請訪問:編程入門!!
這幾篇文章你可能也喜歡:
本文由主機參考刊發,轉載請注明:談如何授權登錄小程序,獲取信息和手機號(如何授權登錄微信小程序) http://m.k2270.com/112999.html
主機參考

















評論前必須登錄!
注冊