跳转至正文

快速入门

GitHub

你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:

终端窗口
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下 prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-wechat` plugin in my project.

如果你更倾向于手动设置,请运行以下命令安装插件,并参考下文针对各平台的具体说明:

终端窗口
bun add @capgo/capacitor-wechat
bunx cap sync
import { CapacitorWechat } from '@capgo/capacitor-wechat';
import { CapacitorWechat } from '@capgo/capacitor-wechat';
await CapacitorWechat.initialize({
appId: 'wx1234567890',
universalLink: 'https://example.com/app/'
});
import { CapacitorWechat } from '@capgo/capacitor-wechat';
const { installed } = await CapacitorWechat.isInstalled();
if (installed) {
console.log('WeChat is installed');
}
import { CapacitorWechat } from '@capgo/capacitor-wechat';
const { code, state } = await CapacitorWechat.auth({
scope: 'snsapi_userinfo',
state: 'my_state'
});
// Use code to get access token from your server
  • 仅需初始化一次: 在应用启动阶段运行 setup。
  • 原生同步: 运行 bunx cap sync 在插件升级后。
  • 单一事实来源: 信任 src/definitions.ts 在 GitHub 仓库中。

使用您的应用凭据初始化 WeChat SDK。

您也可以在以下位置设置这些值: capacitor.config.ts 在 CapacitorWechat 插件配置下。运行时调用此方法会覆盖任何已打包的配置。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
await CapacitorWechat.initialize({
appId: 'wx1234567890',
universalLink: 'https://example.com/app/'
});

检查设备上是否安装了微信 App。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
const { installed } = await CapacitorWechat.isInstalled();
if (installed) {
console.log('WeChat is installed');
}

通过微信 OAuth 进行用户身份验证。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
const { code, state } = await CapacitorWechat.auth({
scope: 'snsapi_userinfo',
state: 'my_state'
});
// Use code to get access token from your server

将内容分享到微信。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
// Share text
await CapacitorWechat.share({
scene: 0, // 0 = Session, 1 = Timeline, 2 = Favorite
type: 'text',
text: 'Hello WeChat!'
});
// Share link
await CapacitorWechat.share({
scene: 1,
type: 'link',
title: 'My Website',
description: 'Check out my website',
link: 'https://example.com',
imageUrl: 'https://example.com/image.jpg'
});

向微信支付发送付款请求。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
// Get payment params from your server first
const paymentParams = await fetchPaymentParamsFromServer();
await CapacitorWechat.sendPaymentRequest({
partnerId: paymentParams.partnerId,
prepayId: paymentParams.prepayId,
nonceStr: paymentParams.nonceStr,
timeStamp: paymentParams.timeStamp,
package: paymentParams.package,
sign: paymentParams.sign
});

打开微信小程序。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
const { extMsg } = await CapacitorWechat.openMiniProgram({
username: 'gh_xxxxxxxxxxxxx',
path: 'pages/index/index',
type: 0 // 0 = Release, 1 = Test, 2 = Preview
});

从微信中选择发票。

import { CapacitorWechat } from '@capgo/capacitor-wechat';
const { cards } = await CapacitorWechat.chooseInvoice({
appId: 'your_app_id',
signType: 'SHA1',
cardSign: 'signature',
timeStamp: '1234567890',
nonceStr: 'random_string'
});
console.log('Selected cards:', cards);

WechatInitializationOptions

“WechatInitializationOptions” 章节

WeChat 初始化配置项。

export interface WechatInitializationOptions {
/**
* Required WeChat application ID.
*/
appId: string;
/**
* iOS universal link that is associated with your WeChat application.
*/
universalLink?: string;
}

WeChat 身份验证配置项。

export interface WechatAuthOptions {
/**
* OAuth scope. Use 'snsapi_userinfo' for user info or 'snsapi_login' for login only.
*/
scope: string;
/**
* Optional state parameter for CSRF protection.
*/
state?: string;
}

WeChat 身份验证响应。

export interface WechatAuthResponse {
/**
* Authorization code to exchange for access token.
*/
code: string;
/**
* State parameter if provided in request.
*/
state?: string;
}

WeChat 分享配置项。

export interface WechatShareOptions {
/**
* Share scene: 0 = Session (chat), 1 = Timeline (moments), 2 = Favorite.
*/
scene: number;
/**
* Share type: 'text', 'image', 'link', 'music', 'video', 'miniprogram'.
*/
type: 'text' | 'image' | 'link' | 'music' | 'video' | 'miniprogram';
/**
* Text content (for type 'text').
*/
text?: string;
/**
* Title (for type 'link', 'music', 'video', 'miniprogram').
*/
title?: string;
/**
* Description (for type 'link', 'music', 'video', 'miniprogram').
*/
description?: string;
/**
* Link URL (for type 'link').
*/
link?: string;
/**
* Image URL or base64 data.
*/
imageUrl?: string;
/**
* Thumbnail URL or base64 data (for type 'link', 'music', 'video').
*/
thumbUrl?: string;
/**
* Music or video URL (for type 'music', 'video').
*/
mediaUrl?: string;
/**
* Mini-program username (for type 'miniprogram').
*/
miniProgramUsername?: string;
/**
* Mini-program path (for type 'miniprogram').
*/
miniProgramPath?: string;
/**
* Mini-program type: 0 = Release, 1 = Test, 2 = Preview (for type 'miniprogram').
*/
miniProgramType?: number;
/**
* Mini-program web page URL fallback (for type 'miniprogram').
*/
miniProgramWebPageUrl?: string;
}

微信支付选项。

export interface WechatPaymentOptions {
/**
* Partner ID (merchant ID).
*/
partnerId: string;
/**
* Prepay ID from unified order API.
*/
prepayId: string;
/**
* Random string.
*/
nonceStr: string;
/**
* Timestamp.
*/
timeStamp: string;
/**
* Package value, typically 'Sign=WXPay'.
*/
package: string;
/**
* Signature.
*/
sign: string;
}

微信小程序选项。

export interface WechatMiniProgramOptions {
/**
* Mini-program username (original ID).
*/
username: string;
/**
* Path to open in mini-program.
*/
path?: string;
/**
* Mini-program type: 0 = Release, 1 = Test, 2 = Preview.
*/
type?: number;
}

微信发票选项。

export interface WechatInvoiceOptions {
/**
* App ID.
*/
appId: string;
/**
* Signature type.
*/
signType: string;
/**
* Card signature.
*/
cardSign: string;
/**
* Timestamp.
*/
timeStamp: string;
/**
* Random string.
*/
nonceStr: string;
}

微信发票响应。

export interface WechatInvoiceResponse {
/**
* Array of selected card IDs.
*/
cards: WechatInvoiceCard[];
}

微信发票卡片项。

export interface WechatInvoiceCard {
/**
* The selected card identifier.
*/
cardId: string;
/**
* Encrypted code returned by WeChat.
*/
encryptCode?: string;
}

本页面由该插件的 src/definitions.ts当上游的公共 API 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 来规划 dashboard 和 API 操作,请将其连接至 使用 @capgo/capacitor-wechat 关于使用 @capgo/capacitor-wechat 中的原生能力, API 概览 关于 API 概览中的实现细节, 简介 有关 Introduction 中的实现细节, API 密钥 有关 API Keys 中的实现细节,以及 设备 有关 Devices 中的实现细节。