1.获取商品详情,SKU,规格值,规格
<?php
namespace app\home\controller;
use app\home\model\Cart;
use app\home\model\Spec;
use app\home\model\Specgoods;
use app\home\model\Brand;
use app\home\model\Goodsm;
use app\home\model\Specvalue;
use think\Controller;
use think\Request;
class Home extends Base
{
//商品首页
public function index(){
return view('index');
}
//商品列表
public function goodsList($id){
$data=Goodsm::getGoods($id);
return view('list',['data'=>$data]);
}
//商品详情
public function info($id){
//商品相册
//相册先不写
//商品表
$goodsData=Goodsm::GoodsInfo($id);
//商品sku
$specGoods=Specgoods::getSpecgoods($id);
// $specValueIds=array_column($specGoods,'value_ids');
$specPrice=[];
foreach ($specGoods as $item){
$row=[
'id'=>$item['id'],
'price'=>$item['price'],
];
$specPrice[$item['value_ids']]=$row;
}
// echo "<pre>";
// print_r($specPrice);die;
//商品规格值 相应规格值价格带出来
$specValueIds=array_column($specGoods,'value_ids');
$specValueIds=implode('_',$specValueIds);
$specValueIds=explode('_',$specValueIds);
$specValueIds=array_unique($specValueIds);
$specValue=Specvalue::getSpecValue($specValueIds);
//商品规格
$specId=array_column($specValue,'spec_id');
$spec=Spec::getSpec($specId);
return view('info',[
'goodsData'=>$goodsData,
'specGods'=>$specGoods,
'specValue'=>$specValue,
'spec'=>$spec,
'specPrice'=>json_encode($specPrice)
]);
}
public function cart(Request $request){
$data=$request->post();
// Array ( [goods_id] => 62 [spec_goods_id] => 807 [number] => 5 )
//添加到购物车
$addData=[
'user_id'=>2,
'goods_id'=>$data['goods_id'],
'number'=>$data['number'],
'spec_goods_id'=>$data['spec_goods_id'],
'is_selected'=>1,
];
$res=Cart::add( $addData);
//登录
//商品规格
$specGoodsData=Cart::cartInfo();
// print_r($specGoodsData);die;
return view('cart',['specGoodsData'=>$specGoodsData]);
}
public function upCartNum(Request $request){
$data=$request->post();
// Array ( [id] => 33 [number] => 2 )
$res=Cart::upCart($data['id'],$user_id=2,$data['number']);
if ($res){
return getJsonData(200,'成功');
}
}
}
2.Base控制器,继承Base,主要获取左侧栏目表
<?php
namespace app\home\controller;
use app\home\model\Category;
use think\Controller;
use think\Request;
class Base extends Controller
{
public function __construct(Request $request = null)
{
parent::__construct($request);
//查询所有分类
$cateAll=Category::getCate();
//处理分类
$Category=getCateTree($cateAll);
// echo "<pre>";
// print_r(collection($Category)->toArray());die;
return $this->assign('Category',$Category);
}
}
3.公共方法,封装两个方法
<?php
// 应用公共文件
function getJsonData($code,$message,$data=[]){
$row=[
'code'=>$code,
'message'=>$message,
'data'=>$data,
'deBug'=>\think\Log::getLog()
];
return json($row);
}
//树状结构
function getCateTree($data,$pid=0){
$new=[];
foreach ($data as $item){
if ($item['pid']==$pid){
$item['child']=getCateTree($data,$item['id']);
$new[]=$item;
}
}
return $new;
}
4.登录
<script>
$('.btn-danger').click(function () {
var phone=$('#prependedInput').val();
var code=$('#code').val();
$.ajax({
url:"/loginDo",
type:"post",
dataType:"json",
data:{
phone:phone,
code:code,
},
success:function (res) {
if(res.code==200){
window.location.href='http://www.laravel1904.com/index';
}
if(res.code==10010){
alert(res.message);
}
}
})
});
$('#dyMobileButton').click(function () {
var time = 60;
var phone = $('#prependedInput').val();
setInterval(function () {
time--;
if (time > 0) {
$('#dyMobileButton').html('重新发送(' + time + ')秒');
$('#dyMobileButton').prop('disabled', true);
} else {
$('#dyMobileButton').html('发送验证码');
$('#dyMobileButton').prop('disabled', false);
}
}, 1000);
$.ajax({
url:"/getCode",
type:"post",
dataType:"json",
data:{
phone:phone,
},
success:function (res) {
if(res.code==200){
alert(res.data)
}
if(res.code==10010){
alert(res.message);
}
}
})
})
</script>
5.详情页面渲染,传值到后端
<script>
var specPrice={$specPrice};
$('#specification').find('a').click(function () {
//先移除在添加
$(this).closest('dl').find('a').removeClass('selected');
$(this).addClass('selected');
//获取选中的规格值id,多个
var spec_value_id=[];
$('.selected').each(function (i,v) {
spec_value_id+=$(v).attr('spec_value_id')+'_';
})
spec_value_id=spec_value_id.slice(0,-1);
//渲染价格到页面
var goodsPrice=specPrice[spec_value_id].price;
$('#goodsPrice').html(goodsPrice);
})
$('.addshopcar').click(function () {
var spec_value_id=[];
$('.selected').each(function (i,v) {
spec_value_id+=$(v).attr('spec_value_id')+'_';
})
spec_value_id=spec_value_id.slice(0,-1);
var spec_goods_id=specPrice[spec_value_id].id;
$('input[name=spec_goods_id]').val(spec_goods_id)
//商品id
var goods_id= $('input[name=goods_id]').val();
$('input[name=goods_id]').val(goods_id)
//数量
var number= $('input[name=number]').val();
$('input[name=number]').val(number)
//提交
$('form').submit();
})
</script>
6.jquery增删改查后端
<script>
$('.check_all').click(function () {
var chebook=$(this).prop('checked')
if (chebook===true){
$('.row_check').prop('checked',true);
}else{
$('.row_check').prop('checked',false);
}
})
$('.row_check').click(function () {
var long= $('.row_check:checked').length;
var longs=$('.row_check').length;
if (long==longs){
$('.check_all').prop('checked',true);
}else{
$('.check_all').prop('checked',false);
}
})
//加减
$('.plus').click(function () {
var num=$(this).prev().val();
num++;
var price=$(this).closest('li').prev().find('.price').html();
var total_price =parseInt(price)*parseInt(num);
$(this).prev().val(num);
$(this).closest('li').next().find('.sum').html(total_price);
$(this).closest('ul').find('.row_check').prop('checked',true)
upNum();
that=$(this)
getajax(that);
// $('.summoney').html(total_price)
})
$('.mins').click(function () {
var num=$(this).next().val();
num--;
var price=$(this).closest('li').prev().find('.price').html();
var total_price =parseInt(price)*parseInt(num);
$(this).next().val(num);
$(this).closest('li').next().find('.sum').html(total_price);
$(this).closest('ul').find('.row_check').prop('checked',true)
upNum();
})
function upNum(){
var totals_price=0;
$('.row_check').each(function (i,v) {
totals_price += parseInt($(v).closest('ul').find('.sum').html());
$('.summoney').html(totals_price);
})
}
function getajax(that) {
$.ajax({
url:"/upCartNum",
type:"post",
dataType:"json",
data:{
id:that.closest('ul').find('.good-item').attr('cart_id'),
number:that.prev().val(),
},
success:function (res) {
if (res.code==200){
alert(res.message)
}
}
})
}
</script>
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!