1.3.0 • Published 4 months ago

cordova-wtto00-wechat v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

cordova-wtto00-wechat

微信 cordova 插件

参考:xu-li/cordova-plugin-wechat

  • 🌟 添加 TS 类型提示
  • 🐛 修改不支持 Android 13 的问题
  • 🌟 改用在线最新微信 SDK 包
  • 🌟 支持拉起微信客服
  • 🌟 支持微信开放标签拉起 APP

支持平台

  • android
  • ios

安装

cordova plugin add cordova-wtto00-wechat --variable WECHATAPPID=YOUR_WECHAT_APPID --variable UNIVERSALLINK=YOUR_UNIVERSAL_LINK

移除

cordova plugin rm cordova-plugin-wechat --variable WECHATAPPID=YOUR_WECHAT_APPID --variable UNIVERSALLINK=YOUR_UNIVERSAL_LINK

用法

检查微信是否安装

Wechat.isInstalled(
  function (installed) {
    alert("Wechat installed: " + (installed ? "Yes" : "No"));
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

微信认证登录

官方文档 | 参数名称 | 参数类型 | 是否必须 | 说明 | | -------- | -------- | -------- | -------------- | | scope | string | 是 | 授权域 | | state | string | 是 | 标识符唯一即可 |

var scope = "snsapi_userinfo",
  state = "_" + +new Date();
Wechat.auth(
  scope,
  state,
  function (response) {
    // you may use response.code to get the access token.
    alert(JSON.stringify(response));
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

微信分享

官方文档

分享文本

参数名称参数类型是否必须说明
textstring分享的文本内容
sceneint发送的目标场
Wechat.share(
  {
    text: "This is just a plain string",
    scene: Wechat.Scene.TIMELINE, // share to Timeline
  },
  function () {
    alert("Success");
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

分享媒体(链接、图片、音乐、视频、小程序)

参数名称参数类型是否必须说明
sceneint发送的目标场
messageobject微信媒体消息内容,媒体类型分享(ShareMessage)
message 参数
参数名称参数类型是否必须说明
titlestring消息标题,限制长度不超过 512Bytes
descriptionstring消息描述,限制长度不超过 1KB
thumbstring缩略图(支持本地资源,远程资源,base64)
mediaobject媒体消息内容(详见下方各类别参数说明)
分享链接网页
参数名称参数类型是否必须说明
typeint分享类型,固定值
webpageUrlstring网页链接
Wechat.share({
    message: {
        ...
        media: {
            type: Wechat.Type.WEBPAGE,
            webpageUrl: "http://www.jason-z.com"
        }
    },
    scene: Wechat.Scene.TIMELINE   // share to Timeline
}, function () {
    alert("Success");
}, function (reason) {
    alert("Failed: " + reason);
});

分享图片

参数名称参数类型是否必须说明
typeint分享类型,固定值
imagestring分享图片(支持本地资源,远程资源,base64)
Wechat.share(
  {
    message: {
      title: "这是分享的标题",
      description: "这是分享的描述",
      thumb: "www/assets/imgs/logo.png",
      media: {
        type: Wechat.Type.IMAGE,
        image: "https://www.jason-z.com/storage/test_image.jpg",
      },
    },
    scene: Wechat.Scene.TIMELINE,
  },
  function () {
    alert("Success");
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

分享音乐

参数名称参数类型是否必须说明
typeint分享类型,固定值
musicUrlstring音频网页的 URL 地址
musicDataUrlstring音频数据的 URL 地址
Wechat.share(
  {
    message: {
      title: "这是分享的标题",
      description: "这是分享的描述",
      thumb: "www/assets/imgs/logo.png",
      media: {
        type: Wechat.Type.MUSIC,
        musicUrl: "https://www.jason-z.com",
        musicDataUrl: "https://www.jason-z.com/storage/test_audio.mp3",
      },
    },
    scene: Wechat.Scene.TIMELINE,
  },
  function () {
    alert("Success");
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

分享视频

参数名称参数类型是否必须说明
typeint分享类型,固定值
videoUrlstring视频网页的 URL 地址
Wechat.share(
  {
    message: {
      title: "这是分享的标题",
      description: "这是分享的描述",
      thumb: "www/assets/imgs/logo.png",
      media: {
        type: Wechat.Type.VIDEO,
        videoUrl: "https://www.jason-z.com/storage/test_video.mp4",
      },
    },
    scene: Wechat.Scene.TIMELINE,
  },
  function () {
    alert("Success");
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

分享到小程序

参数名称参数类型是否必须说明
typeint分享类型,固定值
webpageUrlstring兼容低版本的网页链接
userNamestring小程序原始 id
pathstring小程序页面路径
hdImageDatastring分享缩略图(支持 url 和 base64)
withShareTicketboolean是否使用带 shareTicket 的分享
miniprogramTypeint小程序类型:RELEASE 发布版 TEST 测试版 PREVIEW 体验版
Wechat.share({
    message: {
        ...
        media: {
            type: Wechat.Type.MINI,
            webpageUrl: "https://www.jason-z.com", // 兼容低版本的网页链接
            userName: "wxxxxxxxx", // 小程序原始id
            path: "user/info", // 小程序的页面路径
            hdImageData: "http://wwww.xxx.com/xx.jpg", // 程序新版本的预览图二进制数据 不超过128kb 支持 地址 base64 temp
            withShareTicket: true, // 是否使用带shareTicket的分享
            miniprogramType: Wechat.Mini.RELEASE
        }
    },
    scene: Wechat.Scene.SESSION   // 小程序仅支持聊天界面
}, function () {
    alert("Success");
}, function (reason) {
    alert("Failed: " + reason);
});

发送支付请求

官方文档 | 参数名称 | 参数类型 | 是否必须 | 说明 | | ------ |---|---| -------- | | mch_id |string|是| 商户 ID | | prepay_id |string|是| 预支付交易会话标识 | | nonce |string|是| 随机数 | | timestamp |string|是| 时间戳 | | sign |string|是| 签名 |

var params = {
  partnerid: "10000100", // merchant id
  prepayid: "wx201411101639507cbf6ffd8b0779950874", // prepay id
  noncestr: "1add1a30ac87aa2db72f57a2375d8fec", // nonce
  timestamp: "1439531364", // timestamp
  sign: "0CB01533B8C1EF103065174F50BCA001", // signed string
};

Wechat.sendPaymentRequest(
  params,
  function () {
    alert("Success");
  },
  function (reason) {
    alert("Failed: " + reason);
  }
);

选择卡券包

官方文档 | 参数名称 | 参数类型 | 是否必须 | 说明 | | ------ | --|--|-------- | | signType |string|是| 签名类型 | | cardSign |string|是| 签名 | | nonceStr |string|是| 随机数 | | timeStamp |string|是| 时间戳 |

const params = {
  timeStamp: "1510198391", // timeStamp
  signType: "SHA1", // sign type
  cardSign: "dff450eeeed08120159d285e79737173aec3df94", // cardSign
  nonceStr: "5598190f-5fb3-4bff-8314-fd189ab4e4b8", // nonce
};

Wechat.chooseInvoiceFromWX(
  params,
  function (data) {
    console.log(data);
  },
  function () {
    alert("error");
  }
);

打开微信微信小程序

官方文档 | 参数名称 | 参数类型 | 是否必须 | 说明 | | --------------- | --|--|----------------------------------------------------- | | userName | string|是|小程序原始 id | | path | string|是|小程序页面路径,不填默认进入首页 | | miniprogramType | int|是|小程序类型:RELEASE 发布版 TEST 测试版 PREVIEW 体验版 |

var params = {
  userName: "gh_d43f693ca31f",
  path: "pages/index/index?name1=key1&name2=key2",
  miniprogramType: Wechat.Mini.RELEASE,
};

Wechat.openMiniProgram(
  params,
  function (data) {
    console.log(data); // data:{extMsg:""}  extMsg: Corresponds to the app-parameter attribute in the Mini Program component <button open-type="launchApp">
  },
  function () {
    alert("error");
  }
);

拉起微信客服

官方文档 | 参数 | 备注 | | ------ | -------- | | corpId | 企业 ID | | url | 客服 URL |

Wechat.openCustomerServiceChat(
  { corpId: "corporate_id", url: "https://work.weixin.qq.com/kfid/kfxxxxxx" },
  function (data) {
    console.log(data);
  },
  function (reason) {
    console.log(reason);
  }
);

微信公众号开放标签拉起 APP

官方文档

// 监听开放标签拉起APP的事件
Wechat.listenLaunchFromWX((extinfo) => {
  // 已拉起APP,参数是extinfo
  console.log("extinfo: ", extinfo);
});

// 取消监听事件
Wechat.unListenLaunchFromWX();

iOS 通过 universal link 打开 APP

该方法仅 iOS 有效。

由于微信登录跳转回 APP 使用的是 universal link,所以该插件必须监听 universal link 打开 APP 的事件才能处理登录逻辑。

但是这样会使cordova-wtto00-universal-link插件监听的打开 APP 失效。

所以添加此方法,如果两者同时存在的话,iOS 在微信的监听方法中处理;安卓在原插件处理即可,不受影响。

// 监听universal link拉起APP的事件
Wechat.listenLaunchFromUL((url) => {
  // 已拉起APP,参数是url
  console.log("universal link: ", url);
});

// 取消监听事件
Wechat.unListenLaunchFromUL();