wechat.js 4.3 KB

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