更新记录
1.0.0(2025-09-13)
- 目前已知1个问题
- 问题1.Android分享图片没做完,iOS功能完整
- 1.0.0初始版本发布。
平台兼容性
uni-app x(4.76)
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
× | × | 5.0 | 12 | × | × |
ls-weixin 微信SDK插件
一款专为uni-appx打造的微信SDK插件,支持微信登录、分享、支付等核心功能。基于微信官方SDK封装,提供简洁易用的API接口。
✨ 功能特性
- 🚀 微信登录:一键微信授权登录
- 📤 内容分享:支持文本、图片、链接、小程序分享
- 💰 微信支付:完整的微信支付流程
- 📱 应用管理:检测微信安装状态、打开微信应用
- 🎯 多平台支持:iOS、Android全平台兼容
- 🛡️ 类型安全:完整的TypeScript类型定义
📦 安装使用
安装插件
在HBuilderX中直接导入本插件,或通过uni-app插件市场安装。
配置要求
- iOS配置(在
manifest.json
中配置):{ "app-ios": { "distribute": { "modules": {}, "icons": {}, "splashScreens": {}, "capabilities": { "entitlements": { "com.apple.developer.associated-domains": [ "applinks:你的域名" ] } } } } }
iOS补充配置(在
Info.plist
中配置):<key>CFBundleURLSchemes</key> <array> <string>你的微信AppID</string> </array>
Android补充配置(在AndroidManifest.xml
中配置):
<!-- 将 ${applicationId} 替换为你的应用包名 -->
<activity
android:name=".wxapi.WXEntryActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity="你的应用包名"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
🚀 快速开始
1. 注册微信SDK
在使用任何微信功能前,必须先注册SDK:
import { WxSdk } from '@/uni_modules/ls-weixin'
// 注册微信SDK
const isRegister = WxSdk.register('你的微信AppID', '你的UniversalLink')
console.log('注册结果:', isRegister)
2. 检查微信安装状态
const isInstalled = WxSdk.isWXAppInstalled()
if (!isInstalled) {
uni.showToast({ title: '请先安装微信', icon: 'none' })
}
📖 API文档
微信登录
WxSdk.login((res: WxResult) => {
if (res.errorCode === 0) {
// 登录成功,获取授权码
console.log('授权码:', res.code)
// 将code发送到你的服务器进行后续处理
} else {
// 登录失败
console.log('登录失败:', res.msg)
}
})
内容分享
分享文本
const content = {
type: 'text',
text: '这是一段分享的文本内容',
scene: 0 // 0:聊天 1:朋友圈 2:收藏
}
WxSdk.share(content, (res: WxResult) => {
uni.showToast({ title: res.msg!, icon: 'none' })
})
分享图片
const content = {
type: 'image',
imagePath: '/static/logo.png', // 支持本地路径和网络URL
scene: 0
}
WxSdk.share(content, (res: WxResult) => {
uni.showToast({ title: res.msg!, icon: 'none' })
})
分享网页链接
const content = {
type: 'webpage',
title: 'uni-app官网',
desc: 'uni-app是一个使用Vue.js开发所有前端应用的框架',
thumbPath: 'https://hellouniappx.dcloud.net.cn/web/static/test-image/logo.png',
webpageUrl: 'https://uniapp.dcloud.io',
scene: 0
}
WxSdk.share(content, (res: WxResult) => {
uni.showToast({ title: res.msg!, icon: 'none' })
})
分享小程序
const content = {
type: 'miniprogram',
title: '推荐一个小程序',
desc: '这个小程序很好用',
thumbPath: 'https://example.com/thumb.jpg',
miniProgram: {
userName: 'gh_xxxxxxxxxxxx', // 小程序原始ID
path: 'pages/index/index?param=value', // 小程序页面路径
type: 0 // 0:正式版 1:开发版 2:体验版
},
scene: 0
}
WxSdk.share(content, (res: WxResult) => {
uni.showToast({ title: res.msg!, icon: 'none' })
})
微信支付
const payParams = {
package: "Sign=WXPay",
appid: "你的微信AppID",
sign: "微信支付签名",
partnerid: "微信支付商户号",
prepayid: "微信预支付订单号",
noncestr: "随机字符串",
timestamp: 当前时间戳
}
WxSdk.pay(payParams, (res: WxResult) => {
if (res.errorCode === 0) {
uni.showToast({ title: '支付成功', icon: 'success' })
} else {
uni.showToast({ title: '支付失败: ' + res.msg, icon: 'none' })
}
})
打开微信应用
WxSdk.openWechat((res: WxResult) => {
if (res.errorCode === 0) {
console.log('微信打开成功')
} else {
uni.showToast({ title: res.msg!, icon: 'none' })
}
})
📋 类型定义
WxResult
{
errorCode?: number, // 错误码:0成功,-1失败,-2取消
code?: string, // 登录授权码(登录成功时返回)
msg?: string // 结果描述信息
}
WXShareContent
{
type: string, // 分享类型:text/image/webpage/miniprogram
title?: string, // 分享标题
desc?: string, // 分享描述
text?: string, // 文本内容(text类型必填)
imagePath?: string, // 图片路径(image类型必填)
webpageUrl?: string, // 网页链接(webpage类型必填)
miniProgram?: WXMiniProgramContent, // 小程序参数
scene?: number, // 分享场景:0聊天 1朋友圈 2收藏
thumbPath?: string // 缩略图路径
}
WXPayParams
{
appid: string, // 微信AppID
partnerid: string, // 微信支付商户号
prepayid: string, // 预支付订单号
package: string, // 扩展字段,固定值"Sign=WXPay"
noncestr: string, // 随机字符串
timestamp: number, // 时间戳
sign: string // 签名
}
⚠️ 注意事项
- 使用前必须注册:所有功能使用前都需要先调用
WxSdk.register()
- 权限配置:iOS需要在Xcode中配置URL Scheme和Universal Link
- 图片大小限制:
- 缩略图:最大32KB,推荐120x120像素
- 分享图片:最大10MB
- 测试环境:
- iOS模拟器无法测试微信功能,需要真机测试
- Android可以使用真机或模拟器测试
🔧 常见问题
Q: 分享失败,错误码-1
A: 检查是否已注册微信SDK,以及AppID是否正确配置
Q: 支付失败,提示签名错误
A: 确保支付参数是从服务器获取的,客户端不应自行生成签名
Q: iOS无法回调
A: 检查Xcode中的URL Scheme和Universal Link配置是否正确
📱 示例项目
完整的示例代码请参考项目中的 pages/index/index.uvue
文件,包含了所有功能的使用示例。
🤝 技术支持
如有问题,请添**或进交流群联系开发者获取技术支持。付费用户支持技术售后,也接受付费咨询和定制化插件开发。