1. Obtain product details, SKU, specification value, specification
<? 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:: goodsList($id); return view('list',['data'=>$data]); } public function info($id){$goodsData= :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) '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,' success '); }}}Copy the code
2.Base controller, inherit Base, mainly obtain the left column table
<? 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); }}Copy the code
3. Public method, encapsulating two methods
<? 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; }Copy the code
4. Login
<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). The HTML (' resend (' + time + 'seconds'); $('#dyMobileButton').prop('disabled', true); } else {$('#dyMobileButton').html(' send verification code '); $('#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>Copy the code
5. Render the detail page and transfer the value to the back end
<script> var specPrice={$specPrice}; $(' # specification). The find (' a '). Click (function () {/ / remove first in add $(this) closest (' dl). Find (' a '). RemoveClass (" selected "); $(this).addClass('selected'); Spec_value_id =[]; 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] '). The val (spec_goods_id)/var/commodity id goods_id = $(' input [name = goods_id] '). The val (); $(' input [name = goods_id] '). The val (goods_id)/quantity/var number = $(' input [name = number] '). The val (); $(' input [name = number] "). The val (number) / / submit $(' form '). The submit (); }) </script>Copy the code
6. Jquery add, delete, modify and check the backend
<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); }}) / / to add and subtract $(' plus'). Click (function () {var num = $(this). Prev (). The 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>Copy the code