0.2.6 • Published 2 years ago

@carpenter/wechat-jssdk v0.2.6

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

wechat-jssdk

微信JSSDK封装

安装

npm install @carpenter/wechat-jssdk

配置

成员说明类型是否必须默认值
appIdappidString
imgUrl分享图片地址String
link分享图片Stringwindow.location.href
title分享标题String
desc分享描述String
hideMenu隐藏菜单Booleanfalse
debugdebugBooleanfalse
ticketUrl获取签名api地址String
ticketConfig请求配置Object{}
ticketSuccess请求成功回调Function

示例

import WechatJSSDK from '@carpenter/wechat-jssdk'

const wechat = new WechatJSSDK({
  appId: 'wxxxxxxxxxxxxx', // 可不填,由签名接口返回
  imgUrl: 'http://stor.xxx.xxx',
  link: '',
  title: '我是分享标题',
  desc: '我是分享描述',
  hideMenu: false,
  debug: false,
  ticketUrl: 'http://xxx.com/api/get_jsticket',
  ticketConfig: {
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
    body: `url=${encodeURIComponent(window.location.href.split('#/')[0])}&appid=wxxxxxxxxxxxxx`,
  },
  ticketSuccess: (res) => {
    return res; // { timestamp, nonceStr, signature, appId }
  },
});

Vue.prototype.$wechat = wechat;

wechat.on('ready', () => {
  // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后
  // 如需在页面加载时就调用相关接口,则须把相关接口放在当前函数中调用来确保正确执行。
  // 对于用户触发时才调用的接口,则可以直接调用,不需要放在当前函数中。
  console.log('ready run !');
});

更新 配置

// key:
//   imgUrl     分享图
//   link       分享链接
//   title      分享标题
//   desc       分享描述
//   hideMenu   是否隐藏

// this.$wechat.setState(key, value)

this.$wechat.setState('title', '我是更新后的标题')
            .setState('desc', '我是更新后的描述')
            .update();

分享事件监听

// shareTimeline   分享到朋友圈/分享到QQ空间
// shareAppMessage 分享给朋友/分享到QQ

this.$wechat.on('shareTimeline', () => {
  console.log('分享到朋友圈');
});
this.$wechat.on('shareAppMessage', () => {
  console.log('分享到朋友');
});

动态设置 标题

this.$wechat.setNativeTitle('标题');

获取url参数

const param = this.$wechat.getQueryString('debug')

vConsole 移动端开发者面板 查看 console 日志

地址中添加  debug=true
0.2.6

2 years ago

0.2.3

2 years ago

0.2.5

2 years ago

0.2.2

3 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago