调整优惠券列表获取

master
wangxinglong 2021-12-02 18:34:44 +08:00
parent 08e392494a
commit 347de4f5a5
21 changed files with 404 additions and 61 deletions

View File

@ -779,3 +779,21 @@ if (!function_exists('getEarthSquareRangePoint')) {
return $list;
}
}
if (!function_exists('get_distance')) {
function get_distance($lat1, $lng1, $lat2, $lng2)
{
$earthRadius = 6367000;
$lat1 = ($lat1 * pi()) / 180;
$lng1 = ($lng1 * pi()) / 180;
$lat2 = ($lat2 * pi()) / 180;
$lng2 = ($lng2 * pi()) / 180;
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;
return round($calculatedDistance);
}
}

View File

@ -12,12 +12,9 @@ use app\model\Area as AreaModel;
*/
class Area extends Base
{
protected $middleware = [];
protected function initialize()
{
}
protected $noNeedLogin = [
'index',
];
/**
* 根据上级行政代码 获取下级

View File

@ -2,6 +2,8 @@
namespace app\controller\api;
use app\exception\RepositoryException;
use app\model\BusinessCircle;
use app\model\Category;
use app\repository\AccountRepository;
use app\repository\BusinessRepository;
use app\repository\DictionaryRepository;
@ -220,5 +222,20 @@ class Business extends Base
}
}
/**
* 获取商家类型
* */
public function getBusinessTypeList()
{
$data = Category::getList();
return $this->json(1,"ok",$data);
}
/**
* 获取商家类型
* */
public function getBusinessCircle()
{
$data = BusinessCircle::getList();
return $this->json(1,"ok",$data);
}
}

View File

@ -1,22 +1,80 @@
<?php
namespace app\controller\api;
use app\model\CouponMain;
use app\exception\RepositoryException;
use app\model\Comment as CommentModel;
use app\repository\AccountRepository;
use app\repository\CouponRepository;
use think\Collection;
use think\response\Json;
use think\Exception;
use think\exception\ValidateException;
/**
* 评论相关
*
* Class Consumer
* Class Comment
* @package app\controller\api
*/
class Comment extends Base
{
protected $noNeedLogin = [
'',
'commentList',
];
/**
* 群聊列表 开发中
* */
public function commentList()
{
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 30);
$whereMap = [
["is_delete", "=", CommentModel::COMMON_OFF],//未删除
["state", "=", CommentModel::COMMON_ON],//审核通过
];
$data = CommentModel::findList($whereMap, [], $page, $size, null, ["id" => "desc"]);
return $this->json(0, "success", $data);
}
/**
* 创建一条评论 开发中
* */
public function createComment()
{
$accountId = $this->request->user['user_id'] ?? 0;
$accountRepo = AccountRepository::getInstance();
try {
$account = $accountRepo->findById($accountId, [], function ($q) {
return $q->with(['business', 'parent']);
});
if (empty($account)) {
throw new ValidateException('用户无效!');
}
$param = [
"comment" => input("comment/s", ""),//评论内容 图片类型放入地址 语音类型放置语音文件地址
"user_code" => $account['user_code'],
"create_time" => date("Y-m-d H:i:s"),
"url" => input("url/s", ""),//图片地址 仅图片评论才有
"state" => CommentModel::state_default,
"type" => input("type/s"),//评论类型
"lng" => input("lng/s"),//经度
"lat" => input("lat/s"),//纬度
"location" => input("location/s"),
];
CommentModel::create($param);
return $this->json();
} catch (ValidateException $e) {
return $this->json(4001, $e->getError());
} catch (RepositoryException $e) {
return $this->json(4001, $e->getError());
} catch (Exception $e) {
return $this->json(5001, '服务器繁忙!获取用户个人信息失败');
}
}
}

View File

