当前位置:   article > 正文

thinkphp6 购物车生成订单信息_使用thinkphp 6 爬取京东商品

使用thinkphp 6 爬取京东商品

在生成订单前需要验证是否登录、如果没有登录跳转到登录页面

  1. //验证是否登录
  2. if (!session('?user')){
  3. //登陆成功后返回的地址
  4. session('back_url','getOrderInfo');
  5. return redirect('login');
  6. }

在登录是检测缓存中是否有要返回的路径、如果有则返回

  1. //记录用户信息
  2. session('user',$res);
  3. $back_url = session('back_url') ? : 'index';
  4. return redirect($back_url);

创建订单

  1. /**
  2. * 创建订单
  3. * @param $address_id
  4. * @return array
  5. * @throws Exception
  6. * @throws \think\db\exception\DataNotFoundException
  7. * @throws \think\db\exception\DbException
  8. * @throws \think\db\exception\ModelNotFoundException
  9. */
  10. public static function createOrder($address_id){
  11. //判断是否有库存
  12. //查询购买商品信息
  13. $goods_info = \app\home\model\Cart::with(['Goods','specGoods'])->where('user_id',session('user.id'))->where('is_selected',1)->select()->toArray();
  14. foreach ($goods_info as $val){
  15. if ($val['number'] > $val['store_count'] - $val['store_frozen']){
  16. throw new Exception('库存不足,请稍后购买');
  17. }
  18. }
  19. //判断是否登录
  20. if (!session('?user')){
  21. throw new Exception('请登录');
  22. }
  23. //生成订单号
  24. $order_number = date('YmdHis').rand(1000,9999);
  25. //查询用户选择的地址
  26. $address_info = Address::find($address_id)->toArray();
  27. //计算商品总价格
  28. $total_price = 0;
  29. foreach($goods_info as $val){
  30. $total_price += $val['price'] * $val['number'];
  31. }
  32. //开启事物
  33. Db::startTrans();
  34. try {
  35. //添加订单基本信息
  36. $data = [
  37. 'order_sn' => $order_number,
  38. 'user_id' => session('user.id'),
  39. 'consignee' => $address_info['consignee'],
  40. 'address' => $address_info['area'].$address_info['address'],
  41. 'phone' => $address_info['phone'],
  42. 'goods_price' => $total_price,
  43. 'order_amount' => $total_price,
  44. 'shipping' => $total_price,
  45. 'create_time' => time()
  46. ];
  47. $resulf = \app\home\model\Order::create($data);
  48. if (!$resulf){
  49. throw new Exception('订单信息添加失败');
  50. }
  51. //订单商品信息
  52. $add_order_goods = [];
  53. foreach($goods_info as $val){
  54. $add_order_goods[] = [
  55. 'order_id' => $resulf['id'],
  56. 'goods_id' => $val['id'],
  57. 'spec_goods_id' => $val['spec_goods_id'],
  58. 'number' => $val['number'],
  59. 'goods_name' => $val['goods_name'],
  60. 'goods_logo' => $val['goods_logo'],
  61. 'goods_price' => $val['price'],
  62. 'spec_value_names' => $val['value_names'],
  63. 'create_time' => time()
  64. ];
  65. $store_update[] = [
  66. 'id' => $val['id'],
  67. 'store_count' => $val['store_count'] - $val['number'], //库存 - 购买数量
  68. 'store_frozen' => $val['store_frozen'] + $val['number'], //冻结库存 + 购买数量
  69. 'update_time' => time()
  70. ];
  71. }
  72. //商品订单信息入库
  73. $goods_resulf = (new OrderGoods())->saveAll($add_order_goods);
  74. if (!$goods_resulf){
  75. throw new Exception('下单失败');
  76. }
  77. //修改商品的库存和冻结库存
  78. $store_resulf = (new SpecGoods())->saveAll($store_update);
  79. if (!$store_resulf){
  80. throw new Exception('下单失败');
  81. }
  82. //清空购买的商品信息(购物车)
  83. $delcartgoods = \app\home\model\Cart::destroy(array_column($goods_info,'id'));
  84. if (!$delcartgoods){
  85. throw new Exception('下单失败');
  86. }
  87. //提交事务
  88. Db::commit();
  89. return [
  90. 'order_number' => $order_number,
  91. 'price' => $total_price
  92. ];
  93. }catch (Exception $exception){
  94. //回滚事务
  95. Db::rollback();
  96. throw new Exception($exception->getMessage());
  97. }
  98. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/201033
推荐阅读
相关标签
  

闽ICP备14008679号