Order.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. /**
  8. * 订单
  9. *
  10. * @property int $oid
  11. * @property string $order_sn 订单编号
  12. * @property int $user_id 操作人
  13. * @property int $goods_id 商品ID
  14. * @property int $coupon_id 优惠券ID
  15. * @property int $origin_amount 订单原始总价,单位分
  16. * @property int $amount 订单总价,单位分
  17. * @property \Illuminate\Support\Carbon|null $expired_at 过期时间
  18. * @property int $is_expire 是否已过期:0-未过期、1-已过期
  19. * @property int $pay_type 支付渠道:0-余额、1-支付宝、2-QQ、3-微信、4-虚拟货币、5-paypal
  20. * @property string $pay_way 支付方式:balance、f2fpay、codepay、payjs、bitpayx等
  21. * @property int $status 订单状态:-1-已关闭、0-待支付、1-已支付待确认、2-已完成
  22. * @property \Illuminate\Support\Carbon $created_at 创建时间
  23. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  24. * @property-read \App\Models\Coupon|null $coupon
  25. * @property-read string $pay_type_icon
  26. * @property-read string $pay_type_label
  27. * @property-read string $pay_way_label
  28. * @property-read string $status_label
  29. * @property-read \App\Models\Goods|null $goods
  30. * @property-read \App\Models\Payment|null $payment
  31. * @property-read \App\Models\User|null $user
  32. * @method static Builder|Order newModelQuery()
  33. * @method static Builder|Order newQuery()
  34. * @method static Builder|Order query()
  35. * @method static Builder|Order uid()
  36. * @method static Builder|Order whereAmount($value)
  37. * @method static Builder|Order whereCouponId($value)
  38. * @method static Builder|Order whereCreatedAt($value)
  39. * @method static Builder|Order whereExpiredAt($value)
  40. * @method static Builder|Order whereGoodsId($value)
  41. * @method static Builder|Order whereIsExpire($value)
  42. * @method static Builder|Order whereOid($value)
  43. * @method static Builder|Order whereOrderSn($value)
  44. * @method static Builder|Order whereOriginAmount($value)
  45. * @method static Builder|Order wherePayType($value)
  46. * @method static Builder|Order wherePayWay($value)
  47. * @method static Builder|Order whereStatus($value)
  48. * @method static Builder|Order whereUpdatedAt($value)
  49. * @method static Builder|Order whereUserId($value)
  50. * @mixin \Eloquent
  51. */
  52. class Order extends Model {
  53. protected $table = 'order';
  54. protected $primaryKey = 'oid';
  55. protected $dates = ['expired_at'];
  56. public function scopeUid($query) {
  57. return $query->whereUserId(Auth::id());
  58. }
  59. public function user(): HasOne {
  60. return $this->hasOne(User::class, 'id', 'user_id');
  61. }
  62. public function goods(): HasOne {
  63. return $this->hasOne(Goods::class, 'id', 'goods_id')->withTrashed();
  64. }
  65. public function coupon(): HasOne {
  66. return $this->hasOne(Coupon::class, 'id', 'coupon_id')->withTrashed();
  67. }
  68. public function payment(): HasOne {
  69. return $this->hasOne(Payment::class, 'oid', 'oid');
  70. }
  71. // 订单状态
  72. public function getStatusLabelAttribute(): string {
  73. switch($this->attributes['status']){
  74. case -1:
  75. $status_label = trans('home.invoice_status_closed');
  76. break;
  77. case 1:
  78. $status_label = trans('home.invoice_status_wait_confirm');
  79. break;
  80. case 2:
  81. $status_label = trans('home.invoice_status_payment_confirm');
  82. break;
  83. case 0:
  84. $status_label = trans('home.invoice_status_wait_payment');
  85. break;
  86. default:
  87. $status_label = 'Unknown';
  88. }
  89. return $status_label;
  90. }
  91. public function getOriginAmountAttribute($value) {
  92. return $value / 100;
  93. }
  94. public function setOriginAmountAttribute($value) {
  95. return $this->attributes['origin_amount'] = $value * 100;
  96. }
  97. public function getAmountAttribute($value) {
  98. return $value / 100;
  99. }
  100. public function setAmountAttribute($value) {
  101. return $this->attributes['amount'] = $value * 100;
  102. }
  103. // 支付渠道
  104. public function getPayTypeLabelAttribute(): string {
  105. switch($this->attributes['pay_type']){
  106. case 0:
  107. $pay_type_label = '余额';
  108. break;
  109. case 1:
  110. $pay_type_label = '支付宝';
  111. break;
  112. case 2:
  113. $pay_type_label = 'QQ';
  114. break;
  115. case 3:
  116. $pay_type_label = '微信';
  117. break;
  118. case 4:
  119. $pay_type_label = '虚拟货币';
  120. break;
  121. case 5:
  122. $pay_type_label = 'PayPal';
  123. break;
  124. default:
  125. $pay_type_label = '';
  126. }
  127. return $pay_type_label;
  128. }
  129. // 支付图标
  130. public function getPayTypeIconAttribute(): string {
  131. $base_path = '/assets/images/payment/';
  132. switch($this->attributes['pay_type']){
  133. case 1:
  134. $pay_type_icon = $base_path.'alipay.png';
  135. break;
  136. case 2:
  137. $pay_type_icon = $base_path.'qq.png';
  138. break;
  139. case 3:
  140. $pay_type_icon = $base_path.'wechat.png';
  141. break;
  142. case 5:
  143. $pay_type_icon = $base_path.'paypal.png';
  144. break;
  145. case 0:
  146. case 4:
  147. default:
  148. $pay_type_icon = $base_path.'coin.png';
  149. }
  150. return $pay_type_icon;
  151. }
  152. // 支付方式
  153. public function getPayWayLabelAttribute(): string {
  154. switch($this->attributes['pay_way']){
  155. case 'credit':
  156. $pay_way_label = '余额';
  157. break;
  158. case 'youzan':
  159. $pay_way_label = '有赞云';
  160. break;
  161. case 'f2fpay':
  162. $pay_way_label = '支付宝当面付';
  163. break;
  164. case 'codepay':
  165. $pay_way_label = '码支付';
  166. break;
  167. case 'payjs':
  168. $pay_way_label = 'PayJs';
  169. break;
  170. case 'bitpayx':
  171. $pay_way_label = '麻瓜宝';
  172. break;
  173. case 'paypal':
  174. $pay_way_label = 'PayPal';
  175. break;
  176. default:
  177. $pay_way_label = '未知';
  178. }
  179. return $pay_way_label;
  180. }
  181. }