OrderRefund.php 4.6 KB

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