From e0222e2bef05f01dfe6ff74f977b1a49ad57bd4e Mon Sep 17 00:00:00 2001 From: devil_gong Date: Wed, 20 Feb 2019 15:56:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=B3=E4=BE=A7=E5=BF=AB=E6=8D=B7=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E6=96=B0=E5=A2=9E=E8=B4=AD=E7=89=A9=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common.php | 42 ++++ .../plugins/commonrightnavigation/Index.php | 27 +++ .../commonrightnavigation/index/content.html | 105 ++++++---- application/service/BuyService.php | 18 ++ public/static/common/js/common.js | 183 ++++++++++-------- public/static/index/default/js/buy.js | 2 +- public/static/index/default/js/goods.js | 45 +++-- public/static/index/default/js/useraddress.js | 2 +- .../css/commonrightnavigation/style.css | 16 +- .../plugins/js/commonrightnavigation/style.js | 86 ++++++++ 10 files changed, 377 insertions(+), 149 deletions(-) diff --git a/application/common.php b/application/common.php index 407fcdda9..9250ff49d 100755 --- a/application/common.php +++ b/application/common.php @@ -11,6 +11,22 @@ // 应用公共文件 +/** + * 金额格式化 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-02-20 + * @desc description + * @param [float] $value [金额] + * @param [int] $decimals [保留的位数] + * @param [string] $dec_point [保留小数分隔符] + */ +function PriceNumberFormat($value, $decimals = 2, $dec_point = '.') +{ + return number_format($value, $decimals, $dec_point, ''); +} + /** * json带格式输出 * @author Devil @@ -1215,6 +1231,32 @@ function ParamsChecked($data, $params) return $v['error_msg']; } break; + + // 最小 + case 'min' : + if (!isset($v['checked_data'])) + { + return '验证最小值未定义'; + } + $fun = $v['checked_data']; + if($data[$v['key_name']] < $v['checked_data']) + { + return $v['error_msg']; + } + break; + + // 最大 + case 'max' : + if (!isset($v['checked_data'])) + { + return '验证最大值未定义'; + } + $fun = $v['checked_data']; + if($data[$v['key_name']] > $v['checked_data']) + { + return $v['error_msg']; + } + break; } } return true; diff --git a/application/plugins/commonrightnavigation/Index.php b/application/plugins/commonrightnavigation/Index.php index 17ec11c91..725f5058d 100644 --- a/application/plugins/commonrightnavigation/Index.php +++ b/application/plugins/commonrightnavigation/Index.php @@ -12,6 +12,7 @@ namespace app\plugins\commonrightnavigation; use think\Controller; use app\service\AnswerService; +use app\service\BuyService; /** * 右侧快捷导航 - 前端 @@ -36,5 +37,31 @@ class Index extends Controller $params['user'] = session('user'); return AnswerService::Add($params); } + + /** + * 购物车 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-02-07T08:21:54+0800 + * @param [array] $params [输入参数] + */ + public function cart($params = []) + { + // 购物车 + $cart_list = BuyService::CartList(['user'=>session('user')]); + + // 基础数据 + $base = [ + 'total_price' => empty($cart_list['data']) ? '0.00' : PriceNumberFormat(array_sum(array_column($cart_list['data'], 'total_price'))), + 'cart_count' => empty($cart_list['data']) ? 0 : count($cart_list['data']), + 'ids' => empty($cart_list['data']) ? '' : implode(',', array_column($cart_list['data'], 'id')), + ]; + $data = [ + 'cart_list' => $cart_list['data'], + 'base' => $base, + ]; + return DataReturn('操作成功', 0, $data); + } } ?> \ No newline at end of file diff --git a/application/plugins/view/commonrightnavigation/index/content.html b/application/plugins/view/commonrightnavigation/index/content.html index de0bb9e8f..8379f6c2e 100755 --- a/application/plugins/view/commonrightnavigation/index/content.html +++ b/application/plugins/view/commonrightnavigation/index/content.html @@ -33,18 +33,18 @@ -
+
购物车 -
{{if $cart_total > 9}}9+{{else /}}{{$cart_total}}{{/if}}
+
{{if $cart_total > 9}}9+{{else /}}{{$cart_total}}{{/if}}
- {{if !empty($cart_list)}} - -
+ +
+ {{if !empty($cart_list)}} {{foreach $cart_list as $goods}} @@ -65,48 +65,48 @@ - {{/foreach}}
+ ¥{{$goods.total_price}} x{{$goods.stock}} - 删除 + 删除
-
+ {{/if}} +
- -
-
- {{$base.cart_count}} 种商品 - 共计: - ¥{{$base.total_price}} - - - -
+ +
+
+ {{$base.cart_count}} 种商品 + 共计: + ¥{{$base.total_price}} + + + +
+
+ + +
+ +
+

您的购物车还是空的,您可以

+
- {{/if}} - {{if empty($cart_list)}} -
- -
-

您的购物车还是空的,您可以

- -
-
- {{/if}} +
@@ -178,4 +178,33 @@
- \ No newline at end of file + + + \ No newline at end of file diff --git a/application/service/BuyService.php b/application/service/BuyService.php index d16636808..109297b86 100755 --- a/application/service/BuyService.php +++ b/application/service/BuyService.php @@ -47,6 +47,12 @@ class BuyService 'key_name' => 'stock', 'error_msg' => '购买数量有误', ], + [ + 'checked_type' => 'min', + 'key_name' => 'stock', + 'checked_data' => 1, + 'error_msg' => '购买数量有误', + ], [ 'checked_type' => 'empty', 'key_name' => 'user', @@ -275,6 +281,12 @@ class BuyService 'key_name' => 'stock', 'error_msg' => '购买数量有误', ], + [ + 'checked_type' => 'min', + 'key_name' => 'stock', + 'checked_data' => 1, + 'error_msg' => '购买数量有误', + ], [ 'checked_type' => 'empty', 'key_name' => 'user', @@ -322,6 +334,12 @@ class BuyService 'key_name' => 'stock', 'error_msg' => '购买数量有误', ], + [ + 'checked_type' => 'min', + 'key_name' => 'stock', + 'checked_data' => 1, + 'error_msg' => '购买数量有误', + ], [ 'checked_type' => 'empty', 'key_name' => 'goods_id', diff --git a/public/static/common/js/common.js b/public/static/common/js/common.js index c76fa3405..f1bc46c49 100755 --- a/public/static/common/js/common.js +++ b/public/static/common/js/common.js @@ -785,97 +785,118 @@ function DataDelete(e) var url = e.data('url'); var view = e.data('view') || 'delete'; var value = e.data('value') || null; - var title = e.data('title') || '温馨提示'; - var msg = e.data('msg') || '删除后不可恢复、确认操作吗?'; var ext_delete_tag = e.data('ext-delete-tag') || null; - AMUI.dialog.confirm({ - title: title, - content: msg, - onConfirm: function(options) + if((id || null) == null || (url || null) == null) + { + Prompt('参数配置有误'); + return false; + } + + // 请求删除数据 + $.ajax({ + url:url, + type:'POST', + dataType:"json", + timeout:10000, + data:{"id":id}, + success:function(result) { - if((id || null) == null || (url || null) == null) + if(result.code == 0) { - Prompt('参数配置有误'); - } else { - // 请求删除数据 - $.ajax({ - url:url, - type:'POST', - dataType:"json", - timeout:10000, - data:{"id":id}, - success:function(result) - { - if(result.code == 0) + Prompt(result.msg, 'success'); + + switch(view) + { + // 成功则删除数据列表 + case 'delete' : + Prompt(result.msg, 'success'); + $('#data-list-'+id).remove(); + if(ext_delete_tag != null) { - Prompt(result.msg, 'success'); - - switch(view) - { - // 成功则删除数据列表 - case 'delete' : - Prompt(result.msg, 'success'); - $('#data-list-'+id).remove(); - if(ext_delete_tag != null) - { - $(ext_delete_tag).remove(); - } - break; - - // 刷新 - case 'reload' : - Prompt(result.msg, 'success'); - setTimeout(function() - { - window.location.reload(); - }, 1500); - break; - - // 回调函数 - case 'fun' : - if(IsExitsFunction(value)) - { - result['data_id'] = id; - window[value](result); - } else { - Prompt('['+value+']配置方法未定义'); - } - break; - - // 跳转 - case 'jump' : - Prompt(result.msg, 'success'); - if(value != null) - { - setTimeout(function() - { - window.location.href = value; - }, 1500); - } - break; - - // 默认提示成功 - default : - Prompt(result.msg, 'success'); - } - // 成功则删除数据列表 - $('#data-list-'+id).remove(); - } else { - Prompt(result.msg); + $(ext_delete_tag).remove(); } - }, - error:function(xhr, type) - { - Prompt('网络异常出错'); - } - }); + break; + + // 刷新 + case 'reload' : + Prompt(result.msg, 'success'); + setTimeout(function() + { + window.location.reload(); + }, 1500); + break; + + // 回调函数 + case 'fun' : + if(IsExitsFunction(value)) + { + result['data_id'] = id; + window[value](result); + } else { + Prompt('['+value+']配置方法未定义'); + } + break; + + // 跳转 + case 'jump' : + Prompt(result.msg, 'success'); + if(value != null) + { + setTimeout(function() + { + window.location.href = value; + }, 1500); + } + break; + + // 默认提示成功 + default : + Prompt(result.msg, 'success'); + } + // 成功则删除数据列表 + $('#data-list-'+id).remove(); + } else { + Prompt(result.msg); } }, - onCancel: function(){} + error:function(xhr, type) + { + Prompt('网络异常出错'); + } }); } +/** + * [ConfirmDataDelete 数据删除] + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2018-09-24T08:24:58+0800 + * @param {[object]} e [当前元素对象] + */ +function ConfirmDataDelete(e) +{ + var title = e.data('title') || '温馨提示'; + var msg = e.data('msg') || '删除后不可恢复、确认操作吗?'; + var is_confirm = (e.data('is-confirm') == undefined || e.data('is-confirm') == 1) ? 1 : 0; + + if(is_confirm == 1) + { + AMUI.dialog.confirm({ + title: title, + content: msg, + onConfirm: function(options) + { + DataDelete(e); + }, + onCancel: function(){} + }); + } else { + DataDelete(e); + } +} + /** * [ConfirmNetworkAjax 确认网络请求] * @author Devil @@ -987,7 +1008,7 @@ $(function() */ $(document).on('click', '.submit-delete', function() { - DataDelete($(this)); + ConfirmDataDelete($(this)); }); /** diff --git a/public/static/index/default/js/buy.js b/public/static/index/default/js/buy.js index c2da83ceb..737a7fdab 100755 --- a/public/static/index/default/js/buy.js +++ b/public/static/index/default/js/buy.js @@ -119,7 +119,7 @@ $(function() // 阻止事件冒泡 $('.address-submit-delete').on('click', function(e) { - DataDelete($(this)); + ConfirmDataDelete($(this)); e.stopPropagation(); }); diff --git a/public/static/index/default/js/goods.js b/public/static/index/default/js/goods.js index f5e8ec0cc..24969e580 100755 --- a/public/static/index/default/js/goods.js +++ b/public/static/index/default/js/goods.js @@ -31,8 +31,8 @@ function CartAdd(e) { // 参数 var type = e.attr('data-type'); - var stock = $('#text_box').val(); - if((stock || 0) <= 0) + var stock = $('#text_box').val() || 0; + if(stock <= 0 || stock < 1) { PromptCenter('购买数量有误'); return false; @@ -491,6 +491,26 @@ $(function() { $('.goods-video-submit-start').removeClass('none'); }); + //获得文本框对象 + var t = $('#text_box'); + + //数量增加操作 + $('#add').on('click', function() { + var stock = parseInt($('.stock-tips .stock').text()); + var number = parseInt(t.val()); + if(number < stock) + { + t.val(number + 1) + } else { + Prompt('超出库存数量'); + } + }); + //数量减少操作 + $('#min').on('click', function() { + var value = parseInt(t.val())-1 || 1; + t.val((value <= 1) ? 1 : value); + }); + }); // 浏览器窗口实时事件 @@ -503,25 +523,4 @@ $(window).resize(function() } else { poptit_pc_show(); } -}); - -$(document).ready(function() { - //获得文本框对象 - var t = $('#text_box'); - - //数量增加操作 - $('#add').on('click', function() { - var stock = parseInt($('.stock-tips .stock').text()); - var number = parseInt(t.val()); - if(number < stock) - { - t.val(number + 1) - } - }); - //数量减少操作 - $('#min').on('click', function() { - var value = parseInt(t.val())-1 || 1; - t.val(value); - }) - }); \ No newline at end of file diff --git a/public/static/index/default/js/useraddress.js b/public/static/index/default/js/useraddress.js index 77d804a70..169c2458f 100755 --- a/public/static/index/default/js/useraddress.js +++ b/public/static/index/default/js/useraddress.js @@ -12,7 +12,7 @@ $(function() // 阻止事件冒泡 $('.address-submit-delete').on('click', function(e) { - DataDelete($(this)); + ConfirmDataDelete($(this)); e.stopPropagation(); }); diff --git a/public/static/plugins/css/commonrightnavigation/style.css b/public/static/plugins/css/commonrightnavigation/style.css index 08537215e..644b1e3ca 100755 --- a/public/static/plugins/css/commonrightnavigation/style.css +++ b/public/static/plugins/css/commonrightnavigation/style.css @@ -58,7 +58,7 @@ position: absolute; right: 35px; display: none; - box-shadow: -5px 5px 15px 0px rgba(0,0,0,.4); + box-shadow: -5px 0px 15px 5px rgba(0,0,0,.4); } .commonrightnavigation-right-nav .base-nav:hover { background: #d2364c; @@ -188,7 +188,7 @@ bottom: 0; width: 400px; min-height: 100px; - max-height: 300px; + max-height: 360px; background: #fff; color: #666; } @@ -200,6 +200,7 @@ .commonrightnavigation-right-nav .favor-content, .commonrightnavigation-right-nav .qrcode-content, .commonrightnavigation-right-nav .cart-content, + .commonrightnavigation-right-nav .commonrightnavigation-cart a.nav-a, .commonrightnavigation-right-nav .go-top .mui-mbar-tab-tip, .commonrightnavigation-right-nav .answer-content { display: none; @@ -254,7 +255,7 @@ } .commonrightnavigation-cart .cart-content .cart-items { min-height: 60px; - max-height: 260px; + max-height: 320px; overflow-x: auto; } .commonrightnavigation-cart .cart-content .goods-detail img { @@ -337,6 +338,11 @@ .commonrightnavigation-cart .cart-content .cart-nav .nav-total-price { margin-right: 5px; } -.commonrightnavigation-cart .cart-content .mixed-tips .icon { - margin-top: 25px; +.commonrightnavigation-cart .cart-content .mixed-tips { + margin-bottom: 2%; + margin-top: 2%; } + +.commonrightnavigation-cart .cart-content .mixed-tips-content { + margin-top: 20px; +} \ No newline at end of file diff --git a/public/static/plugins/js/commonrightnavigation/style.js b/public/static/plugins/js/commonrightnavigation/style.js index 8878bec6f..0bb829ac9 100755 --- a/public/static/plugins/js/commonrightnavigation/style.js +++ b/public/static/plugins/js/commonrightnavigation/style.js @@ -10,4 +10,90 @@ $(function() $("#plugins-commonrightnavigation").fadeOut(1000); } }); + + // 购物车查询 + $('.commonrightnavigation-cart').on('mouseenter', function() + { + // 当前鼠标是否还在元素上,防止鼠标直接进入子级元素导致重复执行事件 + if($(this).attr('data-is-has-mouse') == 1) + { + return false; + } else { + $(this).attr('data-is-has-mouse', 1); + } + + // url + var $this = $(this); + var ajax_url = $this.data('cart-ajax-url'); + var delete_url = $this.data('cart-delete-ajax-url'); + + // ajax请求 + $.ajax({ + url: ajax_url, + type: 'post', + dataType: "json", + timeout: 10000, + data: {}, + success: function(result) + { + if(result.code == 0 && result.data.cart_list.length > 0) + { + var html = ''; + for(var i in result.data.cart_list) + { + html += ''; + html += ''; + html += ''; + html += ''; + } + html += '
'; + html += '
'; + html += ''; + html += ''; + html += ''; + html += '
'; + html += ''+result.data.cart_list[i]['title']+''; + if((result.data.cart_list[i]['spec'] || null) != null) + { + html += '
    '; + for(var s in result.data.cart_list[i]['spec']) + { + html += '
  • '+result.data.cart_list[i]['spec'][s]['type']+':'+result.data.cart_list[i]['spec'][s]['value']+'
  • '; + } + html += '
'; + } + html += '
'; + html += '¥'+result.data.cart_list[i]['total_price']+''; + html += ' x'+result.data.cart_list[i]['stock']+''; + html += ''; + html += '删除'; + html += '
'; + $this.find('.cart-items').html(html); + $this.find('.mixed-tips').hide(); + $this.find('.cart-nav').show(); + $this.find('.cart-items').show(); + $this.find('.cart-nav .selected-tips strong').text(result.data.base.cart_count); + $this.find('.cart-nav .nav-total-price').text('¥'+result.data.base.total_price); + $this.find('.cart-nav input[name="ids"]').val(result.data.base.ids); + HomeCartNumberTotalUpdate(result.data.base.cart_count); + } else { + $this.find('.mixed-tips').show(); + $this.find('.cart-nav').hide(); + $this.find('.cart-items').hide(); + $this.find('.cart-nav .selected-tips strong').text(0); + $this.find('.cart-nav .nav-total-price').text('¥0.00'); + $this.find('.cart-nav input[name="ids"]').val(''); + HomeCartNumberTotalUpdate(0); + } + }, + error: function(xhr, type) + { + Prompt('服务器错误'); + } + }); + }).mouseleave(function() + { + // 鼠标离开元素标记 + $(this).attr('data-is-has-mouse', 0); + }); }); \ No newline at end of file