Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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