Order.php 5.6 KB

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