Commit b7575e93 authored by 任国军's avatar 任国军

add course lbs

parent 6c28df54
Pipeline #13924 passed with stage
in 11 seconds
'use strict';
const Controller = require('egg').Controller;
class TestController extends Controller {
async test() {
const { service, ctx } = this;
const from = { lng: 120.069206, lat: 30.291121 };
const to = { lng: 120.212997, lat: 30.29133 };
const ret = await service.course.lbs.getLBSDistance('driving', from, [ to ]);
ctx.success(ret);
}
}
module.exports = TestController;
...@@ -4,4 +4,5 @@ module.exports = app => { ...@@ -4,4 +4,5 @@ module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/common'); const router = app.router.namespace(app.config.projectRootPath + '/common');
router.get('third_oss_signature', '/oss/signature', 'common.oss.signature');// 业务的 oss 签名 router.get('third_oss_signature', '/oss/signature', 'common.oss.signature');// 业务的 oss 签名
router.get('third_test', '/test', 'common.test.test');
}; };
'use strict';
const Service = require('egg').Service;
class LbsService extends Service {
// 计算两个经纬度之间的距离,用Haversine公式,单位:m
async getDistance(from, to) {
const rad_from = from.lat * Math.PI / 180.0;
const rad_to = to.lat * Math.PI / 180.0;
const sub_lat = rad_from - rad_to;
const sub_lng = from.lng * Math.PI / 180.0 - to.lng * Math.PI / 180.0;
const r = 6378137; // 地球半径
const distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(sub_lat / 2), 2) + Math.cos(rad_from) * Math.cos(rad_to) * Math.pow(Math.sin(sub_lng / 2), 2)));
return distance;
}
// 腾讯位置服务WebService API,距离计算(一对多)
// 返回示例:
// {
// "results": [
// {
// "from": {
// "lat": 39.983171,
// "lng": 116.308479
// },
// "to": {
// "lat": 39.996059,
// "lng": 116.353454
// },
// "distance": 5354.7,
// "duration": 1678
// },
// {
// "from": {
// "lat": 39.983171,
// "lng": 116.308479
// },
// "to": {
// "lat": 39.949226,
// "lng": 116.394309
// },
// "distance": 10495.8, // 起点到终点的距离,单位:米。如果radius半径过小或者无法搜索到,则返回-1
// "duration": 2635 // 表示从起点到终点的结合路况的时间,秒为单位。 注:步行方式不计算耗时,该值始终为0
// }
// ]
// }
async getLBSDistance(mode = 'driving', from, toArr) {
const { ctx } = this;
const to = [];
let result = [];
for (const v of toArr) {
to.push(String(v.lat) + ',' + String(v.lng));
}
const params = {
mode, // driving, walking;默认driving
from: String(from.lat) + ',' + String(from.lng),
to: to.join(';'),
key: ctx.app.config.TX_LBS_KEY,
};
const resp = await ctx.helper.send_request(ctx.app.config.TX_LBS_DISTANCE_URL, params, { method: 'GET' });
// resp.data.status 状态码,0:正常,310:请求参数信息有误,311:Key格式错误,306:请求有护持信息请检查字符串,110:请求来源未被授权
ctx.logger.info('tx_lbs_distance_params: ' + JSON.stringify(params));
ctx.logger.info('tx_lbs_distance_resp: ' + JSON.stringify(resp));
if (resp.status === 200) {
// 判断响应是否正确
if (resp.data.status === 0) {
result = resp.data.result.elements;
}
}
return { results: result };
}
// 地址解析
async getLBSAddress(address) {
const { ctx } = this;
let result = {};
const params = {
address,
key: ctx.app.config.TX_LBS_KEY,
};
const resp = await ctx.helper.send_request(ctx.app.config.TX_LBS_ADDRESS_URL, params, { method: 'GET' });
// resp.data.status 状态码,0:正常,310:请求参数信息有误,311:Key格式错误,306:请求有护持信息请检查字符串,110:请求来源未被授权
ctx.logger.info('tx_lbs_address_resp: ' + JSON.stringify(resp));
if (resp.status === 200) {
// 判断响应是否正确
if (resp.data.status === 0) {
result = resp.data.result;
}
}
return { result };
}
}
module.exports = LbsService;
...@@ -133,17 +133,20 @@ module.exports = appInfo => { ...@@ -133,17 +133,20 @@ module.exports = appInfo => {
config.OUT_P_NODE_URL = 'https://dev-nginx.jianbing.com'; config.OUT_P_NODE_URL = 'https://dev-nginx.jianbing.com';
//数盒魔方 // 数盒魔方
config.SHUHEMOFANG_APP_KEY = 'BdNFbog6'; config.SHUHEMOFANG_APP_KEY = 'BdNFbog6';
config.SHUHEMOFANG_APP_SECRET = 'a695e9b3c0f1e3f15fb0b958fd3a4b67'; config.SHUHEMOFANG_APP_SECRET = 'a695e9b3c0f1e3f15fb0b958fd3a4b67';
config.SHUHEMOFANG_URL = 'https://api.soohaid.com:8211/api/mobile/factor3s'; config.SHUHEMOFANG_URL = 'https://api.soohaid.com:8211/api/mobile/factor3s';
//微信支付相关内容 // 微信支付相关内容
config.MCH_APPID = 'wx0369cb31f505d8ce';//商户关联的APPID config.MCH_APPID = 'wx0369cb31f505d8ce';// 商户关联的APPID
config.MCH_ID = '1516742591';//商户id config.MCH_ID = '1516742591';// 商户id
config.MCH_KEY = 'xiXigu4008635151jianbing6floor12';//商户秘钥 config.MCH_KEY = 'xiXigu4008635151jianbing6floor12';// 商户秘钥
// 腾讯位置服务
config.TX_LBS_KEY = 'TNHBZ-A7C36-5DISQ-EM2JH-332V5-PGFW5';
config.TX_LBS_DISTANCE_URL = 'https://apis.map.qq.com/ws/distance/v1/'; // 距离计算(一对多)
config.TX_LBS_ADDRESS_URL = 'https://apis.map.qq.com/ws/geocoder/v1/'; // 地址解析;逆地址解析
return config; return config;
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment