You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

665 line
23 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/11/9
  6. * Time: 10:00 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\unishop\extend\Hashids;
  10. use addons\unishop\extend\Snowflake;
  11. use addons\unishop\model\Area;
  12. use addons\unishop\model\Config;
  13. use addons\unishop\model\Evaluate;
  14. use addons\unishop\model\Product;
  15. use app\admin\model\unishop\Coupon as CouponModel;
  16. use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
  17. use addons\unishop\model\OrderRefund;
  18. use app\admin\model\unishop\OrderRefundProduct;
  19. use think\Db;
  20. use think\Exception;
  21. use addons\unishop\model\Address as AddressModel;
  22. use think\Hook;
  23. use think\Loader;
  24. /**
  25. * 订单相关接口
  26. * Class Order
  27. * @package addons\unishop\controller
  28. */
  29. class Order extends Base
  30. {
  31. /**
  32. * 允许频繁访问的接口
  33. * @var array
  34. */
  35. protected $frequently = ['getorders'];
  36. protected $noNeedLogin = ['count','create','submit'];
  37. /**
  38. * 创建订单
  39. */
  40. public function create()
  41. {
  42. $productId = $this->request->post('id', 0);
  43. try {
  44. // $user_id = $this->auth->id;
  45. $user_id = 1;
  46. // 单个商品
  47. if ($productId) {
  48. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  49. $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
  50. /** 产品基础数据 **/
  51. $spec = $this->request->post('spec', '');
  52. $productData[0] = $product->getDataOnCreateOrder($spec);
  53. } else {
  54. // 多个商品
  55. $cart = $this->request->post('cart');
  56. $carts = (new \addons\unishop\model\Cart)
  57. ->whereIn('id', $cart)
  58. ->with(['product'])
  59. ->order(['id' => 'desc'])
  60. ->select();
  61. foreach ($carts as $cart) {
  62. if ($cart->product instanceof Product) {
  63. $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
  64. }
  65. }
  66. }
  67. if (empty($productData) || !$productData) {
  68. $this->error(__('Product not exist'));
  69. }
  70. /** 默认地址 **/
  71. $address = (new AddressModel)->where(['user_id' => $user_id, 'is_default' => AddressModel::IS_DEFAULT_YES])->find();
  72. if ($address) {
  73. $area = (new Area)->whereIn('id', [$address->province_id, $address->city_id, $address->area_id])->column('name', 'id');
  74. $address = $address->toArray();
  75. $address['province']['name'] = $area[$address['province_id']];
  76. $address['city']['name'] = $area[$address['city_id']];
  77. $address['area']['name'] = $area[$address['area_id']];
  78. }
  79. /** 可用优惠券 **/
  80. $coupon = CouponModel::all(function ($query) {
  81. $time = time();
  82. $query
  83. ->where(['switch' => CouponModel::SWITCH_ON])
  84. ->where('starttime', '<', $time)
  85. ->where('endtime', '>', $time);
  86. });
  87. if ($coupon) {
  88. $coupon = collection($coupon)->toArray();
  89. }
  90. /** 运费数据 **/
  91. $cityId = $address['city_id'] ? $address['city_id'] : 0;
  92. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  93. foreach ($productData as &$product) {
  94. $product['image'] = Config::getImagesFullUrl($product['image']);
  95. $product['sales_price'] = number_format($product['sales_price'], 2);
  96. $product['market_price'] = number_format($product['market_price'], 2);
  97. }
  98. $this->success('', [
  99. 'product' => $productData,
  100. 'address' => $address,
  101. 'coupon' => $coupon,
  102. 'delivery' => $delivery['list']
  103. ]);
  104. } catch (Exception $e) {
  105. $this->error($e->getMessage(), false);
  106. }
  107. }
  108. /**
  109. * 提交订单
  110. */
  111. public function submit()
  112. {
  113. $data = $this->request->post();
  114. $order = new \addons\unishop\model\Order();
  115. $out_trade_no = date('Ymd',time()).uniqid()."1";
  116. $snowflake = new Snowflake();
  117. $id = $snowflake->id();
  118. $productId = \addons\unishop\extend\Hashids::decodeHex($data["product_id"]);
  119. $products = Db::name('unishop_product')
  120. ->where(['id' =>$productId , 'switch' => Product::SWITCH_ON])
  121. ->find();
  122. $specs = explode(',', $data['spec']);
  123. foreach ($specs as &$spec) {
  124. $spec = str_replace('|', ',', $spec);
  125. }
  126. $productInfo = (new \addons\unishop\extend\Product())->getBaseData($products, $specs[0] ? $specs[0] : '');
  127. $price = $productInfo["sales_price"]*$data["number"];
  128. $order->save([
  129. 'id' => $id,
  130. // 'user_id' => $userId,
  131. 'out_trade_no' => $out_trade_no,
  132. 'order_price' => $price,
  133. 'total_price' => $price,
  134. 'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
  135. 'remark' => $data['remark'] ?? '',
  136. 'status' => $order::STATUS_NORMAL,
  137. 'product_id'=>$productId,
  138. 'name'=>$data["name"],
  139. 'mobile'=>$data["mobile"],
  140. 'address'=>$data["address"],
  141. 'email'=>$data["email"],
  142. "spec"=>$data["spec"],
  143. ]);
  144. $this->success('', []);
  145. return ;
  146. try {
  147. // $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
  148. // if (!$validate->check($data, [], 'submit')) {
  149. // throw new Exception($validate->getError());
  150. // }
  151. Db::startTrans();
  152. // 判断创建订单的条件
  153. if (empty(Hook::get('create_order_before'))) {
  154. Hook::add('create_order_before', 'addons\\unishop\\behavior\\Order');
  155. }
  156. // 减少商品库存,增加"已下单未支付数量"
  157. if (empty(Hook::get('create_order_after'))) {
  158. Hook::add('create_order_after', 'addons\\unishop\\behavior\\Order');
  159. }
  160. $orderModel = new \addons\unishop\model\Order();
  161. $result = $orderModel->createOrder(1, $data);
  162. Db::commit();
  163. $this->success('', $result);
  164. } catch (Exception $e) {
  165. Db::rollback();
  166. $this->error($e->getMessage(), false);
  167. }
  168. }
  169. /**
  170. * 获取运费模板
  171. */
  172. public function getDelivery()
  173. {
  174. $cityId = $this->request->get('city_id', 0);
  175. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  176. $this->success('', $delivery['list']);
  177. }
  178. /**
  179. * 获取订单信息
  180. */
  181. public function getOrders()
  182. {
  183. // 0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后
  184. $type = $this->request->get('type', 0);
  185. $page = $this->request->get('page', 1);
  186. $pagesize = $this->request->get('pagesize', 10);
  187. try {
  188. $orderModel = new \addons\unishop\model\Order();
  189. $result = $orderModel->getOrdersByType($this->auth->id, $type, $page, $pagesize);
  190. $this->success('', $result);
  191. } catch (Exception $e) {
  192. $this->error($e->getMessage());
  193. }
  194. }
  195. /**
  196. * 取消订单
  197. * 未支付的订单才叫取消,已支付的叫退货
  198. */
  199. public function cancel()
  200. {
  201. $order_id = $this->request->get('order_id', 0);
  202. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  203. $orderModel = new \addons\unishop\model\Order();
  204. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  205. if (!$order) {
  206. $this->error(__('Order not exist'));
  207. }
  208. switch ($order['status']) {
  209. case \addons\unishop\model\Order::STATUS_REFUND:
  210. $this->error('此订单已退款,无法取消');
  211. break;
  212. case \addons\unishop\model\Order::STATUS_CANCEL:
  213. $this->error('此订单已取消, 无需再取消');
  214. break;
  215. }
  216. if ($order['have_paid'] != \addons\unishop\model\Order::PAID_NO) {
  217. $this->error('此订单已支付,无法取消');
  218. }
  219. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
  220. $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
  221. $order->save();
  222. $this->success('取消成功', true);
  223. }
  224. }
  225. /**
  226. * 删除订单
  227. * 只能删除已取消或已退货的订单
  228. */
  229. public function delete()
  230. {
  231. $order_id = $this->request->get('order_id', 0);
  232. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  233. $orderModel = new \addons\unishop\model\Order();
  234. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  235. if (!$order) {
  236. $this->error(__('Order not exist'));
  237. }
  238. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
  239. $this->error('只能删除已取消或已退货的订单');
  240. }
  241. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
  242. $this->error('订单退款中,不可删除订单');
  243. }
  244. $order->delete();
  245. $this->success('删除成功', true);
  246. }
  247. /**
  248. * 确认收货
  249. */
  250. public function received()
  251. {
  252. $order_id = $this->request->get('order_id', 0);
  253. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  254. $orderModel = new \addons\unishop\model\Order();
  255. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  256. if (!$order) {
  257. $this->error(__('Order not exist'));
  258. }
  259. if ($order->have_delivered == 0) {
  260. $this->error('未发货,不能确认收货');
  261. }
  262. $order->have_received = time();
  263. $order->save();
  264. $this->success('已确认收货', true);
  265. }
  266. /**
  267. * 发表评论
  268. */
  269. public function comment()
  270. {
  271. $rate = $this->request->post('rate', 5);
  272. $anonymous = $this->request->post('anonymous', 0);
  273. $comment = $this->request->post('comment');
  274. $order_id = $this->request->post('order_id', 0);
  275. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  276. $product_id = $this->request->post('product_id');
  277. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  278. $orderProductModel = new \addons\unishop\model\OrderProduct();
  279. $orderProduct = $orderProductModel->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
  280. $orderModel = new \addons\unishop\model\Order();
  281. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  282. if (!$orderProduct || !$order) {
  283. $this->error(__('Order not exist'));
  284. }
  285. if ($order->have_received == $orderModel::RECEIVED_NO) {
  286. $this->error(__('未收货,不可评价'));
  287. }
  288. $result = false;
  289. try {
  290. $evaluate = new Evaluate();
  291. $evaluate->user_id = $this->auth->id;
  292. $evaluate->order_id = $order_id;
  293. $evaluate->product_id = $product_id;
  294. $evaluate->rate = $rate;
  295. $evaluate->anonymous = $anonymous;
  296. $evaluate->comment = $comment;
  297. $evaluate->spec = $orderProduct->spec;
  298. $result = $evaluate->save();
  299. if ($result) {
  300. $order->have_commented = time();
  301. $order->save();
  302. }
  303. } catch (Exception $e) {
  304. $this->error($e->getMessage());
  305. }
  306. if ($result !== false) {
  307. $this->success(__('Thanks for the evaluation'));
  308. } else {
  309. $this->error(__('Evaluation failure'));
  310. }
  311. }
  312. /**
  313. * 获取订单数量
  314. */
  315. public function count()
  316. {
  317. if (!$this->auth->isLogin()) {
  318. $this->error('');
  319. }
  320. $order = new \addons\unishop\model\Order();
  321. $list = $order
  322. ->where([
  323. 'user_id' => $this->auth->id,
  324. ])
  325. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  326. ->where(function ($query) {
  327. $query
  328. ->whereOr([
  329. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  330. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  331. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  332. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  333. ])
  334. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  335. })
  336. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  337. ->select();
  338. $data = [
  339. 'unpaid' => 0,
  340. 'undelivered' => 0,
  341. 'unreceived' => 0,
  342. 'uncomment' => 0,
  343. 'refund' => 0
  344. ];
  345. foreach ($list as $item) {
  346. switch (true) {
  347. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  348. $data['uncomment']++;
  349. break;
  350. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  351. $data['unreceived']++;
  352. break;
  353. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  354. $data['undelivered']++;
  355. break;
  356. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  357. $data['unpaid']++;
  358. break;
  359. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  360. $data['refund']++;
  361. break;
  362. }
  363. }
  364. $this->success('', $data);
  365. }
  366. /**
  367. * 订单详情细节
  368. */
  369. public function detail()
  370. {
  371. $order_id = $this->request->get('order_id', 0);
  372. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  373. try {
  374. $orderModel = new \addons\unishop\model\Order();
  375. $order = $orderModel
  376. ->with([
  377. 'products' => function ($query) {
  378. $query->field('id,order_id,image,number,price,spec,title,product_id');
  379. },
  380. 'extend' => function ($query) {
  381. $query->field('id,order_id,address_id,address_json,express_number');
  382. },
  383. 'evaluate' => function ($query) {
  384. $query->field('id,order_id,product_id');
  385. }
  386. ])
  387. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  388. if ($order) {
  389. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  390. // 快递单号
  391. $order['express_number'] = $order['extend']['express_number'];
  392. // 送货地址
  393. $address = json_decode($order['extend']['address_json'], true);
  394. $area = (new \addons\unishop\model\Area())
  395. ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  396. ->column('name', 'id');
  397. $delivery['username'] = $address['name'];
  398. $delivery['mobile'] = $address['mobile'];
  399. $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  400. $order['delivery'] = $delivery;
  401. // 是否已评论
  402. $evaluate = array_column($order['evaluate'], 'product_id');
  403. foreach ($order['products'] as &$product) {
  404. $product['image'] = Config::getImagesFullUrl($product['image']);
  405. if (in_array($product['id'], $evaluate)) {
  406. $product['evaluate'] = true;
  407. } else {
  408. $product['evaluate'] = false;
  409. }
  410. }
  411. unset($order['evaluate']);
  412. unset($order['extend']);
  413. }
  414. $this->success('', $order);
  415. } catch (Exception $e) {
  416. $this->error($e->getMessage());
  417. }
  418. }
  419. /**
  420. * 申请售后信息
  421. */
  422. public function refundInfo()
  423. {
  424. $order_id = $this->request->post('order_id');
  425. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  426. $orderModel = new \addons\unishop\model\Order();
  427. $order = $orderModel
  428. ->with([
  429. 'products' => function ($query) {
  430. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  431. },
  432. 'refund',
  433. 'refundProducts'
  434. ])
  435. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  436. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  437. if (!$order) {
  438. $this->error(__('Order not exist'));
  439. }
  440. $order = $order->append(['refund_status_text'])->toArray();
  441. foreach ($order['products'] as &$product) {
  442. $product['image'] = Config::getImagesFullUrl($product['image']);
  443. $product['choose'] = 0;
  444. // 如果是已提交退货的全选
  445. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  446. foreach ($order['refund_products'] as $refundProduct) {
  447. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  448. $product['choose'] = 1;
  449. }
  450. }
  451. }
  452. }
  453. unset($order['refund_products']);
  454. $this->success('', $order);
  455. }
  456. /**
  457. * 申请售后
  458. * @throws \think\db\exception\DataNotFoundException
  459. * @throws \think\db\exception\ModelNotFoundException
  460. * @throws \think\exception\DbException
  461. */
  462. public function refund()
  463. {
  464. $order_id = $this->request->post('order_id');
  465. $order_id = Hashids::decodeHex($order_id);
  466. $orderModel = new \addons\unishop\model\Order();
  467. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  468. if (!$order) {
  469. $this->error(__('Order not exist'));
  470. }
  471. if ($order['have_paid'] == 0) {
  472. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  473. }
  474. $amount = $this->request->post('amount', 0);
  475. $serviceType = $this->request->post('service_type');
  476. $receivingStatus = $this->request->post('receiving_status');
  477. $reasonType = $this->request->post('reason_type');
  478. $refundExplain = $this->request->post('refund_explain');
  479. $orderProductId = $this->request->post('order_product_id');
  480. if (!$orderProductId) {
  481. $this->error(__('Please select goods'));
  482. }
  483. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  484. $this->error(__('Please select goods status'));
  485. }
  486. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  487. $this->error(__('Please select service type'));
  488. }
  489. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  490. if (!$amount) {
  491. $this->error(__('Please fill in the refund amount'));
  492. }
  493. }
  494. try {
  495. Db::startTrans();
  496. $orderRefund = new OrderRefund();
  497. $orderRefund->user_id = $this->auth->id;
  498. $orderRefund->order_id = $order_id;
  499. $orderRefund->receiving_status = $receivingStatus;
  500. $orderRefund->service_type = $serviceType;
  501. $orderRefund->reason_type = $reasonType;
  502. $orderRefund->amount = $amount;
  503. $orderRefund->refund_explain = $refundExplain;
  504. $orderRefund->save();
  505. $productIdArr = explode(',', $orderProductId);
  506. $refundProduct = [];
  507. foreach ($productIdArr as $orderProductId) {
  508. $tmp['order_product_id'] = $orderProductId;
  509. $tmp['order_id'] = $order_id;
  510. $tmp['user_id'] = $this->auth->id;
  511. $tmp['refund_id'] = $orderRefund['id'];
  512. $tmp['createtime'] = time();
  513. $refundProduct[] = $tmp;
  514. }
  515. (new OrderRefundProduct)->insertAll($refundProduct);
  516. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  517. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  518. $order->save();
  519. Db::commit();
  520. $this->success(__('Commit'), 1);
  521. } catch (Exception $e) {
  522. Db::rollback();
  523. $this->error($e->getMessage());
  524. }
  525. }
  526. /**
  527. * 售后发货
  528. */
  529. public function refundDelivery()
  530. {
  531. $orderId = $this->request->post('order_id');
  532. $expressNumber = $this->request->post('express_number');
  533. if (!$expressNumber) {
  534. $this->error(__('Please fill in the express number'));
  535. }
  536. $orderId = Hashids::decodeHex($orderId);
  537. $orderModel = new \addons\unishop\model\Order();
  538. $order = $orderModel
  539. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  540. ->with(['refund'])->find();
  541. if (!$order || !$order->refund) {
  542. $this->error(__('Order not exist'));
  543. }
  544. try {
  545. Db::startTrans();
  546. $order->refund->express_number = $expressNumber;
  547. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  548. if ($order->refund->save() && $order->save()) {
  549. Db::commit();
  550. $this->success('', 1);
  551. } else {
  552. throw new Exception(__('Operation failed'));
  553. }
  554. } catch (Exception $e) {
  555. Db::rollback();
  556. $this->success($e->getMessage());
  557. }
  558. }
  559. }