
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@yyyyu/react-native-wechat
Advanced tools
yarn add @yyyyu/react-native-wechat
or
npm install --save @yyyyu/react-native-wechat
react-native link @yyyyu/react-native-wechat
如果项目使用 Pods 管理依赖需要在 Podfile 中添加
pod 'React', :path => '../node_modules/react-native', :subspecs => ['Dependency']
手动配置和非 Pods 管理依赖情况需要在 Linked Frameworks and Libraries 添加 libsqlite3.0 (方法同上)
在 AppDelegate.m 文件中添加下列代码
#import <React/RCTLinkingManager.h>
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}
在 Info.plist 文件中添加 URL Schemes
在 Info.plist 文件添加 LSApplicationQueriesSchemes 字段,值类型为 Array,添加字符串子元素 weixin
react-native link @yyyyu/react-native-wechat
在 android/settings.gradle 文件中添加
include ':react-native-wechat'
project(':react-native-wechat').projectDir = new File(rootProject.projectDir, '../node_modules/@yyyyu/react-native-wechat/android')
在 android/app/build.gradle 文件中依赖部分添加
dependencies {
// other dependencies
compile project(':react-native-wechat')
}
在 MainApplication.java 文件中添加
import com.rnlib.wechat.RNWechatPackage;
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
// other packages
new RNWechatPackage()
);
}
在 MainActivity.java 文件中添加下列代码,标识出当前状态,在微信唤起应用时需要做不同处理
public class MainActivity extends ReactActivity {
public static boolean isActivityCreated;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isActivityCreated = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
isActivityCreated = false;
}
}
在应用包名下创建 wxapi 这个包,并新建 WXEntryActivity 类(这里要严格按照这种形式创建,否则无法接收到微信的应答)
package 包名.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.rnlib.wechat.RNWechatModule;
import 包名.MainActivity;
public class WXEntryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (MainActivity.isActivityCreated) {
RNWechatModule.handleIntent(this.getIntent());
} else {
// 如果应用未在后台启动,就打开应用
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
finish();
}
}
如果需要使用微信支付功能,在 wxapi 这个包下新建 WXPayEntryActivity 类(注意此处类名和上面不同)
package 包名.wxapi;
import android.app.Activity;
import android.os.Bundle;
import com.rnlib.wechat.RNWechatModule;
public class WXPayEntryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RNWechatModule.handleIntent(this.getIntent());
finish();
}
}
在 AndroidManifest.xml 中注册上述 activity
<application>
<activity
android:name=".wxapi.WXEntryActivity"
android:label="@string/app_name"
android:exported="true" />
<!-- 微信支付可选 -->
<activity
android:name=".wxapi.WXPayEntryActivity"
android:label="@string/app_name"
android:exported="true" />
</application>
应用一定要签名才能正常调用接口,开发时也需要签名
# 微信开放平台登记的就是 keystore 的签名,使用
keytool -v -list -keystore 'you keystore' | grep MD5
# 得到 XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
在浏览器 console 格式化一下填到微信开放平台上
'XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX'.replace(/:/g, '').toLowerCase()
import wechat from '@yyyyu/react-native-wechat'
wechat.registerApp({ appId: 'appId' })
.then(res => { console.log(res) })
.catch(err => { console.error(err) })
错误代码
import { ErrCode } from '@yyyyu/react-native-wechat'
console.log(ErrCode)
{
// 微信 sdk 返回错误类型
Success: 0, // 成功
Common: -1, // 普通错误
UserCancel: -2, // 点击取消返回
SentFail: -3, // 发送失败
AuthDeny: -4, // 授权失败
Unsupport: -5, // 不支持
Ban: -6, // 禁止(签名信息不正确时会出现这个错误) androidOnly
// 自定义错误类型
ActiveSuccess: 1, // 发送请求后通过系统唤起(任务列表选择唤起),无法判断成功失败
RequestFailed: -7, // 请求失败
UnRegisteApi: -8, // 未注册接口
UnInstall: -9, // 未安装微信
UnSupportApi: -10, // 不支持 Api
Unknow: -99 // 未知错误
}
抛出错误处理方式
import { WechatError } from '@yyyyu/react-native-wechat'
try {
const res = await wechat.sendText({ text: 'example' })
console.log(res)
} catch (e) {
if (e instanceof WechatError) {
const { errCode, errMsg } = e
console.error(errCode, errMsg)
}
}
// @return Boolean
wechat.registerApp({
appId: 'you app id', // 微信平台注册应用后得到的 appId
isDebug: false, // optional(false)
})
// @return Boolean
wechat.isWXAppInstalled()
// @return Boolean
wechat.isWXAppSupportApi()
// @return String
wechat.getWXAppInstallUrl()
wechat.openWXApp()
wechat.launchMiniProgram({
username: 'you username', // 原始 id,微信开放平台注册小程序处获取
path: '/path1/path2', // 小程序路由地址
type: 'test' // 小程序类型 optional('release')
})
wechat.sendAuthRequest({
state: 'state' // 返回值会包含相同的数据用于验证请求 optional('')
})
wechat.sendText({
text: 'text', // 用于发送的文字
scene: 'session' // 发送场景 optional('session')
})
wechat.sendImage({
image: require('path/image.png'), // 用于发送的图片
scene: 'session' // 发送场景 optional('session')
})
wechat.sendMusic({
music: 'https://music.com/music.html', // 音乐地址
data: 'https://music.com/data', // 音乐数据地址
title: 'title', // 标题
desc: 'description', // 描述
thumb: require('path/thumb.png'), // 缩略图
scene: 'session' // 发送场景 optional('session')
})
wechat.sendVideo({
video: 'https://video.com/video', // 视频地址
title: 'title', // 标题
desc: 'description', // 描述
thumb: require('path/thumb.png'), // 缩略图
scene: 'session' // 发送场景 optional('session')
})
wechat.sendLink({
link: 'https://link.com/', // 链接地址
title: 'title', // 标题
desc: 'description', // 描述
thumb: require('path/thumb.png'), // 缩略图
scene: 'session' // 发送场景 optional('session')
})
wechat.sendMiniProgram({
username: 'you username', // 原始 id,微信开放平台注册小程序处获取
path: '/path1/path2', // 小程序路由地址
title: 'title', // 标题
desc: 'description', // 描述
hdThumb: require('path/thumb.png'), // 缩略图
link: 'https://www.link.com/', // 兼容旧版本链接,不支持小程序的微信版本会以此方式打开
thumb: require('path/thumb.png'), // 兼容旧版本缩略图,不支持小程序的微信版本会看到此缩略图
type: 'test' // 分享小程序类型 optional('release')
})
wechat.pay({
appId: 'you appId', // appId androidOnly 安卓 sdk 就是这样
partnerId: 'you partnerId', // 商家 id
prepayId: 'you prepayId', // 预支付订单 id
nonceStr: 'nonceStr', // 随机串
timestamp: 'timestamp', // 时间戳
packageSign: 'you packageSign', // 财付通签名
sign: 'you sign', // 微信开放平台签名
})
FAQs
wechat api for react native.
The npm package @yyyyu/react-native-wechat receives a total of 4 weekly downloads. As such, @yyyyu/react-native-wechat popularity was classified as not popular.
We found that @yyyyu/react-native-wechat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.