sdk.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. console.log('url', url);
  18. http("wechat.wechatJssdk", {
  19. uri: url
  20. }).then(res => {
  21. console.log(res);
  22. jweixin.config({
  23. debug: res.data.debug,
  24. appId: res.data.appId,
  25. timestamp: res.data.timestamp,
  26. nonceStr: res.data.nonceStr,
  27. signature: res.data.signature,
  28. jsApiList: res.data.jsApiList,
  29. openTagList: res.data.openTagList
  30. });
  31. if (callback) {
  32. callback(res.data);
  33. }
  34. });
  35. },
  36. // 在需要已授权订阅的页面调用
  37. authSubscribe(callback) {
  38. this.isWechat() && this.initJssdk(function(res) {
  39. jweixin.ready(function() {
  40. uni.showToast({
  41. title: '1213122425'
  42. })
  43. // 调用wx.requestSubscribeMessage方法请求用户授权消息订阅
  44. jweixin.requestSubscribeMessage({
  45. success: function(res) {
  46. if (res.subscribe) {
  47. // 用户已同意授权消息订阅
  48. alert('用户已同意授权消息订阅');
  49. } else {
  50. // 用户不同意授权消息订阅
  51. alert('用户不同意授权消息订阅');
  52. }
  53. },
  54. fail: function(err) {
  55. console.log('请求授权消息订阅失败:', err);
  56. }
  57. });
  58. });
  59. });
  60. },
  61. //在需要定位页面调用
  62. getLocation(callback) {
  63. this.isWechat() && this.initJssdk(function(res) {
  64. jweixin.ready(function() {
  65. jweixin.getLocation({
  66. type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  67. success: function(res) {
  68. callback(res);
  69. },
  70. fail: function(res) {
  71. console.log("%c微信H5sdk,getLocation失败:",
  72. "color:green;background:yellow");
  73. },
  74. });
  75. });
  76. });
  77. },
  78. //获取微信收货地址
  79. openAddress(callback) {
  80. this.isWechat() && this.initJssdk(function(res) {
  81. jweixin.ready(function() {
  82. jweixin.openAddress({
  83. success: function(res) {
  84. callback(res);
  85. },
  86. fail: function(err) {
  87. console.log("%c微信H5sdk,openAddress失败:",
  88. "color:green;background:yellow");
  89. },
  90. complete: function(msg) {}
  91. });
  92. });
  93. });
  94. },
  95. // 微信扫码
  96. scanQRCode(callback) {
  97. this.isWechat() && this.initJssdk(function(res) {
  98. jweixin.ready(function() {
  99. jweixin.scanQRCode({
  100. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  101. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  102. success: function(res) {
  103. callback(res);
  104. },
  105. fail: function(res) {
  106. console.log("%c微信H5sdk,scanQRCode失败:",
  107. "color:green;background:yellow");
  108. },
  109. });
  110. });
  111. });
  112. },
  113. // 微信分享
  114. share(data, callback) {
  115. // console.log('1',data);
  116. this.isWechat() && this.initJssdk(function(res) {
  117. jweixin.ready(function() {
  118. // console.log(data);
  119. var shareData = {
  120. title: data.title,
  121. desc: data.desc,
  122. link: data.path,
  123. imgUrl: data.image,
  124. success: function(res) {
  125. console.log('分享成功ll');
  126. // callback(res);
  127. // 分享后的一些操作,比如分享统计等等
  128. },
  129. cancel: function(res) {
  130. // console.log(res);
  131. }
  132. };
  133. jweixin.updateAppMessageShareData(shareData); //新版接口
  134. //分享到朋友圈接口
  135. jweixin.updateTimelineShareData(shareData);
  136. });
  137. });
  138. },
  139. // 打开坐标位置
  140. openLocation(data, callback) { //打开位置
  141. this.isWechat() && this.initJssdk(function(res) {
  142. jweixin.ready(function() {
  143. jweixin.openLocation({ //根据传入的坐标打开地图
  144. latitude: data.latitude,
  145. longitude: data.longitude
  146. });
  147. });
  148. });
  149. },
  150. // 选择图片
  151. chooseImage(callback) { //选择图片
  152. this.isWechat() && this.initJssdk(function(res) {
  153. jweixin.ready(function() {
  154. jweixin.chooseImage({
  155. count: 1,
  156. sizeType: ["compressed"],
  157. sourceType: ["album"],
  158. success: function(rs) {
  159. callback(rs);
  160. }
  161. });
  162. });
  163. });
  164. },
  165. //微信支付
  166. wxpay(data, callback) {
  167. let that = this;
  168. this.isWechat() && this.initJssdk(function(res) {
  169. jweixin.ready(function() {
  170. jweixin.chooseWXPay({
  171. timestamp: data
  172. .timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  173. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  174. package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  175. signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  176. paySign: data.paySign, // 支付签名
  177. success: function(res) {
  178. callback(res);
  179. },
  180. fail: function(res) {
  181. console.log("%c微信H5sdk,chooseWXPay失败:",
  182. "color:green;background:yellow");
  183. callback(res);
  184. },
  185. cancel: function(res) {
  186. },
  187. });
  188. });
  189. });
  190. }
  191. };