社区首页 > 安装使用 > ShopWind开源电商系统 - 商城订单自动确认收货和未付款超时自动关闭的实现

ShopWind开源电商系统 - 商城订单自动确认收货和未付款超时自动关闭的实现

乔帮主发表在 安装使用 2021-07-25 06:07:05 191

ShopWind电商系统也有类似淘宝七天自动确认收货的功能,并且平台管理员可以设置订单自动确认收货和自动关闭的时间。下面说下实现原理。

        ShopWind系统是通过PHP脚本自动执行来完成自动确认收货功能。自动执行的操作有,已发货的订单超时自动确认收货、未支付的订单超时自动关闭、拼团订单已支付到期未成团自动成团。其他的拓展操作也可以加入脚本中自动执行。

        自动执行脚本通过服务器设置计划任务,定时去执行脚本,执行周期自行选择。脚本地址如下:http://你的域名/crontab/index.html。列如宝塔面板中设置计划任务,如下图所示:

未支付订单自动关闭代码,时间可以通过修改 $interval 改变,默认为2天。

private static function autoClosed()
{
  $today = Timezone::gmtime();	
   // 默认2天
   $interval = 2 * 24 * 3600;	
   // 每次仅处理2条记录,注意:处理太多会影响性能
   $list = OrderModel::find()->where("add_time + {$interval} < {$today}")->andWhere(['in', 'status', [Def::ORDER_SUBMITTED, Def::ORDER_PENDING]])->indexBy('order_id')->orderBy(['order_id' => SORT_ASC])->limit(2)->asArray()->all();
   foreach($list as $orderInfo)
   {
    // 修改订单状态
	OrderModel::updateAll(['status' => Def::ORDER_CANCELED], ['order_id' => $orderInfo['order_id']]);			
	// 修改交易状态
	DepositTradeModel::updateAll(['status' => 'CLOSED', 'end_time' => Timezone::gmtime()], ['bizIdentity' => Def::TRADE_ORDER, 'bizOrderId' => $orderInfo['order_sn'], 'buyer_id' => $orderInfo['buyer_id']]);			
	// 订单取消后,归还买家之前被预扣积分 
	IntegralModel::returnIntegral($orderInfo);		
  	// 加回商品库存
	OrderModel::changeStock('+', $orderInfo['order_id']);			
	// 记录订单操作日志
	$model = new OrderLogModel();
	$model->order_id = $orderInfo['order_id'];
	$model->operator = Language::get('system');
	$model->order_status = Def::getOrderStatus($orderInfo['status']);
	$model->changed_status = Def::getOrderStatus(Def::ORDER_CANCELED);
	$model->remark = '';
	$model->log_time = Timezone::gmtime();
	$model->save();
    }
} 

买家未确认收货,超时自动确认收货

private static function autoConfirm($otype = 'normal')
{
  // 排除退款中的订单
    $query = OrderModel::find()->alias('o')->where(['>', 'order_id', 0])->joinWith(['depositTrade' => function($model) {
	$model->alias('dt')->select('dt.tradeNo,dt.buyer_id,dt.bizOrderId,r.status as refund_status')->joinWith('refund r', false)->where(['in', 'r.status',['CLOSED', null, '']]);
    }]);
   $query = self::getConditions($query, $otype);
   // 每次仅处理2条记录,注意:处理太多会影响性能
   $list = $query->orderBy(['order_id' => SORT_ASC])->indexBy('order_id')->limit(2)->asArray()->all();
   foreach($list as $orderInfo)
   {
       // 交易信息 
       if(!($tradeInfo = DepositTradeModel::find()->select('tradeNo')->where(['bizIdentity' => Def::TRADE_ORDER, 'bizOrderId' => $orderInfo['order_sn']])->asArray()->one())) {
	 continue;
       }
	// 如果订单中的商品为空,则认为订单信息不完整,不执行
	$ordergoods = OrderGoodsModel::find()->where(['order_id' => $orderInfo['order_id']])->asArray()->all();
	if(empty($ordergoods)) {
		continue;
	}
	// 更新订单状态 
	$model = OrderModel::findOne($orderInfo['order_id']);
	$model->status = Def::ORDER_FINISHED;
	$model->finished_time = Timezone::gmtime();
	if(!$model->save()) {
		continue;
	}
	// 转到对应的业务实例,不同的业务实例用不同的文件处理,如购物,卖出商品,充值,提现等,每个业务实例又继承支出或者收入 
	$depopay_type    = \common\library\Business::getInstance('depopay')->build('sellgoods');
	$result = $depopay_type->submit(array(
		'trade_info' => array('userid' => $orderInfo['seller_id'], 'party_id' => $orderInfo['buyer_id'], 'amount' => $orderInfo['order_amount']),
		'extra_info' => $orderInfo + array('tradeNo' => $tradeInfo['tradeNo'])
	));
	if($result !== true) {
		continue;
	}
	// 确认收货后,即交易完成,处理订单商品三级返佣 
	DistributeModel::distributeInvite($orderInfo);
	// 如果是社区团购订单,给团长分成
	GuideshopModel::distributeProfit($orderInfo);
	// 确认收货后,即交易完成,将订单积分表中的积分进行派发
	IntegralModel::distributeIntegral($orderInfo);
	// 更新累计销售件数 
	foreach ($ordergoods as $key => $goods) {
		GoodsStatisticsModel::updateAllCounters(['sales' => $goods['quantity']], ['goods_id' => $goods['goods_id']]);
	}
	// 将确认的商品状态设置为 交易完成 
	OrderGoodsModel::updateAll(['status' => 'SUCCESS'], ['order_id' => $orderInfo['order_id']]);
	// 记录订单操作日志 
	$model = new OrderLogModel();
	$model->order_id = $orderInfo['order_id'];
	$model->operator = Language::get('system');
	$model->order_status = Def::getOrderStatus($orderInfo['status']);
	$model->changed_status = Def::getOrderStatus(Def::ORDER_FINISHED);
	$model->remark = '';
	$model->log_time = Timezone::gmtime();
	$model->save(); 
   }
}

拼团订单,到期已支付未成团,设为自动成团

private static function autoTeambuy()
{
   $today = Timezone::gmtime();
   // 到期未付款,自动删除拼团记录
   TeambuyLogModel::deleteAll(['and', ['<=', 'expired', $today], ['pay_time' => 0]]);
   // 已付款,到期未成团的订单,设置为自动成团(待发货)
   $list = TeambuyLogModel::find()->select('logid,order_id')->where(['and', ['<=', 'expired', $today], ['>', 'pay_time', 0], ['status' => 0]])->all();
   foreach($list as $model) {
	$model->status = 1;
	if($model->save()) {
	   // 从待成团状态设置为待发货状态
	   OrderModel::updateAll(['status' => Def::ORDER_ACCEPTED], ['status' => Def::ORDER_TEAMING, 'order_id' => $model->order_id]);
	}
   }
} 

回复
Re :
您需要登录后才可以回帖 登录