OrderRefund.php 4.5 KB

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