OrderRefund.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\api\model\order;
  3. use app\common\model\order\OrderRefund as OrderRefundModel;
  4. /**
  5. * 售后单模型
  6. */
  7. class OrderRefund extends OrderRefundModel
  8. {
  9. /**
  10. * 隐藏字段
  11. * @var array
  12. */
  13. protected $hidden = [
  14. 'app_id',
  15. 'update_time'
  16. ];
  17. /**
  18. * 追加字段
  19. * @var array
  20. */
  21. protected $append = [
  22. 'state_text', // 售后单状态文字描述
  23. ];
  24. /**
  25. * 售后单状态文字描述
  26. */
  27. public function getStateTextAttr($value, $data)
  28. {
  29. // 已完成
  30. if ($data['status'] == 20) {
  31. $text = [10 => '已同意退货并已退款', 20 => '已同意换货并已收货', '30' => '仅退款并已退款'];
  32. return $text[$data['type']];
  33. }
  34. // 已取消
  35. if ($data['status'] == 30) {
  36. return '已取消';
  37. }
  38. // 已拒绝
  39. if ($data['status'] == 10) {
  40. // return $data['type'] == 10 ? '已拒绝退货退款' : '已拒绝换货';
  41. // 售后类型(10退货退款 20换货 30退款)
  42. $map = [10=>'已拒绝退货退款',20=>'已拒绝换货',30=>'已拒绝退款'];
  43. return $map[$data['type']] ?? '已拒绝';
  44. }
  45. // 进行中
  46. if ($data['status'] == 0) {
  47. if ($data['is_agree'] == 0) {
  48. return '等待审核中';
  49. }
  50. if ($data['type'] == 10) {
  51. return $data['is_user_send'] ? '已发货,待平台确认' : '已同意退货,请及时发货';
  52. }
  53. if ($data['type'] == 20) {
  54. return $data['is_user_send'] ? '已发货,待平台确认' : '已同意换货,请及时发货';
  55. }
  56. }
  57. return $value;
  58. }
  59. /**
  60. * 获取用户售后单列表
  61. */
  62. public function getList($user_id, $state, $limit)
  63. {
  64. $model = $this;
  65. $state > -1 && $model = $this->where('status', '=', $state);
  66. return $model->with(['order_master', 'orderproduct.image'])
  67. ->where('user_id', '=', $user_id)
  68. ->order(['create_time' => 'desc'])
  69. ->paginate($limit);
  70. }
  71. /**
  72. * 用户发货
  73. */
  74. public function delivery($data)
  75. {
  76. if ($this['is_agree']['value'] != 10 || $this['is_user_send'] != 0) {
  77. $this->error = '当前售后单不合法,不允许该操作';
  78. return false;
  79. }
  80. if ($data['express_id'] <= 0) {
  81. $this->error = '请选择物流公司';
  82. return false;
  83. }
  84. if (empty($data['express_no'])) {
  85. $this->error = '请填写物流单号';
  86. return false;
  87. }
  88. return $this->save([
  89. 'is_user_send' => 1,
  90. 'send_time' => time(),
  91. 'express_id' => (int)$data['express_id'],
  92. 'express_no' => $data['express_no'],
  93. ]);
  94. }
  95. /**
  96. * 新增售后单记录
  97. */
  98. public function apply($user, $product, $data)
  99. {
  100. $this->startTrans();
  101. try {
  102. // 新增售后单记录
  103. $this->save([
  104. 'order_product_id' => $data['order_product_id'],
  105. 'order_id' => $product['order_id'],
  106. 'user_id' => $user['user_id'],
  107. 'type' => $data['type'],
  108. 'apply_desc' => $data['content'],
  109. 'is_agree' => 0,
  110. 'status' => 0,
  111. 'app_id' => self::$app_id,
  112. ]);
  113. // 记录凭证图片关系
  114. if (isset($data['images']) && !empty($data['images'])) {
  115. $this->saveImages($this['order_refund_id'], $data['images']);
  116. }
  117. $this->commit();
  118. return true;
  119. } catch (\Exception $e) {
  120. $this->error = $e->getMessage();
  121. $this->rollback();
  122. return false;
  123. }
  124. }
  125. /**
  126. * 记录售后单图片
  127. */
  128. private function saveImages($order_refund_id, $images)
  129. {
  130. $images_ids = [];
  131. foreach (json_decode($images, true) as $val) {
  132. $images_ids[] = $val['file_id'];
  133. }
  134. // 生成评价图片数据
  135. $data = [];
  136. foreach ($images_ids as $image_id) {
  137. $data[] = [
  138. 'order_refund_id' => $order_refund_id,
  139. 'image_id' => $image_id,
  140. 'app_id' => self::$app_id
  141. ];
  142. }
  143. return !empty($data) && (new OrderRefundImage)->saveAll($data);
  144. }
  145. }