From fd4e802258e921398f5dbcbae9fb0f29ec913701 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Mon, 6 Dec 2021 18:56:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 34 ++++++- app/controller/api/Business.php | 64 +++++++++++- app/controller/api/Consumer.php | 83 ++++++++++++++- app/controller/api/Coupon.php | 87 ++++++++++++++++ app/controller/api/User.php | 17 ++++ app/model/BusinessFlow.php | 1 + app/repository/BusinessRepository.php | 2 + app/repository/CouponRepository.php | 16 +++ app/repository/DictionaryRepository.php | 28 ++++++ app/traits/CouponMainTrait.php | 40 ++++++++ app/traits/CouponTrait.php | 122 +++++++++++++++++++++-- app/traits/account/BusinessFlowTrait.php | 47 ++++++++- route/api.php | 17 ++-- view/manager/coupon/add.html | 8 +- view/manager/coupon/edit.html | 23 ++++- 15 files changed, 554 insertions(+), 35 deletions(-) create mode 100644 app/traits/CouponMainTrait.php diff --git a/app/common.php b/app/common.php index 87816b1..52e6feb 100644 --- a/app/common.php +++ b/app/common.php @@ -826,7 +826,9 @@ if (!function_exists('getEarthSquareRangePoint')) { } } - +/** + * 获取两个地点之鉴的距离 + * */ if (!function_exists('get_distance')) { function get_distance($lat1, $lng1, $lat2, $lng2) { @@ -842,4 +844,32 @@ if (!function_exists('get_distance')) { $calculatedDistance = $earthRadius * $stepTwo; return round($calculatedDistance); } -} \ No newline at end of file +} + +/** + * 格式化优惠券的使用周期 + * */ +if (!function_exists('encodeCouponCycle')) { + function encodeCouponCycle(array $week) + { + $font =[ + "0"=>"星期天", + "1"=>"星期一", + "2"=>"星期二", + "3"=>"星期三", + "4"=>"星期四", + "5"=>"星期五", + "6"=>"星期六", + ]; + + $str = []; + foreach ($week as $value){ + if (isset($font[$value])) { + $str[] = $font[$value]; + } + } + return implode(",",$str); + } +} + + diff --git a/app/controller/api/Business.php b/app/controller/api/Business.php index 1e434b6..b07c6cf 100644 --- a/app/controller/api/Business.php +++ b/app/controller/api/Business.php @@ -6,13 +6,12 @@ use app\model\BusinessCircle; use app\model\Category; use app\repository\AccountRepository; use app\repository\BusinessRepository; +use app\repository\CouponRepository; use app\repository\DictionaryRepository; use app\validate\BusinessValidate; +use think\Collection; use think\exception\ValidateException; -use app\model\{ - Business as BusinessModel, - Account as AccountModel -}; +use app\model\{Business as BusinessModel, Account as AccountModel, CouponMain}; use think\response\Json; /** @@ -23,6 +22,10 @@ use think\response\Json; */ class Business extends Base { + + protected $noNeedLogin = [ + 'couponDetail', + ]; /** * 商家认证注册 * @@ -217,6 +220,59 @@ class Business extends Base $data = BusinessCircle::getList(); return $this->json(1,"ok",$data); } + + /** + * 商家优惠券详情 + * */ + public function couponDetail() + { + $accountId = $this->request->user['user_id'] ?? 0; + $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + + $couponMainId = input("id/d"); + $couponMain = CouponMain::findOne(["id"=>$couponMainId]); + if(empty($couponMain)){ + return $this->json(4001,"优惠券不存在"); + } + if(!$business = BusinessRepository::getInstance()->findOneByWhere(["code"=>$couponMain["business_code"]])){ + return $this->json(4001,"优惠券商家信息不存在"); + } + + $list = $business->toArray(); + $list = DictionaryRepository::getInstance()->parseAreaText($list); + + $data = []; + + $data["businessAddress"] = $list['province_text'] . $list['city_text'] . $list['county_text'] . $business['business_address']; + $data["businessName"] = $list['business_name']; + $data["count"] = $couponMain->count; + $data["couponName"] = $couponMain->name; + $data["deductionMoney"] = $couponMain->deduction_money; + $data["startTime"] = $couponMain->start_time; + $data["endTime"] = $couponMain->end_time; + $data["id"] = $couponMain->id; + $data["imageUrl"] = $this->request->domain(). $couponMain->image_url; + $data["intro"] = $couponMain->intro;//简介 + $data["lat"] = $couponMain->lat; + $data["lng"] = $couponMain->lng; + $data["money"] = $couponMain->money; + $data["usingRule"] = DictionaryRepository::getInstance()->getUsingRuleByCouponMainId($couponMain->id); + $data["punishRule"] = '';//处罚规则 已取消 + + //未登录可以领取 0可领取 1已领取 + if (empty($account)) { + $data["receiveStatus"] = CouponMain::COMMON_OFF;//领取状态 + }else{ + $data["receiveStatus"] = AccountRepository::getInstance()->getCouponReceiveStatus($account->user_code,$couponMain);//领取状态 + } + $data["receivedCount"] = $couponMain->received_count;//已领取数量 + $data["typeName"] = $couponMain->type_name; + + return $this->json(0,"success",$data); + } + } diff --git a/app/controller/api/Consumer.php b/app/controller/api/Consumer.php index 93e5334..1580e9e 100644 --- a/app/controller/api/Consumer.php +++ b/app/controller/api/Consumer.php @@ -3,9 +3,13 @@ namespace app\controller\api; use app\model\CouponMain; use app\model\Slide; +use app\repository\AccountRepository; +use app\repository\BusinessRepository; use app\repository\CouponRepository; use app\repository\OperationRepository; use think\Collection; +use think\Exception; +use think\exception\ValidateException; use think\response\Json; /** @@ -30,6 +34,7 @@ class Consumer extends Base $params = [ 'businessType' => $this->request->param('businessType/d', 0), // 商家类型 'couponType' => $this->request->param('couponType/d', 0), // 优惠卷类型 + 'businessCode' => $this->request->param('businessCode/s'), // 商家code '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 @@ -51,12 +56,12 @@ class Consumer extends Base $whereMap[] = ['status', '=', CouponMain::status_on]; $whereMap[] = ['on_shelf', '=', CouponMain::on_shelf_on]; - $whereMap[] = ['start_time', '> TIME', $nowDate]; - $whereMap[] = ['end_time', '< TIME', $nowDate]; + //$whereMap[] = ['start_time', '< TIME', $nowDate]; + $whereMap[] = ['end_time', '> TIME', $nowDate]; $whereMap[] = ['using_count', '>', 0]; if (!empty($params['keyword'])) { - $whereMap[] = ['name', 'like', "%{$params['keyword']}%"]; + $whereMap[] = ['name', 'like', "%".$params['keyword']."%"]; } if ($params['businessType'] > 0) { $whereMap[] = ['business_type', '=', $params['businessType']]; @@ -64,6 +69,9 @@ class Consumer extends Base if ($params['couponType'] > 0) { $whereMap[] = ['type', '=', $params['couponType']]; } + if (!empty($params['businessCode'])) { + $whereMap[] = ['business_code', '=', $params['businessCode']]; + } if ($params['dis'] > 0) { $pointList = getEarthSquareRangePoint($params['lat'],$params['lng'], $params['dis']); @@ -76,7 +84,9 @@ class Consumer extends Base } } - $res = $repo->findCouponMainList($whereMap, [], $params['page'], $params['size'], function ($q) use($lngRange, $latRange, $params) { + + + $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); @@ -85,8 +95,20 @@ class Consumer extends Base ->field("*, abs( (IFNULL(lat,0) - {$params['lat']}) * (IFNULL(lng,0) - {$params['lng']}) ) as square"); }, $sortOrder); + $accountRepo= AccountRepository::getInstance(); - $res['list']->each(function ($item)use($params){ + $accountId = $this->request->user['user_id'] ?? 0; + $flowArray = []; + if ($accountId) { + $account = $accountRepo->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if (!empty($account)) { + $flowArray = $accountRepo->getBusinessFlowCodeArray($account->user_code); + } + } + + $res['list']->each(function ($item)use($params,$flowArray){ unset($item->square); $distance = (get_distance($params["lat"], $params["lng"], $item["lat"],$item["lng"])); if ($distance >= 1000) { @@ -95,6 +117,14 @@ class Consumer extends Base $item->distance_text = $distance . "m"; } + //是否收藏了该优惠券的商家 + $item->isFlow = in_array($item->business_code,$flowArray); + + $item->couponId = $item->id; + $item->businessCode = $item->business_code; + $item->businessName = $item->business_name; + $item->cover = $this->request->domain().$item->image_url; + $item->couponName = $item->name; }); return $this->json(0, 'success', $res); @@ -127,4 +157,47 @@ class Consumer extends Base }, $orders); return $this->json(1,"ok",$list["list"]); } + + + /** + * 关注/取消关注一个商家 + * */ + public function flowBusiness() + { + + $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('用户无效!'); + } + + $businessCode = input("businessCode/s"); + $business = BusinessRepository::getInstance()->findOneByWhere(["code"=>$businessCode]); + if(empty($business)){ + throw new ValidateException('商家无效!'); + } + $businessFlow = $accountRepo->hasBusinessFlow($account->user_code,$businessCode); + + //如果关注了 就删除 + if($businessFlow){ + $businessFlow->delete(); + }else{ + //没有关注就添加 + $accountRepo->createBusinessFlow($account->user_code,$businessCode,$business->business_name); + } + return $this->json(); + + } catch (ValidateException $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/Coupon.php b/app/controller/api/Coupon.php index bd2d04c..3fb0b07 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -2,7 +2,12 @@ namespace app\controller\api; use app\exception\RepositoryException; +use app\model\CouponMain; +use app\repository\AccountRepository; use app\repository\CouponRepository; +use think\Exception; +use think\facade\Db; +use think\facade\Log; /** * 优惠卷相关 @@ -65,5 +70,87 @@ class Coupon extends Base } catch (RepositoryException | \Exception $e) { return $this->json(5001, '优惠卷查询失败!'); } + } + + /** + * 领取优惠券 + * */ + public function receiveCoupon() + { + $accountId = $this->request->user['user_id'] ?? 0; + $lat = input("lat/f",0); + $lng = input("lng/f",0); + $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if(empty($account)){ + return $this->json(6001,"无效的用户"); + } + + if ($lat <= 0 || $lng <= 0) { + return $this->json(4001, "请授权定位"); + } + + $couponMainId = input("couponId/d", 0); + $couponMain = CouponMain::findOne(["id" => $couponMainId],[],function ($q){ + //执行领取 开启锁 + return $q->with("business")->lock(true); + }); + + //检查优惠券状态 + $checkCouponMainReceiveStatus = CouponRepository::getInstance()->checkCouponMainReceiveStatus($couponMain); + if( $checkCouponMainReceiveStatus !== true ){ + return $checkCouponMainReceiveStatus; + } + + try { + //检查是否可以领取 0可领取 1已领取 + AccountRepository::getInstance()->getCouponReceiveStatusText($account->user_code,$couponMain);//领取状态 + }catch (RepositoryException $e){ + return $this->json(4001,$e->getMessage()); + } + + + + //检查通过 执行领取 + $time = time(); + Db::startTrans(); + try { + //写入领取记录 + $data = [ + "coupon_id" =>$couponMain->id, + "name" =>$couponMain->name, + "type_id" =>$couponMain->type, + "type_name" =>$couponMain->type_name, + "business_code" =>$couponMain->business_code, + "business_name" =>$couponMain->business?$couponMain->business->business_name:'', + "consumer_code" =>$account->user_code, + "consumer_name" =>$account->nick_name, + "money" => $couponMain->money, + "content" => createUuid(),//未知作用 + "received_time" => date("Y-m-d H:i:s",$time), + "lat" => $lat, + "lng" => $lng, + "end_time" => date($couponMain->end_time . " 00:00:00"), + "edition" => couponMain::COMMON_ON,//版本 未知作用 + "is_verificated" => couponMain::COMMON_OFF,//版本 未知作用 + ]; + CouponRepository::getInstance()->receiveCoupon($data); + Db::commit(); + return $this->json(); + }catch (RepositoryException $e){ + Log::error("优惠券领取失败RepositoryException:".$e->getMessage()); + Db::rollback(); + return $this->json(5001,"领取失败"); + }catch (Exception $e){ + Log::error("优惠券领取失败:".$e->getMessage()); + Db::rollback(); + return $this->json(5001,"领取失败"); + } + + + + + } } \ No newline at end of file diff --git a/app/controller/api/User.php b/app/controller/api/User.php index 78d40f4..050ce38 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -231,8 +231,25 @@ class User extends Base * */ public function businessFlowList() { + return false; $page = $this->request->param('page/d', 1); $size = $this->request->param('size/d', 30); + + $accountRepo = AccountRepository::getInstance(); + try { + $account = $accountRepo->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if (empty($account)) { + throw new ValidateException('用户无效!'); + } + }catch (ValidateException $e) { + return $this->json(4001, $e->getError()); + } catch (Exception $e) { + return $this->json(5001, '服务器繁忙!获取用户个人信息失败'); + } $data = AccountRepository::getInstance()->getBusinessFlowList(); } + + } \ No newline at end of file diff --git a/app/model/BusinessFlow.php b/app/model/BusinessFlow.php index 126769b..0f95b24 100644 --- a/app/model/BusinessFlow.php +++ b/app/model/BusinessFlow.php @@ -5,6 +5,7 @@ namespace app\model; //用户关注的商家 class BusinessFlow extends Base { + public function account() { return $this->hasOne(Account::class,"user_code","user_code"); diff --git a/app/repository/BusinessRepository.php b/app/repository/BusinessRepository.php index 5ec6fa4..ac4fb6f 100644 --- a/app/repository/BusinessRepository.php +++ b/app/repository/BusinessRepository.php @@ -9,6 +9,7 @@ use app\model\Deduction; use app\model\Recharge; use app\service\Repository; use app\traits\CouponBillTrait; +use app\traits\CouponMainTrait; use think\Collection; use think\Model; @@ -22,6 +23,7 @@ use think\Model; class BusinessRepository extends Repository { use CouponBillTrait; + use CouponMainTrait; /** * 根据条件查询列表 diff --git a/app/repository/CouponRepository.php b/app/repository/CouponRepository.php index 516eeef..ff0d196 100644 --- a/app/repository/CouponRepository.php +++ b/app/repository/CouponRepository.php @@ -8,6 +8,7 @@ use app\model\CouponMain; use app\model\CouponType; use app\model\UsingRule; use app\service\Repository; +use app\traits\CouponMainTrait; use Exception; use think\Collection; use think\Db; @@ -23,6 +24,7 @@ use think\Model; */ class CouponRepository extends Repository { + use CouponMainTrait; /** * 优惠券持有信息列表 * @@ -98,4 +100,18 @@ class CouponRepository extends Repository return CouponMain::findList($where, $fields, $page, $size, $call, $sortOrder); } + /** + * 写入一个领取优惠券 + * @param array $data + * @return Coupon|Model + */ + public function receiveCoupon(array $data) + { + return Coupon::create($data); + } + + + + + } \ No newline at end of file diff --git a/app/repository/DictionaryRepository.php b/app/repository/DictionaryRepository.php index a6eae2f..0660ea0 100644 --- a/app/repository/DictionaryRepository.php +++ b/app/repository/DictionaryRepository.php @@ -6,6 +6,7 @@ use app\model\Area; use app\model\BusinessCircle; use app\model\Category; use app\model\Model; +use app\model\UsingRule; use app\service\Repository; use think\Collection; @@ -74,6 +75,11 @@ class DictionaryRepository extends Repository /** * 获取商家分类数据 + * @param array $where + * @param array $fields + * @param callable|null $call + * @param array $order + * @return mixed|Collection */ public function getBusinessTypeList(array $where=[], array $fields = [], callable $call=null, array $order = []) { @@ -131,6 +137,9 @@ class DictionaryRepository extends Repository /** * 解析地址编码 * [province, city, county] + * @param array $list + * @param array $areaList + * @return array */ public function parseAreaText(array $list, array $areaList = []): array { @@ -149,4 +158,23 @@ class DictionaryRepository extends Repository return $list; } + /** + * 获取一个优惠券的使用规则 + * @param int $couponMainId + * @return string + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getUsingRuleByCouponMainId(int $couponMainId) + { + $usingRule =UsingRule::where("coupon_id",$couponMainId)->find(); + if(empty($usingRule)){ + return ''; + } + return "