Files

198 lines
5.4 KiB
PHP

<?php
namespace app\api\controller\order;
use app\common\controller\Api;
use think\Db;
use app\common\model\order\Order as OrderModel;
use app\api\model\order\Service as ServiceModel;
use app\common\model\order\Item as ItemOrderModel;
use app\api\model\order\RefundApply as RefundApplyModel;
use think\Validate;
/**
* 售后服务
*/
class Service extends Api
{
protected $noNeedRight = '*';
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->orderModel = new OrderModel();
$this->serviceModel = new ServiceModel();
}
/**
* 申请退款
* @return void
*/
public function apply(){
$params = $this->request->post();
$orderInfo = $this->orderModel->getOrderDetail($params['order_no']);
if(!$orderInfo || $orderInfo['user_id'] != $this->auth->id){
$this->error('未获取到相关订单信息,请刷新后重试');
}
//判断订单是否可以售后
if($orderInfo['service'] == 1){
$this->error("售后正在处理中");
}
if(in_array($orderInfo['status'],[\app\common\constant\order\Status::STATUS_UNPAID,\app\common\constant\order\Status::STATUS_CANCEL,\app\common\constant\order\Status::STATUS_SERVICE,\app\common\constant\order\Status::STATUS_REFUND])){
$this->error("当前订单状态不支持退款");
}
//效验表单
$refundReason = $this->serviceModel->getRefundReason();
if(!in_array($params['reason'],array_keys($refundReason))){
$this->error("售后原因错误,请刷新后重试");
}
// 定义验证规则
$rules = [
'mobile' => 'require|max:20',
'remark' => 'max:200'
];
$messages = [
'mobile.require' => '联系电话不能为空',
'mobile.max' => '联系电话最多输入20字',
'remark.max' => '售后说明最多输入200字',
];
$validate = new Validate($rules,$messages);
$validateResult = $validate->check($params);
if (!$validateResult) {
$this->error($validate->getError());
}
//保存申请,变更订单状态
Db::startTrans();
try{
OrderModel::where([
'order_no'=>$orderInfo['order_no']
])->update([
'service'=>1
]);
ItemOrderModel::where([
'order_no'=>$orderInfo['order_no']
])->update([
'refund_status'=>\app\common\constant\order\Status::REFUND_STATUS_ING,
'after_sale_status'=>\app\common\constant\order\Status::AFTERSALE_STATUS_AFTERING
]);
RefundApplyModel::insert([
'uniacid'=>UNIACID,
'user_id'=>$this->auth->id,
'order_no'=>$orderInfo['order_no'],
'reason'=>$params['reason'],
'mobile'=>$params['mobile'],
'remark'=>$params['remark'],
'status'=>\app\common\constant\order\Status::REFUND_APPLY_STATUS_WAITING,
'createtime'=>time()
]);
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
$this->error("处理退款申请失败,请刷新页面后重试");
}
$this->success('申请成功');
}
/**
* 撤销售后申请
* @return void
*/
public function cancel(){
$orderNo = input('order_no');
$orderInfo = $this->orderModel->getOrderDetail($orderNo);
if(!$orderInfo || $orderInfo['user_id'] != $this->auth->id){
$this->error('未获取到相关订单信息,请刷新后重试');
}
//判断订单是否可以售后
if($orderInfo['service'] == 0 || $orderInfo['service'] == 2){
$this->error("不可撤销");
}
OrderModel::where([
'order_no'=>$orderNo
])->update([
'service'=>0
]);
$this->success("撤销成功");
}
/**
* 获取退款原因
* @return string[]
*/
public function getRefundReason(){
$this->success('获取成功', $this->serviceModel->getRefundReason());
}
/**
* 获取售后详情
* @return void
*/
public function getDetail(){
$orderNo = input('order_no');
$detail = RefundApplyModel::where([
'order_no'=>$orderNo,
'user_id'=>$this->auth->id
])
->order("id","desc")
->find();
if(!$detail){
$this->error('未获取到相关订单信息,请刷新后重试');
}
$this->success('获取成功', $detail);
}
/**
* 获取退款记录
* @return void
*/
public function getRefundLog(){
$limit = input('limit',10);
$page = input('page',1);
$orderNo = input('order_no');
$where = [
'user_id'=>$this->auth->id,
'refund_log.order_no'=>$orderNo,
'refund_log.status'=>1
];
$list = \app\api\model\order\RefundLog::with(['item'])
->where($where)
->limit($limit)->page($page)
->field(['refund_fee','refund_log.createtime','item.item_name'])
->order('refund_log.createtime','desc')
->select();
$this->success('获取成功', $list);
}
}