Shop.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller;
  4. use app\BaseController;
  5. use app\model\api\StoreOrder;
  6. use app\model\api\StoreOrderCartInfo;
  7. use app\model\api\StoreCart;
  8. use app\model\api\PayTrade;
  9. use app\model\api\UserAddress;
  10. use app\model\api\StoreProductAttrValue;
  11. use app\Request;
  12. use library\services\UtilService;
  13. use think\facade\Db;
  14. use think\db\exception\DbException;
  15. class Shop extends BaseController
  16. {
  17. /**
  18. * 创建订单
  19. * @param Request $request
  20. */
  21. public function createOrder(Request $request)
  22. {
  23. $post = UtilService::getMore([
  24. ['cart_ids', '', 'empty', '参数错误'],
  25. ['address_id', 0],
  26. ], $request);
  27. // 获取购物车商品
  28. $cartIds = explode(',', $post['cart_ids']);
  29. $cartList = Db::name('store_cart')
  30. ->whereIn('id', $cartIds)
  31. ->where('uid', $request->user['uid'])
  32. ->select()
  33. ->toArray();
  34. if (empty($cartList)) {
  35. return app('json')->fail('购物车商品不存在');
  36. }
  37. // 获取收货地址
  38. $address = null;
  39. if ($post['address_id'] > 0) {
  40. $address = Db::name('user_address')->where('id', $post['address_id'])->find();
  41. }
  42. if (!$address) {
  43. $address = (new UserAddress())->getDefaultAddress($request->user['uid']);
  44. }
  45. if (!$address) {
  46. return app('json')->fail('请先添加收货地址');
  47. }
  48. // 计算订单金额
  49. $totalMoney = 0;
  50. $payMoney = 0;
  51. $cartInfo = [];
  52. foreach ($cartList as $cart) {
  53. $product = Db::name('store_product')->where('id', $cart['product_id'])->find();
  54. if (!$product) {
  55. continue;
  56. }
  57. $price = $product['price'];
  58. $skuPrice = null;
  59. // 如果有SKU,获取SKU价格
  60. if (!empty($cart['product_attr_unique'])) {
  61. $sku = (new StoreProductAttrValue())->getSkuBySuk($cart['product_id'], $cart['product_attr_unique']);
  62. if ($sku) {
  63. $price = $sku['price'];
  64. }
  65. }
  66. $productMoney = $price * $cart['cart_num'];
  67. $totalMoney += $productMoney;
  68. $cartInfo[] = [
  69. 'product_id' => $cart['product_id'],
  70. 'cart_num' => $cart['cart_num'],
  71. 'product_attr_unique' => $cart['product_attr_unique'],
  72. 'title' => $product['title'],
  73. 'image' => $product['image'],
  74. 'price' => $price,
  75. 'total_price' => $productMoney,
  76. ];
  77. }
  78. // 运费(暂时设置为0,后续可以根据运费模板计算)
  79. $postage = 0;
  80. $payMoney = $totalMoney + $postage;
  81. // 生成订单号
  82. $orderId = makeOrderId($request->user['uid'], 'SO');
  83. // 开启事务
  84. Db::startTrans();
  85. try {
  86. // 创建订单
  87. $orderData = [
  88. 'order_id' => $orderId,
  89. 'uid' => $request->user['uid'],
  90. 'real_name' => $address['real_name'],
  91. 'phone' => $address['phone'],
  92. 'province' => $address['province'],
  93. 'city' => $address['city'],
  94. 'district' => $address['district'],
  95. 'detail' => $address['detail'],
  96. 'total_price' => $totalMoney,
  97. 'total_num' => count($cartInfo),
  98. 'total_postage' => $postage,
  99. 'pay_price' => $payMoney,
  100. 'pay_postage' => $postage,
  101. 'paid' => 0,
  102. 'status' => 0,
  103. 'time' => time(),
  104. ];
  105. $orderIdDb = Db::name('store_order')->insertGetId($orderData);
  106. // 创建订单商品
  107. foreach ($cartInfo as &$item) {
  108. $item['oid'] = $orderIdDb;
  109. $item['time'] = time();
  110. }
  111. Db::name('store_order_cart_info')->insertAll($cartInfo);
  112. // 创建支付流水
  113. $payTrade = new PayTrade();
  114. $payNo = $payTrade->credentials(
  115. 'score',
  116. $request->user['uid'],
  117. 'shop_order',
  118. $payMoney,
  119. '商城订单支付',
  120. $orderId,
  121. ['order_id' => $orderId],
  122. 0
  123. );
  124. // 更新支付流水的o_id
  125. if ($payNo) {
  126. Db::name('pay_trade')->where('pay_no', $payNo)->update(['o_id' => $orderIdDb]);
  127. }
  128. // 清除购物车
  129. Db::name('store_cart')->whereIn('id', $cartIds)->delete();
  130. Db::commit();
  131. return app('json')->success([
  132. 'order_id' => $orderId,
  133. 'pay_money' => $payMoney,
  134. 'pay_no' => $payNo
  135. ], '订单创建成功');
  136. } catch (DbException $e) {
  137. Db::rollback();
  138. return app('json')->fail('订单创建失败');
  139. }
  140. }
  141. /**
  142. * 订单列表
  143. * @param Request $request
  144. */
  145. public function orderList(Request $request)
  146. {
  147. $post = UtilService::getMore([
  148. ['page', 1],
  149. ['pageSize', 20],
  150. ['status', ''],
  151. ], $request);
  152. $where = [
  153. ['uid', '=', $request->user['uid']]
  154. ];
  155. if ($post['status'] !== '' && in_array((string)$post['status'], ["0", "1", "2", "3", "-1"])) {
  156. $where[] = ['status', '=', (int)$post['status']];
  157. }
  158. $result = (new StoreOrder())->getList($where, $post['page'], $post['pageSize']);
  159. return app('json')->success($result);
  160. }
  161. /**
  162. * 订单详情
  163. * @param Request $request
  164. */
  165. public function orderDetail(Request $request)
  166. {
  167. $post = UtilService::getMore([
  168. ['order_id', '', 'empty', '参数错误'],
  169. ], $request);
  170. $order = Db::name('store_order')
  171. ->where('order_id', $post['order_id'])
  172. ->where('uid', $request->user['uid'])
  173. ->find();
  174. if (!$order) {
  175. return app('json')->fail('订单不存在');
  176. }
  177. // 获取订单商品
  178. $cartInfo = Db::name('store_order_cart_info')
  179. ->where('oid', $order['id'])
  180. ->select()
  181. ->toArray();
  182. $order['cart_info'] = $cartInfo;
  183. return app('json')->success($order);
  184. }
  185. /**
  186. * 取消订单
  187. * @param Request $request
  188. */
  189. public function cancelOrder(Request $request)
  190. {
  191. $post = UtilService::getMore([
  192. ['order_id', '', 'empty', '参数错误'],
  193. ], $request);
  194. $order = Db::name('store_order')
  195. ->where('order_id', $post['order_id'])
  196. ->where('uid', $request->user['uid'])
  197. ->where('status', 0)
  198. ->find();
  199. if (!$order) {
  200. return app('json')->fail('订单不存在或已取消');
  201. }
  202. Db::name('store_order')->where('id', $order['id'])->update(['status' => -1]);
  203. return app('json')->success('订单已取消');
  204. }
  205. /**
  206. * 确认收货
  207. * @param Request $request
  208. */
  209. public function confirmOrder(Request $request)
  210. {
  211. $post = UtilService::getMore([
  212. ['order_id', '', 'empty', '参数错误'],
  213. ], $request);
  214. $order = Db::name('store_order')
  215. ->where('order_id', $post['order_id'])
  216. ->where('uid', $request->user['uid'])
  217. ->where('status', 2)
  218. ->find();
  219. if (!$order) {
  220. return app('json')->fail('订单不存在或状态错误');
  221. }
  222. Db::name('store_order')->where('id', $order['id'])->update([
  223. 'status' => 3,
  224. 'confirm_time' => time()
  225. ]);
  226. return app('json')->success('确认收货成功');
  227. }
  228. }