wxMap.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template />
  2. <script>
  3. import wx from 'weixin-js-sdk'
  4. import { getSignature, getLonAndLat } from "@/api/wechat";
  5. export default {
  6. name: 'WxSignature',
  7. data() {
  8. return {};
  9. },
  10. created() {},
  11. mounted() {},
  12. methods: {
  13. //jsSDK 签名验证
  14. getJsApiSign(mapParam){
  15. let reqData = {}
  16. var system = this.isIosOrAndroid()
  17. if (system == 1) {
  18. // 安卓
  19. reqData.url = encodeURI(window.location.href.split('#')[0]);
  20. } else if (system == 2) {
  21. // iOS
  22. // let url = window.location.href.split('smartParkH5')[0]+"smartParkH5/home";
  23. reqData.url = encodeURI(window.entryUrl?window.entryUrl:window.location.href.split('#')[0])
  24. }
  25. getSignature(reqData).then((res) => {
  26. if(res.key == 200){
  27. wx.config({
  28. "debug": false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  29. "appId": 'wx7302e8b315a9a6f3', // 必填,公众号的唯一标识
  30. "timestamp": res.data.timestamp, // 必填,生成签名的时间戳
  31. "nonceStr": res.data.noncestr, // 必填,生成签名的随机串
  32. "signature": res.data.signature, // 必填,签名,见附录1
  33. "jsApiList": ['openLocation'], // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  34. });
  35. //通过ready接口处理成功验证
  36. wx.ready(function () {
  37. //导航
  38. wx.openLocation({
  39. latitude: mapParam.latitude, // 纬度,浮点数,范围为90 ~ -90
  40. longitude: mapParam.longitude, // 经度,浮点数,范围为180 ~ -180。
  41. name: mapParam.name, // 位置名
  42. address: mapParam.address, // 地址详情说明
  43. scale: mapParam.scale, // 地图缩放级别,整型值,范围从1~28。默认为最大
  44. infoUrl: mapParam.infoUrl // 在查看位置界面底部显示的超链接,可点击跳转
  45. });
  46. })
  47. wx.error(function(res){
  48. });
  49. }
  50. });
  51. },
  52. // 导航
  53. navigation(address){
  54. let data = {
  55. address : address
  56. }
  57. getLonAndLat(data).then((res) => {
  58. if(res.key == 200){
  59. if(res.data.status == 0){
  60. let mapParam = {
  61. name: res.data.result.title,
  62. latitude: res.data.result.location.lat,
  63. longitude: res.data.result.location.lng,
  64. scale: 28,
  65. address: data.address,
  66. infoUrl: '#'
  67. }
  68. this.getJsApiSign(mapParam);
  69. }
  70. }
  71. });
  72. },
  73. //判断是iOS还是android
  74. isIosOrAndroid() {
  75. var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1; //android终端
  76. var isIos = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  77. if(isAndroid){
  78. // 相关逻辑代码
  79. return 1
  80. }
  81. if(isIos){
  82. // 相关逻辑代码
  83. return 2
  84. }
  85. return 0;
  86. }
  87. },
  88. };
  89. </script>