1 前台页面
- html页面放到view下,改成??.blade.php
- 内容路径改为绝对路径,将‘./’换成‘/’
2. 路由
//商城
Route::get('/' , 'ShopController@index');
Route::get('goods/{id}' , 'ShopController@goods');
3. 生成ShopController
php artisan make:controller ShopController
4. ShopController方法逻辑
class ShopController extends Controller
{
//这块为数据库中的数据
protected $gs = [
1=>['goods_id'=>1,'goods_name'=>'白百合 清香宜人', 'price'=>23.1],
2=>['goods_id'=>2,'goods_name'=>'红玫瑰 热烈奔放', 'price'=>23.2],
3=>['goods_id'=>3,'goods_name'=>'黄牡丹 雍容华贵', 'price'=>23.3],
4=>['goods_id'=>4,'goods_name'=>'狗尾巴 淡泊名利', 'price'=>23.4],
];
// 首页
public function index(Request $request)
{
return view('index' , ['gs'=>$this->gs]);
}
public function goods($id)
{
$goods = $this->gs[$id];
return view('goods' , ['goods'=>$goods]);
}
}
//index.blae.php
@foreach ($gs as $v)
<div class="col-xs-6 goods">
<a href="/goods/{{$v['goods_id']}}"><img src="/images/goods.jpg" alt=""></a>
<p>
{{$v['goods_name']}}
¥<span>{{$v['price']}}</span>
</p>
</div>
@endforeach
//goods.blade.php
<p>
{{$goods['goods_name']}}
¥<span>{{$goods['price']}}</span>
</p>
<p>
<a class="btn btn-primary" href="{{url('/buy',['goods_id'=>$goods['goods_id']])}}">加入购物车</a>
</p>
5. 购物车
到https://packagist.org 搜索cart相关包
安装darryldecode/cart.
composer require darryldecode/cart
按说明配置:
在<project>/app/config.php中的Providers Array中加一个单元
Darryldecode\Cart\CartServiceProvider::class
在alias别名数组中加一行
Cart' => Darryldecode\Cart\Facades\CartFacade::class
添加路由
Route::get('buy/{id}' , 'ShopController@buy');
Route::get('cart' , 'ShopController@cart');
use Cart;//引入cart类
...
public function buy($id)
{
$good = $this->gs[$id];//在数据库中搜索
Cart::add($id,$good['goods_name'],$good['price'],1,[]);//向购物车中插入一条数据
return redirect('cart');//跳转到购物车
}
public function cart(Request $req)
{
/*使用微信时再打开
if( !$req->session()->has('user') ) {//判断是否有用户名
return redirect('login');
}
*/
$goods = Cart::getContent();//获取购物车商品内容
//dd($goods);
$total = Cart::getTotal();//获取购物车总价
return view('cart',['goods'=>$goods,'total'=>$total]);
}
//cart.blade.php
(cart类的相关参数看文档说明 https://packagist.org/packages/darryldecode/cart)
<h2>购物车结算</h2>
<table class="table">
<tr>
<th>商品</th>
<th>价格</th>
<th>数量</th>
</tr>
@foreach($goods as $v)
<tr>
<td>{{$v['name']}}</td>
<td>{{$v['price']}}</td>
<td>{{$v['quantity']}}</td>
</tr>
@endforeach
<tr>
<td colspan="3">小计:¥{{$total}}元</td>
</tr>
</table>
6. 订单入库
- 迁移文件建两张表orders和items
字段如下:
//items
Schema::create('items', function (Blueprint $table) {
$table->increments('iid');
$table->integer('oid');
$table->integer('gid');
$table->string('goods_name');
$table->float('price',7,2);
$table->smallinteger('amount');
});
//orders
Schema::create('orders', function (Blueprint $table) {
$table->increments('oid');
$table->char('ordsn',15);
$table->integer('uid')->unsigned();
$table->char('openid',32);
$table->char('xm',20);
$table->char('address',60);
$table->char('tel',11);
$table->float('money',7,2);
$table->boolean('ispay')->default(0);
$table->integer('ordtime')->unsigned();
});
- 建立两个表的model
php artsian make:model Order
php artsian make:model Item
- demo
use App\Item;
use App\Order;
public function done(Request $req)
{
if( !$req->session()->has('user') ) {//判断是否有用户名(使用微信调试的时候再加)
return redirect('center');
}
//接收POST数据(如地址,手机号,姓名)和登陆用户的session数据(如uid),写入orders表
$order = new Order();
$order->ordsn = date('Ymd').mt_rand(1000000,9999999);
//用户信息
//dd(session()->get('user'));
$order->openid = session()->get('user')['id'];
$order->xm = $req->xm;//用户姓名
$order->tel = $req->mobile;//用户电话
$order->address = $req->address;//用户地址
$order->money = Cart::getTotal();//总价
$order->ispay = 0;//未支付
$order->ordtime = time(); //订单生成时间
$order->save();//插入数据库orders表
//相应产品插入到items表
$goods = Cart::getContent();//所有购物车商品(购物车只能全结账)
foreach($goods as $v){
$item = new Item();
$item->oid = $order->oid;//订单的自增id
$item->gid = $v['id'];//订单id
$item->goods_name = $v['name'];//商品名称
$item->price = $v['price'];//商品价格
$item->amount = $v['quantity'];//商品数量
$item->save();
}
Cart::clear();//清空购物车
return view('done',['ordsn'=>$order->ordsn]);
}
7 支付完成,完成分销(默认支付,后面看微信支付)
- 建立fenxiao表
Schema::create('fenxiao', function (Blueprint $table) {
$table->increments('fid');
$table->integer('oid');
$table->integer('byid');
$table->integer('uid');//
$table->float('money',7,2);
});
- Fenxiao Model
class Fenxiao extends Model
{
protected $table = 'fenxiao';
protected $primaryKey = 'fid';
public $timestemps =false;
}
- demo
public function pay(Request $req){
if( !$req->session()->has('user') ) {//判断是否有用户名
return redirect('center');
}
//调用微信支付接口
//先默认支付完成
$order = Order::where('ordsn',$req->ordsn)->first();
$order->ispay = 1;//修改订单状态为支付
$order->save();
$money = $order->money;//获取支付金额
$openid = session()->get('user')['id'];//当前微信的openid
$user = User::where('openid',$openid)->first();
//分钱
foreach(['0.5'=>$user->p1,'0.25'=>$user->p2,'0.1'=>$user->p3] as $rate=>$p){
if($p > 0) {
$fee = new Fenxiao();
$fee->uid = $p;//受益人id
$fee->byid= $user->uid;//购买者id
$fee->oid= $order->oid;//订单 id
$fee->money= $order->money * $rate;//受益者分得资产
$fee->save();
}
return '购物成功';
}
}