更新:消费者查看商家发布的优惠卷列表接口
parent
a93dc86036
commit
c5429ca5bc
|
@ -683,4 +683,87 @@ if (!function_exists('generateDefaultNickName')) {
|
|||
{
|
||||
return '用户'.generateRand('6', 'mix');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据当前坐标和范围距离计算方形范围内的经纬度坐标取值范围
|
||||
* $curLat double类型 当前坐标纬度
|
||||
* $curLng double类型 当前坐标经度
|
||||
* $distance double类型 查询范围(单位km),当前坐标所在园的半径,与此范围正方形内切
|
||||
*
|
||||
* 相关数学知识:半正矢公式(Haversine公式)、正/反弦函数和正/反切函数等三角函数
|
||||
* 平均地球半径:6371km
|
||||
* 经度范围:-180 ~ 180 (注意差值180度限制时的计算)
|
||||
* 纬度范围:-90 ~ 90
|
||||
*
|
||||
*/
|
||||
if (!function_exists('getEarthSquareRangePoint')) {
|
||||
function getEarthSquareRangePoint($curLat, $curLng, $distance)
|
||||
{
|
||||
$list = [];
|
||||
$earthRadius = 6371; // 地球平均半径 6371km
|
||||
$disLat = rad2deg($distance / $earthRadius);
|
||||
$disLng = rad2deg(2 * asin(sin($distance / (2 * $earthRadius) / cos(deg2rad($curLat)))));
|
||||
$disLat = abs($disLat);
|
||||
$disLng = abs($disLng);
|
||||
|
||||
$list['cur_lat'] = $curLat;
|
||||
$list['cur_lng'] = $curLng;
|
||||
$list['dis_lat'] = $disLat;
|
||||
$list['dis_lng'] = $disLng;
|
||||
|
||||
$latTop = $curLat + $disLat;
|
||||
$latBottom = $curLat - $disLat;
|
||||
$lngLeft = $curLng - $disLng;
|
||||
$lngRight = $curLng + $disLng;
|
||||
|
||||
|
||||
$checkLatTopRem = abs(fmod(floatval($latTop), 180));
|
||||
$checkLatTop = abs(fmod(intval($latTop / 90), 4));
|
||||
if ($latTop >= 0) {
|
||||
$latTop = floatval($checkLatTopRem > 90 ? (180 - $checkLatTopRem) : $checkLatTopRem);
|
||||
$latTop = $checkLatTop >= 2 ? (-$latTop) : $latTop; // 南半球为负数
|
||||
} else {
|
||||
$latTop = floatval($checkLatTopRem > 90 ? (180 - $checkLatTopRem) : $checkLatTopRem);
|
||||
$latTop = $checkLatTop < 2 ? (-$latTop) : $latTop; // 南半球为负数
|
||||
}
|
||||
|
||||
$checkLatBottomRem = abs(fmod(floatval($latBottom) ,180));
|
||||
$checkLatBottom = abs(intval($latBottom / 90) % 4);
|
||||
if ($latBottom >= 0) {
|
||||
$latBottom = floatval($checkLatBottomRem > 90 ? (180 - $checkLatBottomRem) : $checkLatBottomRem);
|
||||
$latBottom = $checkLatBottom >= 2 ? (-$latBottom) : $latBottom; // 南半球为负数
|
||||
} else {
|
||||
$latBottom = floatval($checkLatBottomRem > 90 ? (180 - $checkLatBottomRem) : $checkLatBottomRem);
|
||||
$latBottom = $checkLatBottom < 2 ? (-$latBottom) : $latBottom; // 南半球为负数
|
||||
}
|
||||
|
||||
$list['lat_min'] = min([$latTop, $curLat, $latBottom]);
|
||||
$list['lat_max'] = max([$latTop, $curLat, $latBottom]);
|
||||
|
||||
|
||||
$checkLngLeftRem = abs(fmod(floatval($lngLeft), 360));
|
||||
if ($lngLeft >= 0) {
|
||||
$lngLeft = floatval($checkLngLeftRem > 180 ? -(360 - $checkLngLeftRem) : $checkLngLeftRem); // 西半球为负数
|
||||
} else {
|
||||
$lngLeft = floatval($checkLngLeftRem > 180 ? (360 - $checkLngLeftRem) : (-$checkLngLeftRem)); // 西半球为负数
|
||||
}
|
||||
|
||||
$checkLngRightRem = abs(fmod(floatval($lngRight), 360));
|
||||
if ($lngRight >= 0) {
|
||||
$lngRight = floatval($checkLngRightRem > 180 ? -(360 - $checkLngRightRem) : $checkLngRightRem); // 西半球为负数
|
||||
} else {
|
||||
$lngRight = floatval($checkLngRightRem > 180 ? (360 - $checkLngRightRem) : (-$checkLngRightRem)); // 西半球为负数
|
||||
}
|
||||
|
||||
$list['lng_min'] = min([$lngLeft, $curLng, $lngRight]);
|
||||
$list['lng_max'] = max([$lngLeft, $curLng, $lngRight]);
|
||||
|
||||
$list['lat_top'] = $latTop;
|
||||
$list['lat_bottom'] = $latBottom;
|
||||
$list['lng_left'] = $lngLeft;
|
||||
$list['lng_right'] = $lngRight;
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
namespace app\controller\api;
|
||||
|
||||
/**
|
||||
* 商家端:商家
|
||||
*
|
||||
* Class Business
|
||||
* @package app\controller\api
|
||||
*/
|
||||
class Business extends Base
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
namespace app\controller\api;
|
||||
|
||||
use app\model\CouponMain;
|
||||
use app\repository\CouponRepository;
|
||||
use think\Collection;
|
||||
|
||||
/**
|
||||
* 消费者端:游客、普通用户
|
||||
*
|
||||
* Class Consumer
|
||||
* @package app\controller\api
|
||||
*/
|
||||
class Consumer extends Base
|
||||
{
|
||||
protected $noNeedLogin = [
|
||||
'home',
|
||||
];
|
||||
|
||||
public function home()
|
||||
{
|
||||
$params = [
|
||||
'businessType' => $this->request->param('businessType/d', 0), // 商家类型
|
||||
'couponType' => $this->request->param('couponType/d', 0), // 优惠卷类型
|
||||
'dis' => $this->request->param('dis/d', -1), // 距离,单位为km
|
||||
'lng' => $this->request->param('lng/f', 0), // 经度 104.752890
|
||||
'lat' => $this->request->param('lat/f', 0), // 纬度 31.465040
|
||||
'keyword' => $this->request->param('key/s', ''), // 关键词查询
|
||||
'page' => $this->request->param('page/d', 1),
|
||||
'size' => $this->request->param('size/d', 10),
|
||||
];
|
||||
|
||||
|
||||
try {
|
||||
$repo = CouponRepository::getInstance();
|
||||
$nowDate = date('Y-m-d H:i:s');
|
||||
$sumPoint = $params['lng'] + $params['lat'];
|
||||
|
||||
$lngRange = [];
|
||||
$latRange = [];
|
||||
$whereMap = [];
|
||||
$sortOrder = ['dislatlng'=> 'asc', 'start_time'=>'asc'];
|
||||
|
||||
$whereMap[] = ['status', '=', CouponMain::status_on];
|
||||
$whereMap[] = ['on_shelf', '=', CouponMain::on_shelf_on];
|
||||
$whereMap[] = ['start_time', '> TIME', $nowDate];
|
||||
$whereMap[] = ['end_time', '< TIME', $nowDate];
|
||||
$whereMap[] = ['using_count', '>', 0];
|
||||
if (!empty($params['keyword'])) {
|
||||
$whereMap[] = ['name', 'like', "%{$params['keyword']}%"];
|
||||
}
|
||||
if ($params['businessType'] > 0) {
|
||||
$whereMap[] = ['business_type', '=', $params['businessType']];
|
||||
}
|
||||
if ($params['couponType'] > 0) {
|
||||
$whereMap[] = ['type', '=', $params['couponType']];
|
||||
}
|
||||
|
||||
if ($params['dis'] > 0) {
|
||||
$pointList = getEarthSquareRangePoint($params['lat'],$params['lng'], $params['dis']);
|
||||
$latRange[] = ['lat', 'BETWEEN', [$pointList['lat_min'], $pointList['lat_max']]];
|
||||
if (abs($pointList['cur_lng'] - $pointList['lng_min']) != $pointList['dis_lng'] && $pointList['lng_min'] < 0) {
|
||||
$lngRange[] = ['lng', 'BETWEEN' , [-180, $pointList['lng_min']]];
|
||||
$lngRange[] = ['lng', 'BETWEEN' , [($pointList['cur_lng'] - $pointList['dis_lng']), 180]];
|
||||
} else {
|
||||
$lngRange[] = ['lng', 'BETWEEN' , [$pointList['lng_min'], $pointList['lng_max']]];
|
||||
}
|
||||
}
|
||||
|
||||
$res = $repo->findCouponMainList($whereMap, [], $params['page'], $params['size'], function ($q) use($lngRange, $latRange, $sumPoint) {
|
||||
return $q->when(!empty($lngRange) && !empty($latRange), function($query) use($lngRange, $latRange){
|
||||
$query->where(function ($queryB) use($lngRange) {
|
||||
$queryB->whereOr($lngRange);
|
||||
})->where($latRange);
|
||||
})
|
||||
->field("*, abs(IFNULL(lat,0) + IFNULL(lng,0) - {$sumPoint})as dislatlng");
|
||||
}, $sortOrder);
|
||||
|
||||
return $this->json(0, 'success', $res);
|
||||
} catch (\Exception $e) {
|
||||
return $this->json(0, 'success', [
|
||||
"total" => 0,
|
||||
"current" => $params['page'],
|
||||
"size" => $params['size'],
|
||||
"list" => new Collection()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -117,4 +117,9 @@ class User extends Base
|
|||
}
|
||||
|
||||
|
||||
public function test()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,7 @@ namespace app\repository;
|
|||
use app\exception\RepositoryException;
|
||||
use app\model\CouponMain;
|
||||
use app\service\Repository;
|
||||
use Exception;
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
|
@ -29,4 +30,21 @@ class CouponRepository extends Repository
|
|||
return CouponMain::where($where);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询商家发布的优惠卷列表
|
||||
*
|
||||
* @param array $where
|
||||
* @param array $fields
|
||||
* @param int $page
|
||||
* @param int $size
|
||||
* @param callable|null $call
|
||||
* @param array $sortOrder
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function findCouponMainList(array $where, array $fields = [], int $page=1, int $size = 0, callable $call=null, array $sortOrder= []): array
|
||||
{
|
||||
return CouponMain::findList($where, $fields, $page, $size, $call, $sortOrder);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue