wechat.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import * as WeChat from "react-native-wechat";
  2. /**
  3. public static String WX_APPID = "wx51acc19c8f7a0f6f";
  4. public static String WX_SECRET = "e830d45f497025041269ef6221140c3d";
  5. */
  6. WeChat.registerApp("wx51acc19c8f7a0f6f");
  7. export default class wechat {
  8. /**
  9. * 好友文字分享
  10. * @param {string} descriptiontext 测试微信好友分享的文本内容
  11. */
  12. static shareToSessionText(descriptiontext) {
  13. WeChat.isWXAppInstalled().then(isInstalled => {
  14. if (isInstalled) {
  15. WeChat.shareToSession({
  16. type: "text",
  17. description: descriptiontext
  18. }).catch(error => {
  19. console.log(error.message);
  20. });
  21. } else {
  22. alert("请安装微信");
  23. }
  24. });
  25. }
  26. /**
  27. * 微信好友分享链接
  28. * @param {string} title_text 分享的标题
  29. * @param {string} description_text 分享的标题内容
  30. * @param {string} thumbImage_url 分享的标题图片
  31. * @param {string} webpageUrl_url 分享的链接
  32. */
  33. static shareToSessionNews(
  34. title_text,
  35. description_text,
  36. thumbImage_url,
  37. webpageUrl_url
  38. ) {
  39. WeChat.isWXAppInstalled().then(isInstalled => {
  40. if (isInstalled) {
  41. WeChat.shareToSession({
  42. title: title,
  43. description: description,
  44. thumbImage: thumbImage,
  45. type: "news",
  46. webpageUrl: webpageUrl
  47. }).catch(error => {
  48. alert(error.message);
  49. });
  50. } else {
  51. alert("请安装微信");
  52. }
  53. });
  54. }
  55. /**
  56. * 微信朋友圈分享的文本
  57. * @param {string} description_text 测试微信朋友圈分享的文本内容
  58. */
  59. static shareToTimelineText(description_text) {
  60. WeChat.isWXAppInstalled().then(isInstalled => {
  61. if (isInstalled) {
  62. WeChat.shareToTimeline({
  63. type: "text",
  64. description: description_text
  65. }).catch(error => {
  66. console.log(error.message);
  67. });
  68. } else {
  69. alert("请安装微信");
  70. }
  71. });
  72. }
  73. /**
  74. * 微信好友分享链接
  75. * @param {string} title_text 分享的标题
  76. * @param {string} description_text 分享的标题内容
  77. * @param {string} thumbImage_url 分享的标题图片
  78. * @param {string} webpageUrl_url 分享的链接
  79. */
  80. static shareToTimelineNews(
  81. title_text,
  82. description_text,
  83. thumbImage_url,
  84. webpageUrl_url
  85. ) {
  86. WeChat.isWXAppInstalled().then(isInstalled => {
  87. if (isInstalled) {
  88. WeChat.shareToTimeline({
  89. title: title_text,
  90. description: description_text,
  91. thumbImage: thumbImage_url,
  92. type: "news",
  93. webpageUrl: webpageUrl_url
  94. }).catch(error => {
  95. console.log(error.message);
  96. });
  97. } else {
  98. alert("请安装微信");
  99. }
  100. });
  101. }
  102. /**
  103. * 微信支付
  104. * @param {string} partnerId 商家向财付通申请的商家id
  105. * @param {string} prepayId 预支付订单
  106. * @param {string} nonceStr 随机串,防重发
  107. * @param {string} timeStamp 时间戳,防重发.
  108. * @param {string} package 商家根据财付通文档填写的数据和签名
  109. * @param {string} sign 商家根据微信开放平台文档对数据做的签名
  110. */
  111. static pay(partnerId, prepayId, nonceStr, timeStamp, packages, sign) {
  112. WeChat.isWXAppInstalled().then(isInstalled => {
  113. if (isInstalled) {
  114. WeChat.pay({
  115. partnerId: partnerId, // 商家向财付通申请的商家id
  116. prepayId: prepayId, // 预支付订单
  117. nonceStr: nonceStr, // 随机串,防重发
  118. timeStamp: timeStamp, // 时间戳,防重发.
  119. package: packages, // "Sign=WXPay", // 商家根据财付通文档填写的数据和签名
  120. sign: sign // 商家根据微信开放平台文档对数据做的签名
  121. })
  122. .then(requestJson => {
  123. //支付成功回调
  124. if (requestJson.errCode == "0") {
  125. //回调成功处理
  126. }
  127. })
  128. .catch(err => {
  129. alert("支付失败");
  130. });
  131. } else {
  132. alert("请安装微信");
  133. }
  134. });
  135. }
  136. }