wechat.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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);
  23. var getUserUrl =
  24. 'https://api.weixin.qq.com/sns/userinfo?access_token=' +
  25. res['access_token'] +
  26. '&openid=' +
  27. res['openid'];
  28. request(getUserUrl).then((user) => {
  29. console.log('微信返回信息');
  30. console.log('user', user);
  31. console.log('====================================');
  32. callback(user);
  33. });
  34. });
  35. });
  36. } else {
  37. alert('请安装微信');
  38. }
  39. });
  40. }
  41. /**
  42. * 好友文字分享
  43. * @param {string} descriptiontext 测试微信好友分享的文本内容
  44. */
  45. static shareToSessionText(descriptiontext) {
  46. WeChat.isWXAppInstalled().then((isInstalled) => {
  47. if (isInstalled) {
  48. WeChat.shareToSession({
  49. type: 'text',
  50. description: descriptiontext
  51. })
  52. .then((result) => {
  53. console.log(result);
  54. })
  55. .catch((error) => {
  56. error(error.message);
  57. });
  58. } else {
  59. alert('请安装微信');
  60. }
  61. });
  62. }
  63. /**
  64. * 微信好友分享链接
  65. * @param {string} title_text 分享的标题
  66. * @param {string} description_text 分享的标题内容
  67. * @param {string} thumbImage_url 分享的标题图片
  68. * @param {string} webpageUrl_url 分享的链接
  69. */
  70. static shareToSessionNews(title_text, description_text, thumbImage_url, webpageUrl_url) {
  71. WeChat.isWXAppInstalled().then((isInstalled) => {
  72. if (isInstalled) {
  73. WeChat.shareToSession({
  74. title: title_text,
  75. description: description_text,
  76. thumbImage: thumbImage_url,
  77. type: 'news',
  78. webpageUrl: webpageUrl_url
  79. }).catch((error) => {
  80. alert(error.message);
  81. });
  82. } else {
  83. alert('请安装微信');
  84. }
  85. });
  86. }
  87. /**
  88. * 微信朋友圈分享的文本
  89. * @param {string} description_text 测试微信朋友圈分享的文本内容
  90. */
  91. static shareToTimelineText(description_text) {
  92. WeChat.isWXAppInstalled().then((isInstalled) => {
  93. if (isInstalled) {
  94. WeChat.shareToTimeline({
  95. type: 'text',
  96. description: description_text
  97. }).catch((error) => {
  98. console.log(error.message);
  99. });
  100. } else {
  101. alert('请安装微信');
  102. }
  103. });
  104. }
  105. /**
  106. * 微信朋友圈分享链接
  107. * @param {string} title_text 分享的标题
  108. * @param {string} description_text 分享的标题内容
  109. * @param {string} thumbImage_url 分享的标题图片
  110. * @param {string} webpageUrl_url 分享的链接
  111. */
  112. static shareToTimelineNews(title_text, description_text, thumbImage_url, webpageUrl_url) {
  113. WeChat.isWXAppInstalled().then((isInstalled) => {
  114. if (isInstalled) {
  115. WeChat.shareToTimeline({
  116. title: title_text,
  117. description: description_text,
  118. thumbImage: thumbImage_url,
  119. type: 'news',
  120. webpageUrl: webpageUrl_url
  121. }).catch((error) => {
  122. console.log(error.message);
  123. });
  124. } else {
  125. alert('请安装微信');
  126. }
  127. });
  128. }
  129. /**
  130. * 微信支付
  131. * @param {string} partnerId 商家向财付通申请的商家id
  132. * @param {string} prepayId 预支付订单
  133. * @param {string} nonceStr 随机串,防重发
  134. * @param {string} timeStamp 时间戳,防重发.
  135. * @param {string} package 商家根据财付通文档填写的数据和签名
  136. * @param {string} sign 商家根据微信开放平台文档对数据做的签名
  137. */
  138. static pay(partnerId, prepayId, nonceStr, timeStamp, packages, sign, callback) {
  139. WeChat.isWXAppInstalled().then((isInstalled) => {
  140. if (isInstalled) {
  141. WeChat.pay({
  142. partnerId: partnerId, // 商家向财付通申请的商家id
  143. prepayId: prepayId, // 预支付订单
  144. nonceStr: nonceStr, // 随机串,防重发
  145. timeStamp: timeStamp, // 时间戳,防重发.
  146. package: packages, // "Sign=WXPay", // 商家根据财付通文档填写的数据和签名
  147. sign: sign // 商家根据微信开放平台文档对数据做的签名
  148. })
  149. .then((requestJson) => {
  150. // //支付成功回调
  151. // if (requestJson.errCode == '0') {
  152. // //回调成功处理
  153. // }
  154. callback(requestJson);
  155. })
  156. .catch((err) => {
  157. alert('支付失败:' + err);
  158. });
  159. } else {
  160. alert('请安装微信');
  161. }
  162. });
  163. }
  164. /**
  165. 跳转小程序(需要改 react-native-wechat插件
  166. 1.删除react-native-android默认的jar包,直接引用微信官网的包,或者下载官方最新包wechat-sdk-android-with-mta-x.x.x.jar
  167. 2.在react-native-wechat插件的index.js中,增加toMiniProgram方法)
  168. 3.在react-native-wechat插件WeChatModule中,删除旧的引用,增加新的引用
  169. import com.tencent.mm.opensdk.constants.ConstantsAPI;
  170. import com.tencent.mm.opensdk.modelbase.BaseReq;
  171. import com.tencent.mm.opensdk.modelbase.BaseResp;
  172. import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
  173. import com.tencent.mm.opensdk.modelmsg.SendAuth;
  174. import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
  175. import com.tencent.mm.opensdk.modelmsg.WXFileObject;
  176. import com.tencent.mm.opensdk.modelmsg.WXImageObject;
  177. import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
  178. import com.tencent.mm.opensdk.modelmsg.WXMusicObject;
  179. import com.tencent.mm.opensdk.modelmsg.WXTextObject;
  180. import com.tencent.mm.opensdk.modelmsg.WXVideoObject;
  181. import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
  182. import com.tencent.mm.opensdk.modelpay.PayReq;
  183. import com.tencent.mm.opensdk.modelpay.PayResp;
  184. import com.tencent.mm.opensdk.openapi.IWXAPI;
  185. import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
  186. import com.tencent.mm.opensdk.openapi.WXAPIFactory;
  187. 4.在react-native-wechat插件WeChatModule中,增加跳转小程序方法
  188. @ReactMethod
  189. public void toMiniProgram(String programId, String path) {
  190. WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
  191. req.userName = programId; // 填小程序原始id
  192. req.path = path; //拉起小程序页面的可带参路径,不填默认拉起小程序首页
  193. req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版,体验版和正式版
  194. api.sendReq(req);
  195. }
  196. */
  197. static toMiniProgram(programId, path) {
  198. WeChat.toMiniProgram(programId, path);
  199. }
  200. }
  201. /***
  202. //github:https://github.com/yorkie/react-native-wechat
  203. 使用方法:
  204. 1.登录使用方法,返回object
  205. wechat.wechatLogin((user) => {
  206. console.log('openid:' + user['openid']);
  207. console.log('unionid:' + user['unionid']);
  208. console.log('nickname:' + user['nickname']);
  209. console.log('sex:' + user['sex']);
  210. console.log('avatar:' + user['province'] + '--' + user['city']);
  211. });
  212. 2.跳转小程序方法
  213. wechat.toMiniProgram('gh_d81480f7a2fd', '');
  214. //微信各种分享方法传参定义
  215. {
  216. type {Number} type of this message. Can be {news|text|imageUrl|imageFile|imageResource|video|audio|file}
  217. thumbImage {String} Thumb image of the message, which can be a uri or a resource id.
  218. description {String} The description about the sharing.
  219. webpageUrl {String} Required if type equals news. The webpage link to share.
  220. imageUrl {String} Provide a remote image if type equals image.
  221. videoUrl {String} Provide a remote video if type equals video.
  222. usicUrl {String} Provide a remote music if type equals audio.
  223. filePath {String} Provide a local file if type equals file.
  224. fileExtension {String} Provide the file type if type equals file.
  225. }
  226. */