'json', ]; #未支付过期时间 const EXP_PAY=30; #待收货过期时间 const EXP_REC=10*86400; const PT_QYWY=1; const PT_WX=2; const PT_ZFB=3; const PT_YL=4; const PT_DF=5; const PT_OFF=6; public static $pay_types=[ self::PT_QYWY=>'企业网银', self::PT_WX=>'微信', self::PT_ZFB=>'支付宝', self::PT_YL=>'银联', self::PT_DF=>'代付', self::PT_OFF=>'线下支付', ]; const S_WAIT_PAY=0; const S_WAIT_REC=10; const S_OVER=20; const S_CANCEL=30; const S_REFUND=40; public static $status=[ self::S_WAIT_PAY=>'待支付', self::S_WAIT_REC=>'待收货', self::S_OVER=>'已完成', self::S_CANCEL=>'已取消', self::S_REFUND=>'退款退货', ]; /** * @return string[] */ public static function getStatus(): array { return self::$status; } protected $autoWriteTimestamp=true; public function info(){ return $this->hasMany(OrderInfo::class,'order_id'); } public function user(){ return $this->belongsTo(User::class); } public function address(){ return $this->hasOne(OrderAddress::class,'order_id'); } public function getGoodsAttr(){ $info=$this->info()->with(['goodsBak'])->find(); $goods=$info['goodsBak']; return $goods['goods']; } public function getIsWaitPayAttr($_,$model){ return $model['status']==self::S_WAIT_PAY; } /** * @return string[] */ public static function getPayTypes(): array { return self::$pay_types; } public static function continue($status){ $now=time(); return self::where('status',$status)->where('continue_expire_time','<',$now); } #未支付过期 public function makeCancel(){ $this['status']=self::S_CANCEL; $this['cancel_time']=time(); $this->save(); } #待收货过期 public function makeRec(){ $this['status']=self::S_OVER; $this['rec_time']=time(); $this->save(); } public function makePayInfo($pay_type){ $user=$this->user; return Payment::pay( $user, $pay_type, $this['amount_pay'], self::class, 'whenPaySuccess', $this['id'], "订单【{$this['order_no']}】付款", ); } public static function makePayed(Payment $payment){ $order=Orders::find($payment['payment_id']); if(!$order){ return false; } if(!$order->isNotPay()){ return false; } $order['status']=self::S_WAIT_REC; $order['pay_time']=$payment['pay_time']; $order->save(); return true; } public function isNotPay(){ return $this['status']===self::S_WAIT_PAY; } protected static function init() { self::beforeInsert(function (self $orders){ #优惠总金额 $orders['amount_discount']=bcAddAll($orders['amount_coupon']??0,$orders['amount_coupon_kill']); #过期时间 $orders['continue_expire_time']=time()+60*self::EXP_PAY; }); self::beforeUpdate(function (self $order){ $data=$order->getChangedData(); if(!empty($data['status'])){ if($data['status']==self::S_WAIT_REC){ $orders['continue_expire_time']=time()+self::EXP_REC; } } }); } }