...
|
...
|
@@ -12,10 +12,20 @@ namespace app\index\controller; |
|
|
use app\common\controller\HomeBase;
|
|
|
use app\index\model\Address;
|
|
|
use app\index\model\Area;
|
|
|
use app\index\model\OrderDetail;
|
|
|
use app\index\model\OrderInfo;
|
|
|
use app\index\model\Store;
|
|
|
use think\Db;
|
|
|
|
|
|
class Order extends HomeBase
|
|
|
{
|
|
|
/**
|
|
|
* 确认订单
|
|
|
* @return mixed
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
public function confirm(){
|
|
|
$user_id = $this->getUserId();
|
|
|
// $car_ids = $this->request->param('car_ids');
|
...
|
...
|
@@ -77,19 +87,19 @@ class Order extends HomeBase |
|
|
//配送价格
|
|
|
$postage_price = 0;
|
|
|
if($goods_total < $s['money']){
|
|
|
$postage_price = $store['freight'];
|
|
|
$postage_price = $s['freight'];
|
|
|
}
|
|
|
$store[$key2]['postage_price'] = $postage_price;
|
|
|
$store[$key2]['goods'] = $goods_arr;
|
|
|
}
|
|
|
//总合计
|
|
|
$goods_total1 = 0;
|
|
|
$postage_price1 = 0;
|
|
|
$postage_total1 = 0;
|
|
|
foreach($store as $key => $vo3){
|
|
|
$goods_total1 = $goods_total1 + $vo3['goods_total'];
|
|
|
$postage_price1 = $postage_price1 + $vo3['postage_price'];
|
|
|
$postage_total1 = $postage_total1 + $vo3['postage_price'];
|
|
|
}
|
|
|
$total = $goods_total1 + $postage_price1;
|
|
|
$total = $goods_total1 + $postage_total1;
|
|
|
//收货地址
|
|
|
$addressModel = new Address();
|
|
|
$address = $addressModel->selectData(['user_id'=>$user_id]);
|
...
|
...
|
@@ -104,9 +114,347 @@ class Order extends HomeBase |
|
|
$this->assign('city',$city);
|
|
|
$this->assign('county',$county);
|
|
|
$this->assign('store',$store);
|
|
|
// dump(collection($store)->toArray());
|
|
|
$this->assign('total',$total);
|
|
|
$this->assign('goods_total',$goods_total1);
|
|
|
$this->assign('postage_total',$postage_total1);
|
|
|
$this->assign('address',$address);
|
|
|
$this->assign('data',json_encode($data));
|
|
|
$this->assign('province_id',$province_id);
|
|
|
$this->assign('title','确认订单');
|
|
|
return $this->fetch();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 计算价格
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
public function confirm_ajax(){
|
|
|
$user_id = $this->getUserId();
|
|
|
// $car_ids = $this->request->param('car_ids');
|
|
|
$data = $this->request->param('data');
|
|
|
$province_id = $this->request->param('province_id',0,'intval');
|
|
|
$express = $this->request->param('express',0,'intval');
|
|
|
if(empty($data) || empty($province_id) || empty($express)){
|
|
|
$this->error('缺少必要参数');
|
|
|
}
|
|
|
$data = json_decode($data,true);
|
|
|
/*if(!empty($car_ids)){
|
|
|
$car_ids = explode(',',$car_ids);
|
|
|
$carModel = new \app\index\model\Car();
|
|
|
$result = $carModel->where(['id'=>$car_ids])->delete();
|
|
|
if(empty($result)){
|
|
|
$this->error('sql执行失败');
|
|
|
}
|
|
|
}*/
|
|
|
$goodsModel = new \app\index\model\Goods();
|
|
|
$goods_price = [];
|
|
|
$store_ids = [];
|
|
|
foreach($data as $key1 => $vo){
|
|
|
//获取店铺id
|
|
|
$goods = $goodsModel->findData(['g.id'=>$vo['goods_id']]);
|
|
|
$data[$key1]['store_id'] = $goods['store_id'];
|
|
|
$store_ids[] = $goods['store_id'];
|
|
|
}
|
|
|
$storeModel = new Store();
|
|
|
$store = $storeModel->selectData(['id'=>['in',$store_ids]]);
|
|
|
foreach($store as $key2 => $s){
|
|
|
if($s['is_svip'] != '1'){
|
|
|
if($s['is_vip'] != '1' && !in_array($province_id,$s['province_ids'])){
|
|
|
$this->error("店铺 【$s[name]】 不可以进行购买~");
|
|
|
}
|
|
|
}
|
|
|
$goods_ids = [];
|
|
|
$number = [];
|
|
|
foreach($data as $key3 => $vo){
|
|
|
if($vo['store_id'] == $s['id']){
|
|
|
$goods_ids[] = $vo['goods_id'];
|
|
|
$number[] = $vo['number'];
|
|
|
}
|
|
|
}
|
|
|
$store[$key2]['goods_ids'] = $goods_ids;
|
|
|
$goods_arr = [];
|
|
|
foreach($goods_ids as $key4 => $goods_id){
|
|
|
$goods = $goodsModel->findData(['g.id'=>$goods_id]);
|
|
|
if($goods['inventory'] < $number[$key4]){
|
|
|
$this->error("商品 【$goods[goodsname]】 库存不足~");
|
|
|
}
|
|
|
$goods['number'] = $number[$key4];
|
|
|
$goods_price[] = $goods['price']*$number[$key4];
|
|
|
$goods_arr[] = $goods;
|
|
|
}
|
|
|
//商品总价格
|
|
|
$store[$key2]['sum_goods_price'] = array_sum($goods_price);
|
|
|
//商品实际支付价格
|
|
|
$goods_total = $store[$key2]['sum_goods_price'];
|
|
|
$store[$key2]['goods_total'] = $goods_total;
|
|
|
//配送价格
|
|
|
$postage_price = 0;
|
|
|
if($express == 1){
|
|
|
if($goods_total < $s['money']){
|
|
|
$postage_price = $s['freight'];
|
|
|
}
|
|
|
}
|
|
|
$store[$key2]['postage_price'] = $postage_price;
|
|
|
$store[$key2]['goods'] = $goods_arr;
|
|
|
}
|
|
|
//总合计
|
|
|
$goods_total1 = 0;
|
|
|
$postage_total1 = 0;
|
|
|
foreach($store as $key => $vo3){
|
|
|
$goods_total1 = $goods_total1 + $vo3['goods_total'];
|
|
|
$postage_total1 = $postage_total1 + $vo3['postage_price'];
|
|
|
}
|
|
|
$total = $goods_total1 + $postage_total1;
|
|
|
$this->success('SUCCESS','',['total'=>$total,'postage_total'=>$postage_total1,'goods_total'=>$goods_total1]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建订单
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
public function create_order(){
|
|
|
$user_id = $this->getUserId();
|
|
|
$data = $this->request->param('data');
|
|
|
$express = $this->request->param('express',0,'intval');
|
|
|
$address_id = $this->request->param('address_id',0,'intval');
|
|
|
$pay_type = $this->request->param('pay_type',0,'intval');
|
|
|
$car_ids = $this->request->param('car_ids');
|
|
|
$province_id = $this->request->param('province_id',0,'intval');
|
|
|
if(empty($data) || empty($express) || empty($pay_type) || empty($province_id)){
|
|
|
$this->error('缺少必要参数');
|
|
|
}
|
|
|
if($express == 1){
|
|
|
if(empty($address_id)){
|
|
|
$this->error('缺少必要参数');
|
|
|
}
|
|
|
$addressModel = new Address();
|
|
|
$address = $addressModel->findData(['id'=>$address_id]);
|
|
|
}
|
|
|
$data = json_decode($data,true);
|
|
|
Db::startTrans();
|
|
|
if(!empty($car_ids)){
|
|
|
$car_ids = explode(',',$car_ids);
|
|
|
$carModel = new \app\index\model\Car();
|
|
|
$result = $carModel->where(['id'=>$car_ids])->delete();
|
|
|
if(empty($result)){
|
|
|
$this->error('sql执行失败');
|
|
|
}
|
|
|
}
|
|
|
$goodsModel = new \app\index\model\Goods();
|
|
|
$goods_price = [];
|
|
|
$store_ids = [];
|
|
|
foreach($data as $key1 => $vo){
|
|
|
//获取店铺id
|
|
|
$goods = $goodsModel->findData(['g.id'=>$vo['goods_id']]);
|
|
|
$data[$key1]['store_id'] = $goods['store_id'];
|
|
|
$store_ids[] = $goods['store_id'];
|
|
|
}
|
|
|
$storeModel = new Store();
|
|
|
$store = $storeModel->selectData(['id'=>['in',$store_ids]]);
|
|
|
foreach($store as $key2 => $s){
|
|
|
if($s['is_svip'] != '1'){
|
|
|
if($s['is_vip'] != '1' && !in_array($province_id,$s['province_ids'])){
|
|
|
$this->error("店铺 【$s[name]】 不可以进行购买~");
|
|
|
}
|
|
|
}
|
|
|
$goods_ids = [];
|
|
|
$number = [];
|
|
|
foreach($data as $key3 => $vo){
|
|
|
if($vo['store_id'] == $s['id']){
|
|
|
$goods_ids[] = $vo['goods_id'];
|
|
|
$number[] = $vo['number'];
|
|
|
}
|
|
|
}
|
|
|
$store[$key2]['goods_ids'] = $goods_ids;
|
|
|
$goods_arr = [];
|
|
|
foreach($goods_ids as $key4 => $goods_id){
|
|
|
$goods = $goodsModel->findData(['g.id'=>$goods_id]);
|
|
|
if($goods['inventory'] < $number[$key4]){
|
|
|
$this->error("商品 【$goods[goodsname]】 库存不足~");
|
|
|
}
|
|
|
$goods['number'] = $number[$key4];
|
|
|
$goods_price[] = $goods['price']*$number[$key4];
|
|
|
$goods_arr[] = $goods;
|
|
|
}
|
|
|
//商品总价格
|
|
|
$store[$key2]['sum_goods_price'] = array_sum($goods_price);
|
|
|
//商品实际支付价格
|
|
|
$goods_total = $store[$key2]['sum_goods_price'];
|
|
|
$store[$key2]['goods_total'] = $goods_total;
|
|
|
//配送价格
|
|
|
$postage_price = 0;
|
|
|
if($express == 1){
|
|
|
if($goods_total < $s['money']){
|
|
|
$postage_price = $s['freight'];
|
|
|
}
|
|
|
}
|
|
|
$store[$key2]['postage_price'] = $postage_price;
|
|
|
$store[$key2]['goods'] = $goods_arr;
|
|
|
}
|
|
|
//总合计
|
|
|
$goods_total1 = 0;
|
|
|
$postage_total1 = 0;
|
|
|
foreach($store as $key => $vo3){
|
|
|
$goods_total1 = $goods_total1 + $vo3['goods_total'];
|
|
|
$postage_total1 = $postage_total1 + $vo3['postage_price'];
|
|
|
}
|
|
|
$total = $goods_total1 + $postage_total1;
|
|
|
//创建主订单
|
|
|
$order_num = get_order_sn();
|
|
|
$arr1['num'] = $order_num."1";
|
|
|
$arr1['user_id'] = $user_id;
|
|
|
$arr1['pay_type'] = $pay_type;
|
|
|
$arr1['express'] = $express;
|
|
|
$arr1['total'] = $total;
|
|
|
$arr1['goods_total'] = $goods_total1;
|
|
|
$arr1['postage_total'] = $postage_total1;
|
|
|
$arr1['createtime'] = time();
|
|
|
$arr1['status'] = '1';
|
|
|
$orderModel = new \app\index\model\Order();
|
|
|
$result1 = $orderModel->insertData($arr1);
|
|
|
if(empty($result1)){
|
|
|
Db::rollback();
|
|
|
$this->error('sql执行失败');
|
|
|
}
|
|
|
//创建子订单
|
|
|
$orderDetailModel = new OrderDetail();
|
|
|
$orderInfoModel = new OrderInfo();
|
|
|
foreach($store as $key => $s){
|
|
|
$order_detail_num = get_order_sn();
|
|
|
$arr2['num'] = $order_detail_num."2";
|
|
|
$arr2['user_id'] = $user_id;
|
|
|
$arr2['store_id'] = $s['id'];
|
|
|
$arr2['order_id'] = $result1;
|
|
|
$arr2['pay_type'] = $pay_type;
|
|
|
$arr2['express'] = $express;
|
|
|
$arr2['total'] = $s['goods_total']+$s['postage_price'];
|
|
|
$arr2['goods_total'] = $s['goods_total'];
|
|
|
$arr2['postage_total'] = $s['postage_price'];
|
|
|
if(!empty($address)){
|
|
|
$arr2['name'] = $address['name'];
|
|
|
$arr2['mobile'] = $address['phone'];
|
|
|
$arr2['province_name'] = $address['province_name'];
|
|
|
$arr2['city_name'] = $address['city_name'];
|
|
|
$arr2['county_name'] = $address['county_name'];
|
|
|
$arr2['address'] = $address['address'];
|
|
|
}
|
|
|
$arr2['createtime'] = time();
|
|
|
$arr2['status'] = '1';
|
|
|
$result2 = $orderDetailModel->insertData($arr2);
|
|
|
if(empty($result2)){
|
|
|
Db::rollback();
|
|
|
$this->error('sql执行失败');
|
|
|
}
|
|
|
//创建订单详情
|
|
|
foreach($s['goods'] as $key2 => $g){
|
|
|
$arr3 = [];
|
|
|
$arr3['order_id'] = $result1;
|
|
|
$arr3['order_detail_id'] = $result2;
|
|
|
$arr3['user_id'] = $user_id;
|
|
|
$arr3['goods_id'] = $g['id'];
|
|
|
$arr3['goodsname'] = $g['goodsname'];
|
|
|
$arr3['goods_image'] = $g['thumbnail'];
|
|
|
$arr3['goods_price'] = $g['price'];
|
|
|
$arr3['number'] = $g['number'];
|
|
|
$arr3['createtime'] = time();
|
|
|
$result3 = $orderInfoModel->insertData($arr3);
|
|
|
if(empty($result3)){
|
|
|
Db::rollback();
|
|
|
$this->error('sql执行失败');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
Db::commit();
|
|
|
$this->success('SUCCESS','',['order_id'=>$result1,'url'=>url("index/order/pay",array('order_id'=>$result1),false,true)]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 唤起支付
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
public function pay(){
|
|
|
$user_id = $this->getUserId();
|
|
|
$order_id = $this->request->param('order_id',0,'intval');
|
|
|
$order_detail_id = $this->request->param('order_detail_id',0,'intval');
|
|
|
if(empty($order_id) && empty($order_detail_id)){
|
|
|
$this->error('缺少必要参数');
|
|
|
}
|
|
|
$orderInfoModel = new OrderInfo();
|
|
|
if(!empty($order_id)){
|
|
|
$orderModel = new \app\index\model\Order();
|
|
|
$data = $orderModel->findData(['id'=>$order_id,'user_id'=>$user_id]);
|
|
|
$order_info = $orderInfoModel->selectData(['order_id'=>$order_id,'user_id'=>$user_id]);
|
|
|
}else{
|
|
|
$orderDetailModel = new OrderDetail();
|
|
|
$data = $orderDetailModel->findData(['id'=>$order_detail_id,'user_id'=>$user_id]);
|
|
|
$order_info = $orderInfoModel->selectData(['order_detail_id'=>$order_detail_id,'user_id'=>$user_id]);
|
|
|
}
|
|
|
if(empty($data)){
|
|
|
$this->error('查询为空');
|
|
|
}
|
|
|
if($data['status'] != '1'){
|
|
|
$this->error('订单状态不合法');
|
|
|
}
|
|
|
$goodsModel = new \app\index\model\Goods();
|
|
|
foreach($order_info as $key => $o_i){
|
|
|
$goods = $goodsModel->findData(['g.id'=>$o_i['goods_id']]);
|
|
|
if($goods['inventory'] < $o_i['number']){
|
|
|
$this->error("商品 【$goods[goodsname]】 库存不足~");
|
|
|
}
|
|
|
}
|
|
|
if($data['pay_type'] == '1'){
|
|
|
//微信支付
|
|
|
$wechatPay = new WechatPay();
|
|
|
$code_url = $wechatPay->index2($data);
|
|
|
if(empty($code_url)){
|
|
|
$this->error('生成二维码失败');
|
|
|
}
|
|
|
\PHPQRCode\QRcode::png($code_url);
|
|
|
}else if($data['pay_type'] == '2'){
|
|
|
//支付宝支付
|
|
|
$params['subject'] = "购买商品";
|
|
|
$params['out_trade_no'] = $data['num'];
|
|
|
$params['total_amount'] = 0.01;//$data['total'];
|
|
|
$result = \alipay\Pagepay::pay2($params);
|
|
|
echo $result;
|
|
|
}else{
|
|
|
$this->error('未知的支付类型');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检测订单支付状态
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
public function order_pay(){
|
|
|
$user_id = $this->getUserId();
|
|
|
$order_id = $this->request->param('order_id',0,'intval');
|
|
|
$order_detail_id = $this->request->param('order_detail_id',0,'intval');
|
|
|
if(empty($order_id) && empty($order_detail_id)){
|
|
|
$this->error('缺少必要参数');
|
|
|
}
|
|
|
if(!empty($order_id)){
|
|
|
$orderModel = new \app\index\model\Order();
|
|
|
$data = $orderModel->findData(['id'=>$order_id,'user_id'=>$user_id]);
|
|
|
}else{
|
|
|
$orderDetailModel = new OrderDetail();
|
|
|
$data = $orderDetailModel->findData(['id'=>$order_detail_id,'user_id'=>$user_id]);
|
|
|
}
|
|
|
if(empty($data)){
|
|
|
$this->error('查询为空');
|
|
|
}
|
|
|
if($data['status'] != '1'){
|
|
|
$this->success('SUCCESS',url("index/index/index"));
|
|
|
}
|
|
|
$this->error('尚未支付');
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|