buy order

feat/task1-c-wallet
devil_gong 2018-09-26 18:41:25 +08:00
parent dd26728a07
commit 077b4dbcc0
10 changed files with 366 additions and 30 deletions

View File

@ -43,23 +43,13 @@ class BuyController extends CommonController
{
if(IS_POST)
{
// 获取商品列表
$params = $_POST;
$params['user'] = $this->user;
switch(I('buy_type'))
{
// 正常购买
case 'goods' :
$ret = BuyService::BuyGoods($params);
break;
// 购物车
case 'cart' :
$ret = BuyService::BuyCart($params);
break;
}
$ret = BuyService::BuyTypeGoodsList($params);
// 商品校验
if(isset($ret['code']) && $ret['code'] == 0 && !empty($ret['data']))
if(isset($ret['code']) && $ret['code'] == 0)
{
// 用户地址
$this->assign('user_address_list', UserService::UserAddressList(['user'=>$this->user])['data']);
@ -103,7 +93,10 @@ class BuyController extends CommonController
{
if(IS_POST)
{
print_r($_POST);
$params = $_POST;
$params['user'] = $this->user;
$ret = BuyService::OrderAdd($params);
$this->ajaxReturn($ret['msg'], $ret['code'], $ret['data']);
} else {
$this->assign('msg', L('common_unauthorized_access'));
$this->display('/Public/TipsError');

View File

@ -179,7 +179,7 @@
<div class="buy-message">
<div class="order-extra">
<div class="order-user-info">
<div id="holyshit257" class="memo">
<div class="memo">
<label>买家留言:</label>
<input type="text" title="选填,对本次交易的说明(建议填写已经和卖家达成一致的说明)" placeholder="选填,建议填写和卖家达成一致的说明" class="memo-input">
</div>
@ -245,6 +245,7 @@
<input type="hidden" name="address_id" value="0" />
<input type="hidden" name="express_id" value="0" />
<input type="hidden" name="payment_id" value="0" />
<input type="hidden" name="user_note" value="" />
<div class="go-btn-wrap">
<button type="submit" class="btn-go btn-loading-example" title="点击此按钮,提交订单" data-am-loading="{loadingText:'{{:L('common_form_loading_tips')}}'}">提交订单</button>
</div>

View File

@ -29,6 +29,7 @@ class BannerService
$images_host = C('IMAGE_HOST');
foreach($banner as &$v)
{
$v['images_url_old'] = $v['images_url'];
$v['images_url'] = $images_host.$v['images_url'];
$v['jump_url'] = empty($v['jump_url']) ? null : $v['jump_url'];
}

View File

@ -41,7 +41,8 @@ class BrandService
$images_host = C('IMAGE_HOST');
foreach($brand as &$v)
{
$v['logo'] = $images_host.$v['logo'];
$v['logo_old'] = $v['logo'];
$v['logo'] = empty($v['logo']) ? null : $images_host.$v['logo'];
$v['website_url'] = empty($v['website_url']) ? null : $v['website_url'];
}
}

View File

@ -3,6 +3,7 @@
namespace Service;
use Service\GoodsService;
use Service\UserService;
/**
* 购买服务层
@ -154,19 +155,24 @@ class BuyService
$where = (!empty($params['where']) && is_array($params['where'])) ? $params['where'] : [];
$where['c.user_id'] = $params['user']['id'];
$field = 'c.*, g.title, g.images, g.original_price, g.price, g.inventory, g.inventory_unit, g.is_shelves, g.is_delete_time';
$field = 'c.*, g.title, g.images, g.original_price, g.price, g.inventory, g.inventory_unit, g.is_shelves, g.is_delete_time, g.buy_min_number, g.buy_max_number';
$data = M('Cart')->alias('c')->join(' __GOODS__ AS g ON g.id=c.goods_id')->where($where)->field($field)->select();
if(!empty($data) && is_array($data))
if(empty($data) || !is_array($data))
{
$images_host = C('IMAGE_HOST');
foreach($data as &$v)
{
$v['goods_url'] = HomeUrl('Goods', 'Index', ['id'=>$v['goods_id']]);
$v['images'] = empty($v['images']) ? null : $images_host.$v['images'];
$v['attribute'] = empty($v['attribute']) ? null : json_decode($v['attribute'], true);
$v['total_price'] = $v['stock']*$v['price'];
}
return DataReturn(L('common_not_data_tips'), -100);
}
// 数据处理
$images_host = C('IMAGE_HOST');
foreach($data as &$v)
{
$v['goods_url'] = HomeUrl('Goods', 'Index', ['id'=>$v['goods_id']]);
$v['images_old'] = $v['images'];
$v['images'] = empty($v['images']) ? null : $images_host.$v['images'];
$v['attribute'] = empty($v['attribute']) ? null : json_decode($v['attribute'], true);
$v['total_price'] = $v['stock']*$v['price'];
}
return DataReturn(L('common_operation_success'), 0, $data);
}
@ -316,7 +322,7 @@ class BuyService
'g.is_delete_time' => 0,
'g.is_shelves' => 1,
],
'field' => 'g.id, g.id AS goods_id, g.title, g.images, g.original_price, g.price, g.inventory, g.inventory_unit',
'field' => 'g.id, g.id AS goods_id, g.title, g.images, g.original_price, g.price, g.inventory, g.inventory_unit, g.buy_min_number, g.buy_max_number',
];
$goods = GoodsService::GoodsList($p);
if(empty($goods[0]))
@ -376,5 +382,266 @@ class BuyService
];
return self::CartList($params);
}
/**
* 根据购买类型获取商品列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-26
* @desc description
* @param [array] $params [输入参数]
*/
public static function BuyTypeGoodsList($params = [])
{
switch($params['buy_type'])
{
// 正常购买
case 'goods' :
$ret = BuyService::BuyGoods($params);
break;
// 购物车
case 'cart' :
$ret = BuyService::BuyCart($params);
break;
// 默认
$ret = DataReturn('参数有误', -1);
}
return $ret;
}
/**
* 购买商品校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-26
* @desc description
* @param [array] $params [输入参数]
*/
public static function BuyGoodsCheck($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'goods',
'error_msg' => '商品信息有误',
]
];
$ret = params_checked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 数据校验
$total_price = 0;
$m = M('Goods');
$attr_type_m = M('GoodsAttributeType');
$attr_m = M('GoodsAttribute');
foreach($params['goods'] as $v)
{
$goods = $m->find($v['goods_id']);
// 基础判断
if(empty($goods))
{
return DataReturn('['.$v['goods_id'].']商品不存在', -1);
}
if($goods['is_shelves'] != 1)
{
return DataReturn('['.$v['goods_id'].']商品已下架', -1);
}
if($v['stock'] > $goods['inventory'])
{
return DataReturn('['.$v['goods_id'].']购买数量超过商品库存数量['.$v['stock'].'>'.$goods['inventory'].']', -1);
}
if($goods['buy_min_number'] > 1 && $v['stock'] < $goods['buy_min_number'])
{
return DataReturn('['.$v['goods_id'].']低于商品起购数量['.$v['stock'].'<'.$goods['buy_min_number'].']', -1);
}
if($goods['buy_max_number'] > 1 && $v['stock'] > $goods['buy_max_number'])
{
return DataReturn('['.$v['goods_id'].']超过商品限购数量['.$v['stock'].'>'.$goods['buy_max_number'].']', -1);
}
// 属性
if(!empty($v['attribute']))
{
foreach($v['attribute'] as $vs)
{
// 属性类型
$attr_type = $attr_type_m->find($vs['attr_type_id']);
if(empty($attr_type))
{
return DataReturn('['.$v['goods_id'].']商品属性类型有误['.$vs['attr_type_id'].']', -1);
}
// 具体属性
$attr_content = $attr_m->find($vs['attr_id']);
if(empty($attr_content))
{
return DataReturn('['.$v['goods_id'].']商品属性有误-'.$vs['attr_id'].']', -1);
}
}
}
// 总价
$total_price += $goods['price']*$v['stock'];
$result[] = $goods;
}
$data = [
'total_price' => $total_price,
];
return DataReturn(L('common_operation_success'), 0, $data);
}
/**
* 订单添加
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-26
* @desc description
* @param [array] $params [输入参数]
*/
public static function OrderAdd($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'user',
'error_msg' => '用户信息有误',
],
[
'checked_type' => 'empty',
'key_name' => 'address_id',
'error_msg' => '地址有误',
],
[
'checked_type' => 'empty',
'key_name' => 'express_id',
'error_msg' => '快递有误',
],
[
'checked_type' => 'empty',
'key_name' => 'payment_id',
'error_msg' => '支付方式有误',
],
];
$ret = params_checked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 清单商品
$goods = self::BuyTypeGoodsList($params);
if(!isset($goods['code']) || $goods['code'] != 0)
{
return $goods;
}
$check = self::BuyGoodsCheck(['goods'=>$goods['data']]);
if(!isset($check['code']) || $check['code'] != 0)
{
return $check;
}
// 用户地址
$address = UserService::UserAddressRow(array_merge($params, ['id'=>$params['address_id']]));
if(empty($address))
{
return $address;
}
// 优惠金额
$preferential_price = 0.00;
// 店铺
$shop_id = 0;
// 订单写入
$order = [
'user_id' => $params['user']['id'],
'shop_id' => $shop_id,
'receive_address_id' => $address['data']['id'],
'receive_name' => $address['data']['name'],
'receive_tel' => $address['data']['tel'],
'receive_province' => $address['data']['province'],
'receive_city' => $address['data']['city'],
'receive_county' => $address['data']['county'],
'receive_address' => $address['data']['address'],
'user_note' => I('user_note', null, null, $params),
'status' => 1,
'preferential_price' => $preferential_price,
'price' => $check['data']['total_price'],
'total_price' => $check['data']['total_price']-$preferential_price,
'express_id' => intval($params['express_id']),
'payment_id' => intval($params['payment_id']),
'add_time' => time(),
'confirm_time' => time(),
];
// 开始事务
$m = M('Order');
$m->startTrans();
// 订单添加
$order_id = $m->add($order);
if($order_id > 0)
{
$detail_m = M('OrderDetail');
foreach($goods['data'] as $v)
{
$detail = [
'order_id' => $order_id,
'user_id' => $params['user']['id'],
'shop_id' => $shop_id,
'goods_id' => $v['id'],
'title' => $v['title'],
'images' => $v['images_old'],
'original_price' => $v['original_price'],
'price' => $v['price'],
'attribute' => empty($v['attribute']) ? '' : json_encode($v['attribute']),
'buy_number' => $v['stock'],
'add_time' => time(),
];
if($detail_m->add($detail) <= 0)
{
$m->rollback();
return DataReturn('订单详情添加失败', -1);
}
}
} else {
$m->rollback();
return DataReturn('订单添加失败', -1);
}
$m->commit();
// 获取订单信息
switch($order['status'])
{
// 预约成功
case 0 :
$msg = L('common_booking_success');
break;
// 提交成功
case 1 :
$msg = L('common_submit_success');
break;
// 默认操作成功
default :
$msg = L('common_operation_success');
}
return DataReturn($msg, 0, $m->find($order_id));
}
}
?>

