一、在微信平臺上申請appid、appsecret,
二、app --》 manifest.json--》SDK配置(填寫申請好的appid和appsecret)
三、在登錄頁,點擊微信登錄按鈕,若系結微信,則免賬號密碼登錄直接跳轉到首頁;若未系結,則彈出未系結微信,
// 微信授權登錄物件var aweixin=null; // 呼叫plus.oauth.getServices獲取保存// 當前環境支持的所有授權登錄物件var auths = {};mui.plusReady(function() { // 獲取鑒權服務 getService();})// 獲取登錄授權認證服務串列,單獨保存微信登錄授權物件// 5+APP在plusready事件中呼叫,uni-app在vue頁面的onLoad中呼叫function getService(){ plus.oauth.getServices(function(services){ for(var i=0;i<services.length;i++){ auths[services[i].id] = services[i]; } aweixin = auths['weixin']; }, function(e){ plus.nativeUI.alert("獲取登錄授權服務串列失敗:"+JSON.stringify(e)); } );}//wx登錄
document.getElementById('wxdenglu').addEventListener('tap',function(){ plus.nativeUI.showWaiting("正在登錄中...",{padlock: true}); if(!aweixin.authResult){ authLogin().then(res=>{ wxLogin()//呼叫自定義的登錄介面 }).catch(res=>{ plus.nativeUI.closeWaiting(); }) }else{ wxLogin()//呼叫自定義的登錄介面 }});
四、用賬號密碼登進首頁時,判斷是否系結微信,若為系結微信,則提示讓其系結微信,
五、微信系結和解綁,
// 微信授權登錄物件// 呼叫plus.oauth.getServices獲取保存var aweixin=null; // 當前環境支持的所有授權登錄物件var auths = {};mui.plusReady(function(){ // 獲取鑒權服務 getService()});//wx系結function weixinBind(){ plus.nativeUI.showWaiting("正在系結中...",{padlock: true}); if(!aweixin.authResult){ authorize().then(res=>{ wxBind(1,aweixin.authResult.openid) }).catch(res=>{ plus.nativeUI.closeWaiting(); }) }else{ wxBind(1,aweixin.authResult.openid) }};//wx解綁function weixinUnBind(){ plus.nativeUI.showWaiting("正在解綁中...",{padlock: true}); wxBind(2,"")}// 獲取登錄授權認證服務串列,單獨保存微信登錄授權物件// 5+APP在plusready事件中呼叫,uni-app在vue頁面的onLoad中呼叫function getService(){ plus.oauth.getServices(function(services){ for(var i=0;i<services.length;i++){ auths[services[i].id] = services[i]; } aweixin = auths['weixin']; }, function(e){ plus.nativeUI.alert("獲取登錄授權服務串列失敗:"+JSON.stringify(e)); } );}// 獲取微信登錄授權物件后可進行授權操作function authorize(){ return new Promise(function (resolve, reject) { if(!aweixin){ plus.nativeUI.alert("當前環境不支持微信登錄"); return; } aweixin.authorize(function(e){ // plus.nativeUI.alert("授權成功:"+JSON.stringify(e)); authLogin().then(res=>{ resolve(res); }); }, function(e){ // plus.nativeUI.alert("授權失敗:"+JSON.stringify(e)); plus.nativeUI.closeWaiting(); reject(e); }, {scope:'snsapi_userinfo',state:'authorize test'}); })}// 獲取微信登錄授權物件后可進行登錄認證操作function authLogin(){ return new Promise(function (resolve, reject) { if(!aweixin){ plus.nativeUI.alert("當前環境不支持微信登錄"); return; } if(!aweixin.authResult){ aweixin.login(function(e){ // plus.nativeUI.alert("登錄認證成功!"+JSON.stringify(e)); resolve(e); }, function(e){ reject(e); } ); }else{ // plus.nativeUI.alert("已經登錄認證!"); } })}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/36748.html
標籤:HTML5
上一篇:Node初識
