feat: 修改IndexController类以创建订单,并移除Cart模型的主键和表名

This commit is contained in:
mkm 2024-06-03 15:31:36 +08:00
parent 438ef7dfd6
commit d49f9438b7
4 changed files with 120 additions and 9 deletions

View File

@ -4,16 +4,22 @@ namespace app\api\controller;
use app\admin\validate\tools\GenerateTableValidate;
use app\admin\logic\tools\GeneratorLogic;
use app\common\logic\store_order\StoreOrderLogic;
use think\facade\Db;
use Webman\Config;
class IndexController extends BaseApiController
{
public $notNeedLogin = ['index','app_update','express_list','province','city','area','street'];
public $notNeedLogin = ['app_update','express_list','province','city','area','street'];
public function index()
{
d(uniqid(1 . '_', true));
$params=['store_id'=>2,'pay_type'=>17];
$a=StoreOrderLogic::createOrder([1],0,null,$params);
d($a);
return json(['msg' =>create_password(123456, '11d3')]);
}

View File

@ -74,13 +74,9 @@ class UserTokenCache extends BaseCache
$user = User::where('id', '=', $userSession->user_id)
->find();
$merchant=Merchant::where('uid',$userSession->user_id)->field('mer_id,mer_name,service_phone')->find();
$supplier=Supplier::where('uid',$userSession->user_id)->with('userAuth')->find();
$userInfo = [
'user_id' => $user->id,
'nickname' => $user->nickname,
'merchant' => $merchant,
'supplier' => $supplier,
'token' => $token,
'mobile' => $user->mobile,
'avatar' => $user->avatar,

View File

@ -0,0 +1,110 @@
<?php
namespace app\common\logic\store_order;
use app\common\logic\BaseLogic;
use app\common\enum\PayEnum;
use app\common\model\order\Cart;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\user\UserAddress;
use think\facade\Db;
class StoreOrderLogic extends BaseLogic
{
public static $total;
/**
* @notes 获取购物车商品信息
* @param $params
* @return array|bool
*/
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [])
{
$where = ['is_pay' => 0, 'is_fail' => 0];
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray();
if (empty($cart_select)) {
self::setError('购物车为空');
return false;
}
try {
self::$total = 0;
/** 计算价格 */
foreach ($cart_select as $k => $v) {
$find = StoreProduct::where(['id' => $v['product_id']])->field('store_name,image,unit,price')->find();
if(!$find){
continue;
}
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2);
$cart_select[$k]['price'] = $find['price'];
$cart_select[$k]['name'] = $find['store_name'];
$cart_select[$k]['image'] = $find['image'];
$cart_select[$k]['cart_id'] = $v['id'];
$cart_select[$k]['product_id'] = $v['product_id'];
$cart_select[$k]['cart_num'] = $v['cart_num'];
$cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
self::$total=bcadd(self::$total, $cart_select[$k]['total'], 2);
}
$order = [
'time' => time(),
'number' => getNewOrderId('PF'),
'total' => self::$total,
'pay_type' => $params['pay_type'] ?? 0,
'cart_id' => implode(',', $cartId),
'delivery_msg'=>' 预计48小时发货 '
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
return ['order' => $order, 'cart_list' => $cart_select];
}
/**
* 创建新订单
* @return Object|bool
*/
static public function createOrder($cartId, $addressId, $user = null, $params = [])
{
$orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params);
if(!$orderInfo){
return false;
}
$_order = $orderInfo['order'];
$_order['deduction_price'] = 0;
$_order['store_id'] = $params['store_id'];
$_order['uid'] = request()->userId;
if($addressId>0){
$address=UserAddress::where(['id'=>$addressId,'uid'=>Request()->userId])->find();
if($address){
$_order['real_name'] = $address['real_name'];
$_order['user_phone'] = $address['phone'];
$_order['user_address'] = $address['detail'];
// $_order['address_id'] = $addressId;
}
}
if($params['pay_type']==PayEnum::CASH_PAY){
$_order['money']=$_order['total'];
}
Db::startTrans();
try {
$order = StoreOrder::create($_order);
$goods_list = $orderInfo['cart_list'];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['oid'] = $order->id;
$goods_list[$k]['uid'] = request()->userId;
}
(new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0, 'is_fail' => 0];
Cart::whereIn('id', $cartId)->where($where)->update(['is_pay'=>1]);
Db::commit();
return $order;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}

View File

@ -15,8 +15,7 @@ use think\model\concern\SoftDelete;
class Cart extends BaseModel
{
use SoftDelete;
protected $pk = 'cart_id';
protected $name = 'cart';
protected $name = 'store_cart';
protected $deleteTime = 'delete_time';