sdk-h5-weixin.js 4.8 KB

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