wechat.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var exec = require('cordova/exec');
  2. module.exports = {
  3. Scene: {
  4. SESSION: 0, // 聊天界面
  5. TIMELINE: 1, // 朋友圈
  6. FAVORITE: 2 // 收藏
  7. },
  8. Type: {
  9. APP: 1,
  10. EMOTION: 2,
  11. FILE: 3,
  12. IMAGE: 4,
  13. MUSIC: 5,
  14. VIDEO: 6,
  15. WEBPAGE: 7
  16. },
  17. isInstalled: function (onSuccess, onError) {
  18. exec(onSuccess, onError, "Wechat", "isWXAppInstalled", []);
  19. },
  20. /**
  21. * Share a message to wechat app
  22. *
  23. * @example
  24. * <code>
  25. * Wechat.share({
  26. * message: {
  27. * title: "Message Title",
  28. * description: "Message Description(optional)",
  29. * mediaTagName: "Media Tag Name(optional)",
  30. * thumb: "http://YOUR_THUMBNAIL_IMAGE",
  31. * media: {
  32. * type: Wechat.Type.WEBPAGE, // webpage
  33. * webpageUrl: "https://github.com/xu-li/cordova-plugin-wechat" // webpage
  34. * }
  35. * },
  36. * scene: Wechat.Scene.TIMELINE // share to Timeline
  37. * }, function () {
  38. * alert("Success");
  39. * }, function (reason) {
  40. * alert("Failed: " + reason);
  41. * });
  42. * </code>
  43. */
  44. share: function (message, onSuccess, onError) {
  45. exec(onSuccess, onError, "Wechat", "share", [message]);
  46. },
  47. /**
  48. * Sending an auth request to Wechat
  49. *
  50. * @example
  51. * <code>
  52. * Wechat.auth(function (response) { alert(response.code); });
  53. * </code>
  54. */
  55. auth: function (scope, state, onSuccess, onError) {
  56. if (typeof scope == "function") {
  57. // Wechat.auth(function () { alert("Success"); });
  58. // Wechat.auth(function () { alert("Success"); }, function (error) { alert(error); });
  59. return exec(scope, state, "Wechat", "sendAuthRequest");
  60. }
  61. if (typeof state == "function") {
  62. // Wechat.auth("snsapi_userinfo", function () { alert("Success"); });
  63. // Wechat.auth("snsapi_userinfo", function () { alert("Success"); }, function (error) { alert(error); });
  64. return exec(state, onSuccess, "Wechat", "sendAuthRequest", [scope]);
  65. }
  66. return exec(onSuccess, onError, "Wechat", "sendAuthRequest", [scope, state]);
  67. },
  68. /**
  69. * Send a payment request
  70. *
  71. * @link https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_1
  72. * @example
  73. * <code>
  74. * var params = {
  75. * mch_id: '10000100', // merchant id
  76. * prepay_id: 'wx201411101639507cbf6ffd8b0779950874', // prepay id returned from server
  77. * nonce: '1add1a30ac87aa2db72f57a2375d8fec', // nonce string returned from server
  78. * timestamp: '1439531364', // timestamp
  79. * sign: '0CB01533B8C1EF103065174F50BCA001', // signed string
  80. * };
  81. * Wechat.sendPaymentRequest(params, function () {
  82. * alert("Success");
  83. * }, function (reason) {
  84. * alert("Failed: " + reason);
  85. * });
  86. * </code>
  87. */
  88. sendPaymentRequest: function (params, onSuccess, onError) {
  89. exec(onSuccess, onError, "Wechat", "sendPaymentRequest", [params]);
  90. },
  91. /**
  92. * jumpToBizProfile (跳转到某个微信公众号)2016-11-11 测试是失效的,囧
  93. *
  94. * @link https://segmentfault.com/a/1190000007204624
  95. * @link https://segmentfault.com/q/1010000003907796
  96. * @example
  97. * <code>
  98. * var params = {
  99. * info: 'gh_xxxxxxx', // 公众帐号原始ID
  100. * type: 'Normal' // 普通号
  101. * }
  102. * or
  103. * var params = {
  104. * info: 'extMsg', // 相关的硬件二维码串
  105. * type: 'Device' // 硬件号
  106. * };
  107. * Wechat.jumpToBizProfile(params, function () {
  108. * alert("Success");
  109. * }, function (reason) {
  110. * alert("Failed: " + reason);
  111. * });
  112. * </code>
  113. */
  114. jumpToBizProfile: function (params, onSuccess, onError) {
  115. exec(onSuccess, onError, "Wechat", "jumpToBizProfile", [params]);
  116. },
  117. /**
  118. * jumpToWechat (因为jumpToBizProfile失效了,暂时新增了一个临时的api)
  119. *
  120. * @link https://segmentfault.com/a/1190000007204624
  121. * @example
  122. * <code>
  123. * var url = "wechat://" 现阶段貌似只支持这一个协议了
  124. * Wechat.jumpToWechat(url, function () {
  125. * alert("Success");
  126. * }, function (reason) {
  127. * alert("Failed: " + reason);
  128. * });
  129. * </code>
  130. */
  131. jumpToWechat: function (url, onSuccess, onError) {
  132. exec(onSuccess, onError, "Wechat", "jumpToWechat", [url]);
  133. }
  134. };