diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php index 9cdaf21..cb1c00a 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -8,6 +8,7 @@ use app\model\CouponBill; use app\model\CouponMain; use app\model\Deduction; use app\model\Redpack; +use app\model\Business as BusinessModel; use app\model\Score; use app\model\UsingRule; use app\repository\AccountRepository; @@ -302,15 +303,15 @@ class Coupon extends Base //签到距离 Config::load('extra/wechat', 'wechat'); - $signDistance = config('wechat.signDistance')??0; + $signDistance = config('wechat.signDistance') ?? 0; + if($signDistance > 0 ){ $Distance = get_distance($coupon->lat,$coupon->lng,$lat,$lng); if($Distance > $signDistance){ - return $this->json(4001, "请在规定区域内使用优惠券"); + return $this->json(4001, "您距离商家位置距离超过规定距离{$Distance}米"); } } - $business = BusinessRepository::getInstance()->getModel()->with(["agency"])->where(["code"=>$coupon->couponMain->business_code])->lock(true)->find(); if(empty($business)){ return $this->json(4001, "商家不存在"); @@ -335,14 +336,22 @@ class Coupon extends Base ]); - //可分配金额 - $deductionMoney = $coupon->couponMain->deduction_money; - $agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency); - $adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin); - $consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer); - $agencyMoney = round($agencyMoney,2); //四舍五入 精确到分 - $adminMoney = round($adminMoney,2); //四舍五入 精确到分 - $consumerMoney = round($consumerMoney,2); //四舍五入 精确到分 + //可分配金额 如果是普通商家 + if($account->business["model"] == BusinessModel::model_ordinary) { + $deductionMoney = $coupon->couponMain->deduction_money; + $agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency); + $adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin); + $consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer); + $agencyMoney = round($agencyMoney,2); //四舍五入 精确到分 + $adminMoney = round($adminMoney,2); //四舍五入 精确到分 + $consumerMoney = round($consumerMoney,2); //四舍五入 精确到分 + }else{ + $deductionMoney = $coupon->couponMain->deduction_money; + $adminMoney = 0; + $consumerMoney = round($deductionMoney,2) ; + $agencyMoney = 0; //四舍五入 精确到分 + } + // 2. 写入优惠券流水 $couponBillData = [ "coupon_main_id" =>$coupon->couponMain->id, @@ -464,7 +473,17 @@ class Coupon extends Base $totalC = $distributionProportion['agency'] + $distributionProportion['admin'] + $distributionProportion['consumer']; if ($totalC != 100) { - return $this->json(5002, "系统设置分配比例总和不等于100,不能发布"); + return $this->json(4002, "系统设置分配比例总和不等于100,不能发布"); + } + + //持有限量 + Config::load("extra/wechat","wechat"); + $hasCouponMax = config("wechat.hasCouponMax")??0; + if ($hasCouponMax > 0) { + $hasCouponCount = CouponRepository::getInstance()->getBusinessOnShelfOnCount($account->business->code); + if ($hasCouponCount > $hasCouponMax) { + return $this->json(4001, "商家持有商家优惠券不能超过{$hasCouponMax}"); + } } //验证通过 不管是商家还是工作人员 都可以发布优惠券 @@ -491,7 +510,10 @@ class Coupon extends Base $usingRule = input("using_rule/a"); $validate = new CouponRelease(); - if (!$validate->check($couponMain)) { + + //普通商家要验证扣除金额 + + if (!$validate->scene($account->business["model"] == BusinessModel::model_ordinary ? "ordinary" : "")->check($couponMain)) { return $this->json(4001, $validate->getError()); } @@ -513,9 +535,10 @@ class Coupon extends Base //未领取的优惠券 $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($account->business["code"]); - - if ($account->business["balance"] < ($totalMoney + $NotClaimedMoney)) { - return $this->json(4001, '商家余额不足'); + if($account->business["model"] == BusinessModel::model_ordinary) { + if ($account->business["balance"] < ($totalMoney + $NotClaimedMoney)) { + return $this->json(4001, '商家余额不足'); + } } Db::startTrans(); diff --git a/app/controller/api/User.php b/app/controller/api/User.php index 44396de..fded2c3 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -44,8 +44,8 @@ class User extends Base 'gender' => $this->request->param('gender/d', 0), 'real_name' => $this->request->param('real_name/s', '',"trim"), 'mobile' => $this->request->param('mobile/s', ''), - 'lat' => $this->request->param('lat/f', 0), - 'lng' => $this->request->param('lng/f', 0), + 'lat' => $this->request->param('lat/f', 0), + 'lng' => $this->request->param('lng/f', 0), ]; $validate = new UserValidate(); diff --git a/app/controller/manager/Business.php b/app/controller/manager/Business.php index dd5c808..6a077f6 100644 --- a/app/controller/manager/Business.php +++ b/app/controller/manager/Business.php @@ -241,6 +241,7 @@ class Business extends Base $this->data["item"] = $business; $this->data["type"] = CategoryModel::getByGroup(); $this->data["businessCircle"] = BusinessCircleModel::getList(); + $this->data["model"] = BusinessModel::allModel(); return $this->view(); } diff --git a/app/controller/manager/Coupon.php b/app/controller/manager/Coupon.php index aaa3da8..b72b662 100644 --- a/app/controller/manager/Coupon.php +++ b/app/controller/manager/Coupon.php @@ -6,11 +6,13 @@ use app\exception\RepositoryException; use app\model\CouponMain; use app\model\Member; +use app\model\Business as BusinessModel; use app\repository\BusinessRepository; use app\repository\CouponRepository; use app\validate\CouponRelease; use app\validate\CouponUsingRule; use Exception; +use think\facade\Config; use think\facade\Db; use think\response\Json; use think\response\View; @@ -186,8 +188,16 @@ class Coupon extends Base if ($this->request->isPost()) { $data = input("item/a", []); $usingRule = input("using_rule/a", []); + + + $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); + + if (empty($business)) { + return $this->json(4001, '商家不存在'); + } $validate = new CouponRelease(); - if (!$validate->check($data)) { + //普通商家要验证扣除金额 + if (!$validate->scene($business["model"] ==BusinessModel::model_ordinary?"ordinary":"")->check($data)) { return $this->json(4001, $validate->getError()); } @@ -196,11 +206,18 @@ class Coupon extends Base return $this->json(4001, $usingRuleValidate->getError()); } - $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); - - if (empty($business)) { - return $this->json(4001, '商家不存在'); + //持有限量 + Config::load("extra/wechat","wechat"); + $hasCouponMax = config("wechat.hasCouponMax")??0; + if ($hasCouponMax > 0) { + $hasCouponCount = CouponRepository::getInstance()->getBusinessOnShelfOnCount($business->code); + if ($hasCouponCount > $hasCouponMax) { + return $this->json(4001, "商家持有商家优惠券不能超过{$hasCouponMax}"); + } } + + + $data['business_type'] = $business['type']; $data['business_name'] = $business['business_name']; $data['lng'] = $business['lng']; @@ -227,10 +244,12 @@ class Coupon extends Base $totalMoney = $data['money'] * $data['count']; //未领取的优惠券 - $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($account->business["code"]); + $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($business["code"]); - if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { - return $this->json(4001, '商家余额不足'); + if($business["model"] == BusinessModel::model_ordinary){ + if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { + return $this->json(4001, '商家余额不足'); + } } $data['type_name'] = $type[$data['type']]['name']; diff --git a/app/controller/manager/Statistical.php b/app/controller/manager/Statistical.php index 8560539..2266e60 100644 --- a/app/controller/manager/Statistical.php +++ b/app/controller/manager/Statistical.php @@ -12,7 +12,7 @@ use app\repository\CouponRepository; class Statistical extends Base { /** - * 性别比例 + * 签到 * */ public function sign() { @@ -74,5 +74,44 @@ class Statistical extends Base return $this->view(); } + /** + * 注册位置 + * */ + public function register(){ + if($this->request->isPost()){ + $page =input("page/d",1); + $size =input("size/d",1000); + $data = AccountRepository::getInstance()->findList([ + ["lat",">",0], + ["lng",">",0], + ],["lat","lng","id as value"],$page,$size,function ($q){ + return $q->withAttr("value",function ($value){ + return 100; + }); + },["id"=>"desc"]); + return $this->json(0,"success",$data); + } + return $this->view(); + } + + /** + * 领取优惠券位置 + * */ + public function receive(){ + if($this->request->isPost()){ + $page =input("page/d",1); + $size =input("size/d",1000); + $data = CouponRepository::getInstance()->findList([ + ["lat",">",0], + ["lng",">",0], + ],["lat","lng","id as value"],$page,$size,function ($q){ + return $q->withAttr("value",function ($value){ + return 100; + }); + },["id"=>"desc"]); + return $this->json(0,"success",$data); + } + return $this->view(); + } } diff --git a/app/model/Business.php b/app/model/Business.php index a0aa4f8..189c965 100644 --- a/app/model/Business.php +++ b/app/model/Business.php @@ -6,14 +6,25 @@ use think\model\relation\HasOne; class Business extends Base { - const state_reviewing = 0; - const state_on = 1; - const state_off = 2; + const state_reviewing = 0;//待审核 + const state_on = 1;//审核通过 + const state_off = 2;//驳回 + const model_ordinary = 0;//普通商家 + const model_annual_fee = 1;//年费商家 + const model_zero = 2;//0元商家 /** * @remarks 代理商、平台商、平台代理商、渠道商等词组均描述的是代理商,因此文案统一为【代理商】 */ + static function allModel() + { + return [ + self::model_ordinary=>"普通商家", + self::model_annual_fee=>"年费商家", + self::model_zero=>"0元商家", + ]; + } public function category() { return $this->hasOne(Category::class, 'id',"type"); diff --git a/app/traits/CouponMainTrait.php b/app/traits/CouponMainTrait.php index f787fb5..1495b78 100644 --- a/app/traits/CouponMainTrait.php +++ b/app/traits/CouponMainTrait.php @@ -144,4 +144,14 @@ trait CouponMainTrait ->toArray() ; } + + /** + * 获取商家商家的优惠券 + * */ + public function getBusinessOnShelfOnCount($businessCode) + { + return CouponMain::where("business_code",$businessCode) + ->where("on_shelf",CouponMain::on_shelf_on) + ->count(); + } } \ No newline at end of file diff --git a/app/validate/CouponRelease.php b/app/validate/CouponRelease.php index 5fa7a3e..1d7d99c 100644 --- a/app/validate/CouponRelease.php +++ b/app/validate/CouponRelease.php @@ -16,8 +16,8 @@ class CouponRelease extends Validate 'start_time|开始时间' => 'require|date', 'end_time|结束时间' => 'require|date|checkEndTime', 'name|优惠券名称' => 'require|length:3,32', - 'money|金额' => 'require|>:0|<:5000', - 'deduction_money|扣除金额' => 'require|checkDeductionMoney|>=:0.1|<:5000', + 'money|金额' => 'require|>:0|<=:5000', + 'deduction_money|扣除金额' => 'require|>=:0.1|<=:5000', //'image_url|预览图' => '', //'using_rule|使用规则' => '', //'punishing_rule|处罚规则' => '', @@ -35,6 +35,12 @@ class CouponRelease extends Validate 'api_edit' => ['name', 'type', 'start_time',"end_time","image_url"], ]; + // edit 验证场景定义 + public function sceneOrdinary() + { + return $this->append('deduction_money', 'checkDeductionMoney'); + } + protected function checkEndTime($value, $rule, $data = []) { if (strtotime($value) <= strtotime($data['start_time'])) { diff --git a/app/validate/User.php b/app/validate/User.php index f88648c..e4235d6 100644 --- a/app/validate/User.php +++ b/app/validate/User.php @@ -16,7 +16,10 @@ class User extends Validate 'lng|位置' => 'require|>:0', ]; - + protected $message = [ + 'lat.require' => '请授权位置信息', + 'lng.require' => '请授权位置信息', + ]; protected $scene = [ // 微信小程序登录 'wx_applets' => ['code', 'nick_name', 'gender',"lat","lng"], diff --git a/view/manager/business/business_detail.html b/view/manager/business/business_detail.html index 7fca966..2a15c6a 100644 --- a/view/manager/business/business_detail.html +++ b/view/manager/business/business_detail.html @@ -192,6 +192,16 @@ +