| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- import Vue from 'vue';
- import { getTicket } from '@/api/index';
- import { toastLoading } from '@/utils/commonVant';
- import { CJ02BD, gcj02BD } from '@/utils/index';
- import { jsonp } from 'vue-jsonp';
- // 微信JSSDK实例
- const wx = Vue.prototype.wx;
- // 腾讯位置服务 key
- const TxMapKey = 'WLCBZ-HRM6L-YOMPV-ME62B-AQOG6-JUBW6';
- // 当前设备
- const isDevice = localStorage.getItem('isDevice');
- /**
- * 获取当前定位 调用之前确保当前页面已经授权(getTicketFun)
- * @param isPermit // PC 模式下不能获取定位,是否允许跳过定位
- * */
- export function getPosition(isPermit = false) {
- return new Promise((resolve, reject) => {
- toastLoading(0, '定位中...', true);
- // PC端没有定位权限依然可以访问
- if (process.env.NODE_ENV === 'test') {
- // 本地开发 test 环境时跳过获取定位功能 模拟定位
- let resData = {
- latitude: 34.615684509277344,
- longitude: 112.4474105834961,
- };
- // 定位坐标转换 腾讯转百度
- let TXisBD = CJ02BD(resData.latitude, resData.longitude);
- localStorage.setItem('lat', TXisBD.lat);
- localStorage.setItem('lon', TXisBD.lon);
- toastLoading().clear();
- resolve({ TXisBD: TXisBD, resData: resData });
- } else if (isDevice == 'PC') {
- // PC状态 分允许通过和不允许通过 isPermit: tru允许,false不允许
- localStorage.setItem('lat', '');
- localStorage.setItem('lon', '');
- toastLoading().clear();
- if (isPermit) {
- resolve({ TXisBD: { lat: '', lon: '' }, resData: { latitude: '', longitude: '' } });
- } else {
- reject('GPS未开启');
- }
- } else {
- wx.ready(() => {
- wx.getLocation({
- type: 'gcj02',
- success: (resData) => {
- console.log('success');
- toastLoading().clear();
- console.log('处理前');
- console.log(resData.latitude, resData.longitude);
- // 定位坐标转换 腾讯转百度
- let TXisBD = CJ02BD(resData.latitude, resData.longitude);
- console.log('处理后');
- console.log(TXisBD);
- localStorage.setItem('lat', TXisBD.lat);
- localStorage.setItem('lon', TXisBD.lon);
- resolve({ TXisBD: TXisBD, resData: resData });
- },
- fail: () => {
- console.log('fail');
- toastLoading().clear();
- reject('GPS未开启');
- },
- complete: () => {
- console.log('complete');
- toastLoading().clear();
- },
- });
- });
- wx.error((err) => {
- console.log('wx.error');
- toastLoading().clear();
- console.log(err);
- localStorage.setItem('lat', '');
- localStorage.setItem('lon', '');
- reject('定位失败,请开启企微定位权限');
- });
- }
- });
- }
- /**
- *当前页面授权 一个url只需要授权一次,wx.config 调用多次,只有第一次会调用成功,后面的都会走失败
- * @param {*object} jsApiList //授权接口列表
- * @param {*String} getLocation //获取定位
- * @param {*String} configType //注入类型
- * config,agentConfig
- * config注入的是企业的身份与权限,
- * 而agentConfig注入的是应用的身份与权限。
- * 尤其是当调用者为第三方服务商时,通过config无法准确区分出调用者是哪个第三方应用,
- * 而在部分场景下,又必须严谨区分出第三方应用的身份,此时即需要通过agentConfig来注入应用的身份信息。
- *
- */
- export function getTicketFun(jsApiList = ['getLocation'], configType = 'config') {
- return new Promise((resolve, reject) => {
- // 当前页面
- let url = window.location.href;
- let params = {
- url: url,
- };
- if (configType == 'agentConfig') {
- params.agent = 1;
- }
- // 获取签名
- getTicket(params).then((response) => {
- console.log(response);
- toastLoading().clear();
- if (response.code == 200) {
- let qiyeData = response.data;
- if (configType == 'config') {
- wx.config({
- beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
- debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- appId: qiyeData.appId, // 必填,企业微信的corpID
- timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
- nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
- signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
- jsApiList: ['ready', ...jsApiList], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
- });
- console.log('获取签名成功');
- resolve('获取签名成功');
- } else if (configType == 'agentConfig') {
- wx.agentConfig({
- corpid: qiyeData.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
- agentid: qiyeData.agentId, // 必填,企业微信的应用id (e.g. 1000247)
- timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
- nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
- signature: qiyeData.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
- jsApiList: [...jsApiList], //必填,传入需要使用的接口名称
- success: function (res) {
- console.log('获取签名成功');
- resolve(qiyeData);
- },
- fail: function (res) {
- console.log(res);
- if (res.errMsg.indexOf('function not exist') > -1) {
- alert('版本过低请升级');
- }
- },
- });
- }
- } else {
- console.log('获取签名失败');
- reject('获取签名失败');
- alert('获取签名失败');
- }
- });
- });
- }
- /**
- * 地点搜索 获取500米范围poi点
- * @param {*object} location //当前位置
- * @param {*number} radius //搜索半径
- * @param {*number} auto_extend //是否自动扩大范围 0 不扩大
- * @returns
- */
- export function getMapPoi(location, radius = 500, auto_extend = 0) {
- return new Promise((resolve, reject) => {
- let api =
- 'https://apis.map.qq.com/ws/place/v1/search?page_size=10&page_index=1&orderby=_distance&output=jsonp';
- let boundary = `&boundary=nearby(${location.latitude},${location.longitude},${radius},${auto_extend})`;
- let key = `&key=${TxMapKey}`;
- jsonp(api + boundary + key)
- .then((res) => {
- console.log(res);
- resolve(res);
- })
- .catch((err) => {
- console.log(err);
- reject(err);
- });
- });
- }
- /**
- * 关键词搜索 在当前城市范围内搜索
- * @param {*object} location //当前位置
- * @param {*string} keywordValue //关键字
- * @returns
- */
- export function getkeywordPoi(location, keywordValue) {
- return new Promise((resolve, reject) => {
- let keyword = keywordValue ? '&keyword=' + encodeURI(keywordValue) : '';
- // 关键词搜索
- let api =
- 'https://apis.map.qq.com/ws/place/v1/suggestion?output=jsonp&page_size=10®ion_fix=1';
- let key = `&key=${TxMapKey}`;
- let locationPos = `&location=${location.latitude},${location.longitude}`;
- jsonp(api + locationPos + keyword + key)
- .then((res) => {
- console.log(keyword);
- console.log(res);
- resolve(res);
- })
- .catch((err) => {
- console.log(err);
- reject(err);
- });
- });
- }
- /**
- * 逆地址解析
- * @param {*object} location //当前位置
- * @returns
- */
- export function getGeocoder(location) {
- return new Promise((resolve, reject) => {
- let api = 'https://apis.map.qq.com/ws/geocoder/v1/?get_poi=0&output=jsonp';
- let key = `&key=${TxMapKey}`;
- let locationPos = `&location=${location.latitude},${location.longitude}`;
- jsonp(api + locationPos + key)
- .then((res) => {
- console.log(res);
- resolve(res);
- })
- .catch((err) => {
- console.log(err);
- reject(err);
- });
- });
- }
|