wechat.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import * as WeChat from 'react-native-wechat';
  2. import request from '../utils/request';
  3. /**
  4. public static String WX_APPID = "wx51acc19c8f7a0f6f";
  5. public static String WX_SECRET = "e830d45f497025041269ef6221140c3d";
  6. */
  7. WeChat.registerApp('wx51acc19c8f7a0f6f');
  8. export default class wechat {
  9. //登录方法
  10. static wechatLogin(callback) {
  11. WeChat.isWXAppInstalled().then((isInstalled) => {
  12. if (isInstalled) {
  13. WeChat.sendAuthRequest('snsapi_userinfo').then((result) => {
  14. var object = JSON.parse(JSON.stringify(result));
  15. var loginUrl =
  16. 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx51acc19c8f7a0f6f&secret=e830d45f497025041269ef6221140c3d&code=' +
  17. object['code'] +
  18. '&grant_type=authorization_code';
  19. request(loginUrl).then((res) => {
  20. console.log('code请求下一步返回结果:' + res['openid']);
  21. callback(res['openid']);
  22. });
  23. });
  24. } else {
  25. alert('请安装微信');
  26. }
  27. });
  28. }
  29. /**
  30. * 好友文字分享
  31. * @param {string} descriptiontext 测试微信好友分享的文本内容
  32. */
  33. static shareToSessionText(descriptiontext) {
  34. WeChat.isWXAppInstalled().then((isInstalled) => {
  35. if (isInstalled) {
  36. WeChat.shareToSession({
  37. type: 'text',
  38. description: descriptiontext
  39. })
  40. .then((result) => {
  41. console.log(result);
  42. })
  43. .catch((error) => {
  44. error(error.message);
  45. });
  46. } else {
  47. alert('请安装微信');
  48. }
  49. });
  50. }
  51. /**
  52. * 微信好友分享链接
  53. * @param {string} title_text 分享的标题
  54. * @param {string} description_text 分享的标题内容
  55. * @param {string} thumbImage_url 分享的标题图片
  56. * @param {string} webpageUrl_url 分享的链接
  57. */
  58. static shareToSessionNews(title_text, description_text, thumbImage_url, webpageUrl_url) {
  59. WeChat.isWXAppInstalled().then((isInstalled) => {
  60. if (isInstalled) {
  61. WeChat.shareToSession({
  62. title: title,
  63. description: description,
  64. thumbImage: thumbImage,
  65. type: 'news',
  66. webpageUrl: webpageUrl
  67. }).catch((error) => {
  68. alert(error.message);
  69. });
  70. } else {
  71. alert('请安装微信');
  72. }
  73. });
  74. }
  75. /**
  76. * 微信朋友圈分享的文本
  77. * @param {string} description_text 测试微信朋友圈分享的文本内容
  78. */
  79. static shareToTimelineText(description_text) {
  80. WeChat.isWXAppInstalled().then((isInstalled) => {
  81. if (isInstalled) {
  82. WeChat.shareToTimeline({
  83. type: 'text',
  84. description: description_text
  85. }).catch((error) => {
  86. console.log(error.message);
  87. });
  88. } else {
  89. alert('请安装微信');
  90. }
  91. });
  92. }
  93. /**
  94. * 微信好友分享链接
  95. * @param {string} title_text 分享的标题
  96. * @param {string} description_text 分享的标题内容
  97. * @param {string} thumbImage_url 分享的标题图片
  98. * @param {string} webpageUrl_url 分享的链接
  99. */
  100. static shareToTimelineNews(title_text, description_text, thumbImage_url, webpageUrl_url) {
  101. WeChat.isWXAppInstalled().then((isInstalled) => {
  102. if (isInstalled) {
  103. WeChat.shareToTimeline({
  104. title: title_text,
  105. description: description_text,
  106. thumbImage: thumbImage_url,
  107. type: 'news',
  108. webpageUrl: webpageUrl_url
  109. }).catch((error) => {
  110. console.log(error.message);
  111. });
  112. } else {
  113. alert('请安装微信');
  114. }
  115. });
  116. }
  117. /**
  118. * 微信支付
  119. * @param {string} partnerId 商家向财付通申请的商家id
  120. * @param {string} prepayId 预支付订单
  121. * @param {string} nonceStr 随机串,防重发
  122. * @param {string} timeStamp 时间戳,防重发.
  123. * @param {string} package 商家根据财付通文档填写的数据和签名
  124. * @param {string} sign 商家根据微信开放平台文档对数据做的签名
  125. */
  126. static pay(partnerId, prepayId, nonceStr, timeStamp, packages, sign) {
  127. WeChat.isWXAppInstalled().then((isInstalled) => {
  128. if (isInstalled) {
  129. WeChat.pay({
  130. partnerId: partnerId, // 商家向财付通申请的商家id
  131. prepayId: prepayId, // 预支付订单
  132. nonceStr: nonceStr, // 随机串,防重发
  133. timeStamp: timeStamp, // 时间戳,防重发.
  134. package: packages, // "Sign=WXPay", // 商家根据财付通文档填写的数据和签名
  135. sign: sign // 商家根据微信开放平台文档对数据做的签名
  136. })
  137. .then((requestJson) => {
  138. //支付成功回调
  139. if (requestJson.errCode == '0') {
  140. //回调成功处理
  141. }
  142. })
  143. .catch((err) => {
  144. alert('支付失败');
  145. });
  146. } else {
  147. alert('请安装微信');
  148. }
  149. });
  150. }
  151. }
  152. /***
  153. //github:https://github.com/yorkie/react-native-wechat
  154. 使用方法:
  155. 1.登录使用方法,返回openid
  156. // wechat.wechatLogin((openid) => {
  157. // alert(openid);
  158. // });
  159. //微信方法传参
  160. {
  161. type {Number} type of this message. Can be {news|text|imageUrl|imageFile|imageResource|video|audio|file}
  162. thumbImage {String} Thumb image of the message, which can be a uri or a resource id.
  163. description {String} The description about the sharing.
  164. webpageUrl {String} Required if type equals news. The webpage link to share.
  165. imageUrl {String} Provide a remote image if type equals image.
  166. videoUrl {String} Provide a remote video if type equals video.
  167. usicUrl {String} Provide a remote music if type equals audio.
  168. filePath {String} Provide a local file if type equals file.
  169. fileExtension {String} Provide the file type if type equals file.
  170. }
  171. */