success(1, $data); } //售后列表 public function index() { $user = auth_user(); $params = $this->request->param(); $type = $params['type'] ?? 'all'; $aftersales = AftersaleModel::where('user_id', $user->id); if ($type != 'all') { $aftersales = $aftersales->{$type}(); } $aftersales = $aftersales->order('id', 'desc') ->autopage()->select(); // ->paginate($this->request->param('list_rows', 10)); //综合状态 foreach($aftersales as $key => $aftersale){ $aftersale['status_show_text'] = $aftersale['aftersale_status_text'].','.$aftersale['refund_status_text']; $aftersales[$key] = $aftersale; } $this->success('获取成功', $aftersales); } //售后详情 public function detail() { $user = auth_user(); $id = $this->request->param('id'); $aftersale = AftersaleModel::where('user_id', $user->id)->with('logs')->where('id', $id)->find(); if (!$aftersale) { $this->error(__('No Results were found')); } //寄回地址 $aftersale_address = Db::name('shopro_aftersale_address')->order('is_default','desc')->order('id desc')->find(); $aftersale['aftersale_address'] = $aftersale_address; //综合状态 $aftersale['status_show_text'] = $aftersale['aftersale_status_text'].','.$aftersale['refund_status_text']; $this->success('获取成功', $aftersale); } //申请售后 public function add() { $user = auth_user(); $params = $this->request->param(); $this->svalidate($params, ".add"); $aftersale = Db::transaction(function () use ($user, $params) { $type = $params['type']; $order_id = $params['order_id']; $order_item_id = $params['order_item_id']; $mobile = $params['mobile'] ?? ''; $reason = $params['reason'] ?? '用户申请售后'; $content = $params['content'] ?? ''; $images = $params['images'] ? json_decode(htmlspecialchars_decode($params['images']),true) : []; //申请退款金额 $refund_money = round(floatval($params['apply_refund_price']), 2); if ($refund_money <= 0) { $this->error('请输入正确的退款金额'); } //订单状态:已支付,已完成 $order = OrderModel::canAftersale()->where('user_id', $user->id)->lock(true)->where('id', $order_id)->find(); if (!$order) { error_stop('订单不存在或不可售后'); } $item = OrderItemModel::where('user_id', $user->id)->where('order_id',$order_id)->where('id', $order_item_id)->find(); if (!$item) { error_stop('参数错误'); } if (!in_array($item->aftersale_status, [ OrderItemModel::AFTERSALE_STATUS_REFUSE, //拒绝 OrderItemModel::AFTERSALE_STATUS_NOAFTER //未申请 ])) { error_stop('当前订单商品不可申请售后'); } //apply_refund_price 限制金额 $ordernew = OrderModel::withTrashed()->with('items')->find($order_id); if (!$ordernew) { $this->error('订单不存在'); } $items = $ordernew->items; $items = array_column($items, null, 'id'); // 当前订单已退款总金额 $refunded_money = array_sum(array_column($items, 'refund_fee')); // 剩余可退款金额 $refund_surplus_money = bcsub($order->pay_fee, (string)$refunded_money, 2); // 如果退款金额大于订单支付总金额 if (bccomp((string)$refund_money, $refund_surplus_money, 2) === 1) { $this->error('退款总金额不能大于实际支付金额'); } $aftersale = new AftersaleModel(); $aftersale->aftersale_sn = get_sn($user->id, 'A'); $aftersale->user_id = $user->id; $aftersale->type = $type; $aftersale->mobile = $mobile; $aftersale->activity_id = $item['activity_id']; $aftersale->activity_type = $item['activity_type']; $aftersale->order_id = $order_id; $aftersale->order_item_id = $order_item_id; $aftersale->goods_id = $item['goods_id']; $aftersale->goods_sku_price_id = $item['goods_sku_price_id']; $aftersale->goods_sku_text = $item['goods_sku_text']; $aftersale->goods_title = $item['goods_title']; $aftersale->goods_image = $item['goods_image']; $aftersale->goods_original_price = $item['goods_original_price']; $aftersale->discount_fee = $item['discount_fee']; $aftersale->goods_price = $item['goods_price']; $aftersale->goods_num = $item['goods_num']; $aftersale->dispatch_status = $item['dispatch_status']; $aftersale->dispatch_fee = $item['dispatch_fee']; $aftersale->aftersale_status = AftersaleModel::AFTERSALE_STATUS_NOOPER; // 未处理 $aftersale->refund_status = AftersaleModel::REFUND_STATUS_NOREFUND; // 未退款 $aftersale->apply_refund_price = $refund_money; $aftersale->refund_fee = 0; $aftersale->reason = $reason; $aftersale->content = $content; $aftersale->express_status = 'noinfo'; $aftersale->save(); // 增加售后单变动记录、 AftersaleLogModel::add($order, $aftersale, $user, 'user', [ 'log_type' => 'apply_aftersale', 'content' => "申请原因:$reason
相关描述: $content", 'images' => $images ]); $ext = $item->ext ?: []; $ext['aftersale_id'] = $aftersale->id; // 修改订单 item 状态,申请售后 $item->aftersale_status = OrderItemModel::AFTERSALE_STATUS_ING; $item->ext = $ext; $item->save(); OrderActionModel::add($order, $item, $user, 'user', '用户申请售后'); return $aftersale; }); $this->success('申请成功', $aftersale); } //售后寄回 public function send(){ $user = auth_user(); $id = input('id',0,'intval'); $express_id = input('express_id',0,'intval'); $express_no = input('express_no','','trim'); if(!$id || !$express_id || !$express_no){ $this->error(); } $express = Db::name('shopro_data_express')->where('id',$express_id)->find(); if(!$express){ $this->error('请输入正确的快递公司'); } $aftersale = Db::name('shopro_order_aftersale')->where('id',$id)->where('user_id',$user['id'])->find(); if (!$aftersale) { $this->error('售后订单不存在'); } if($aftersale['type'] != 'return'){ $this->error('退货退款的订单才需要寄回商品'); } // 同意并处理中 if($aftersale['aftersale_status'] != AftersaleModel::AFTERSALE_STATUS_ING){ $this->error('商家还没有同意售后'); } // 不要重复寄回 if(!empty($aftersale['express_no'])){ $this->error('已经寄回了无需重复操作'); } $data = [ 'express_name' => $express['name'], 'express_code' => $express['code'], 'express_no' => $express_no, ]; $rs = Db::name('shopro_order_aftersale')->where('id',$id)->update($data); //参照后台发货 $this->subscribeExpressInfo($data); $this->success('操作成功'); } // 订阅物流追踪 private function subscribeExpressInfo($orderExpress) { try { $expressLib = new ExpressLib(); $a = $expressLib->subscribe([ 'express_code' => $orderExpress['express_code'], 'express_no' => $orderExpress['express_no'], 'Callback' => 'Aftersale', ]); } catch (\Exception $e) { // Nothing TODO return; } } //取消售后 public function cancel() { $user = auth_user(); $id = $this->request->param('id'); $aftersale = AftersaleModel::canCancel()->where('user_id', $user->id)->where('id', $id)->find(); if (!$aftersale) { $this->error('售后单不存在或不可取消'); } if ($aftersale['refund_status'] != AftersaleModel::REFUND_STATUS_NOREFUND) { //已经退款 $this->error('退款商品已退款'); } $order = OrderModel::where('user_id', $user->id)->find($aftersale['order_id']); if (!$order) { $this->error(__('No Results were found')); } $orderItem = OrderItemModel::find($aftersale['order_item_id']); if (!$orderItem) { // 不存在 $this->error('退款商品不存在'); } //这里用了两个状态,因为shopro也搞不清是哪个 if (in_array($orderItem['refund_status'], [OrderItemModel::REFUND_STATUS_AGREE, OrderItemModel::REFUND_STATUS_COMPLETED])) { //已经退款 $this->error('退款商品已退款'); } $aftersale = Db::transaction(function () use ($aftersale, $order, $orderItem, $user) { $aftersale->aftersale_status = AftersaleModel::AFTERSALE_STATUS_CANCEL; // 取消售后单 $aftersale->save(); AftersaleLogModel::add($order, $aftersale, $user, 'user', [ 'log_type' => 'cancel', 'content' => '用户取消申请售后', 'images' => [] ]); // 修改订单 item 为未申请售后 $orderItem->aftersale_status = OrderItemModel::AFTERSALE_STATUS_NOAFTER; $orderItem->refund_status = OrderItemModel::REFUND_STATUS_NOREFUND; $orderItem->save(); OrderActionModel::add($order, $orderItem, $user, 'user', '用户取消申请售后'); return $aftersale; }); $this->success('取消成功', $aftersale); } public function delete() { $user = auth_user(); $id = $this->request->param('id'); $aftersale = AftersaleModel::canDelete()->where('user_id', $user->id)->where('id', $id)->find(); if (!$aftersale) { $this->error('售后单不存在或不可删除'); } $order = OrderModel::withTrashed()->where('id', $aftersale['order_id'])->find(); Db::transaction(function () use ($aftersale, $order, $user) { AftersaleLogModel::add($order, $aftersale, $user, 'user', [ 'log_type' => 'delete', 'content' => '用户删除售后单', 'images' => [] ]); $aftersale->delete(); // 删除售后单(后删除,否则添加记录时 $aftersale 没数据) }); $this->success('删除成功'); } //退款的物流信息 public function express() { $user = auth_user(); $id = $this->request->param('id'); $expresses = AftersaleModel::with(['expresslogs'])->where('user_id', $user->id)->where('id', $id)->find(); // 更新包裹信息(5分钟缓存) (new ExpressLib)->updateOrderExpress($expresses['order_id']); $order_sn = Db::name('shopro_order')->where('id',$expresses['order_id'])->value('order_sn'); //寄回地址 $aftersale_address = Db::name('shopro_aftersale_address')->order('is_default','desc')->order('id desc')->find(); $result = [ 'order_sn' => $order_sn, 'address' => $aftersale_address, 'expresses' => $expresses, 'goods_image' => localpath_to_netpath($expresses['goods_image']), ]; $this->success('获取成功', $result); } }