会员等级

This commit is contained in:
gongfuxiang 2019-04-27 01:53:29 +08:00
parent 4daba5fd90
commit e323d0fb8a
7 changed files with 234 additions and 81 deletions

View File

@ -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'];

View File

@ -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'] = '<span class="plugins-membershiplevel-goods-price-icon">'.$price_title.'</span>';
}
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 '<style type="text/css">
@media only screen and (min-width:640px) {
.plugins-membershiplevel-home-adv ul.am-gallery img {
-webkit-transition: transform .2s ease-in;
-moz-transition: transform .2s ease-in;
-ms-transition: transform .2s ease-in;
-o-transition: transform .2s ease-in;
transition: transform .2s ease-in;
}
.plugins-membershiplevel-home-adv ul.am-gallery img:hover {
-ms-transform: translate3d(0px, -3px, 0px);
-webkit-transform: translate3d(0px, -3px, 0px);
-o-transform: translate3d(0px, -3px, 0px);
transform: translate3d(0px, -3px, 0px);
}
}
@media only screen and (min-width:1025px) {
.plugins-membershiplevel-home-adv {
overflow: hidden;
}
.plugins-membershiplevel-home-adv ul.am-gallery {
width: calc(100% + 20px);
margin-left: -10px;
margin-top: 10px;
}
}
</style>';
// 用户等级
$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);
}
}
}
?>

View File

@ -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 [];
}
}
?>

View File

@ -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"
]
}

View File

@ -46,9 +46,9 @@
<div class="am-form-group">
<label>规则值</label>
<div class="rules-value">
<input type="number" placeholder="最小值" name="rules_min" data-validation-message="请填写规则最小值" class="am-radius" {{if isset($data['rules_min'])}} value="{{$data.rules_min}}"{{/if}} required />
<input type="number" placeholder="最小值" name="rules_min" step="0.01" data-validation-message="请填写规则最小值" class="am-radius" {{if isset($data['rules_min'])}} value="{{$data.rules_min}}"{{/if}} required />
<span>~</span>
<input type="number" placeholder="最大值" name="rules_max" data-validation-message="请填写规则最大值" class="am-radius" {{if isset($data['rules_max'])}} value="{{$data.rules_max}}"{{/if}} required />
<input type="number" placeholder="最大值" step="0.01" name="rules_max" data-validation-message="请填写规则最大值" class="am-radius" {{if isset($data['rules_max'])}} value="{{$data.rules_max}}"{{/if}} required />
</div>
</div>

View File

@ -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',
),
);
?>

View File

@ -0,0 +1,5 @@
.plugins-membershiplevel-goods-price-icon {
padding: 3px 5px;
background: #ffc31e;
color: #fff;
}