...
|
...
|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
use app\api\model\UserScoreLog;
|
|
|
use app\common\controller\Api;
|
|
|
use think\Db;
|
|
|
use think\Validate;
|
...
|
...
|
@@ -15,6 +16,13 @@ class Order extends Api |
|
|
protected $carModel;
|
|
|
protected $areaExtendModel;
|
|
|
protected $integralGoodsModel;
|
|
|
protected $goodsModel;
|
|
|
protected $ticketCodeModel;
|
|
|
protected $userTicketModel;
|
|
|
protected $orderAddressModel;
|
|
|
protected $orderAddressInvoiceModel;
|
|
|
protected $orderInfoModel;
|
|
|
protected $userModel;
|
|
|
|
|
|
protected function _initialize()
|
|
|
{
|
...
|
...
|
@@ -23,6 +31,13 @@ class Order extends Api |
|
|
$this->carModel = new \app\api\model\Car();
|
|
|
$this->areaExtendModel = new \app\api\model\AreaExtend();
|
|
|
$this->integralGoodsModel = new \app\api\model\IntegralGoods();
|
|
|
$this->goodsModel = new \app\api\model\Goods();
|
|
|
$this->ticketCodeModel = new \app\api\model\Ticketcode();
|
|
|
$this->userTicketModel = new \app\api\model\UserTicket();
|
|
|
$this->orderAddressModel = new \app\api\model\OrderAddress();
|
|
|
$this->orderAddressInvoiceModel = new \app\api\model\OrderAddressInvoice();
|
|
|
$this->orderInfoModel = new \app\api\model\OrderInfo();
|
|
|
$this->userModel = new \app\api\model\User();
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -39,14 +54,24 @@ class Order extends Api |
|
|
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
|
|
|
* @ApiRoute (/api/order/order)
|
|
|
* @ApiReturn({
|
|
|
|
|
|
"code": 1,
|
|
|
"msg": "success",
|
|
|
"time": "1587691008",
|
|
|
"data": {
|
|
|
"price": 263.7, 总价
|
|
|
"discount_price": 12.3, 折扣价
|
|
|
"original_price": 246, 原价
|
|
|
"ticketcode_discount_price": 5, 优惠码优惠价
|
|
|
"ticket_discount_price": 10, 优惠券优惠价
|
|
|
"integral": 0, 积分
|
|
|
"postage": "30.00" 运费
|
|
|
}
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function order()
|
|
|
{
|
|
|
$carModel = new \app\api\model\Car();
|
|
|
$userAddressModel = new \app\api\model\UserAddress();
|
|
|
$goodsModel = new \app\api\model\Goods();
|
|
|
$ticketCodeModel = new \app\api\model\Ticketcode();
|
|
|
$userTicketModel = new \app\api\model\UserTicket();
|
...
|
...
|
@@ -59,13 +84,11 @@ class Order extends Api |
|
|
$countyId = $this->request->param('county_id');
|
|
|
$postageType = $this->request->param('postage_type');
|
|
|
if (empty($carId)) $this->error('缺少参数 car_id!');
|
|
|
if (empty($provinceId)) $this->error('缺少参数 province_id!');
|
|
|
if (empty($cityId)) $this->error('缺少参数 city_id!');
|
|
|
if (empty($countyId)) $this->error('缺少参数 county_id!');
|
|
|
//判断优惠券和优惠码不能同时使用
|
|
|
if (!empty($ticketCode) && !empty($ticketId)) $this->error('优惠券与优惠码不能同时使用!');
|
|
|
if (empty($provinceId)) {
|
|
|
//获取用户默认地址
|
|
|
$address = $userAddressModel->getDefaultData(['user_id' => $userId]);
|
|
|
$provinceId = $address['province_id'];
|
|
|
}
|
|
|
//获取普通商品id
|
|
|
$goodsIds = $carModel->where(['id' => ['in', $carId], 'type' => 1])->column('goods_id');
|
|
|
//获取积分商品id
|
...
|
...
|
@@ -125,23 +148,31 @@ class Order extends Api |
|
|
if (!empty($v['integral'])) $data['integral'] += $v['integral'];
|
|
|
|
|
|
}
|
|
|
//判断积分
|
|
|
if ($data['integral'] > $this->user['score']) $this->error('积分不足');
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
|
|
|
|
|
|
//特殊地区满200免运费
|
|
|
if ($is_special == 1 && $data['price'] >= 200) {
|
|
|
$postage = 0;
|
|
|
//其他地区满300免运费
|
|
|
} elseif ($data['price'] >= 300) {
|
|
|
$postage = 0;
|
|
|
} else {
|
|
|
//配送为闪送
|
|
|
//都不满足获取对应的运费
|
|
|
$postageWhere = ['province_id' => $provinceId, 'city_id' => $cityId, 'county_id' => $countyId];
|
|
|
if ($postageType == 2) {
|
|
|
//配送为闪送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage2');
|
|
|
}
|
|
|
} else {
|
|
|
//配送为普通配送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
|
|
|
}
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage1');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage1');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$data['postage'] = $postage;
|
...
|
...
|
@@ -170,16 +201,30 @@ class Order extends Api |
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=car_id, type=string, required=true, description="购物车id 例 1,2,3")
|
|
|
* @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
|
|
|
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
|
|
|
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
|
|
|
* @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
|
|
|
* @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
|
|
|
* @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
|
|
|
* @ApiParams (name=address, type=string, required=true, description="商品收货地址")
|
|
|
* @ApiParams (name=name, type=string, required=true, description="商品收货人")
|
|
|
* @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
|
|
|
* @ApiParams (name=present_id, type=string, required=false, description="赠品id")
|
|
|
* @ApiParams (name=delivery, type=string, required=false, description="配送时间")
|
|
|
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=个人或事业单位,2=企业")
|
|
|
* @ApiParams (name=email, type=string, required=false, description="电子发票邮箱")
|
|
|
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
|
|
|
* @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
|
|
|
* @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
|
|
|
* @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
|
|
|
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
|
|
|
* @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
|
|
|
* @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
|
|
|
* @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
|
|
|
* @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
|
|
|
* @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
|
|
|
* @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
|
|
|
* @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
|
|
|
* @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
|
|
|
* @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
|
|
|
* @ApiParams (name=order_note, type=string, required=false, description="订单备注")
|
|
|
* @ApiRoute (/api/order/createOrder)
|
|
|
* @ApiReturn({
|
...
|
...
|
@@ -189,7 +234,293 @@ class Order extends Api |
|
|
*/
|
|
|
public function createOrder()
|
|
|
{
|
|
|
$userId = $this->getUserId();
|
|
|
$param = $this->request->param();
|
|
|
$param['user_id'] = $userId;
|
|
|
$validate = new Validate([
|
|
|
'car_id' => 'require',
|
|
|
'pay_type' => 'require',
|
|
|
'name' => 'require',
|
|
|
'mobile' => 'require',
|
|
|
'address' => 'require',
|
|
|
'province_id' => 'require',
|
|
|
'city_id' => 'require',
|
|
|
'county_id' => 'require',
|
|
|
'postage_type' => 'require',
|
|
|
'invoice_status' => 'require',
|
|
|
|
|
|
]);
|
|
|
|
|
|
$validate->message([
|
|
|
'car_id' => '缺少参数 car_id!',
|
|
|
'pay_type' => '缺少参数 pay_type!',
|
|
|
'name' => '缺少参数 name!',
|
|
|
'mobile' => '缺少参数 mobile!',
|
|
|
'address' => '缺少参数 address!',
|
|
|
'province_id' => '缺少参数 province_id!',
|
|
|
'city_id' => '缺少参数 city_id!',
|
|
|
'county_id' => '缺少参数 county_id!',
|
|
|
'postage_type' => '缺少参数 postage_type!',
|
|
|
'invoice_status' => '缺少参数 invoice_status!',
|
|
|
]);
|
|
|
|
|
|
if (!$validate->check($param)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
|
|
|
//判断优惠券和优惠码不能同时使用
|
|
|
if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
|
|
|
//获取普通商品id
|
|
|
$goodsIds = $this->carModel->where(['id' => ['in', $param['car_id']], 'type' => 1])->column('goods_id');
|
|
|
//获取积分商品id
|
|
|
$goodsIntegralIds = $this->carModel->where(['id' => ['in', $param['car_id']], 'type' => 2])->column('goods_id');
|
|
|
//获取普通商品状态
|
|
|
if ($goodsIds) {
|
|
|
$goodsList = $this->goodsModel->selectGoodsStatus(['id' => ['in', $goodsIds]]);
|
|
|
|
|
|
//判断商品是否有库存和状态
|
|
|
foreach ($goodsList as $k => $v) {
|
|
|
if ($v['status'] == 2) $this->error($v['ch_name'] . '商品已下架');
|
|
|
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取积分商品状态
|
|
|
if ($goodsIntegralIds) {
|
|
|
$goodsIntegralList = $this->integralGoodsModel->selectGoodsStatus(['id' => ['in', $goodsIntegralIds]]);
|
|
|
|
|
|
//判断商品是否有库存和状态
|
|
|
foreach ($goodsIntegralList as $k => $v) {
|
|
|
if ($v['status'] == 2) $this->error($v['ch_name'] . '商品已下架');
|
|
|
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
|
|
|
}
|
|
|
}
|
|
|
//判断优惠码是否可用
|
|
|
if (!empty($param['ticket_code'])) {
|
|
|
$ticketCodeInfo = $this->ticketCodeModel->where('code', $param['ticket_code'])->find();
|
|
|
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
|
|
|
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
|
|
|
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
|
|
|
}
|
|
|
|
|
|
//判断优惠券是否可用
|
|
|
if (!empty($param['ticket_user_id'])) {
|
|
|
$ticketInfo = $this->userTicketModel->where(['user_id' => $userId, 'id' => $param['ticket_user_id'], 'status' => 1])->find();
|
|
|
if (!$ticketInfo) $this->error('优惠码不存在!');
|
|
|
}
|
|
|
|
|
|
//获取原价,现价,折扣
|
|
|
$priceArr = $this->carModel->getCarMoney(['id' => ['in', $param['car_id']]]);
|
|
|
$data['price'] = 0; //折扣后价格
|
|
|
$data['discount_price'] = 0; //折扣金额
|
|
|
$data['original_price'] = 0; //原价
|
|
|
$data['integral'] = 0; //积分
|
|
|
foreach ($priceArr as $k => $v) {
|
|
|
$priceArr[$k]['user_type'] = $this->user['type'];
|
|
|
$data['price'] += round(get_price($v), 2);
|
|
|
$data['discount_price'] += round(get_discount_price($v), 2);
|
|
|
$data['original_price'] += $v['goods_price'];
|
|
|
if (!empty($v['integral'])) $data['integral'] += $v['integral'];
|
|
|
|
|
|
}
|
|
|
//判断积分
|
|
|
if ($data['integral'] > $this->user['score']) $this->error('积分不足');
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
|
|
|
//特殊地区满200免运费
|
|
|
if ($is_special == 1 && $data['price'] >= 200) {
|
|
|
$postage = 0;
|
|
|
//其他地区满300免运费
|
|
|
} elseif ($data['price'] >= 300) {
|
|
|
$postage = 0;
|
|
|
} else {
|
|
|
//都不满足获取对应的运费
|
|
|
$postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
|
|
|
if ($param['postage_type'] == 2) {
|
|
|
//配送为闪送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
|
|
|
}
|
|
|
} else {
|
|
|
//配送为普通配送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$data['postage'] = $postage;
|
|
|
$data['price'] += $postage;
|
|
|
|
|
|
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
//使用优惠码
|
|
|
if (!empty($ticketCodeInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketCodeInfo['price'];
|
|
|
$data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
|
|
|
$data['ticketcode_id'] = $ticketCodeInfo['id'];
|
|
|
$this->ticketCodeModel->where('id', $ticketCodeInfo['id'])->update(['user_id' => $userId]);
|
|
|
}
|
|
|
|
|
|
//使用优惠券
|
|
|
if (!empty($ticketInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketInfo['price'];
|
|
|
$data['ticket_discount_price'] = $ticketInfo['price'];
|
|
|
$data['user_ticket_id'] = $ticketInfo['id'];
|
|
|
$this->userTicketModel->where('id', $ticketInfo['id'])->update(['status' => 2]);
|
|
|
|
|
|
}
|
|
|
|
|
|
//order表数据
|
|
|
$orderData = [
|
|
|
'order_num' => get_order_num(),
|
|
|
'user_id' => $userId,
|
|
|
'pay_type' => $param['pay_type'],
|
|
|
'goods_total' => $data['original_price'],
|
|
|
'pay_total' => $data['price'],
|
|
|
'discount_price' => $data['discount_price'],
|
|
|
'score' => $data['integral'],
|
|
|
'postage_total' => $postage,
|
|
|
'postage_type' => $param['postage_type'],
|
|
|
'postage_date' => $param['postage_date'],
|
|
|
'postage_time' => $param['postage_time'],
|
|
|
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
|
|
|
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
|
|
|
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
|
|
|
'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
|
|
|
'invoice_status' => $param['invoice_status'],
|
|
|
'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
|
|
|
'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
|
|
|
'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
|
|
|
'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
|
|
|
'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
|
|
|
'type' => 1,
|
|
|
'status' => 1,
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res1 = $this->orderModel->insertGetId($orderData);
|
|
|
|
|
|
//收货地址数据
|
|
|
$orderAddress = [
|
|
|
'order_id' => $res1,
|
|
|
'name' => $param['name'],
|
|
|
'mobile' => $param['mobile'],
|
|
|
'address' => $param['address'],
|
|
|
'province_id' => $param['province_id'],
|
|
|
'city_id' => $param['city_id'],
|
|
|
'county_id' => $param['county_id'],
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res2 = $this->orderAddressModel->insertData($orderAddress);
|
|
|
if ($param['invoice_status'] != 3) {
|
|
|
//收货地址数据
|
|
|
$orderAddressInvoice = [
|
|
|
'order_id' => $res1,
|
|
|
'name' => $param['invoice_name'],
|
|
|
'mobile' => $param['invoice_mobile'],
|
|
|
'address' => $param['invoice_address'],
|
|
|
'province_id' => $param['invoice_province_id'],
|
|
|
'city_id' => $param['invoice_city_id'],
|
|
|
'county_id' => $param['invoice_county_id'],
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddress);
|
|
|
}
|
|
|
$where = ['order_address_id' => $res2, 'id' => $res1];
|
|
|
if (!empty($res3)) $where['order_address_invoice_id'] = $res3;
|
|
|
|
|
|
$this->orderModel->update($where);
|
|
|
|
|
|
$list = $this->carModel->getCarMoney(['id' => ['in', $param['car_id']]]);
|
|
|
$orderInfoData = [];
|
|
|
foreach ($list as $k => $goodsItem) {
|
|
|
$goodsItem['user_type'] = $this->user['type'];
|
|
|
$orderInfoData[$k] = [
|
|
|
'order_id' => $res1,
|
|
|
'pay_type' => $param['pay_type'],
|
|
|
'user_id' => $userId,
|
|
|
'goods_id' => $goodsItem['id'],
|
|
|
'ch_goods_name' => $goodsItem['ch_name'],
|
|
|
'en_goods_name' => $goodsItem['en_name'],
|
|
|
'goods_image' => $goodsItem['image'],
|
|
|
'number' => $goodsItem['number'],
|
|
|
'type' => $goodsItem['type'],
|
|
|
'score' => !empty($goodsItem['integral']) ? $goodsItem['integral'] : '',
|
|
|
'goods_total' => get_price($goodsItem),
|
|
|
'pay_total' => $data['price'],
|
|
|
'postage_total' => $postage,
|
|
|
'status' => 1,
|
|
|
];
|
|
|
|
|
|
|
|
|
}
|
|
|
$res4 = $this->orderInfoModel->saveAll($orderInfoData);
|
|
|
|
|
|
//有积分商品减积分
|
|
|
if (!empty($goodsItem['integral'])) {
|
|
|
$userScoreLogModel = new UserScoreLog();
|
|
|
$res5 = $userScoreLogModel->setDecScore($userId, $goodsItem['integral']);
|
|
|
}
|
|
|
//减库存
|
|
|
|
|
|
if ($res1 && $res2 && $res4 && (empty($res3) || $res3) && (empty($res5) || $res5)) {
|
|
|
Db::commit();
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
Db::rollback();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (订单付款)
|
|
|
* @ApiSummary (订单付款)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/order/orderPay)
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
* @ApiParams (name=order_id, type=string, required=true, description="订单id")
|
|
|
* @ApiReturn ({
|
|
|
})
|
|
|
*/
|
|
|
public function orderPay()
|
|
|
{
|
|
|
$param = $this->request->param();
|
|
|
$rule = [
|
|
|
'order_id' => 'require',
|
|
|
];
|
|
|
|
|
|
$msg = [
|
|
|
'order_id' => '缺少参数 order_id!',
|
|
|
];
|
|
|
|
|
|
$validate = new Validate($rule, $msg);
|
|
|
$result = $validate->check($param);
|
|
|
if (!$result) {
|
|
|
$this->error(__($validate->getError()));
|
|
|
}
|
|
|
|
|
|
$order = $this->orderModel->where(['id' => $param['order_id']])->find();
|
|
|
$amount = $order['pay_total'];
|
|
|
$payType = $order['pay_type'];
|
|
|
if ($payType == 2) {
|
|
|
//余额支付
|
|
|
if ($this->user['money'] >= $amount) {
|
|
|
$res = $this->orderModel->balancePay($param['order_id'], $this->auth->id, $amount);
|
|
|
if ($res) $this->success('支付成功');
|
|
|
else $this->error('支付失败');
|
|
|
} else {
|
|
|
$this->error('余额不足');
|
|
|
}
|
|
|
} elseif ($payType == '1') {
|
|
|
//微信支付
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|