@m775/jsweixin
v1.0.2
Published
A lightweight JavaScript wrapper for WeChat JS-SDK, providing easy configuration and share API integration for web applications.
Maintainers
Readme
Gentbo Wxshare
微信 JS-SDK 分享工具,基于官方
jweixin封装,采用链式调用方式快速完成微信分享配置,支持好友分享、朋友圈分享及自定义 JS-SDK 接口配置。
功能特性
- 支持微信 JS-SDK 初始化
- 支持链式调用
- 支持好友分享
- 支持朋友圈分享
- 支持自定义 JS API
- 支持 Debug 调试模式
- 支持成功、取消回调
- 支持动态修改分享内容
- 零依赖(依赖微信官方 jweixin)
安装
直接引入:
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script src="https://cloud.m775.net/js/jsweixin.js"></script>npm:
npm i @m775/jsweixinimport "@m775/jsweixin";快速开始
GB.Wxshare()
.appid("wx123456789")
.timestamp(1722040000)
.nonce("abcdef")
.sign("xxxxxxxxxxxxxxxx")
.title("Gentbo")
.desc("Gentbo 微信分享工具")
.link("https://www.gentbo.com")
.image("https://www.gentbo.com/logo.png")
.run();初始化配置
微信 JS-SDK 签名建议由服务器生成。
GB.Wxshare()
.appid(appId)
.timestamp(timestamp)
.nonce(nonceStr)
.sign(signature)
.run();API
debug()
开启 Debug 模式。
.debug(true)|参数|类型|说明| |-------|------|------| |debug|Boolean|是否开启调试|
appid()
设置公众号 AppId。
.appid(appId)|参数|类型| |-------|------| |appid|String|
timestamp()
设置时间戳。
.timestamp(timestamp)|参数|类型| |-------|------| |timestamp|Number / String|
nonce()
设置随机字符串。
.nonce(nonceStr)|参数|类型| |-------|------| |nonce|String|
sign()
设置签名。
.sign(signature)|参数|类型| |-------|------| |signature|String|
api()
设置需要启用的微信 JS API。
支持数组或字符串。
.api([
"onMenuShareAppMessage",
"onMenuShareTimeline"
])或
.api("onMenuShareAppMessage,onMenuShareTimeline")addApi()
追加一个 API。
.addApi("chooseImage")分享配置
title()
分享标题。
.title("Gentbo")desc()
分享描述。
.desc("Gentbo JavaScript Library")link()
分享链接。
.link("https://www.gentbo.com")image()
分享图片。
必须为公网可访问地址。
.image("https://www.gentbo.com/logo.png")success()
分享成功回调。
.success(()=>{
console.log("分享成功");
})cancel()
取消分享回调。
.cancel(()=>{
console.log("取消分享");
})run()
初始化微信 SDK 并注册分享。
.run()完整示例
GB.Wxshare()
.debug(false)
.appid("wx123456789")
.timestamp(1722040000)
.nonce("abcdef")
.sign("xxxxxxxxxxxxxxxx")
.title("Gentbo")
.desc("高性能 JavaScript 工具库")
.link(location.href)
.image("https://www.gentbo.com/logo.png")
.success(()=>{
console.log("分享成功");
})
.cancel(()=>{
console.log("取消分享");
})
.run();默认启用接口
工具默认启用以下微信分享接口:
onMenuShareAppMessage
onMenuShareTimeline如果需要其它接口,可使用:
.api([
"chooseImage",
"previewImage",
"scanQRCode"
])或
.addApi("chooseImage")
.addApi("scanQRCode")注意事项
- 使用前必须先加载微信官方
jweixinSDK。 appId、timestamp、nonceStr、signature必须由服务器生成。- 分享图片必须使用公网 HTTPS 地址。
- 分享链接必须与 JS-SDK 签名使用的页面 URL 保持一致。
- 必须在微信内置浏览器中使用。
- 如果
wx未加载,工具不会执行初始化。
浏览器支持
|浏览器|支持| |-------|------| |微信 Android|✔| |微信 iOS|✔|
Gentbo Custom License
Copyright (c) 2016-Present Zhou Bo
Website: https://developer.gentbo.com
Permission is granted to use this software for personal, educational, and commercial projects without modification.
You may not:
- Modify this software.
- Reverse engineer, decompile, or create derivative works based on this software.
- Redistribute, resell, sublicense, or publish this software or modified versions.
- Remove or alter copyright notices.
This software is provided "as is", without warranty of any kind. For commercial redistribution, OEM licensing, or special authorization:
