sdk-h5-weixin.js 5.4 KB

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