項目中難免會用到第三方登錄和分享,本項目中微信登錄使用的第三方組件:# react-native-wechat
使用
yarn add react-native-wechat
react-native link react-native-wechat
在包名下新建wxapi文件夾,文件夾下新建文件WXEntryActivity.java
package com.xxx.wxapi;
import android.app.Activity;
import android.os.Bundle;
import com.theweflex.react.WeChatModule;
public class WXEntryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WeChatModule.handleIntent(getIntent());
finish();
}
}
授權登錄
首先註冊
WeChat.registerApp(WechatAppID)
授權登錄
WeChat.sendAuthRequest('snsapi_userinfo', 'ares')
.then((response) => {
console.log('-------------------- get wechat data is:', response)
getWechatOpenId(response.code)
})
.catch((error) => {
let errorCode = Number(error.code);
if (errorCode === -2) {
dispatch(showDropdownAlert('error', '提示', '已取消授權登錄')) // errorCode = -2 表示用戶主動取消的情況,下同
} else {
dispatch(showDropdownAlert('error', '提示', WechatAuthFailed)) // errorCode = -2 表示用戶主動取消的情況,下同
}
})
注意問題
- 打包使用簽名文件keystore文件的簽名要和微信開發平臺中填寫的一一致
- WXEntryActivity.java 中的package com.xxx.wxapi; 包名必須和微信開發平臺中填寫的包名一直,不然就會出現點擊授權登錄,回調沒有反應的問題,可以解決這個問題:https://github.com/yorkie/react-native-wechat/issues/448