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

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

1.全球判斷
App({ onLaunch: function () {//調用API從本地緩存獲取數據//顯示本地存儲容量var logs = wx . getstoragesync( # 39;日志 # 39;)| |[]logs . un shift(date . now())wx . setstoragesync( # 39;日志 # 39;對數)that = this//獲取用戶是否第一次進入新版本var versions = wx . getstoragesync( # 39;版本 # 39;);console . log( # 39;版本: # 39;+版本);//確定是否需要授權if(versions = = # 39;1'){//授權,可以直接調用getUserInfo獲取頭像昵稱,不會彈回wx . get userinfo({ success:function(RES){ that . global data . userinfo = RES . userinfo console . log(that . global data . userinfo)},fail:function(){ wx . redirect to({ URL: # 39;../吳銳_豪斯/pages/log in/index # 39;,} } } } } else {//未授權,跳轉到登錄頁面wx . redirect to({ URL: # 39;../吳銳_豪斯/pages/log in/index # 39;,}) },on show:function(){//console . log(getCurrentPages())},on hide:function(){//console . log(getCurrentPages())},on error:function(msg){//console . log(msg)},util:require( # 39;we7/resource/js/util . js # 39;),tabBar:{ ;顏色 : # 123 , 選定的顏色 : # 1 ba 9 ba ;, 邊框樣式 : # 1 ba 9 ba ;, 背景顏色 : # fff , 列表 :[ ] },getLocationInfo:function(CB){ var that = this;if(this . global data . location info){ CB(this . global data . location info)} else { wx . get location({ type: # 39;gcj02 # 39,//默認情況下,wgs84返回gps坐標,gcj02返回成功:function(RES){ that . global data . location info = RES;CB(that . global data . location info)},fail: function () { // fail },complete:function(){//complete } } },globalData: { userInfo: null,appid: ;",app secret: ;",},siteInfo:require( # 39;siteinfo.js # 39)});2 .登錄登錄頁面判斷
index.wxml
ltview wx:if = ;{ { isHide } } gt ltview wx:if = ;{ { canIUse } } gt lt視圖 gt ltimage src = # 39../../../we7/resource/images/log in . png # 39; gt lt/image gt; lt/view gt; lt視圖 gt lttext gt請依次允許訪問您的公共信息和手機號碼
。頁眉{ margin:90 rpx 0 90 rpx 50 rpx;border -bottom:1px solid # CCC;text-align:居中;寬度:650rpx身高:300rpxline -身高:450rpx}.標題圖像{ width:200 rpx;身高:200rpx}.content { margin -left:50 rpx;margin -bottom:90 rpx;}.內容文本{ display:block;顏色:# 9d9d9dmargin -top:40 rpx;} /* .oper BTN { border -radius:80 rpx;邊距:70rpx 50rpxfont -size:35 rpx;}.operBtns{ background: #eef0ed!重要;border -半徑:80rpx邊距:70rpx 50rpxfont -size:35 rpx;顏色:#000300!重要;} */.隱藏{ border -radius:80 rpx;邊距:70rpx 50rpxfont -size:35 rpx;顯示:無;}.顯示{ display:block;/*背景:#eef0ed!重要;*/border -radius:80 rpx;邊距:70rpx 50rpxfont -size:35 rpx;/*顏色:#000300!重要;*/} (C)索引. js
const app = getApp();Page({ data: {//確定是否有API、回調、參數、組件等。小程序的當前版本可用。cani use:wx . cani use( # 39;button . open -type . get userinfo # 39;),isHide: false,authorized log in: # 39;授權 # 39;,用戶電話: # 39;手機授權號 # 39;,李: ",flag: true },onLoad:function(){ var that = this;//檢查是否授權//Get用戶是否第一次進入新版本var versions = wx . getstoragesync( # 39;版本 # 39;);if(versions = = # 39;1'){ wx . get setting({ success:function(RES){ if(RES . auth setting[ # 39;scope.userInfo # 39]){//調用常用登錄方法app . util . get userinfo(function(userinfo){ that . setdata({ userinfo:userinfo })});} else {//用戶未被授權。//更改isHide的值,顯示授權頁面that . setdata({ is hide:true });} } });} else {//用戶未被授權。//更改isHide的值,顯示授權頁面that . setdata({ is hide:true });}},bindge userinfo:function(e){ if(e . detail . userinfo){//用戶按下了允許授權按鈕that = thislet user = e . detail . userinfo;//獲取用戶的信息,打印到控制臺看console . log( ;用戶的信息如下:“);console.log(用戶);//授權成功后,通過改變isHide的值,顯示實現頁面,隱藏授權頁面。that . data . lee if(that . data . lee = = # 39;'){ wx . show toast({ icon: ;無 ,標題: # 39;請繼續點擊手機號碼 # 39;,})、that.setData({ isHide: true,flag:(!that.data.flag)、lee:true })that . wx login();} else if(!that . data . lee){ wx . switch tab({ URL: ;/吳銳_豪斯/pages/index/index ;} } } } else {//用戶按下了拒絕按鈕wx . show modal({ title: # 39;警告 # 39;,內容: # 39;您不能通過單擊“拒絕授權”來訪問小程序。請授權后再進入!!!',showCancel: false,confirm text: # 39;返回授權 # 39;,success: function(res) {//用戶未授權成功,不需要更改isHide的值if(RES . confirm){ console . log( # 39;用戶點擊了“返回授權” # 39;);} } });} },wxlogin: function() {//獲取用戶的openID var that//調用常用登錄方法app . util . get userinfo(function(userinfo){ that . setdata({ userinfo:userinfo })});}、獲取電話號碼:函數(E){//點擊按鈕獲取手機號碼var即= thisthat . data . lee if(that . data . lee = = # 39;'){ wx . show toast({ icon: ;無 ,標題: # 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 = = # 39;getPhoneNumber:用戶拒絕失敗 # 39;){ wx . show modal({ title: # 39;警告 # 39;,內容: # 39;您點擊拒絕授權,部分功能無法使用!!!',showCancel: false,confirm text: # 39;返回授權 # 39;,success: function(res) {//用戶沒有授權成功,不需要修改isHide的值if(RES . confirm){ wx . setstoragesync( # 39;enws # 39, '1');wx . switch tab({ URL: ;/吳銳_豪斯/pages/index/index ;})console . log( # 39;用戶點擊了“返回授權” # 39;);};} })、that.setData({ modalstatus: true,});} else { that.setData({ lee: false,});wx . switch tab({ URL: ;/吳銳_豪斯/pages/index/index ;})//同意授權var userinfo = wx . getstoragesync( # 39;userInfo # 39);app . util . request({ # 39;網址 # 39;: 'entry/wx app/save user mobile # 39;,data:{ session id:userinfo . session id,uid: userinfo.memberInfo.uid,iv: iv,encryptedData: ency },success:function(RES){ if(RES . data . data . error = = 0){ console . log( # 39;成功 # 39;+RES . data . data);//用戶輸入了新版本,可以更新本地數據wx . setstoragesync( # 39;版本 # 39;, '1');wx . setstoragesync( # 39;enws # 39, '2');} else {//用戶保存手機號碼失敗。下次繼續授權手機號wx . setstoragesync( # 39;enws # 39, '1');console . log( # 39;失敗 # 39;+RES . data . data);} },fail:function(RES){ console . log( # 39;失敗 # 39;+RES);} });} },失敗:function(){ console . log( ;Session_key已過期,需要重新執行登錄流程”);that . wxlogin();//再次登錄} });} }})2.詳情頁上手機號碼的授權判斷
使用寫入遮罩層。
(1)index.html
lt代碼 gt lt!--授權彈匣提醒-->; ltview class = 容器 gt ltview class = 浮動 hidden = # 39{ { viewShowed } } # 39 gt lt查看class = # 39浮動內容 # 39; gt lttext 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%自動;border -半徑:20rpx顯示器:flexflex -方向:列;justify -內容:space -左右;align -項:居中;位置:相對;身高:255rpx}.浮動內容文本{ color:# 000;font -size:40 rpx;顯示:塊;邊距:0自動;位置:絕對;頂配:50rpx/* text-align:居中;*//* line -高度:60rpx*/border -radius:30 rpx;}.floatText{寬度:100%;高度:100rpx顯示器:flexjustify -內容:flex -start;位置:絕對;底部:0;} . BTN -can cle { line -height:100 rpx;flex:1;邊距:0;填充:0;border-radius:無;} (3) index.js
data:{ view displayed:true,//控制授權是否可以顯示},can cle:function(){ wx . setstoragesync( # 39;enws # 39, '2');this . setdata({ view showed:true })},/* * *生命周期函數--監控頁面顯示*/on show:function(){ varenws = wx . getstoragesync( # 39;enws # 39);控制臺. log( ;enws: ;,+enws);var that = this如果(enws!= '2'){//判斷那個. setdata({ view displayed:false,})console . log( # 39;判斷是否可以授權 # 39;);} else {},獲取電話號碼:函數(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({ viewShowed: true,})wx . setstoragesync( # 39;enws # 39, '2');if(e . detail . errmsg = = # 39;getPhoneNumber:用戶拒絕失敗 # 39;){} else {//同意授權var userinfo = wx . getstoragesync( # 39;userInfo # 39);app . util . request({ # 39;網址 # 39;: 'entry/wx app/save user mobile # 39;,data:{ session id:userinfo . session id,uid: userinfo.memberInfo.uid,iv: iv,encrypted data:encrypted },success:function(RES){ console . log( # 39;成功 # 39;+RES);if(RES . data . data . error = = 0){ console . log( # 39;成功 # 39;+RES . data . data);//用戶輸入了新版本,可以更新本地數據wx . setstoragesync( # 39;版本 # 39;, '1');} else {//用戶保存手機號失敗,下次繼續授權手機號}},fail:function(RES){ console . log( # 39;失敗 # 39;+RES);} });} } });},更多編程相關知識請訪問:編程入門!!就是這樣授權小程序登錄,獲取信息詳情和手機號的。請多關注主機參考其他相關文章!
這幾篇文章你可能也喜歡:
- 一個手機號可以注冊多個微信ID(一個手機號可以綁定多個微信ID)
- 談如何授權登錄小程序,獲取信息和手機號(如何授權登錄微信小程序)
- 一個手機號可以注冊幾個微信號(一個手機號可以綁定幾個微信號)。
- 一個手機號可以注冊好幾個微信號。
本文由主機參考刊發,轉載請注明:如何授權小程序登錄獲取信息和手機號(小程序獲取手機號授權登錄合法嗎) http://m.k2270.com/77851.html
主機參考

















評論前必須登錄!
注冊