Order.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UserCoupon;
  4. use app\common\model\Carts;
  5. use app\common\model\Order as OrderModel;
  6. use app\common\model\Address;
  7. use app\common\model\Third;
  8. use app\common\model\OrderAction;
  9. use app\common\model\OrderGoods;
  10. use app\common\library\KdApiExpOrder;
  11. use think\Db;
  12. /**
  13. * 订单接口
  14. */
  15. class Order extends Base
  16. {
  17. protected $noNeedLogin = [];
  18. //计算邮费,判断商品
  19. public function carts()
  20. {
  21. $config = get_addon_config('shop');
  22. $cart_ids = $this->request->post('ids');
  23. $address_id = $this->request->post('address_id/d'); //地址id
  24. if (empty($cart_ids)) {
  25. $this->error('参数缺失');
  26. }
  27. $user_coupon_id = $this->request->post('user_coupon_id/d'); //优惠券
  28. $address = Address::get($address_id);
  29. $orderInfo = [
  30. 'order_sn' => '',
  31. 'amount' => 0, //订单金额(包含运费)
  32. 'shippingfee' => 0, //运费
  33. 'goodsprice' => 0 //商品金额
  34. ];
  35. $goodsList = [];
  36. $area_id = !empty($address) ? $address->area_id : 0;
  37. try {
  38. list($orderItem, $goodsList) = OrderModel::computeCarts($orderInfo, $cart_ids, $this->auth->id, $area_id, $user_coupon_id);
  39. if (empty($goodsList)) {
  40. throw new \Exception("未找到商品");
  41. }
  42. } catch (\Exception $e) {
  43. $this->error($e->getMessage());
  44. }
  45. foreach ($goodsList as $item) {
  46. $item->category_id = $item->goods->category_id;
  47. $item->brand_id = $item->goods->brand_id;
  48. $item->goods->visible(explode(',', 'id,title,image,price,marketprice'));
  49. }
  50. //获取我的可以使用的优惠券
  51. $goods_ids = array_column($goodsList, 'goods_id');
  52. $category_ids = array_column($goodsList, 'category_id');
  53. $brand_ids = array_column($goodsList, 'brand_id');
  54. $this->success('获取成功', [
  55. 'coupons' => UserCoupon::myGoodsCoupon($this->auth->id, $goods_ids, $category_ids, $brand_ids),
  56. 'goods_list' => $goodsList,
  57. 'order_info' => $orderInfo,
  58. 'couponTotalPrice' => floatval(!isset($config['shippingfeecoupon']) || $config['shippingfeecoupon'] == 0 ? $orderInfo['goodsprice'] : $orderInfo['amount'])
  59. ]);
  60. }
  61. //提交订单
  62. public function add()
  63. {
  64. $cart_ids = $this->request->post('ids');
  65. $address_id = $this->request->post('address_id/d'); //地址id
  66. $user_coupon_id = $this->request->post('user_coupon_id/d'); //地址id
  67. $memo = $this->request->post('memo');
  68. if (empty($address_id)) {
  69. $this->error('请选择地址');
  70. }
  71. if (empty($cart_ids)) {
  72. $this->error('请选择商品');
  73. }
  74. //为购物车id
  75. //校验购物车id 合法
  76. $row = (new Carts)->where('id', 'IN', $cart_ids)->where('user_id', '<>', $this->auth->id)->find();
  77. if ($row) {
  78. $this->error('存在不合法购物车数据');
  79. }
  80. $order = null;
  81. try {
  82. $order = OrderModel::createOrder($address_id, $this->auth->id, $cart_ids, $user_coupon_id, $memo);
  83. } catch (\Exception $e) {
  84. $this->error($e->getMessage());
  85. }
  86. $this->success('下单成功!', array_intersect_key($order->toArray(), array_flip(['order_sn', 'paystate'])));
  87. }
  88. //订单详情
  89. public function detail()
  90. {
  91. $order_sn = $this->request->param('order_sn');
  92. if (empty($order_sn)) {
  93. $this->error('参数缺失');
  94. }
  95. $order = OrderModel::getDetail($order_sn, $this->auth->id);
  96. if (empty($order)) {
  97. $this->error('未找到订单');
  98. }
  99. if (OrderModel::isExpired($order_sn)) {
  100. $this->error("订单已经失效");
  101. }
  102. $order->append(['status_text']);
  103. $order->hidden(explode(',', 'method,transactionid,updatetime,deletetime'));
  104. $order->expiretime = $order->expiretime - time();
  105. $this->success('', $order);
  106. }
  107. //订单列表
  108. public function index()
  109. {
  110. $param = $this->request->param();
  111. $param['user_id'] = $this->auth->id;
  112. $list = OrderModel::tableList($param);
  113. foreach ($list as $item) {
  114. $item->append(['status_text']);
  115. $field = 'id,order_sn,amount,expressno,expressname,saleamount,shippingfee,paystate,orderstate,shippingstate,order_goods,status,status_text';
  116. $item->visible(explode(',', $field));
  117. }
  118. $this->success('获取成功', $list);
  119. }
  120. //取消订单
  121. public function cancel()
  122. {
  123. $order_sn = $this->request->post('order_sn');
  124. if (!$order_sn) {
  125. $this->error('参数错误');
  126. }
  127. $order = OrderModel::getByOrderSn($order_sn);
  128. if (empty($order)) {
  129. $this->error('订单不存在');
  130. }
  131. if ($order->user_id != $this->auth->id) {
  132. $this->error('不能越权操作');
  133. }
  134. if ($order->status == 'hidden') {
  135. $this->error('订单已失效!');
  136. }
  137. //可以取消
  138. if (!$order->paystate && !$order->orderstate) {
  139. // 启动事务
  140. Db::startTrans();
  141. try {
  142. $order->orderstate = 1;
  143. $order->canceltime = time();
  144. $order->save();
  145. foreach ($order->order_goods as $item) {
  146. $sku = $item->sku;
  147. $goods = $item->goods;
  148. //商品库存恢复
  149. if ($sku) {
  150. $sku->setInc('stocks', $item->nums);
  151. }
  152. if ($goods) {
  153. $goods->setInc('stocks', $item->nums);
  154. }
  155. }
  156. //恢复优惠券
  157. UserCoupon::resetUserCoupon($order->user_coupon_id, $order->order_sn);
  158. // 提交事务
  159. Db::commit();
  160. } catch (\Exception $e) {
  161. // 回滚事务
  162. Db::rollback();
  163. $this->error('订单取消失败');
  164. }
  165. //记录操作
  166. OrderAction::push($order->order_sn, '系统', '订单取消成功');
  167. $this->success('订单取消成功!', $order['status']);
  168. } else {
  169. $this->error('订单不允许取消');
  170. }
  171. }
  172. //订单支付
  173. public function pay()
  174. {
  175. $order_sn = $this->request->post('order_sn');
  176. $paytype = $this->request->post('paytype');
  177. $method = $this->request->post('method');
  178. $appid = $this->request->post('appid'); //为APP的应用id
  179. $returnurl = $this->request->post('returnurl', '', 'trim');
  180. $openid = $this->request->post('openid', '', null);
  181. $logincode = $this->request->post('logincode', '', null);
  182. $orderInfo = OrderModel::getByOrderSn($order_sn);
  183. if (!$orderInfo) {
  184. $this->error("未找到指定的订单");
  185. }
  186. if ($orderInfo['paystate']) {
  187. $this->error("订单已经支付,请勿重复支付");
  188. }
  189. if (OrderModel::isExpired($order_sn)) {
  190. $this->error("订单已经失效");
  191. }
  192. //如果有传递logincode,则直接使用code换openid
  193. if (!$openid && $logincode) {
  194. $json = (new \addons\shop\library\Wechat\Service())->getWechatSession($logincode);
  195. $openid = $json['openid'] ?? $openid;
  196. }
  197. //必须传递openid
  198. if (in_array($method, ['miniapp', 'mp', 'mini']) && !$openid) {
  199. $this->error("未找到第三方用户信息", 'bind');
  200. }
  201. $response = null;
  202. try {
  203. $response = OrderModel::pay($order_sn, $this->auth->id, $paytype, $method, $openid, '', $returnurl);
  204. } catch (\Exception $e) {
  205. $this->error($e->getMessage());
  206. }
  207. $this->success("请求成功", $response);
  208. }
  209. //确认收货
  210. public function takedelivery()
  211. {
  212. $order_sn = $this->request->post('order_sn');
  213. if (!$order_sn) {
  214. $this->error('参数错误');
  215. }
  216. $order = OrderModel::getByOrderSn($order_sn);
  217. if (empty($order)) {
  218. $this->error('订单不存在');
  219. }
  220. if ($order->user_id != $this->auth->id) {
  221. $this->error('不能越权操作');
  222. }
  223. if ($order->status == 'hidden') {
  224. $this->error('订单已失效!');
  225. }
  226. if ($order->paystate == 1 && !$order->orderstate && $order->shippingstate == 1) {
  227. $order->shippingstate = 2;
  228. $order->receivetime = time();
  229. $order->save();
  230. //记录操作
  231. OrderAction::push($order->order_sn, '系统', '订单确认收货成功');
  232. $this->success('确认收货成功');
  233. }
  234. $this->error('未可确认收货');
  235. }
  236. //查询物流
  237. public function logistics()
  238. {
  239. $order_sn = $this->request->param('order_sn');
  240. if (empty($order_sn)) {
  241. $this->error('参数缺失');
  242. }
  243. $order = OrderModel::getDetail($order_sn, $this->auth->id);
  244. if (empty($order)) {
  245. $this->error('未找到订单');
  246. }
  247. if (!$order->shippingstate) {
  248. $this->error('订单未发货');
  249. }
  250. $electronics = Db::name('shop_order_electronics')->where('order_sn', $order_sn)->where('status', 0)->find();
  251. if (!$electronics) {
  252. $this->error('订单未发货');
  253. }
  254. $result = KdApiExpOrder::getLogisticsQuery([
  255. 'order_sn' => $order_sn,
  256. 'logistic_code' => $electronics['logistic_code'],
  257. 'shipper_code' => $electronics['shipper_code']
  258. ]);
  259. if ($result['Success']) {
  260. $this->success('查询成功', $result['Traces']);
  261. }
  262. $this->error('查询失败');
  263. }
  264. }