sdk1.js 4.6 KB

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