From 347de4f5a5a606823819bb7c022a33e0517b66e8 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Thu, 2 Dec 2021 18:34:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BC=98=E6=83=A0=E5=88=B8?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 18 +++++++ app/controller/api/Area.php | 9 ++-- app/controller/api/Business.php | 21 +++++++- app/controller/api/Comment.php | 68 ++++++++++++++++++++++-- app/controller/api/Consumer.php | 51 +++++++++++++++--- app/controller/api/User.php | 11 ++++ app/controller/manager/Coupon.php | 68 +++++++++++++++--------- app/model/CouponMain.php | 5 +- app/model/Slide.php | 1 + app/model/UsingRule.php | 16 ++++++ app/repository/AccountRepository.php | 3 ++ app/repository/BusinessRepository.php | 2 +- app/repository/CouponRepository.php | 15 ++++-- app/traits/account/BusinessFlowTrait.php | 32 +++++++++++ app/validate/Comment.php | 28 ++++++++++ app/validate/CouponUsingRule.php | 20 +++++++ route/api.php | 3 ++ view/manager/coupon/add.html | 14 ++--- view/manager/coupon/edit.html | 59 +++++++++++++++++++- view/manager/slide/add.html | 11 ++++ view/manager/slide/edit.html | 10 ++++ 21 files changed, 404 insertions(+), 61 deletions(-) create mode 100644 app/model/UsingRule.php create mode 100644 app/traits/account/BusinessFlowTrait.php create mode 100644 app/validate/Comment.php create mode 100644 app/validate/CouponUsingRule.php diff --git a/app/common.php b/app/common.php index de80d12..97a42a9 100644 --- a/app/common.php +++ b/app/common.php @@ -778,4 +778,22 @@ 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); + } } \ No newline at end of file diff --git a/app/controller/api/Area.php b/app/controller/api/Area.php index 003cd51..419c360 100644 --- a/app/controller/api/Area.php +++ b/app/controller/api/Area.php @@ -12,12 +12,9 @@ use app\model\Area as AreaModel; */ class Area extends Base { - protected $middleware = []; - - protected function initialize() - { - - } + protected $noNeedLogin = [ + 'index', + ]; /** * 根据上级行政代码 获取下级 diff --git a/app/controller/api/Business.php b/app/controller/api/Business.php index 3712ab1..2ad6f22 100644 --- a/app/controller/api/Business.php +++ b/app/controller/api/Business.php @@ -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 } } - -} \ No newline at end of file + /** + * 获取商家类型 + * */ + public function getBusinessTypeList() + { + $data = Category::getList(); + return $this->json(1,"ok",$data); + } + /** + * 获取商家类型 + * */ + public function getBusinessCircle() + { + $data = BusinessCircle::getList(); + return $this->json(1,"ok",$data); + } +} diff --git a/app/controller/api/Comment.php b/app/controller/api/Comment.php index 6fb3d1a..035ef62 100644 --- a/app/controller/api/Comment.php +++ b/app/controller/api/Comment.php @@ -1,22 +1,80 @@ 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, '服务器繁忙!获取用户个人信息失败'); + } + } } \ No newline at end of file diff --git a/app/controller/api/Consumer.php b/app/controller/api/Consumer.php index b5296ca..222401d 100644 --- a/app/controller/api/Consumer.php +++ b/app/controller/api/Consumer.php @@ -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"]); + } } \ No newline at end of file diff --git a/app/controller/api/User.php b/app/controller/api/User.php index c98dfaf..319b5d6 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -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(); + } } \ No newline at end of file diff --git a/app/controller/manager/Coupon.php b/app/controller/manager/Coupon.php index 1a1fa82..d4fbf75 100644 --- a/app/controller/manager/Coupon.php +++ b/app/controller/manager/Coupon.php @@ -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; @@ -48,7 +49,7 @@ class Coupon extends Base $size = $this->request->param('size/d', 30); $whereMap = []; - $orders = ['sort' => 'desc',"id"=>"desc"]; + $orders = ['sort' => 'desc', "id" => "desc"]; if (!empty($on_shelf) && in_array($on_shelf, [CouponMain::COMMON_ON, CouponMain::COMMON_OFF])) { $whereMap[] = ['on_shelf', '=', $on_shelf]; } @@ -68,26 +69,26 @@ class Coupon extends Base return $q::hasWhere('business', function ($q) use ($keyword) { $q->where('business_name', 'like', "%" . $keyword . "%") //如果是渠道商或者工作人员 只查看自己的下级商家 - ->when(Member::is_agency($this->auth['roles']),function ($q){ - $q->where( 'agency_code', '=', $this->auth['business_code']); + ->when(Member::is_agency($this->auth['roles']), function ($q) { + $q->where('agency_code', '=', $this->auth['business_code']); }) ->field("code,business_name,business_subtitle,type"); - //->with('category'); + //->with('category'); }); } - if(Member::is_agency($this->auth['roles'])){ + if (Member::is_agency($this->auth['roles'])) { - return $q::hasWhere('business', function ($q) { + return $q::hasWhere('business', function ($q) { $q//如果是渠道商或者工作人员 只查看自己的下级商家 - ->where( 'agency_code', '=', $this->auth['business_code']) + ->where('agency_code', '=', $this->auth['business_code']) ->field("code,business_name,business_subtitle,type"); - //->with('category'); + //->with('category'); }); - }else{ + } else { return $q->with(["business" => function ($query) { $query->field("code,business_name,business_subtitle,type"); - //->with('category'); + //->with('category'); }]); } @@ -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(); diff --git a/app/model/CouponMain.php b/app/model/CouponMain.php index f5a07a9..3b8d2ba 100644 --- a/app/model/CouponMain.php +++ b/app/model/CouponMain.php @@ -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; diff --git a/app/model/Slide.php b/app/model/Slide.php index b6b795f..e90a1c5 100644 --- a/app/model/Slide.php +++ b/app/model/Slide.php @@ -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(); diff --git a/app/model/UsingRule.php b/app/model/UsingRule.php new file mode 100644 index 0000000..b56b7ff --- /dev/null +++ b/app/model/UsingRule.php @@ -0,0 +1,16 @@ +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; diff --git a/app/repository/CouponRepository.php b/app/repository/CouponRepository.php index 397fa58..516eeef 100644 --- a/app/repository/CouponRepository.php +++ b/app/repository/CouponRepository.php @@ -1,10 +1,12 @@ "desc"]) { try { @@ -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(); } @@ -86,7 +93,7 @@ class CouponRepository extends Repository * @return array * @throws Exception */ - public function findCouponMainList(array $where, array $fields = [], int $page=1, int $size = 0, callable $call=null, array $sortOrder= []): array + 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); } diff --git a/app/traits/account/BusinessFlowTrait.php b/app/traits/account/BusinessFlowTrait.php new file mode 100644 index 0000000..ecde742 --- /dev/null +++ b/app/traits/account/BusinessFlowTrait.php @@ -0,0 +1,32 @@ +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(); + } +} \ No newline at end of file diff --git a/app/validate/Comment.php b/app/validate/Comment.php new file mode 100644 index 0000000..34e6868 --- /dev/null +++ b/app/validate/Comment.php @@ -0,0 +1,28 @@ + '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 : '评论类型错误'; + } +} \ No newline at end of file diff --git a/app/validate/CouponUsingRule.php b/app/validate/CouponUsingRule.php new file mode 100644 index 0000000..135f27e --- /dev/null +++ b/app/validate/CouponUsingRule.php @@ -0,0 +1,20 @@ + 'require', + 'day_end_time|每日时段限制' => 'require', + 'cycle|周期' => 'require|array', + 'day_total|每日领取总数' => 'require|number', + 'person_day_total|单人日限制' => 'require|number', + 'person_total|单人总限制' => 'require|number', + ]; + + + +} \ No newline at end of file diff --git a/route/api.php b/route/api.php index 6a670a8..a35db7e 100644 --- a/route/api.php +++ b/route/api.php @@ -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");//首页获取商家商圈 diff --git a/view/manager/coupon/add.html b/view/manager/coupon/add.html index c8094a2..f854ff8 100644 --- a/view/manager/coupon/add.html +++ b/view/manager/coupon/add.html @@ -104,13 +104,13 @@