老师我有一个问题
来源:10-14 测试订单接口
qq_唱首绅士给党听_0
2018-09-26
点击下单接口之后报这样的错误,我看着也是很无奈呀!这个是怎么回事,也看不懂是什么错误,下面的代码是我的service/order的代码。请老师帮我看看谢谢!!
<?php
/**
* Created by PhpStorm.
* User: 田博阳
* Date: 2018/9/22
* Time: 20:56
*/
namespace app\api\service;
use app\api\modle\OrderProduct;
use app\api\modle\Product;
use app\api\modle\UserAddress;
use app\api\validate\OrderException;
use app\lib\exception\UserException;
use think\Exception;
class Order
{
//订单商品列表,也就是客户端传过来的products参数
protected $oProducts;
//真实的商品信息(库存量)
protected $products;
protected $uid;
public function place($uid, $oProducts)
{
//oProducts与products做对比
//products从数据库中查出来
$this->oProducts = $oProducts;
$this->products = $this->getProductsByOrder($oProducts);
$this->uid = $uid;
$status = $this->getOrderStatus();
if (!$status['pass']) {
$status['order_id'] = -1;
return $status;
}
//订单快照
$orderSnap = $this->snapOrder($status);
//开始创建订单
$order = $this->createOrder($orderSnap);
$order['pass'] = true;
return $order;
}
//写入订单的信息
private function createOrder($snap)
{
try {
$orderNo = $this->makeOrderNo();
$order = new \app\api\modle\Order();
$order->user_id = $this->uid;
$order->order_no = $orderNo;
$order->total_price = $snap['orderPrice'];
$order->total_count = $snap['totalCount'];
$order->snap_img = $snap['snapImg'];
$order->snap_name = $snap['snapName'];
$order->snap_address = $snap['snapAddress'];
$order->snap_items = json_encode($snap['pStatus']);
$order->save();
$orderID = $order->id;
$create_time = $order->create_time;
foreach ($this->oProducts as &$p) {
$p['order_id'] = $orderID;
}
$orderProduct = new OrderProduct();
$orderProduct->saveAll($this->oProducts);
return [
'order_no' => $orderNo,
'order_id' => $orderID,
'create_time' => $create_time
];
} catch (Exception $ex) {
throw $ex;
}
}
//生成订单号
public static function makeOrderNo()
{
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$orderSn =
$yCode[intval(date('Y')) - 2017] . strtoupper(dechex(date('m'))) . date(
'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
'%02d', rand(0, 99));
return $orderSn;
}
//生成订单快照
private function snapOrder($status)
{
$snap = [
'orderPrice' => 0,
'totalCount' => 0,
'pStatus' => [],
'snapAddress' => null,
'snapName' => '',
'snapImg' => ''
];
$snap['orderPrice'] = $status['orderPrice'];
$snap['totalCount'] = $status['totalCount'];
$snap['pStatus'] = $status['pStatusArray'];
$snap['snapAddress'] = json_encode($this->getUserAddress());
$snap['snapName'] = $this->products[0]['name'];
$snap['snapImg'] = $this->products[0]['main_img_url'];
if (count($this->products) > 1) {
$snap['snapName'] .= '等';
}
}
private function getUserAddress()
{
$userAddress = UserAddress::where('user_id', '=', $this->uid)->find();
if (!$userAddress) {
throw new UserException([
'msg' => '用户收货地址不存在,下单失败',
'errorCode' => 60001
]);
}
return $userAddress->toArray();
}
private function getOrderStatus()
{
$status = [
'pass' => true,
'orderPrice' => 0,
'totalCount' => 0,
'pStatusArray' => []
];
foreach ($this->oProducts as $oProduct) {
$pStatus = $this->getProductsStatus(
$oProduct['product_id'], $oProduct['count'], $this->products
);
if (!$pStatus['haveStock']) {
$status['pass'] = false;
}
$status['orderPrice'] += $pStatus['totalPrice'];
$status['totalCount'] += $pStatus['count'];
array_push($status['pStatusArray'], $pStatus);
}
return $status;
}
private function getProductsStatus($oPID, $oCount, $products)
{
$pIndex = -1;
$pStatus = [
'id' => null,
'haveStock' => false,
'count' => 0,
'name' => '',
'totalPrice' => 0
];
for ($i = 0; $i < count($products); $i++) {
if ($oPID == $products[$i]['id']) {
$pIndex = $i;
}
}
if ($pIndex == -1) {
//客户端传递的products_id可能根本是不存在的
throw new OrderException([
'msg' => 'id为' . $oPID . '商品不存在,创建订单失败'
]);
} else {
$product = $products[$pIndex];
$pStatus['id'] = $product['id'];
$pStatus['name'] = $product['name'];
$pStatus['count'] = $oCount;
$pStatus['totalPrice'] = $product['price'] * $oCount;
if ($product['stock'] - $oCount >= 0) {
$pStatus['haveStock'] = true;
}
}
return $pStatus;
}
//根据订单信息,查找真实的商品信息
private function getProductsByOrder($oProducts)
{
// foreach ($oProducts as $oProduct){
//// //循环的查询数据库,非常容易把数据库弄坏,数据量太大
//// }
//先将每一个商品的id号查询出来
$oPIDs = [];
foreach ($oProducts as $item) {
array_push($oPIDs, $item['product_id']);
}
$products = Product::all($oPIDs)
->visible(['id', 'price', 'stock', 'name', 'main_img_url'])
->toArray();
return $products;
}
}
写回答
1回答
-
7七月
2018-09-26
你太为难我了,报错说了 一个值是null。但你贴代码,我肯定看不出来,我估计也没几个人能从这么长代码里帮你找到错误。还是要自己调试,没别的办法。
022018-09-26
相似问题