| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- declare (strict_types=1);
- namespace app\api\controller;
- use app\BaseController;
- use app\model\api\StoreOrder;
- use app\model\api\StoreOrderCartInfo;
- use app\model\api\StoreCart;
- use app\model\api\PayTrade;
- use app\model\api\UserAddress;
- use app\model\api\StoreProductAttrValue;
- use app\Request;
- use library\services\UtilService;
- use think\facade\Db;
- use think\db\exception\DbException;
- class Shop extends BaseController
- {
- /**
- * 创建订单
- * @param Request $request
- */
- public function createOrder(Request $request)
- {
- $post = UtilService::getMore([
- ['cart_ids', '', 'empty', '参数错误'],
- ['address_id', 0],
- ], $request);
- // 获取购物车商品
- $cartIds = explode(',', $post['cart_ids']);
- $cartList = Db::name('store_cart')
- ->whereIn('id', $cartIds)
- ->where('uid', $request->user['uid'])
- ->select()
- ->toArray();
- if (empty($cartList)) {
- return app('json')->fail('购物车商品不存在');
- }
- // 获取收货地址
- $address = null;
- if ($post['address_id'] > 0) {
- $address = Db::name('user_address')->where('id', $post['address_id'])->find();
- }
- if (!$address) {
- $address = (new UserAddress())->getDefaultAddress($request->user['uid']);
- }
- if (!$address) {
- return app('json')->fail('请先添加收货地址');
- }
- // 计算订单金额
- $totalMoney = 0;
- $payMoney = 0;
- $cartInfo = [];
- foreach ($cartList as $cart) {
- $product = Db::name('store_product')->where('id', $cart['product_id'])->find();
- if (!$product) {
- continue;
- }
- $price = $product['price'];
- $skuPrice = null;
- // 如果有SKU,获取SKU价格
- if (!empty($cart['product_attr_unique'])) {
- $sku = (new StoreProductAttrValue())->getSkuBySuk($cart['product_id'], $cart['product_attr_unique']);
- if ($sku) {
- $price = $sku['price'];
- }
- }
- $productMoney = $price * $cart['cart_num'];
- $totalMoney += $productMoney;
- $cartInfo[] = [
- 'product_id' => $cart['product_id'],
- 'cart_num' => $cart['cart_num'],
- 'product_attr_unique' => $cart['product_attr_unique'],
- 'title' => $product['title'],
- 'image' => $product['image'],
- 'price' => $price,
- 'total_price' => $productMoney,
- ];
- }
- // 运费(暂时设置为0,后续可以根据运费模板计算)
- $postage = 0;
- $payMoney = $totalMoney + $postage;
- // 生成订单号
- $orderId = makeOrderId($request->user['uid'], 'SO');
- // 开启事务
- Db::startTrans();
- try {
- // 创建订单
- $orderData = [
- 'order_id' => $orderId,
- 'uid' => $request->user['uid'],
- 'real_name' => $address['real_name'],
- 'phone' => $address['phone'],
- 'province' => $address['province'],
- 'city' => $address['city'],
- 'district' => $address['district'],
- 'detail' => $address['detail'],
- 'total_price' => $totalMoney,
- 'total_num' => count($cartInfo),
- 'total_postage' => $postage,
- 'pay_price' => $payMoney,
- 'pay_postage' => $postage,
- 'paid' => 0,
- 'status' => 0,
- 'time' => time(),
- ];
- $orderIdDb = Db::name('store_order')->insertGetId($orderData);
- // 创建订单商品
- foreach ($cartInfo as &$item) {
- $item['oid'] = $orderIdDb;
- $item['time'] = time();
- }
- Db::name('store_order_cart_info')->insertAll($cartInfo);
- // 创建支付流水
- $payTrade = new PayTrade();
- $payNo = $payTrade->credentials(
- 'score',
- $request->user['uid'],
- 'shop_order',
- $payMoney,
- '商城订单支付',
- $orderId,
- ['order_id' => $orderId],
- 0
- );
- // 更新支付流水的o_id
- if ($payNo) {
- Db::name('pay_trade')->where('pay_no', $payNo)->update(['o_id' => $orderIdDb]);
- }
- // 清除购物车
- Db::name('store_cart')->whereIn('id', $cartIds)->delete();
- Db::commit();
- return app('json')->success([
- 'order_id' => $orderId,
- 'pay_money' => $payMoney,
- 'pay_no' => $payNo
- ], '订单创建成功');
- } catch (DbException $e) {
- Db::rollback();
- return app('json')->fail('订单创建失败');
- }
- }
- /**
- * 订单列表
- * @param Request $request
- */
- public function orderList(Request $request)
- {
- $post = UtilService::getMore([
- ['page', 1],
- ['pageSize', 20],
- ['status', ''],
- ], $request);
- $where = [
- ['uid', '=', $request->user['uid']]
- ];
- if ($post['status'] !== '' && in_array((string)$post['status'], ["0", "1", "2", "3", "-1"])) {
- $where[] = ['status', '=', (int)$post['status']];
- }
- $result = (new StoreOrder())->getList($where, $post['page'], $post['pageSize']);
- return app('json')->success($result);
- }
- /**
- * 订单详情
- * @param Request $request
- */
- public function orderDetail(Request $request)
- {
- $post = UtilService::getMore([
- ['order_id', '', 'empty', '参数错误'],
- ], $request);
- $order = Db::name('store_order')
- ->where('order_id', $post['order_id'])
- ->where('uid', $request->user['uid'])
- ->find();
- if (!$order) {
- return app('json')->fail('订单不存在');
- }
- // 获取订单商品
- $cartInfo = Db::name('store_order_cart_info')
- ->where('oid', $order['id'])
- ->select()
- ->toArray();
- $order['cart_info'] = $cartInfo;
- return app('json')->success($order);
- }
- /**
- * 取消订单
- * @param Request $request
- */
- public function cancelOrder(Request $request)
- {
- $post = UtilService::getMore([
- ['order_id', '', 'empty', '参数错误'],
- ], $request);
- $order = Db::name('store_order')
- ->where('order_id', $post['order_id'])
- ->where('uid', $request->user['uid'])
- ->where('status', 0)
- ->find();
- if (!$order) {
- return app('json')->fail('订单不存在或已取消');
- }
- Db::name('store_order')->where('id', $order['id'])->update(['status' => -1]);
- return app('json')->success('订单已取消');
- }
- /**
- * 确认收货
- * @param Request $request
- */
- public function confirmOrder(Request $request)
- {
- $post = UtilService::getMore([
- ['order_id', '', 'empty', '参数错误'],
- ], $request);
- $order = Db::name('store_order')
- ->where('order_id', $post['order_id'])
- ->where('uid', $request->user['uid'])
- ->where('status', 2)
- ->find();
- if (!$order) {
- return app('json')->fail('订单不存在或状态错误');
- }
- Db::name('store_order')->where('id', $order['id'])->update([
- 'status' => 3,
- 'confirm_time' => time()
- ]);
- return app('json')->success('确认收货成功');
- }
- }
|