sdk.js 4.5 KB

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