Order.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. /**
  8. * 订单
  9. */
  10. class Order extends Model {
  11. protected $table = 'order';
  12. protected $dates = ['expired_at'];
  13. protected $fillable = ['expired_at', 'is_expire', 'status'];
  14. public function user(): BelongsTo {
  15. return $this->belongsTo(User::class);
  16. }
  17. public function goods(): BelongsTo {
  18. return $this->belongsTo(Goods::class)->withTrashed();
  19. }
  20. public function coupon(): BelongsTo {
  21. return $this->belongsTo(Coupon::class)->withTrashed();
  22. }
  23. public function payment(): HasOne {
  24. return $this->hasOne(Payment::class);
  25. }
  26. public function scopeUid($query, $uid = null) {
  27. return $query->whereUserId($uid?: Auth::id());
  28. }
  29. public function scopeRecentUnPay($query) {
  30. return $query->whereStatus(0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-15 minutes")));
  31. }
  32. public function scopeUserPrepay($query, $uid = null) {
  33. return $query->uid($uid)->whereStatus(3);
  34. }
  35. public function scopeActive($query) {
  36. return $query->whereIsExpire(0)->whereStatus(2);
  37. }
  38. public function scopeActivePlan($query) {
  39. return $query->active()->with('goods')->whereHas('goods', static function($list) { $list->whereType(2); });
  40. }
  41. public function scopeActivePackage($query) {
  42. return $query->active()->with('goods')->whereHas('goods', static function($list) { $list->whereType(1); });
  43. }
  44. public function scopeUserActivePlan($query, $uid = null) {
  45. return $query->uid($uid)->activePlan();
  46. }
  47. public function scopeUserActivePackage($query, $uid = null) {
  48. return $query->uid($uid)->activePackage();
  49. }
  50. // 订单状态
  51. public function getStatusLabelAttribute(): string {
  52. switch($this->attributes['status']){
  53. case -1:
  54. $status_label = trans('home.invoice_status_closed');
  55. break;
  56. case 1:
  57. $status_label = trans('home.invoice_status_wait_confirm');
  58. break;
  59. case 2:
  60. $status_label = trans('home.invoice_status_payment_confirm');
  61. break;
  62. case 0:
  63. $status_label = trans('home.invoice_status_wait_payment');
  64. break;
  65. default:
  66. $status_label = 'Unknown';
  67. }
  68. return $status_label;
  69. }
  70. public function getOriginAmountAttribute($value) {
  71. return $value / 100;
  72. }
  73. public function setOriginAmountAttribute($value) {
  74. return $this->attributes['origin_amount'] = $value * 100;
  75. }
  76. public function getAmountAttribute($value) {
  77. return $value / 100;
  78. }
  79. public function setAmountAttribute($value) {
  80. return $this->attributes['amount'] = $value * 100;
  81. }
  82. // 支付渠道
  83. public function getPayTypeLabelAttribute(): string {
  84. switch($this->attributes['pay_type']){
  85. case 0:
  86. $pay_type_label = '余额';
  87. break;
  88. case 1:
  89. $pay_type_label = '支付宝';
  90. break;
  91. case 2:
  92. $pay_type_label = 'QQ';
  93. break;
  94. case 3:
  95. $pay_type_label = '微信';
  96. break;
  97. case 4:
  98. $pay_type_label = '虚拟货币';
  99. break;
  100. case 5:
  101. $pay_type_label = 'PayPal';
  102. break;
  103. default:
  104. $pay_type_label = '';
  105. }
  106. return $pay_type_label;
  107. }
  108. // 支付图标
  109. public function getPayTypeIconAttribute(): string {
  110. $base_path = '/assets/images/payment/';
  111. switch($this->attributes['pay_type']){
  112. case 1:
  113. $pay_type_icon = $base_path.'alipay.png';
  114. break;
  115. case 2:
  116. $pay_type_icon = $base_path.'qq.png';
  117. break;
  118. case 3:
  119. $pay_type_icon = $base_path.'wechat.png';
  120. break;
  121. case 5:
  122. $pay_type_icon = $base_path.'paypal.png';
  123. break;
  124. case 0:
  125. case 4:
  126. default:
  127. $pay_type_icon = $base_path.'coin.png';
  128. }
  129. return $pay_type_icon;
  130. }
  131. // 支付方式
  132. public function getPayWayLabelAttribute(): string {
  133. switch($this->attributes['pay_way']){
  134. case 'credit':
  135. $pay_way_label = '余额';
  136. break;
  137. case 'youzan':
  138. $pay_way_label = '有赞云';
  139. break;
  140. case 'f2fpay':
  141. $pay_way_label = '支付宝当面付';
  142. break;
  143. case 'codepay':
  144. $pay_way_label = '码支付';
  145. break;
  146. case 'payjs':
  147. $pay_way_label = 'PayJs';
  148. break;
  149. case 'bitpayx':
  150. $pay_way_label = '麻瓜宝';
  151. break;
  152. case 'paypal':
  153. $pay_way_label = 'PayPal';
  154. break;
  155. default:
  156. $pay_way_label = '未知';
  157. }
  158. return $pay_way_label;
  159. }
  160. }