View File

@ -101,6 +101,7 @@ class GoodsService
}
if(isset($v['big_images']))
{
$v['big_images_old'] = $v['big_images'];
$v['big_images'] = empty($v['big_images']) ? null : $images_host.$v['big_images'];
}
}
@ -210,12 +211,14 @@ class GoodsService
// 商品封面图片
if(isset($v['images']))
{
$v['images_old'] = $v['images'];
$v['images'] = empty($v['images']) ? null : $images_host.$v['images'];
}
// 商品首页推荐图片,不存在则使用商品封面图片
if(isset($v['home_recommended_images']))
{
$v['home_recommended_images_old'] = $v['home_recommended_images'];
$v['home_recommended_images'] = empty($v['home_recommended_images']) ? (empty($v['images']) ? null : $v['images']) : $images_host.$v['home_recommended_images'];
}

View File

@ -0,0 +1,66 @@
<?php
namespace Service;
use Service\GoodsService;
/**
* 订单服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class OrderService
{
/**
* 订单支付
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-26
* @desc description
* @param [array] $params [输入参数]
*/
public static function Pay($params = [])
{
if(empty($params['id']))
{
return DataReturn('请选择订单', -1);
}
$m = M('Order');
$where = ['id'=>intval($params['id']), 'user_id' => $this->user['id']];
$data = $m->where($where)->field('id,status,total_price')->find();
if(empty($data))
{
return DataReturn(L('common_data_no_exist_error'), -1);
}
if($data['total_price'] <= 0.00)
{
return DataReturn('金额不能为0', -1);
}
if($data['status'] != 1)
{
$status_text = L('common_order_user_status')[$data['status']]['name'];
return DataReturn('状态不可操作['.$status_text.']', -1);
}
// 发起支付
$notify_url = __MY_URL__.'Notify/order.php';
$pay_data = array(
'out_user' => md5($this->user['id']),
'order_sn' => $data['id'].GetNumberCode(6),
'name' => '订单支付',
'total_price' => $data['total_price'],
'notify_url' => $notify_url,
);
$pay = (new \Library\Alipay())->SoonPay($pay_data, C("alipay_key_secret"));
if(empty($pay))
{
return DataReturn('支付接口异常', -1);
}
return DataReturn(L('common_operation_success'), 0, $pay);
}
}
?>

View File

@ -43,6 +43,7 @@ class ResourcesService
$images_host = C('IMAGE_HOST');
foreach($data as &$v)
{
$v['icon_old'] = $v['icon'];
$v['icon'] = empty($v['icon']) ? null : $images_host.$v['icon'];
}
}
@ -67,6 +68,7 @@ class ResourcesService
$images_host = C('IMAGE_HOST');
foreach($data as &$v)
{
$v['logo_old'] = $v['logo'];
$v['logo'] = empty($v['logo']) ? null : $images_host.$v['logo'];
}
}

View File

@ -63,7 +63,7 @@ class UserService
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-09-23T23:19:25+0800
* @param array $params [description]
* @param [array] $params [输入参数]
*/
public static function UserAddressRow($params = [])
{

View File

@ -164,8 +164,10 @@ $(function()
return false;
}
console.log(address_id, express_id, payment_id);
$('form.nav-buy input[name=address_id]').val(address_id);
$('form.nav-buy input[name=express_id]').val(express_id);
$('form.nav-buy input[name=payment_id]').val(payment_id);
$('form.nav-buy input[name=user_note]').val($('.order-user-info input.memo-input').val());
});
});