@gyjshow/react-native-wechat 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,245 @@
1
+ # @gyjshow/react-native-wechat
2
+
3
+ A lightweight **React Native** plugin for integrating **WeChat Pay & WeChat Share** on both **iOS** and **Android**.
4
+
5
+ Supports:
6
+
7
+ - ✅ WeChat Pay
8
+ - ✅ Share Text
9
+ - ✅ Share Webpage
10
+ - ✅ Share Image
11
+ - ✅ Share to Friends & Moments
12
+ - ✅ Get WeChat SDK Version
13
+
14
+ Compatible with **React Native >= 0.83**
15
+
16
+ ---
17
+
18
+ # Features
19
+
20
+ - 🔹 Unified API for iOS & Android
21
+ - 🔹 Supports WeChat Pay
22
+ - 🔹 Share text / webpage / image
23
+ - 🔹 Share to friends (`session`) & Moments (`timeline`)
24
+ - 🔹 Fetch WeChat SDK version
25
+ - 🔹 Lightweight & easy to integrate
26
+
27
+ ---
28
+
29
+ # Installation
30
+
31
+ ```bash
32
+ # Using npm
33
+ npm install @gyjshow/react-native-wechat
34
+
35
+ # Using pnpm
36
+ pnpm add @gyjshow/react-native-wechat
37
+ ```
38
+
39
+ ---
40
+
41
+ # Platform Setup
42
+
43
+ ---
44
+
45
+ ## iOS Setup
46
+
47
+ ### 1️⃣ Add URL Scheme (WeChat AppID)
48
+
49
+ Open `ios/YourProject/Info.plist` and add:
50
+
51
+ ```xml
52
+ <key>CFBundleURLTypes</key>
53
+ <array>
54
+ <dict>
55
+ <key>CFBundleURLSchemes</key>
56
+ <array>
57
+ <string>wx1234567890abcdef</string>
58
+ </array>
59
+ </dict>
60
+ </array>
61
+ ```
62
+
63
+ ⚠️ Replace `wx1234567890abcdef` with your real WeChat AppID.
64
+
65
+ ---
66
+
67
+ ### 2️⃣ Install Pods
68
+
69
+ ```bash
70
+ cd ios
71
+ pod install
72
+ ```
73
+
74
+ ---
75
+
76
+ ## Android Setup
77
+
78
+ No additional configuration required beyond normal React Native setup.
79
+
80
+ Make sure:
81
+
82
+ - Your AppID is registered in WeChat Open Platform
83
+ - You are using official SDK version `6.8.0`
84
+ - Your package name matches the one registered in WeChat
85
+
86
+ ---
87
+
88
+ # Usage
89
+
90
+ ---
91
+
92
+ ## Import
93
+
94
+ ```ts
95
+ import WeChat from '@gyjshow/react-native-wechat';
96
+ ```
97
+
98
+ ---
99
+
100
+ ## Register App (Required)
101
+
102
+ You must register your AppID before using any feature.
103
+
104
+ ```ts
105
+ WeChat.registerApp('wx1234567890abcdef');
106
+ ```
107
+
108
+ Call this once when your app starts.
109
+
110
+ ---
111
+
112
+ # WeChat Pay
113
+
114
+ WeChat Pay requires server-side order generation.
115
+
116
+ ```ts
117
+ const payParams = {
118
+ appId: 'wx123456',
119
+ partnerId: '1900000109',
120
+ prepayId: 'wx201411101639507cbf6ffd8b0779950874',
121
+ nonceStr: '1101000000140429eb40476f8896f4c9',
122
+ timeStamp: '1398746574',
123
+ package: 'Sign=WXPay',
124
+ sign: 'C380BEC2BFD727A4B6845133519F3AD6'
125
+ };
126
+
127
+ try {
128
+ const result = await WeChat.pay(payParams);
129
+ console.log('Pay result:', result);
130
+ } catch (error) {
131
+ console.error('Pay failed:', error);
132
+ }
133
+ ```
134
+
135
+ ---
136
+
137
+ # Share Text
138
+
139
+ ```ts
140
+ await WeChat.shareText('Hello WeChat!', 'session'); // Share to friend
141
+ await WeChat.shareText('Hello Moments!', 'timeline'); // Share to Moments
142
+ ```
143
+
144
+ ---
145
+
146
+ # Share Webpage
147
+
148
+ ```ts
149
+ await WeChat.shareWebpage(
150
+ 'https://your-website.com',
151
+ 'Website Title',
152
+ 'Website description',
153
+ null, // optional thumbnail (base64, ≤32KB)
154
+ 'session'
155
+ );
156
+ ```
157
+
158
+ ---
159
+
160
+ # Share Image
161
+
162
+ ```ts
163
+ await WeChat.shareImage(base64ImageString, 'timeline');
164
+ ```
165
+
166
+ > Image must be Base64 format.
167
+
168
+ ---
169
+
170
+ # Get WeChat SDK Version
171
+
172
+ ```ts
173
+ const version = await WeChat.getVersion();
174
+ console.log('WeChat SDK version:', version);
175
+ ```
176
+
177
+ ---
178
+
179
+ # API Reference
180
+
181
+ | Method | Params | Returns | Description |
182
+ |--------|--------|---------|------------|
183
+ | `registerApp(appId: string)` | WeChat AppID | `void` | Register WeChat App |
184
+ | `pay(params)` | WeChat pay params | `Promise<string>` | Initiate WeChat payment |
185
+ | `shareText(text, scene)` | text, `session` / `timeline` | `Promise<string>` | Share text |
186
+ | `shareWebpage(url, title, desc, thumbBase64, scene)` | webpage params | `Promise<string>` | Share webpage |
187
+ | `shareImage(base64, scene)` | base64 image | `Promise<string>` | Share image |
188
+ | `getVersion()` | - | `Promise<string>` | Get WeChat SDK version |
189
+
190
+ ---
191
+
192
+ # Example
193
+
194
+ ```ts
195
+ import React, { useEffect } from 'react';
196
+ import { Button, View, Alert } from 'react-native';
197
+ import WeChat from '@gyjshow/react-native-wechat';
198
+
199
+ export default function App() {
200
+
201
+ useEffect(() => {
202
+ WeChat.registerApp('wx1234567890abcdef');
203
+ }, []);
204
+
205
+ const handlePay = async () => {
206
+ try {
207
+ const result = await WeChat.pay({
208
+ appId: 'wx123456',
209
+ partnerId: '1900000109',
210
+ prepayId: 'prepay_id',
211
+ nonceStr: 'nonce',
212
+ timeStamp: '1234567890',
213
+ package: 'Sign=WXPay',
214
+ sign: 'xxxx'
215
+ });
216
+
217
+ Alert.alert('Pay Result', result);
218
+ } catch (e: any) {
219
+ Alert.alert('Pay Failed', e.message);
220
+ }
221
+ };
222
+
223
+ return (
224
+ <View>
225
+ <Button title="Pay with WeChat" onPress={handlePay} />
226
+ </View>
227
+ );
228
+ }
229
+ ```
230
+
231
+ ---
232
+
233
+ # Notes
234
+
235
+ - You must register your app in WeChat Open Platform.
236
+ - WeChat Pay requires backend order generation.
237
+ - Thumbnail size must be ≤ 32KB (WeChat official requirement).
238
+ - Ensure your AppID matches your bundle identifier (iOS) and package name (Android).
239
+ - Test in sandbox before production.
240
+
241
+ ---
242
+
243
+ # License
244
+
245
+ MIT
@@ -0,0 +1,33 @@
1
+ buildscript {
2
+ def kotlinVersion = rootProject.ext.has('kotlinVersion')
3
+ ? rootProject.ext.get('kotlinVersion')
4
+ : '2.1.20'
5
+
6
+ repositories {
7
+ google()
8
+ mavenCentral()
9
+ }
10
+
11
+ dependencies {
12
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
13
+ }
14
+ }
15
+
16
+ apply plugin: 'com.android.library'
17
+ apply plugin: 'kotlin-android'
18
+
19
+ android {
20
+ namespace "com.gyj.wechat"
21
+ compileSdkVersion rootProject.ext.compileSdkVersion
22
+
23
+ defaultConfig {
24
+ minSdkVersion rootProject.ext.minSdkVersion
25
+ targetSdkVersion rootProject.ext.targetSdkVersion
26
+ }
27
+ }
28
+
29
+ dependencies {
30
+ implementation "com.facebook.react:react-android"
31
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
32
+ api 'com.tencent.mm.opensdk:wechat-sdk-android:+'
33
+ }
@@ -0,0 +1,103 @@
1
+ // Kotlin 模块实现
2
+ package com.gyj.wechat
3
+
4
+ import com.facebook.react.bridge.*
5
+ import com.tencent.mm.opensdk.modelpay.PayReq
6
+ import com.tencent.mm.opensdk.modelmsg.*
7
+ import com.tencent.mm.opensdk.openapi.WXAPIFactory
8
+ import com.tencent.mm.opensdk.openapi.IWXAPI
9
+ import android.util.Base64
10
+
11
+ class RNWeChatModule(private val reactContext: ReactApplicationContext) :
12
+ ReactContextBaseJavaModule(reactContext) {
13
+
14
+ private var wxApi: IWXAPI? = null
15
+
16
+ override fun getName() = "RNWeChat"
17
+
18
+ @ReactMethod
19
+ fun registerApp(appId: String) {
20
+ wxApi = WXAPIFactory.createWXAPI(reactContext, appId, true)
21
+ wxApi?.registerApp(appId)
22
+ }
23
+
24
+ @ReactMethod
25
+ fun getVersion(promise: Promise) {
26
+ promise.resolve(wxApi?.WXAppSupportAPI?.toString() ?: "unknown")
27
+ }
28
+
29
+ @ReactMethod
30
+ fun pay(payInfo: ReadableMap, promise: Promise) {
31
+ val req = PayReq()
32
+ req.appId = payInfo.getString("appId")
33
+ req.partnerId = payInfo.getString("partnerId")
34
+ req.prepayId = payInfo.getString("prepayId")
35
+ req.nonceStr = payInfo.getString("nonceStr")
36
+ req.timeStamp = payInfo.getString("timeStamp")
37
+ req.packageValue = payInfo.getString("package")
38
+ req.sign = payInfo.getString("sign")
39
+
40
+ val result = wxApi?.sendReq(req) ?: false
41
+ if (result) promise.resolve("success")
42
+ else promise.reject("PAY_FAILED", "支付请求失败")
43
+ }
44
+
45
+ @ReactMethod
46
+ fun shareText(text: String, scene: Int, promise: Promise) {
47
+ val textObj = WXTextObject()
48
+ textObj.text = text
49
+
50
+ val msg = WXMediaMessage()
51
+ msg.mediaObject = textObj
52
+
53
+ val req = SendMessageToWX.Req()
54
+ req.transaction = System.currentTimeMillis().toString()
55
+ req.message = msg
56
+ req.scene = scene
57
+
58
+ val result = wxApi?.sendReq(req) ?: false
59
+ if (result) promise.resolve("success")
60
+ else promise.reject("SHARE_FAILED", "分享失败")
61
+ }
62
+
63
+ @ReactMethod
64
+ fun shareWebpage(url: String, title: String, desc: String, thumbBase64: String?, scene: Int, promise: Promise) {
65
+ val webpage = WXWebpageObject()
66
+ webpage.webpageUrl = url
67
+
68
+ val msg = WXMediaMessage(webpage)
69
+ msg.title = title
70
+ msg.description = desc
71
+
72
+ thumbBase64?.let {
73
+ msg.thumbData = Base64.decode(it, Base64.DEFAULT)
74
+ }
75
+
76
+ val req = SendMessageToWX.Req()
77
+ req.transaction = System.currentTimeMillis().toString()
78
+ req.message = msg
79
+ req.scene = scene
80
+
81
+ val result = wxApi?.sendReq(req) ?: false
82
+ if (result) promise.resolve("success")
83
+ else promise.reject("SHARE_FAILED", "分享失败")
84
+ }
85
+
86
+ @ReactMethod
87
+ fun shareImage(base64: String, scene: Int, promise: Promise) {
88
+ val imgObj = WXImageObject()
89
+ imgObj.imageData = Base64.decode(base64, Base64.DEFAULT)
90
+
91
+ val msg = WXMediaMessage()
92
+ msg.mediaObject = imgObj
93
+
94
+ val req = SendMessageToWX.Req()
95
+ req.transaction = System.currentTimeMillis().toString()
96
+ req.message = msg
97
+ req.scene = scene
98
+
99
+ val result = wxApi?.sendReq(req) ?: false
100
+ if (result) promise.resolve("success")
101
+ else promise.reject("SHARE_FAILED", "分享失败")
102
+ }
103
+ }
@@ -0,0 +1,18 @@
1
+ // Kotlin 包实现
2
+
3
+ package com.gyj.wechat
4
+
5
+ import com.facebook.react.ReactPackage
6
+ import com.facebook.react.bridge.NativeModule
7
+ import com.facebook.react.uimanager.ViewManager
8
+ import com.facebook.react.bridge.ReactApplicationContext
9
+
10
+ class RNWeChatPackage : ReactPackage {
11
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
12
+ return listOf(RNWeChatModule(reactContext))
13
+ }
14
+
15
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
16
+ return emptyList()
17
+ }
18
+ }
@@ -0,0 +1,5 @@
1
+ #import <React/RCTBridgeModule.h>
2
+
3
+ @interface RNWeChat : NSObject <RCTBridgeModule>
4
+
5
+ @end
@@ -0,0 +1,83 @@
1
+ #import "RNWeChat.h"
2
+ #import <WechatOpenSDK/WXApi.h>
3
+
4
+ @implementation RNWeChat
5
+
6
+ RCT_EXPORT_MODULE();
7
+
8
+ RCT_EXPORT_METHOD(registerApp:(NSString *)appId) {
9
+ [WXApi registerApp:appId];
10
+ }
11
+
12
+ RCT_EXPORT_METHOD(getVersion:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
13
+ resolve([WXApi getApiVersion]);
14
+ }
15
+
16
+ RCT_EXPORT_METHOD(pay:(NSDictionary *)params resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
17
+ PayReq *req = [[PayReq alloc] init];
18
+ req.appId = params[@"appId"];
19
+ req.partnerId = params[@"partnerId"];
20
+ req.prepayId = params[@"prepayId"];
21
+ req.nonceStr = params[@"nonceStr"];
22
+ req.timeStamp = [params[@"timeStamp"] intValue];
23
+ req.package = params[@"package"];
24
+ req.sign = params[@"sign"];
25
+
26
+ BOOL result = [WXApi sendReq:req];
27
+ result ? resolve(@"success") : reject(@"PAY_FAILED", @"支付失败", nil);
28
+ }
29
+
30
+ RCT_EXPORT_METHOD(shareText:(NSString *)text scene:(NSInteger)scene resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
31
+ SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
32
+ req.text = text;
33
+ req.bText = YES;
34
+ req.scene = (int)scene;
35
+
36
+ BOOL result = [WXApi sendReq:req];
37
+ result ? resolve(@"success") : reject(@"SHARE_FAILED", @"分享失败", nil);
38
+ }
39
+
40
+ RCT_EXPORT_METHOD(shareWebpage:(NSString *)url title:(NSString *)title description:(NSString *)desc thumbBase64:(NSString *)thumb scene:(NSInteger)scene resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
41
+
42
+ WXWebpageObject *webpage = [WXWebpageObject object];
43
+ webpage.webpageUrl = url;
44
+
45
+ WXMediaMessage *msg = [WXMediaMessage message];
46
+ msg.title = title;
47
+ msg.description = desc;
48
+ msg.mediaObject = webpage;
49
+
50
+ if (thumb) {
51
+ NSData *data = [[NSData alloc] initWithBase64EncodedString:thumb options:0];
52
+ msg.thumbData = data;
53
+ }
54
+
55
+ SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
56
+ req.bText = NO;
57
+ req.message = msg;
58
+ req.scene = (int)scene;
59
+
60
+ BOOL result = [WXApi sendReq:req];
61
+ result ? resolve(@"success") : reject(@"SHARE_FAILED", @"分享失败", nil);
62
+ }
63
+
64
+ RCT_EXPORT_METHOD(shareImage:(NSString *)base64 scene:(NSInteger)scene resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
65
+
66
+ NSData *data = [[NSData alloc] initWithBase64EncodedString:base64 options:0];
67
+
68
+ WXImageObject *imgObj = [WXImageObject object];
69
+ imgObj.imageData = data;
70
+
71
+ WXMediaMessage *msg = [WXMediaMessage message];
72
+ msg.mediaObject = imgObj;
73
+
74
+ SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
75
+ req.bText = NO;
76
+ req.message = msg;
77
+ req.scene = (int)scene;
78
+
79
+ BOOL result = [WXApi sendReq:req];
80
+ result ? resolve(@"success") : reject(@"SHARE_FAILED", @"分享失败", nil);
81
+ }
82
+
83
+ @end
package/lib/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export type Scene = 'session' | 'timeline';
2
+ declare const WeChat: {
3
+ registerApp(appId: string): void;
4
+ getVersion(): Promise<string>;
5
+ wxpay(params: Record<string, string>): Promise<string>;
6
+ shareText(text: string, scene: Scene): Promise<string>;
7
+ shareWebpage(url: string, title: string, description: string, thumbBase64: string | null, scene: Scene): Promise<string>;
8
+ shareImage(base64: string, scene: Scene): Promise<string>;
9
+ };
10
+ export default WeChat;
package/lib/index.js ADDED
@@ -0,0 +1,24 @@
1
+ import { NativeModules } from 'react-native';
2
+ const { RNWeChat } = NativeModules;
3
+ const getScene = (scene) => (scene === 'session' ? 0 : 1);
4
+ const WeChat = {
5
+ registerApp(appId) {
6
+ RNWeChat.registerApp(appId);
7
+ },
8
+ getVersion() {
9
+ return RNWeChat.getVersion();
10
+ },
11
+ wxpay(params) {
12
+ return RNWeChat.pay(params);
13
+ },
14
+ shareText(text, scene) {
15
+ return RNWeChat.shareText(text, getScene(scene));
16
+ },
17
+ shareWebpage(url, title, description, thumbBase64, scene) {
18
+ return RNWeChat.shareWebpage(url, title, description, thumbBase64, getScene(scene));
19
+ },
20
+ shareImage(base64, scene) {
21
+ return RNWeChat.shareImage(base64, getScene(scene));
22
+ }
23
+ };
24
+ export default WeChat;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@gyjshow/react-native-wechat",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "description": "React Native WeChat plugin for WeChat Pay and Share (iOS & Android)",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "files": [
9
+ "lib",
10
+ "ios",
11
+ "android",
12
+ "*.podspec",
13
+ "README.md"
14
+ ],
15
+ "scripts": {
16
+ "build": "tsc",
17
+ "prepare": "npm run build"
18
+ },
19
+ "keywords": [
20
+ "react-native",
21
+ "wechat",
22
+ "wechatpay",
23
+ "share"
24
+ ],
25
+ "author": "Allen.ge",
26
+ "license": "MIT",
27
+ "peerDependencies": {
28
+ "react": "*",
29
+ "react-native": ">=0.60.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/react": "^19.2.13",
33
+ "react-native": "^0.83.1",
34
+ "typescript": "^5.9.3"
35
+ }
36
+ }
@@ -0,0 +1,21 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "react-native-wechat"
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.license = package['license']
10
+
11
+ s.authors = package['author']
12
+ s.homepage = package['homepage']
13
+ s.platform = :ios, "9.0"
14
+
15
+ s.source = { :http => 'file://' + __dir__ }
16
+
17
+ s.source_files = "ios/**/*.{h,m}"
18
+
19
+ s.dependency 'React-Core'
20
+ s.dependency 'WechatOpenSDK-XCFramework'
21
+ end