getPosition.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import Vue from "vue";
  2. import { getTicket } from "@/api/index";
  3. import { toastLoading } from "@/utils/commonVant";
  4. import { CJ02BD, gcj02BD } from "@/utils/index";
  5. import { jsonp } from "vue-jsonp";
  6. // 微信JSSDK实例
  7. const wx = Vue.prototype.wx;
  8. // 腾讯位置服务 key
  9. const TxMapKey = "WLCBZ-HRM6L-YOMPV-ME62B-AQOG6-JUBW6";
  10. /**
  11. * 获取当前定位 调用之前确保当前页面已经授权(getTicketFun)
  12. * */
  13. export function getPosition() {
  14. return new Promise((resolve, reject) => {
  15. toastLoading(0, "定位中...", true);
  16. // 本地开发 test 环境时跳过获取定位功能 模拟定位
  17. if (process.env.NODE_ENV === "test") {
  18. let resData = {
  19. latitude: 34.615684509277344,
  20. longitude: 112.4474105834961,
  21. };
  22. localStorage.setItem("lat", resData.latitude);
  23. localStorage.setItem("lon", resData.longitude);
  24. // 定位坐标转换 腾讯转百度
  25. let TXisBD = CJ02BD(resData.latitude, resData.longitude);
  26. toastLoading().clear();
  27. resolve({ TXisBD, resData });
  28. } else {
  29. // let url = window.location.href;
  30. // // 获取签名
  31. // getTicket({ url: url }).then((response) => {
  32. // console.log(response);
  33. // toastLoading().clear();
  34. // if (response.code == 200) {
  35. // let qiyeData = response.data;
  36. // wx.config({
  37. // beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  38. // debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  39. // appId: qiyeData.appId, // 必填,企业微信的corpID
  40. // timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  41. // nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  42. // signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  43. // jsApiList: ["ready", "getLocation"], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  44. // });
  45. wx.ready(() => {
  46. wx.getLocation({
  47. type: "gcj02",
  48. success: (resData) => {
  49. toastLoading().clear();
  50. console.log("处理前");
  51. console.log(resData.latitude, resData.longitude);
  52. // 定位坐标转换 腾讯转百度
  53. let TXisBD = CJ02BD(resData.latitude, resData.longitude);
  54. console.log("处理后");
  55. console.log(TXisBD);
  56. localStorage.setItem("lat", resData.latitude);
  57. localStorage.setItem("lon", resData.longitude);
  58. resolve({ TXisBD, resData });
  59. },
  60. fail: () => {
  61. toastLoading().clear();
  62. reject("GPS未开启");
  63. },
  64. });
  65. });
  66. wx.error((err) => {
  67. toastLoading().clear();
  68. console.log(err);
  69. reject("定位失败,请开启企微定位权限");
  70. });
  71. // } else {
  72. // toastLoading().clear();
  73. // reject("获取签名失败");
  74. // }
  75. // });
  76. }
  77. });
  78. }
  79. /**
  80. *当前页面授权 一个url只需要授权一次
  81. */
  82. export function getTicketFun() {
  83. return new Promise((resolve, reject) => {
  84. // 当前页面
  85. let url = window.location.href;
  86. // 获取签名
  87. getTicket({ url: url }).then((response) => {
  88. console.log(response);
  89. toastLoading().clear();
  90. if (response.code == 200) {
  91. let qiyeData = response.data;
  92. wx.config({
  93. beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  94. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  95. appId: qiyeData.appId, // 必填,企业微信的corpID
  96. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  97. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  98. signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  99. jsApiList: ["ready", "getLocation"], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  100. });
  101. resolve("获取签名成功");
  102. } else {
  103. reject("获取签名失败");
  104. }
  105. });
  106. });
  107. }
  108. /**
  109. * 地点搜索 获取500米范围poi点
  110. * @param {*object} location //当前位置
  111. * @param {*number} radius //搜索半径
  112. * @param {*number} auto_extend //是否自动扩大范围 0 不扩大
  113. * @returns
  114. */
  115. export function getMapPoi(location, radius = 500, auto_extend = 0) {
  116. return new Promise((resolve, reject) => {
  117. let api =
  118. "https://apis.map.qq.com/ws/place/v1/search?page_size=10&page_index=1&orderby=_distance&output=jsonp";
  119. let boundary = `&boundary=nearby(${location.latitude},${location.longitude},${radius},${auto_extend})`;
  120. let key = `&key=${TxMapKey}`;
  121. jsonp(api + boundary + key)
  122. .then((res) => {
  123. console.log(res);
  124. resolve(res);
  125. })
  126. .catch((err) => {
  127. console.log(err);
  128. reject(err);
  129. });
  130. });
  131. }
  132. /**
  133. * 关键词搜索 在当前城市范围内搜索
  134. * @param {*object} location //当前位置
  135. * @param {*string} keywordValue //关键字
  136. * @param {*number} radius //搜索半径
  137. * @param {*number} auto_extend //是否自动扩大范围 0 不扩大
  138. * @returns
  139. */
  140. export function getkeywordPoi(location, keywordValue) {
  141. return new Promise((resolve, reject) => {
  142. let keyword = keywordValue ? "&keyword=" + encodeURI(keywordValue) : "";
  143. // 关键词搜索
  144. let api =
  145. "https://apis.map.qq.com/ws/place/v1/suggestion?output=jsonp&page_size=10&region_fix=1";
  146. let key = `&key=${TxMapKey}`;
  147. let locationPos = `&location=${location.latitude},${location.longitude}`;
  148. jsonp(api + locationPos + keyword + key)
  149. .then((res) => {
  150. console.log(keyword);
  151. console.log(res);
  152. resolve(res);
  153. })
  154. .catch((err) => {
  155. console.log(err);
  156. reject(err);
  157. });
  158. });
  159. }