wechat.js 5.6 KB

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