wechat.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. MINI: 8
  17. },
  18. Mini: {
  19. RELEASE: 0, // 正式版
  20. TEST: 1, // 测试版
  21. PREVIEW: 2 // 体验版
  22. },
  23. isInstalled: function (onSuccess, onError) {
  24. exec(onSuccess, onError, "Wechat", "isWXAppInstalled", []);
  25. },
  26. /**
  27. * Share a message to wechat app
  28. *
  29. * @example
  30. * <code>
  31. * Wechat.share({
  32. * message: {
  33. * title: "Message Title",
  34. * description: "Message Description(optional)",
  35. * mediaTagName: "Media Tag Name(optional)",
  36. * thumb: "http://YOUR_THUMBNAIL_IMAGE",
  37. * media: {
  38. * type: Wechat.Type.WEBPAGE, // webpage
  39. * webpageUrl: "https://github.com/xu-li/cordova-plugin-wechat" // webpage
  40. * }
  41. * },
  42. * scene: Wechat.Scene.TIMELINE // share to Timeline
  43. * }, function () {
  44. * alert("Success");
  45. * }, function (reason) {
  46. * alert("Failed: " + reason);
  47. * });
  48. * </code>
  49. */
  50. share: function (message, onSuccess, onError) {
  51. exec(onSuccess, onError, "Wechat", "share", [message]);
  52. },
  53. /**
  54. * Sending an auth request to Wechat
  55. *
  56. * @example
  57. * <code>
  58. * Wechat.auth(function (response) { alert(response.code); });
  59. * </code>
  60. */
  61. auth: function (scope, state, onSuccess, onError) {
  62. if (typeof scope == "function") {
  63. // Wechat.auth(function () { alert("Success"); });
  64. // Wechat.auth(function () { alert("Success"); }, function (error) { alert(error); });
  65. return exec(scope, state, "Wechat", "sendAuthRequest");
  66. }
  67. if (typeof state == "function") {
  68. // Wechat.auth("snsapi_userinfo", function () { alert("Success"); });
  69. // Wechat.auth("snsapi_userinfo", function () { alert("Success"); }, function (error) { alert(error); });
  70. return exec(state, onSuccess, "Wechat", "sendAuthRequest", [scope]);
  71. }
  72. return exec(onSuccess, onError, "Wechat", "sendAuthRequest", [scope, state]);
  73. },
  74. /**
  75. * Send a payment request
  76. *
  77. * @link https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_1
  78. * @example
  79. * <code>
  80. * var params = {
  81. * mch_id: '10000100', // merchant id
  82. * prepay_id: 'wx201411101639507cbf6ffd8b0779950874', // prepay id returned from server
  83. * nonce: '1add1a30ac87aa2db72f57a2375d8fec', // nonce string returned from server
  84. * timestamp: '1439531364', // timestamp
  85. * sign: '0CB01533B8C1EF103065174F50BCA001', // signed string
  86. * };
  87. * Wechat.sendPaymentRequest(params, function () {
  88. * alert("Success");
  89. * }, function (reason) {
  90. * alert("Failed: " + reason);
  91. * });
  92. * </code>
  93. */
  94. sendPaymentRequest: function (params, onSuccess, onError) {
  95. exec(onSuccess, onError, "Wechat", "sendPaymentRequest", [params]);
  96. },
  97. /**
  98. * jumpToBizProfile (跳转到某个微信公众号)2016-11-11 测试是失效的,囧
  99. *
  100. * @link https://segmentfault.com/a/1190000007204624
  101. * @link https://segmentfault.com/q/1010000003907796
  102. * @example
  103. * <code>
  104. * var params = {
  105. * info: 'gh_xxxxxxx', // 公众帐号原始ID
  106. * type: 'Normal' // 普通号
  107. * }
  108. * or
  109. * var params = {
  110. * info: 'extMsg', // 相关的硬件二维码串
  111. * type: 'Device' // 硬件号
  112. * };
  113. * Wechat.jumpToBizProfile(params, function () {
  114. * alert("Success");
  115. * }, function (reason) {
  116. * alert("Failed: " + reason);
  117. * });
  118. * </code>
  119. */
  120. jumpToBizProfile: function (params, onSuccess, onError) {
  121. exec(onSuccess, onError, "Wechat", "jumpToBizProfile", [params]);
  122. },
  123. /**
  124. * jumpToWechat (因为jumpToBizProfile失效了,暂时新增了一个临时的api)
  125. *
  126. * @link https://segmentfault.com/a/1190000007204624
  127. * @example
  128. * <code>
  129. * var url = "wechat://" 现阶段貌似只支持这一个协议了
  130. * Wechat.jumpToWechat(url, function () {
  131. * alert("Success");
  132. * }, function (reason) {
  133. * alert("Failed: " + reason);
  134. * });
  135. * </code>
  136. */
  137. jumpToWechat: function (url, onSuccess, onError) {
  138. exec(onSuccess, onError, "Wechat", "jumpToWechat", [url]);
  139. },
  140. /**
  141. * chooseInvoiceFromWX exq:choose invoices from Wechat card list
  142. *
  143. * @example
  144. * <code>
  145. * params: signType, cardSign, nonceStr, timeStamp all required
  146. * Wechat.chooseInvoiceFromWX(params, function () {
  147. * alert("Success");
  148. * }, function (reason) {
  149. * alert("Failed: " + reason);
  150. * });
  151. * </code>
  152. */
  153. chooseInvoiceFromWX: function (params, onSuccess, onError) {
  154. exec(onSuccess, onError, "Wechat", "chooseInvoiceFromWX", [params]);
  155. },
  156. /**
  157. * openMiniProgram exq:app opens wechat mini program
  158. *
  159. * @example
  160. * <code>
  161. * params: userName, path, miniprogramType all required
  162. * Wechat.openMiniProgram(params, function (data) {
  163. * alert(data.extMsg);
  164. * }, function (reason) {
  165. * alert("Failed: " + reason);
  166. * });
  167. * </code>
  168. */
  169. openMiniProgram: function (params, onSuccess, onError) {
  170. exec(onSuccess, onError, "Wechat", "openMiniProgram", [params]);
  171. }
  172. };