diff --git a/application/plugins/freightfee/Hook.php b/application/plugins/freightfee/Hook.php index 131db1203..fc720b956 100755 --- a/application/plugins/freightfee/Hook.php +++ b/application/plugins/freightfee/Hook.php @@ -116,7 +116,7 @@ class Hook } // 免运费商品 - $free_goods = $this->FreeShippingGoods($ret['data']['goods_ids'], $params); + $free_goods = $this->FreeShippingGoods(empty($ret['data']['goods_ids']) ? '' : $ret['data']['goods_ids'], $params); $params['data']['base']['buy_count'] -= $free_goods['buy_count']; $params['data']['base']['spec_weight_total'] -= $free_goods['spec_weight']; diff --git a/application/plugins/membershiplevel/Hook.php b/application/plugins/membershiplevel/Hook.php index e6e41fa9d..4b14b76d8 100755 --- a/application/plugins/membershiplevel/Hook.php +++ b/application/plugins/membershiplevel/Hook.php @@ -12,7 +12,6 @@ namespace app\plugins\membershiplevel; use think\Controller; use app\plugins\membershiplevel\Service; -use app\service\PluginsService; /** * 会员等级插件 - 钩子入口 @@ -33,106 +32,97 @@ class Hook extends Controller */ public function run($params = []) { + // 后端访问不处理 + if(isset($params['params']['is_admin_access']) && $params['params']['is_admin_access'] == 1) + { + return DataReturn('无需处理', 0); + } + + // 钩子名称 if(!empty($params['hook_name'])) { + // 当前模块/控制器/方法 + $module_name = strtolower(request()->module()); + $controller_name = strtolower(request()->controller()); + $action_name = strtolower(request()->action()); + + // 页面参数 + $input = input(); + + $ret = ''; switch($params['hook_name']) { - // style css - case 'plugins_common_header' : - $ret = $this->StyleCss($params); + case 'plugins_css' : + $ret = __MY_ROOT_PUBLIC__.'static/plugins/css/membershiplevel/style.css'; break; - // 楼层数据上面 - case 'plugins_view_home_floor_top' : - $ret = $this->HomeFloorTopAdv($params); + // 商品数据处理后 + case 'plugins_service_goods_handle_end' : + if(!empty($params['goods']['id']) && !empty($input['id']) && $params['goods']['id'] == $input['id'] && $module_name.$controller_name.$action_name == 'indexgoodsindex') + { + $this->GoodsHandleEnd($params['goods']); + } + break; + + // 商品规格基础数据 + case 'plugins_service_goods_spec_base' : + $this->GoodsSpecBase($params['spec_base']); break; - default : - $ret = ''; } return $ret; + } else { + return ''; } } /** - * 首页楼层顶部广告 + * 商品处理结束钩子 * @author Devil - * @blog http://gong.gg/ - * @version 1.0.0 - * @datetime 2019-02-06T16:16:34+0800 - * @param [array] $params [输入参数] + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-03-26 + * @desc description + * @param [array] &$goods [商品数据] */ - public function HomeFloorTopAdv($params = []) + private function GoodsHandleEnd(&$goods = []) { - // 获取应用数据 - $ret = PluginsService::PluginsData('membershiplevel'); - if($ret['code'] == 0) + // 用户等级 + $level = Service::UserLevelMatching(); + if(!empty($level) && $level['discount_rate'] > 0) { - // 有效时间 - if(!empty($ret['data']['time_start'])) + if(empty($goods['original_price'])) { - // 是否已开始 - if(strtotime($ret['data']['time_start']) > time()) - { - return ''; - } + $goods['original_price'] = $goods['price']; } - if(!empty($ret['data']['time_end'])) - { - // 是否已结束 - if(strtotime($ret['data']['time_end']) < time()) - { - return ''; - } - } - } - // 获取图片列表 - $ret = Service::DataList(); - if($ret['code'] == 0 && !empty($ret['data'])) - { - $this->assign('data_list', $ret['data']); - return $this->fetch('../../../plugins/view/membershiplevel/index/content'); + // 价格处理 + $goods['price'] = Service::PriceCalculate($goods['price'], $level['discount_rate'], 0); + $price_title = empty($level['name']) ? '会员价' : $level['name']; + $goods['show_field_price_text'] = ''.$price_title.''; } - return ''; } /** - * css + * 商品规格基础数据 * @author Devil - * @blog http://gong.gg/ - * @version 1.0.0 - * @datetime 2019-02-06T16:16:34+0800 - * @param [array] $params [输入参数] + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-03-26 + * @desc description + * @param [array] &$spec_base [规格信息] */ - public function StyleCss($params = []) + private function GoodsSpecBase(&$spec_base = []) { - return ''; + // 用户等级 + $level = Service::UserLevelMatching(); + if(!empty($level) && $level['discount_rate'] > 0 && isset($spec_base['price'])) + { + if(empty($spec_base['original_price'])) + { + $spec_base['original_price'] = $spec_base['price']; + } + $spec_base['price'] = Service::PriceCalculate($spec_base['price'], $level['discount_rate'], 0); + } } } ?> \ No newline at end of file diff --git a/application/plugins/membershiplevel/Service.php b/application/plugins/membershiplevel/Service.php index eb015b5af..a066e3a40 100755 --- a/application/plugins/membershiplevel/Service.php +++ b/application/plugins/membershiplevel/Service.php @@ -10,8 +10,10 @@ // +---------------------------------------------------------------------- namespace app\plugins\membershiplevel; +use think\Db; use app\service\PluginsService; use app\service\ResourcesService; +use app\service\UserService; /** * 会员等级服务层 @@ -50,6 +52,22 @@ class Service // 获取数据 $ret = PluginsService::PluginsData('membershiplevel', self::$base_config_attachment_field); $data = (empty($ret['data']) || empty($ret['data'][$data_field])) ? [] : $ret['data'][$data_field]; + + // 数据处理 + return self::LevelDataHandle($data, $params); + } + + /** + * 用户等级数据列表处理 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-04-27T01:08:23+0800 + * @param [array] $data [等级数据] + * @param [array] $params [输入参数] + */ + public static function LevelDataHandle($data, $params = []) + { if(!empty($data)) { $common_is_enable_tips = lang('common_is_enable_tips'); @@ -183,8 +201,8 @@ class Service // 数据 $data = [ 'name' => $params['name'], - 'rules_min' => intval($params['rules_min']), - 'rules_max' => intval($params['rules_max']), + 'rules_min' => $params['rules_min'], + 'rules_max' => $params['rules_max'], 'images_url' => $attachment['data']['images_url'], 'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0, 'discount_rate' => isset($params['discount_rate']) ? $params['discount_rate'] : 0, @@ -299,5 +317,133 @@ class Service // 保存 return PluginsService::PluginsDataSave(['plugins'=>'membershiplevel', 'data'=>$ret['data']]); } + + /** + * 优惠价格计算 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-03-26 + * @desc description + * @param [string] $price [商品展示金额] + * @param [int] $plugins_discount [折扣系数] + * @param [int] $plugins_price [减金额] + */ + public static function PriceCalculate($price, $plugins_discount = 0, $plugins_price = 0) + { + if($plugins_discount <= 0 && $plugins_price <= 0) + { + return $price; + } + + // 折扣 + if($plugins_discount > 0) + { + if(stripos($price, '-') !== false) + { + $text = explode('-', $price); + $min_price = $text[0]*$plugins_discount; + $max_price = $text[1]*$plugins_discount; + $price = ($min_price <= 0) ? '0.00' : PriceNumberFormat($min_price); + $price .= '-'.(($max_price <= 0) ? '0.00' : PriceNumberFormat($max_price)); + } else { + $price = (float) $price *$plugins_discount; + $price = ($price <= 0) ? '0.00' : PriceNumberFormat($price); + } + } + + // 减金额 + if($plugins_price > 0) + { + if(stripos($price, '-') !== false) + { + $text = explode('-', $price); + $min_price = $text[0]-$plugins_price; + $max_price = $text[1]-$plugins_price; + $price = ($min_price <= 0) ? '0.00' : PriceNumberFormat($min_price); + $price .= '-'.(($max_price <= 0) ? '0.00' : PriceNumberFormat($max_price)); + } else { + $price = (float) $price-$plugins_price; + $price = ($price <= 0) ? '0.00' : PriceNumberFormat($price); + } + } + return $price; + } + + /** + * 用户等级匹配 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-04-27T00:32:00+0800 + */ + public static function UserLevelMatching() + { + // 用户 + $user = UserService::LoginUserInfo(); + if(!empty($user)) + { + // 缓存key + $key = 'plugins_membershiplevel_cache_user_level_'.$user['id']; + $level = cache($key); + + // 应用配置 + if(empty($level) || config('app_debug') == true) + { + $base = PluginsService::PluginsData('membershiplevel', Service::$base_config_attachment_field); + if(!empty($base['data']['level_list'])) + { + // 匹配类型 + $value = 0; + switch($base['data']['level_rules']) + { + // 积分(可用积分) + case 0 : + $value = isset($user['integral']) ? intval($user['integral']) : 0; + break; + + // 消费总额(已完成订单) + // 订单状态(0待确认, 1已确认/待支付, 2已支付/待发货, 3已发货/待收货, 4已完成, 5已取消, 6已关闭) + case 1 : + $where = ['user_id'=>$user['id'], 'status'=>4]; + $value = (float) Db::name('Order')->where($where)->sum('total_price'); + break; + } + + // 匹配相应的等级 + $level_list = self::LevelDataHandle($base['data']['level_list']); + foreach($level_list['data'] as $rules) + { + if(isset($rules['is_enable']) && $rules['is_enable'] == 1) + { + // 0-* + if($rules['rules_min'] <= 0 && $rules['rules_max'] > 0 && $value < $rules['rules_max']) + { + $level = $rules; + break; + } + + // *-* + if($rules['rules_min'] > 0 && $rules['rules_max'] > 0 && $value >= $rules['rules_min'] && $value < $rules['rules_max']) + { + $level = $rules; + break; + } + + // *-0 + if($rules['rules_max'] <= 0 && $rules['rules_min'] > 0 && $value > $rules['rules_min']) + { + $level = $rules; + break; + } + } + } + cache($key, $level); + } + } + return $level; + } + return []; + } } ?> \ No newline at end of file diff --git a/application/plugins/membershiplevel/config.json b/application/plugins/membershiplevel/config.json index b7673e9e4..1502eb542 100755 --- a/application/plugins/membershiplevel/config.json +++ b/application/plugins/membershiplevel/config.json @@ -21,10 +21,13 @@ "is_home":false }, "hook":{ - "plugins_common_header":[ + "plugins_css":[ "app\\plugins\\membershiplevel\\Hook" ], - "plugins_view_home_floor_top":[ + "plugins_service_goods_handle_end":[ + "app\\plugins\\membershiplevel\\Hook" + ], + "plugins_service_goods_spec_base":[ "app\\plugins\\membershiplevel\\Hook" ] } diff --git a/application/plugins/view/membershiplevel/level/saveinfo.html b/application/plugins/view/membershiplevel/level/saveinfo.html index 732bfcf96..99d8e1d1b 100644 --- a/application/plugins/view/membershiplevel/level/saveinfo.html +++ b/application/plugins/view/membershiplevel/level/saveinfo.html @@ -46,9 +46,9 @@
- + ~ - +
diff --git a/application/tags.php b/application/tags.php index 1b47de904..efdfcced1 100755 --- a/application/tags.php +++ b/application/tags.php @@ -107,6 +107,7 @@ return array ( 0 => 'app\\plugins\\freightfee\\Hook', 1 => 'app\\plugins\\share\\Hook', 2 => 'app\\plugins\\footercustomerservice\\Hook', + 3 => 'app\\plugins\\membershiplevel\\Hook', ), 'plugins_service_buy_handle' => array ( @@ -141,5 +142,13 @@ return array ( array ( 0 => 'app\\plugins\\footercustomerservice\\Hook', ), + 'plugins_service_goods_handle_end' => + array ( + 0 => 'app\\plugins\\membershiplevel\\Hook', + ), + 'plugins_service_goods_spec_base' => + array ( + 0 => 'app\\plugins\\membershiplevel\\Hook', + ), ); ?> \ No newline at end of file diff --git a/public/static/plugins/css/membershiplevel/style.css b/public/static/plugins/css/membershiplevel/style.css new file mode 100644 index 000000000..bd842aa97 --- /dev/null +++ b/public/static/plugins/css/membershiplevel/style.css @@ -0,0 +1,5 @@ +.plugins-membershiplevel-goods-price-icon { + padding: 3px 5px; + background: #ffc31e; + color: #fff; +} \ No newline at end of file