sdk-h5-weixin.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * 本模块封装微信浏览器下的一些方法。
  3. * 更多微信网页开发sdk方法,详见:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
  4. * 有 the permission value is offline verifying 报错请参考 @see https://segmentfault.com/a/1190000042289419 解决
  5. */
  6. import jweixin from 'weixin-js-sdk';
  7. import $helper from '@/sheep/helper';
  8. import AuthUtil from '@/sheep/api/member/auth';
  9. let configSuccess = false;
  10. export default {
  11. // 判断是否在微信中
  12. isWechat() {
  13. const ua = window.navigator.userAgent.toLowerCase();
  14. // noinspection EqualityComparisonWithCoercionJS
  15. return ua.match(/micromessenger/i) == 'micromessenger';
  16. },
  17. isReady(api) {
  18. jweixin.ready(api);
  19. },
  20. // 初始化 JSSDK
  21. async init(callback) {
  22. if (!this.isWechat()) {
  23. $helper.toast('请使用微信网页浏览器打开');
  24. return;
  25. }
  26. // 调用后端接口,获得 JSSDK 初始化所需的签名
  27. const url = location.href.split('#')[0];
  28. const { code, data } = await AuthUtil.createWeixinMpJsapiSignature(url);
  29. if (code === 0) {
  30. jweixin.config({
  31. debug: false,
  32. appId: data.appId,
  33. timestamp: data.timestamp,
  34. nonceStr: data.nonceStr,
  35. signature: data.signature,
  36. jsApiList: ['chooseWXPay', 'openLocation', 'getLocation','updateTimelineShareData','scanQRCode'], // TODO 芋艿:后续可以设置更多权限;
  37. openTagList: data.openTagList
  38. });
  39. }
  40. // 监听结果
  41. configSuccess = true;
  42. jweixin.error((err) => {
  43. configSuccess = false;
  44. console.error('微信 JSSDK 初始化失败', err);
  45. // $helper.toast('微信JSSDK:' + err.errMsg);
  46. });
  47. jweixin.ready(() => {
  48. if (configSuccess) {
  49. console.log('微信 JSSDK 初始化成功');
  50. }
  51. })
  52. // 回调
  53. if (callback) {
  54. callback(data);
  55. }
  56. },
  57. //在需要定位页面调用 TODO 芋艿:未测试
  58. getLocation(callback) {
  59. this.isReady(() => {
  60. jweixin.getLocation({
  61. type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  62. success: function (res) {
  63. callback(res);
  64. },
  65. fail: function (res) {
  66. console.log('%c微信H5sdk,getLocation失败:', 'color:green;background:yellow');
  67. },
  68. });
  69. });
  70. },
  71. // 获取微信收货地址
  72. openAddress(callback) {
  73. this.isReady(() => {
  74. jweixin.openAddress({
  75. success: function (res) {
  76. callback.success && callback.success(res);
  77. },
  78. fail: function (err) {
  79. callback.error && callback.error(err);
  80. console.log('%c微信H5sdk,openAddress失败:', 'color:green;background:yellow');
  81. },
  82. complete: function (res) {},
  83. });
  84. });
  85. },
  86. // 微信扫码 TODO 芋艿:未测试
  87. scanQRCode(callback) {
  88. this.isReady(() => {
  89. jweixin.scanQRCode({
  90. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  91. scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  92. success: function (res) {
  93. callback(res);
  94. },
  95. fail: function (res) {
  96. console.log('%c微信H5sdk,scanQRCode失败:', 'color:green;background:yellow');
  97. },
  98. });
  99. });
  100. },
  101. // 更新微信分享信息 TODO 芋艿:未测试
  102. updateShareInfo(data, callback = null) {
  103. this.isReady(() => {
  104. const shareData = {
  105. title: data.title,
  106. desc: data.desc,
  107. link: data.link,
  108. imgUrl: data.image,
  109. success: function (res) {
  110. if (callback) {
  111. callback(res);
  112. }
  113. // 分享后的一些操作,比如分享统计等等
  114. },
  115. cancel: function (res) {},
  116. };
  117. // 新版 分享聊天api
  118. jweixin.updateAppMessageShareData(shareData);
  119. // 新版 分享到朋友圈api
  120. jweixin.updateTimelineShareData(shareData);
  121. });
  122. },
  123. // 打开坐标位置 TODO 芋艿:未测试
  124. openLocation(data, callback) {
  125. this.isReady(() => {
  126. jweixin.openLocation({
  127. ...data,
  128. success: function (res) {
  129. console.log(res);
  130. }
  131. });
  132. });
  133. },
  134. // 选择图片 TODO 芋艿:未测试
  135. chooseImage(callback) {
  136. this.isReady(() => {
  137. jweixin.chooseImage({
  138. count: 1,
  139. sizeType: ['compressed'],
  140. sourceType: ['album'],
  141. success: function (rs) {
  142. callback(rs);
  143. },
  144. });
  145. });
  146. },
  147. // 微信支付
  148. wxpay(data, callback) {
  149. this.isReady(() => {
  150. jweixin.chooseWXPay({
  151. timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  152. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  153. package: data.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  154. signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  155. paySign: data.paySign, // 支付签名
  156. success: function (res) {
  157. callback.success && callback.success(res);
  158. },
  159. fail: function (err) {
  160. callback.fail && callback.fail(err);
  161. },
  162. cancel: function (err) {
  163. callback.cancel && callback.cancel(err);
  164. },
  165. });
  166. });
  167. },
  168. };