uniapp微信登陆
生活随笔
收集整理的這篇文章主要介紹了
uniapp微信登陆
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
export function loginAppOtherAction(){return new Promise((resolve, reject) => {uni.getProvider({service: 'oauth',success: (prod) => {//判斷當前是否可以進行微信登錄if (~prod.provider.indexOf('weixin')) {if(plus.runtime.isApplicationExist({pname: 'com.tencent.mm', action: 'weixin://'})){//安裝了微信if(uni.getSystemInfoSync().platform == 'ios'){//請求小程序api,獲取需要的數據codeuni.login({provider: 'weixin',onlyAuthorize: true,success: (res) => {resolve({...res});//獲取code,并進行請求傳參},fail: () => {reject('微信授權失敗');}})}else{//注意安卓系統使用uni.login的話不會返回code,如果需要code使用h5+app原生api如下let weixinService = null;//獲取系統登錄授權列表plus.oauth.getServices((services) => { if (services && services.length) {for (var i = 0, len = services.length; i < len; i++) {if (services[i].id === 'weixin') {weixinService = services[i]; //獲取到微信授權break;}}if (!weixinService) {return;}weixinService.authorize((event) => { //獲取coderesolve({code: event.code});})}})}}else{//未安裝微信reject('請先安裝微信或升級版本');}} else {reject('微信登錄錯誤!');}}});})
}
安卓和ios獲取code的方式不一樣,ios可以直接調用uni.login API 獲取code,但是安卓不行,安卓只能獲取到unicode, 所以如果需要獲取code傳給后端,安卓app需要使用H5+的api獲取code
導入使用:
async handleLogin() {try{const message = await loginAppOtherAction();// 這里做登陸請求或者其他操作。 message.code可以獲取到code}catch(e){uni.showToast({title: e,icon:'error'})} }開啟第三方登錄權限, 微信開放平臺申請appID
總結
以上是生活随笔為你收集整理的uniapp微信登陆的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库安全与保密
- 下一篇: java 字符串编码转换的实现方法