sdk-h5-weixin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 '@/common/helper';
  8. import AuthUtil from '@/common/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.origin;
  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: [
  37. 'chooseWXPay',
  38. 'openLocation',
  39. 'getLocation',
  40. 'updateAppMessageShareData',
  41. 'updateTimelineShareData',
  42. 'scanQRCode',
  43. ], // TODO 芋艿:后续可以设置更多权限;
  44. openTagList: data.openTagList,
  45. });
  46. } else {
  47. console.log('请求 JSSDK 配置失败,错误码:', code);
  48. }
  49. // 监听结果
  50. configSuccess = true;
  51. jweixin.error((err) => {
  52. configSuccess = false;
  53. console.error('微信 JSSDK 初始化失败', err);
  54. $helper.toast('微信JSSDK:' + err.errMsg);
  55. });
  56. jweixin.ready(() => {
  57. if (configSuccess) {
  58. console.log('微信 JSSDK 初始化成功');
  59. }
  60. });
  61. // 回调
  62. if (callback) {
  63. callback(data);
  64. }
  65. },
  66. //在需要定位页面调用 TODO 芋艿:未测试
  67. getLocation(callback) {
  68. this.isReady(() => {
  69. jweixin.getLocation({
  70. type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  71. success: function (res) {
  72. callback(res);
  73. },
  74. fail: function (res) {
  75. console.log('%c微信H5sdk,getLocation失败:', 'color:green;background:yellow');
  76. },
  77. });
  78. });
  79. },
  80. // 获取微信收货地址
  81. openAddress(callback) {
  82. this.isReady(() => {
  83. jweixin.openAddress({
  84. success: function (res) {
  85. callback.success && callback.success(res);
  86. },
  87. fail: function (err) {
  88. callback.error && callback.error(err);
  89. console.log('%c微信H5sdk,openAddress失败:', 'color:green;background:yellow');
  90. },
  91. complete: function (res) {},
  92. });
  93. });
  94. },
  95. // 微信扫码 TODO 芋艿:未测试
  96. scanQRCode(callback) {
  97. this.isReady(() => {
  98. jweixin.scanQRCode({
  99. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  100. scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  101. success: function (res) {
  102. callback(res);
  103. },
  104. fail: function (res) {
  105. console.log('%c微信H5sdk,scanQRCode失败:', 'color:green;background:yellow');
  106. },
  107. });
  108. });
  109. },
  110. // 更新微信分享信息
  111. updateShareInfo(data, callback = null) {
  112. this.isReady(() => {
  113. const shareData = {
  114. title: data.title,
  115. desc: data.desc,
  116. link: data.link,
  117. imgUrl: data.image,
  118. success: function (res) {
  119. if (callback) {
  120. callback(res);
  121. }
  122. // 分享后的一些操作,比如分享统计等等
  123. },
  124. cancel: function (res) {},
  125. };
  126. // 新版 分享聊天api
  127. jweixin.updateAppMessageShareData(shareData);
  128. // 新版 分享到朋友圈api
  129. jweixin.updateTimelineShareData(shareData);
  130. });
  131. },
  132. // 打开坐标位置 TODO 芋艿:未测试
  133. openLocation(data, callback) {
  134. this.isReady(() => {
  135. jweixin.openLocation({
  136. ...data,
  137. success: function (res) {
  138. console.log(res);
  139. },
  140. });
  141. });
  142. },
  143. // 选择图片 TODO 芋艿:未测试
  144. chooseImage(callback) {
  145. this.isReady(() => {
  146. jweixin.chooseImage({
  147. count: 1,
  148. sizeType: ['compressed'],
  149. sourceType: ['album'],
  150. success: function (rs) {
  151. callback(rs);
  152. },
  153. });
  154. });
  155. },
  156. // 微信支付
  157. wxpay(data, callback) {
  158. this.isReady(() => {
  159. jweixin.chooseWXPay({
  160. timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  161. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  162. package: data.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  163. signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  164. paySign: data.paySign, // 支付签名
  165. success: function (res) {
  166. callback.success && callback.success(res);
  167. },
  168. fail: function (err) {
  169. callback.fail && callback.fail(err);
  170. },
  171. cancel: function (err) {
  172. callback.cancel && callback.cancel(err);
  173. },
  174. });
  175. });
  176. },
  177. };