@ -2,7 +2,9 @@
namespace app\controller\api;
use app\model\CouponMain;
use app\model\Slide;
use app\repository\CouponRepository;
use app\repository\OperationRepository;
use think\Collection;
use think\response\Json;
@ -16,6 +18,7 @@ class Consumer extends Base
{
protected $noNeedLogin = [
'home',
'bannerList',
];
/**
@ -44,13 +47,15 @@ class Consumer extends Base
$lngRange = [];
$latRange = [];
$whereMap = [];
$sortOrder = ['dislatlng'=> 'asc', 'start_time'=>'asc'];
$sortOrder = ['square'=> '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];
$whereMap[] = ['id', '>', 300];
$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']}%"];
}
@ -72,15 +77,27 @@ class Consumer extends Base
}
}
$res = $repo->findCouponMainList($whereMap, [], $params['page'], $params['size'], function ($q) use($lngRange, $latRange, $sumPoint) {
$res = $repo->findCouponMainList($whereMap, [], $params['page'], $params['size'], function ($q) use($lngRange, $latRange, $params) {
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");
->field("*, abs( (IFNULL(lat,0) - {$params['lat']}) * (IFNULL(lng,0) - {$params['lng']}) ) as square");
}, $sortOrder);
$res['list']->each(function ($item)use($params){
unset($item->square);
$distance = (get_distance($params["lat"], $params["lng"], $item["lat"],$item["lng"]));
if ($distance >= 1000) {
$item->distance_text = round($distance / 1000, 2) . "km";
} else {
$item->distance_text = $distance . "m";
}
});
return $this->json(0, 'success', $res);
} catch (\Exception $e) {
return $this->json(0, 'success', [
@ -93,4 +110,22 @@ class Consumer extends Base
}
/**
* banner列表
* */
public function bannerList()
{
$repo = OperationRepository::getInstance();
$whereMap = [];
$orders = ['sort'=>'asc'];
$page = input("page/d",1);
$size = input("size/d",1000);
$whereMap[] = ['position', '=', Slide::homePosition];
$list = $repo->slideList($whereMap, ["id","title","src as image","url","url_type as type"], $page, $size, function ($q){
return $q->withAttr("image",function ($value){
return $this->request->domain() . $value;
});
}, $orders);
return $this->json(1,"ok",$list["list"]);
}
}

View File

@ -204,4 +204,15 @@ class User extends Base
}
}
/**
* 关注商家的列表
* */
public function businessFlowList()
{
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 30);
$data = AccountRepository::getInstance()->getBusinessFlowList();
}
}

View File

