AfterSale.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\OrderAction;
  4. use app\common\model\OrderAftersales;
  5. use app\common\model\OrderGoods as orderGoodsModel;
  6. use think\Db;
  7. /**
  8. * 订单接口
  9. */
  10. class OrderGoods extends Base
  11. {
  12. protected $noNeedLogin = [];
  13. public function detail()
  14. {
  15. $id = $this->request->param('id');
  16. if (empty($id)) {
  17. $this->error('参数缺失');
  18. }
  19. $row = orderGoodsModel::get($id, ['Order']);
  20. if (!$row) {
  21. $this->error('未找到记录');
  22. }
  23. if ($row->salestate && $row->salestate != 6) {
  24. $this->error('未拒绝,不能重复申请');
  25. }
  26. $order = $row->order;
  27. if (empty($order)) {
  28. $this->error('订单不存在');
  29. }
  30. if ($order->user_id != $this->auth->id) {
  31. $this->error('不可越权操作');
  32. }
  33. //订单正常,已发货,已支付
  34. if (in_array($order->orderstate, [0, 3, 4]) && $order->shippingstate && $order->paystate) {
  35. $row->visible(explode(',', 'id,order_sn,goods_id,title,image,attrdata,price,nums'));
  36. $this->success('', $row);
  37. }
  38. $this->error('不允许的退款订单');
  39. }
  40. //申请售后
  41. public function apply()
  42. {
  43. $id = $this->request->post('id');
  44. $reason = $this->request->post('reason');
  45. $images = $this->request->post('images');
  46. $expressno = $this->request->post('expressno');
  47. $type = $this->request->post('type'); //1=仅退款,2=退款退货
  48. if (empty($id)) {
  49. $this->error('参数缺失');
  50. }
  51. if ($type != 1 && empty($reason)) {
  52. $this->error('请输入售后原因');
  53. }
  54. if (!in_array($type, [1, 2])) {
  55. $this->error('不存在的售后类型');
  56. }
  57. $row = orderGoodsModel::get($id, ['Order']);
  58. if (!$row) {
  59. $this->error('未找到记录');
  60. }
  61. if ($row->salestate && $row->salestate != 6) {
  62. $this->error('未审核,不能重复申请');
  63. }
  64. $order = $row->order;
  65. if (empty($order)) {
  66. $this->error('订单不存在');
  67. }
  68. if ($order->user_id != $this->auth->id) {
  69. $this->error('不可越权操作');
  70. }
  71. //未退商品
  72. $goodsNums = orderGoodsModel::where('order_sn', $order->order_sn)->where('salestate', 'IN', [0, 6])->count();
  73. $realprice = $row['realprice'];
  74. //商品退到最后一件 需要退邮费
  75. $shippingfee = $goodsNums > 1 ? 0 : $order->shippingfee;
  76. $refundprice = bcadd($realprice, $shippingfee, 2);
  77. //订单正常,已发货,已支付
  78. if (in_array($order->orderstate, [0, 3, 4]) && $order->paystate && $goodsNums > 0) {
  79. if ($type != 1 && !$order->shippingstate) {
  80. $this->error('申请售后类型错误');
  81. }
  82. $reason = $type == 1 && empty($reason) ? '仅退款' : $reason;
  83. // 启动事务
  84. Db::startTrans();
  85. try {
  86. $row->salestate = $type == 1 ? 2 : 1;
  87. $row->save();
  88. $order->orderstate = 4;
  89. $order->save();
  90. //商品退到最后一件 需要退邮费
  91. $shippingfee = $goodsNums > 1 ? 0 : $order->shippingfee;
  92. //添加售后记录
  93. OrderAftersales::create([
  94. 'user_id' => $this->auth->id,
  95. 'order_id' => $order->id,
  96. 'order_goods_id' => $id,
  97. 'type' => $type,
  98. 'nums' => $row['nums'],
  99. 'reason' => $reason,
  100. 'realprice' => $realprice,
  101. 'shippingfee' => $shippingfee,
  102. 'refund' => $refundprice,
  103. 'images' => $images,
  104. ]);
  105. // 提交事务
  106. Db::commit();
  107. } catch (\Exception $e) {
  108. // 回滚事务
  109. Db::rollback();
  110. $this->error('申请失败');
  111. }
  112. //记录操作
  113. OrderAction::push($order->order_sn, '用户', $type == 1 ? '申请退款' : '用户申请退款退货');
  114. $this->success('申请售后成功,等待审核', $order->url);
  115. }
  116. $this->error('不允许的退款订单');
  117. }
  118. //查看售后
  119. public function aftersale()
  120. {
  121. $id = $this->request->param('id');
  122. if (empty($id)) {
  123. $this->error('参数缺失');
  124. }
  125. $row = OrderAftersales::with(['OrderGoods'])->where('order_goods_id', $id)->order('id desc')->where('user_id', $this->auth->id)->find();
  126. if (empty($row)) {
  127. $this->error('未找到记录');
  128. }
  129. $this->success('获取成功', $row);
  130. }
  131. //保存快递信息
  132. public function saveExpressInfo()
  133. {
  134. $id = $this->request->post('id');
  135. $expressname = $this->request->post('expressname');
  136. $expressno = $this->request->post('expressno');
  137. if (empty($id)) {
  138. $this->error('参数缺失');
  139. }
  140. if (empty($expressname)) {
  141. $this->error('快递名称不能为空');
  142. }
  143. if (empty($expressno)) {
  144. $this->error('快递单号不能为空');
  145. }
  146. $row = OrderAftersales::where('order_goods_id', $id)->order('id desc')->where('user_id', $this->auth->id)->find();
  147. if (empty($row)) {
  148. $this->error('未找到记录');
  149. }
  150. if (!empty($row->expressname)) {
  151. $this->error('快递已提交');
  152. }
  153. $row->expressname = $expressname;
  154. $row->expressno = $expressno;
  155. $row->save();
  156. $this->success('保存成功');
  157. }
  158. }