TXApiFun.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. const isDevice = localStorage.getItem('isDevice');
  12. /**
  13. * 获取当前定位 调用之前确保当前页面已经授权(getTicketFun)
  14. * @param isPermit // PC 模式下不能获取定位,是否允许跳过定位
  15. * */
  16. export function getPosition(isPermit = false) {
  17. return new Promise((resolve, reject) => {
  18. toastLoading(0, '定位中...', true);
  19. // PC端没有定位权限依然可以访问
  20. if (process.env.NODE_ENV === 'test') {
  21. // 本地开发 test 环境时跳过获取定位功能 模拟定位
  22. let resData = {
  23. latitude: 34.615684509277344,
  24. longitude: 112.4474105834961,
  25. };
  26. // 定位坐标转换 腾讯转百度
  27. let TXisBD = CJ02BD(resData.latitude, resData.longitude);
  28. localStorage.setItem('lat', TXisBD.lat);
  29. localStorage.setItem('lon', TXisBD.lon);
  30. toastLoading().clear();
  31. resolve({ TXisBD: TXisBD, resData: resData });
  32. } else if (isDevice == 'PC') {
  33. // PC状态 分允许通过和不允许通过 isPermit: tru允许,false不允许
  34. localStorage.setItem('lat', '');
  35. localStorage.setItem('lon', '');
  36. toastLoading().clear();
  37. if (isPermit) {
  38. resolve({ TXisBD: { lat: '', lon: '' }, resData: { latitude: '', longitude: '' } });
  39. } else {
  40. reject('GPS未开启');
  41. }
  42. } else {
  43. wx.ready(() => {
  44. wx.getLocation({
  45. type: 'gcj02',
  46. success: (resData) => {
  47. console.log('success');
  48. toastLoading().clear();
  49. console.log('处理前');
  50. console.log(resData.latitude, resData.longitude);
  51. // 定位坐标转换 腾讯转百度
  52. let TXisBD = CJ02BD(resData.latitude, resData.longitude);
  53. console.log('处理后');
  54. console.log(TXisBD);
  55. localStorage.setItem('lat', TXisBD.lat);
  56. localStorage.setItem('lon', TXisBD.lon);
  57. resolve({ TXisBD: TXisBD, resData: resData });
  58. },
  59. fail: () => {
  60. console.log('fail');
  61. toastLoading().clear();
  62. reject('GPS未开启');
  63. },
  64. complete: () => {
  65. console.log('complete');
  66. toastLoading().clear();
  67. },
  68. });
  69. });
  70. wx.error((err) => {
  71. console.log('wx.error');
  72. toastLoading().clear();
  73. console.log(err);
  74. localStorage.setItem('lat', '');
  75. localStorage.setItem('lon', '');
  76. reject('定位失败,请开启企微定位权限');
  77. });
  78. }
  79. });
  80. }
  81. /**
  82. *当前页面授权 一个url只需要授权一次,wx.config 调用多次,只有第一次会调用成功,后面的都会走失败
  83. * @param {*object} jsApiList //授权接口列表
  84. * @param {*String} getLocation //获取定位
  85. * @param {*String} configType //注入类型
  86. * config,agentConfig
  87. * config注入的是企业的身份与权限,
  88. * 而agentConfig注入的是应用的身份与权限。
  89. * 尤其是当调用者为第三方服务商时,通过config无法准确区分出调用者是哪个第三方应用,
  90. * 而在部分场景下,又必须严谨区分出第三方应用的身份,此时即需要通过agentConfig来注入应用的身份信息。
  91. *
  92. */
  93. export function getTicketFun(jsApiList = ['getLocation'], configType = 'config') {
  94. return new Promise((resolve, reject) => {
  95. // 当前页面
  96. let url = window.location.href;
  97. let params = {
  98. url: url,
  99. };
  100. if (configType == 'agentConfig') {
  101. params.agent = 1;
  102. }
  103. // 获取签名
  104. getTicket(params).then((response) => {
  105. console.log(response);
  106. toastLoading().clear();
  107. if (response.code == 200) {
  108. let qiyeData = response.data;
  109. if (configType == 'config') {
  110. wx.config({
  111. beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  112. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  113. appId: qiyeData.appId, // 必填,企业微信的corpID
  114. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  115. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  116. signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
  117. jsApiList: ['ready', ...jsApiList], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  118. });
  119. console.log('获取签名成功');
  120. resolve('获取签名成功');
  121. } else if (configType == 'agentConfig') {
  122. wx.agentConfig({
  123. corpid: qiyeData.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
  124. agentid: qiyeData.agentId, // 必填,企业微信的应用id (e.g. 1000247)
  125. timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
  126. nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
  127. signature: qiyeData.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  128. jsApiList: [...jsApiList], //必填,传入需要使用的接口名称
  129. success: function (res) {
  130. console.log('获取签名成功');
  131. resolve(qiyeData);
  132. },
  133. fail: function (res) {
  134. console.log(res);
  135. if (res.errMsg.indexOf('function not exist') > -1) {
  136. alert('版本过低请升级');
  137. }
  138. },
  139. });
  140. }
  141. } else {
  142. console.log('获取签名失败');
  143. reject('获取签名失败');
  144. alert('获取签名失败');
  145. }
  146. });
  147. });
  148. }
  149. /**
  150. * 地点搜索 获取500米范围poi点
  151. * @param {*object} location //当前位置
  152. * @param {*number} radius //搜索半径
  153. * @param {*number} auto_extend //是否自动扩大范围 0 不扩大
  154. * @returns
  155. */
  156. export function getMapPoi(location, radius = 500, auto_extend = 0) {
  157. return new Promise((resolve, reject) => {
  158. let api =
  159. 'https://apis.map.qq.com/ws/place/v1/search?page_size=10&page_index=1&orderby=_distance&output=jsonp';
  160. let boundary = `&boundary=nearby(${location.latitude},${location.longitude},${radius},${auto_extend})`;
  161. let key = `&key=${TxMapKey}`;
  162. jsonp(api + boundary + key)
  163. .then((res) => {
  164. console.log(res);
  165. resolve(res);
  166. })
  167. .catch((err) => {
  168. console.log(err);
  169. reject(err);
  170. });
  171. });
  172. }
  173. /**
  174. * 关键词搜索 在当前城市范围内搜索
  175. * @param {*object} location //当前位置
  176. * @param {*string} keywordValue //关键字
  177. * @returns
  178. */
  179. export function getkeywordPoi(location, keywordValue) {
  180. return new Promise((resolve, reject) => {
  181. let keyword = keywordValue ? '&keyword=' + encodeURI(keywordValue) : '';
  182. // 关键词搜索
  183. let api =
  184. 'https://apis.map.qq.com/ws/place/v1/suggestion?output=jsonp&page_size=10&region_fix=1';
  185. let key = `&key=${TxMapKey}`;
  186. let locationPos = `&location=${location.latitude},${location.longitude}`;
  187. jsonp(api + locationPos + keyword + key)
  188. .then((res) => {
  189. console.log(keyword);
  190. console.log(res);
  191. resolve(res);
  192. })
  193. .catch((err) => {
  194. console.log(err);
  195. reject(err);
  196. });
  197. });
  198. }
  199. /**
  200. * 逆地址解析
  201. * @param {*object} location //当前位置
  202. * @returns
  203. */
  204. export function getGeocoder(location) {
  205. return new Promise((resolve, reject) => {
  206. let api = 'https://apis.map.qq.com/ws/geocoder/v1/?get_poi=0&output=jsonp';
  207. let key = `&key=${TxMapKey}`;
  208. let locationPos = `&location=${location.latitude},${location.longitude}`;
  209. jsonp(api + locationPos + key)
  210. .then((res) => {
  211. console.log(res);
  212. resolve(res);
  213. })
  214. .catch((err) => {
  215. console.log(err);
  216. reject(err);
  217. });
  218. });
  219. }