@ -9,6 +9,7 @@ use app\model\Member;
use app\repository\BusinessRepository;
use app\repository\CouponRepository;
use app\validate\CouponRelease;
use app\validate\CouponUsingRule;
use Exception;
use think\facade\Db;
use think\response\Json;
@ -184,19 +185,26 @@ class Coupon extends Base
{
if ($this->request->isPost()) {
$data = input("item/a", []);
$usingRule = input("using_rule/a", []);
$validate = new CouponRelease();
if (!$validate->check($data)) {
return $this->json(4001, $validate->getError());
}
$usingRuleValidate = new CouponUsingRule();
if (!$usingRuleValidate->check($usingRule)) {
return $this->json(4001, $usingRuleValidate->getError());
}
$business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true);
if (empty($business)) {
return $this->json(4001, '商家不存在');
}
$data['business_type'] = $business->business['type'];
$data['business_name'] = $business->business['business_name'];
$data['lng'] = $business->business['lng'];
$data['lat'] = $business->business['lat'];
$data['business_type'] = $business['type'];
$data['business_name'] = $business['business_name'];
$data['lng'] = $business['lng'];
$data['lat'] = $business['lat'];
$type = CouponRepository::getInstance()->getCouponTypeAll();
@ -217,7 +225,7 @@ class Coupon extends Base
$data['money'] = floor($data['money'] * 100) / 100;
$totalMoney = $data['money'] * $data['count'];
if ($business->business["balance"] < $totalMoney) {
if ($business["balance"] < $totalMoney) {
return $this->json(4001, '商家余额不足');
}
@ -226,9 +234,10 @@ class Coupon extends Base
$data['create_time'] = $date;
$data['update_time'] = $date;
Db::startTrans();
try {
CouponRepository::getInstance()->releaseCouponMain($data, $totalMoney);
CouponRepository::getInstance()->releaseCouponMain($data, $totalMoney, $usingRule);
Db::commit();
return $this->json();
} catch (RepositoryException $e) {
@ -257,10 +266,12 @@ class Coupon extends Base
public function edit()
{
$couponMain = CouponMain::findById(input("id/d", 0));
$couponMain = CouponMain::findOne(["id" => input("id/d", 0)], [], function ($q) {
return $q->with("usingRule");
});
if ($this->request->isPost()) {
$data = input("item/a", []);
$usingRule = input("using_rule/a", []);
if (empty($couponMain)) {
return $this->json(4001, "优惠券不存在");
}
@ -268,16 +279,22 @@ class Coupon extends Base
if (!$validate->scene("edit")->check($data)) {
return $this->json(4001, $validate->getError());
}
$usingRuleValidate = new CouponUsingRule();
if (!$usingRuleValidate->check($usingRule)) {
return $this->json(4001, $usingRuleValidate->getError());
}
$business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true);
if (empty($business)) {
return $this->json(4001, '商家不存在');
}
//更新经纬度
$data['business_type'] = $business->business['type'];
$data['business_name'] = $business->business['business_name'];
$data['lng'] = $business->business['lng'];
$data['lat'] = $business->business['lat'];
$data['business_type'] = $business['type'];
$data['business_name'] = $business['business_name'];
$data['lng'] = $business['lng'];
$data['lat'] = $business['lat'];
$data['commission_agency'] = input("item.commission_agency/d", 0);
@ -293,20 +310,21 @@ class Coupon extends Base
Db::startTrans();
try {
$couponMain->save($data);
$couponMain->usingRule->save($usingRule);
Db::commit();
return $this->json();
} catch (RepositoryException $e) {
Db::rollback();
return $this->json(5001, "发布失败" . $e->getMessage());
} catch (\think\Exception $e) {
} catch (Exception $e) {
Db::rollback();
return $this->json(5002, "发布失败" . $e->getMessage());
}
}
if (empty($couponMain)) {
return $this->error("优惠券不存在");
if (empty($couponMain)||empty($couponMain['usingRule'])) {
return $this->error("优惠券不存在或关联属性不存在");
}
$this->data['business'] = BusinessRepository::getInstance()->getBusinessAll();
$this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll();

View File

@ -28,7 +28,10 @@ class CouponMain extends Base
{
return $this->hasOne(CouponType::class, 'id',"type_id");
}
public function usingRule()
{
return $this->hasOne(UsingRule::class, 'coupon_id',"id");
}
public static function onAfterInsert( $obj)
{
$obj->sort = $obj->id;

View File

@ -4,6 +4,7 @@ namespace app\model;
class Slide extends Base
{
public const homePosition = "home-banner";
public static function delByIds($ids)
{
return self::whereIn('id', $ids)->delete();

16
app/model/UsingRule.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace app\model;
//优惠券使用规则
class UsingRule extends Base
{
public function setCycleAttr($value)
{
return implode(",",$value);
}
public function getCycleAttr($value)
{
return explode(",",$value);
}
}

View File

@ -5,6 +5,7 @@ namespace app\repository;
use app\exception\RepositoryException;
use app\service\Repository;
use app\traits\account\BusinessFlowTrait;
use app\traits\CommentTrait;
use app\traits\CouponBillTrait;
use app\traits\CouponTrait;
@ -23,6 +24,8 @@ use think\Model;
class AccountRepository extends Repository
{
use CommentTrait;
use CommentTrait;
use BusinessFlowTrait;
use CouponTrait;
use CouponBillTrait;
/**

View File

@ -156,7 +156,7 @@ class BusinessRepository extends Repository
$Flow = Business::with(["account"])->where("code", $businessCode)->when($lock, function ($q) {
$q->lock(true);
})->find();
if (empty($Flow) || empty($Flow->account) || empty($Flow->business)) {
if (empty($Flow) || empty($Flow->account)) {
return null;
}
return $Flow;

View File

@ -1,10 +1,12 @@
<?php
namespace app\repository;
use app\model\Business;
use app\model\Coupon;
use app\model\CouponMain;
use app\model\CouponType;
use app\model\UsingRule;
use app\service\Repository;
use Exception;
use think\Collection;
@ -67,10 +69,15 @@ class CouponRepository extends Repository
* 发布优惠券 扣除商家余额
* @param $data
* @param $totalMoney
* @param $usingRule 使用规则
*/
public function releaseCouponMain($data, $totalMoney)
public function releaseCouponMain($data, $totalMoney,array $usingRule)
{
CouponMain::create($data);
//创建优惠券根表
$couponMain = CouponMain::create($data);
//创建优惠券使用规则
$usingRule["coupon_id"] = $couponMain->id;
UsingRule::create($usingRule);
Business::where("code", $data["business_code"])->dec("balance", $totalMoney)->update();
}

View File

@ -0,0 +1,32 @@
<?php
namespace app\traits\account;
use app\model\BusinessFlow;
trait BusinessFlowTrait
{
/**
* 获取关注的商家列表
* @param $accountCode
* @param $page
* @param $size
* @return
*/
public function getBusinessFlowList($accountCode, $page, $size)
{
return BusinessFlow::alias("a")
->join("business b","a.business_code = b.code")
->where("a.user_code",$accountCode)
->field(["b.code",
"b.business_name",
"b.business_subtitle",
"b.lat",
"b.lng",
"b.characteristic",
"b.background",
])
->page($page,$size)
->select();
}
}

28
app/validate/Comment.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace app\validate;
use think\Validate;
use app\model\Comment as CommentModel;
class Comment extends Validate
{
protected $rule = [
'comment|评论内容' => 'require|max:1024',
'user_code|用户' => 'require|length:32',
'url|图片文件地址' => 'length:0,255',
'type|类型' => 'require|checkType',
'lng|定位信息' => 'require',
'lat|定位信息' => 'require',
'location|位置信息' => 'length:0,200',
];
protected $scene = [
'edit_password' => ['old_password', 'password', 'confirm_password'], //修改密码
];
protected function checkType($value,$rule,$data=[])
{
return isset(CommentModel::allType()[$value]) ? true : '评论类型错误';
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace app\validate;
use think\Validate;
class CouponUsingRule extends Validate
{
protected $rule = [
'day_start_time|每日时段限制' => 'require',
'day_end_time|每日时段限制' => 'require',
'cycle|周期' => 'require|array',
'day_total|每日领取总数' => 'require|number',
'person_day_total|单人日限制' => 'require|number',
'person_total|单人总限制' => 'require|number',
];
}

View File

@ -12,6 +12,9 @@ use think\facade\Route;
Route::rule('account/loginByCode', "\\app\\controller\\api\\user@login");//用户登录
Route::rule('consumer/home', "\\app\\controller\\api\\Consumer@home");//首页列表(优惠券列表)
Route::rule('consumer/bannerList', "\\app\\controller\\api\\Consumer@bannerList");//首页列表(优惠券列表)
Route::rule('dic/getDisList', "\\app\\controller\\api\\dictionary@getDisList");//距离选项列表
Route::rule('dic/getBusinessTypeListByPid', "\\app\\controller\\api\\dictionary@getBusinessTypeList");//首页获取商家类型
Route::rule('dic/getCouponTypeList', "\\app\\controller\\api\\dictionary@getCouponTypeList");//首页获取优惠券类型
Route::rule('dic/getBusinessTypeList', "\\app\\controller\\api\\business@getBusinessTypeList");//首页获取商家类型
Route::rule('dic/getBusinessCircle', "\\app\\controller\\api\\business@getBusinessCircle");//首页获取商家商圈

View File

@ -104,13 +104,13 @@
<div class="layui-form-item">
<label class="layui-form-label">使用周期限制</label>
<div class="layui-input-block">
<input type="checkbox" name="using_rule[cycle][1]" value="0" title="周一" />
<input type="checkbox" name="using_rule[cycle][2]" value="1" title="周二" />
<input type="checkbox" name="using_rule[cycle][3]" value="1" title="周三" />
<input type="checkbox" name="using_rule[cycle][4]" value="1" title="周四" />
<input type="checkbox" name="using_rule[cycle][5]" value="1" title="周五" />
<input type="checkbox" name="using_rule[cycle][6]" value="1" title="周六" />
<input type="checkbox" name="using_rule[cycle][0]" value="1" title="周天" />
<input type="checkbox" name="using_rule[cycle][0]" value="1" title="周一" />
<input type="checkbox" name="using_rule[cycle][1]" value="2" title="周二" />
<input type="checkbox" name="using_rule[cycle][2]" value="3" title="周三" />
<input type="checkbox" name="using_rule[cycle][3]" value="4" title="周四" />
<input type="checkbox" name="using_rule[cycle][4]" value="5" title="周五" />
<input type="checkbox" name="using_rule[cycle][5]" value="6" title="周六" />
<input type="checkbox" name="using_rule[cycle][6]" value="0" title="周天" />
</div>
</div>

View File

@ -90,13 +90,68 @@
</div>
</div>
<fieldset class="layui-elem-field layui-field-title site-title">
<legend><a >使用规则</a></legend>
</fieldset>
<div class="layui-form-item">
<label class="layui-form-label">使用规则</label>
<label class="layui-form-label">每日领取总数</label>
<div class="layui-input-block">
<textarea name="item[using_rule]" cols="30" rows="10">{$item['using_rule']}</textarea>
<input class="layui-input" type="number" name="using_rule[day_total]" value="{$item['usingRule']['day_total']}" lay-verify="required|number" />
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">使用周期限制</label>
<div class="layui-input-block">
<input type="checkbox" {if in_array(1,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][0]" value="1" title="周一" />
<input type="checkbox" {if in_array(2,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][1]" value="2" title="周二" />
<input type="checkbox" {if in_array(3,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][2]" value="3" title="周三" />
<input type="checkbox" {if in_array(4,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][3]" value="4" title="周四" />
<input type="checkbox" {if in_array(5,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][4]" value="5" title="周五" />
<input type="checkbox" {if in_array(6,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][5]" value="6" title="周六" />
<input type="checkbox" {if in_array(0,$item['usingRule']['cycle']) } checked {/if} name="using_rule[cycle][6]" value="0" title="周天" />
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">每日时段限制</label>
<div class="layui-inline">
<label class="layui-form-label">开始时间</label>
<div class="layui-inline">
<input type="text" name="using_rule[day_start_time]" value="{$item['usingRule']['day_start_time']}" class="layui-input" id="using_rule_day_start_time" placeholder="HH:mm:ss">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">结束</label>
<div class="layui-inline">
<input type="text" name="using_rule[day_end_time]" value="{$item['usingRule']['day_end_time']}" class="layui-input" id="using_rule_day_end_time" placeholder="HH:mm:ss">
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">单人日限制</label>
<div class="layui-input-block">
<input class="layui-input" type="number" name="using_rule[person_day_total]" value="{$item['usingRule']['person_day_total']}" lay-verify="required|number" />
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">单人总限制</label>
<div class="layui-input-block">
<input class="layui-input" type="number" name="using_rule[person_total]" value="{$item['usingRule']['person_total']}" lay-verify="required|number"/>
</div>
</div>
<fieldset class="layui-elem-field layui-field-title site-title">
<legend><a >处罚规则</a></legend>
</fieldset>
<div class="layui-form-item">
<label class="layui-form-label">处罚规则</label>
<div class="layui-input-block">

View File

@ -18,6 +18,7 @@
</div>
</div>
<div class="layui-form-item field-div">
<label class="layui-form-label">跳转链接</label>
<div class="layui-input-block">
@ -25,6 +26,16 @@
</div>
</div>
<div class="layui-form-item field-div">
<label class="layui-form-label required">跳转类型</label>
<div class="layui-input-block">
<input type="radio" name="item[url_type]" value="1" title="小程序(跳转链接需传入优惠券id)" />
<input type="radio" name="item[url_type]" checked value="0" title="网页" />
</div>
</div>
<div class="layui-form-item layui-row field-div" >
<label class="layui-form-label required">轮播图:</label>
<div class="layui-inline layui-col-xs12 layui-col-md8">

View File

@ -25,6 +25,16 @@
</div>
</div>
<div class="layui-form-item field-div">
<label class="layui-form-label required">跳转类型</label>
<div class="layui-input-block">
<input type="radio" {if $item['url_type'] == 1}checked="checked"{/if} name="item[url_type]" value="1" title="小程序(跳转链接需传入优惠券id)" />
<input type="radio" {if $item['url_type'] == 0}checked="checked"{/if} name="item[url_type]" value="0" title="网页" />
</div>
</div>
<div class="layui-form-item layui-row field-div " >
<label class="layui-form-label required">轮播图:</label>
<div class="layui-inline layui-col-xs12 layui-col-md8">