sdk-h5-weixin.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. console.log('success:', res)
  90. alert('success:', res)
  91. callback.success && callback.success(res);
  92. },
  93. fail: function(err) {
  94. callback.error && callback.error(err);
  95. console.log('微信H5sdk,openAddress失败,原因是:', err);
  96. },
  97. complete: function(res) {},
  98. });
  99. });
  100. },
  101. // 微信扫码 TODO 芋艿:未测试
  102. scanQRCode(callback) {
  103. this.isReady(() => {
  104. jweixin.scanQRCode({
  105. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  106. scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  107. success: function(res) {
  108. callback(res);
  109. },
  110. fail: function(res) {
  111. console.log('%c微信H5sdk,scanQRCode失败:', 'color:green;background:yellow');
  112. },
  113. });
  114. });
  115. },
  116. // 更新微信分享信息 TODO 芋艿:未测试
  117. updateShareInfo(data, callback = null) {
  118. this.isReady(() => {
  119. const shareData = {
  120. title: data.title,
  121. desc: data.desc,
  122. link: data.link,
  123. imgUrl: data.image,
  124. success: function(res) {
  125. if (callback) {
  126. callback(res);
  127. }
  128. // 分享后的一些操作,比如分享统计等等
  129. },
  130. cancel: function(res) {},
  131. };
  132. // 新版 分享聊天api
  133. jweixin.updateAppMessageShareData(shareData);
  134. // 新版 分享到朋友圈api
  135. jweixin.updateTimelineShareData(shareData);
  136. });
  137. },
  138. // 打开坐标位置 TODO 芋艿:未测试
  139. openLocation(data, callback) {
  140. this.isReady(() => {
  141. jweixin.openLocation({
  142. //根据传入的坐标打开地图
  143. latitude: data.latitude,
  144. longitude: data.longitude,
  145. });
  146. });
  147. },
  148. // 选择图片 TODO 芋艿:未测试
  149. chooseImage(callback) {
  150. this.isReady(() => {
  151. jweixin.chooseImage({
  152. count: 1,
  153. sizeType: ['compressed'],
  154. sourceType: ['album'],
  155. success: function(rs) {
  156. callback(rs);
  157. },
  158. });
  159. });
  160. },
  161. // 微信支付
  162. wxpay(data, callback) {
  163. this.isReady(() => {
  164. jweixin.chooseWXPay({
  165. timestamp: data
  166. .timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  167. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  168. package: data.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  169. signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  170. paySign: data.paySign, // 支付签名
  171. success: function(res) {
  172. callback.success && callback.success(res);
  173. },
  174. fail: function(err) {
  175. callback.fail && callback.fail(err);
  176. },
  177. cancel: function(err) {
  178. callback.cancel && callback.cancel(err);
  179. },
  180. });
  181. });
  182. },
  183. };