合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# event事件 事件是处理不同业务之间关联关系的核心,是插件扩展的重要组成部分 ## event目录(app下) ``` ├─app │ event 事件实现类文件 │ event.php 事件配置 ``` ## event目录(addon下) ``` ├─addon │ event 事件实现类文件 │ config │ ├─event.php 事件配置 ``` **一,event.php事件配置** ~~~ 'OrderComplete' => [ //订单完成后执行 后续事件 'app\event\OrderComplete', //订单支付后统计 ], //订单完成后执行事件 ~~~ **二,event事件实现** ~~~ <?php /** * Niushop商城系统 - 团队十年电商经验汇集巨献! * ========================================================= * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。 * ---------------------------------------------- * 官方网址: https://www.niushop.com * ========================================================= */ namespace app\event; use app\model\member\Member; use app\model\member\MemberAccount; use app\model\member\MemberLevel; use app\model\order\OrderCommon; /** * 订单完成后 */ class OrderComplete { // 行为扩展的执行入口必须是run public function handle($data) { //订单返还积分 $order_model = new OrderCommon(); $condition = array( ['order_id', '=', $data['order_id']] ); $order_info_result = $order_model->getOrderInfo($condition, 'order_money,order_status,site_id,member_id'); $order_info = $order_info_result['data']; //如果缺失已完成 if ($order_info['order_status'] == 10) { //会员等级 计算积分返还比率 $site_id = $order_info['site_id']; $member_id = $order_info['member_id']; $member_model = new Member(); $member_info_result = $member_model->getMemberInfo([['member_id', '=', $member_id], ['site_id', '=', $site_id]], 'member_level'); $member_info = $member_info_result['data']; if ($member_info['member_level'] > 0) { $member_level_model = new MemberLevel(); $member_level_info_result = $member_level_model->getMemberLevelInfo([['level_id', '=', $member_info['member_level']], ['site_id', '=', $site_id]], "point_feedback"); $member_level_info = $member_level_info_result['data']; if ($member_level_info['point_feedback'] > 0) { //计算返还的积分 $point = round($order_info['order_money'] * $member_level_info['point_feedback']); $member_account_model = new MemberAccount(); $result = $member_account_model->addMemberAccount($site_id, $member_id, 'point', $point, 'order', '会员消费回馈积分', '会员消费奖励发放'); if ($result['code'] < 0) { return $result; } } } } return $order_model->success(); } } ~~~ **一,event调用** event("OrderComplete", $order); ~~~ /** * 订单完成 * * @param int $order_id */ public function orderComplete($order_id) { $cache = Cache::get('order_complete_execute_' . $order_id); if (empty($cache)) { Cache::set('order_complete_execute_' . $order_id, 1); } else { return $this->success(); } $lock_result = $this->verifyOrderLock($order_id); if ($lock_result[ 'code' ] < 0) return $lock_result; $order_info = model('order')->getInfo([ [ 'order_id', '=', $order_id ] ], 'member_id, order_money, refund_money,order_status,site_id'); if ($order_info[ 'order_status' ] == self::ORDER_COMPLETE) { return $this->success(); } $order_data = array ( 'order_status' => self::ORDER_COMPLETE, 'order_status_name' => $this->order_status[ self::ORDER_COMPLETE ][ 'name' ], 'order_status_action' => json_encode($this->order_status[ self::ORDER_COMPLETE ], JSON_UNESCAPED_UNICODE), 'finish_time' => time() ); $config = new Config(); $order_event = $config->getOrderEventTimeConfig($order_info[ 'site_id' ]); $after_sales_time = $order_event[ 'data' ][ 'value' ][ 'after_sales_time' ] ?? 0; if ($after_sales_time > 0) { $cron = new Cron(); $execute_time = strtotime("+ {$after_sales_time} day"); $cron->addCron(1, 0, '订单售后自动关闭', 'CronOrderAfterSaleClose', $execute_time, $order_id); } else { $order_data[ 'is_enable_refund' ] = 0; } $res = model('order')->update($order_data, [ [ 'order_id', '=', $order_id ] ]); Cache::set('order_complete_execute_' . $order_id, ''); //修改用户表order_complete_money和order_complete_num model('member')->setInc([ [ 'member_id', '=', $order_info[ 'member_id' ] ] ], 'order_complete_money', $order_info[ 'order_money' ] - $order_info[ 'refund_money' ]); model('member')->setInc([ [ 'member_id', '=', $order_info[ 'member_id' ] ] ], 'order_complete_num'); event('OrderComplete', [ 'order_id' => $order_id ]); $order_refund_model = new OrderRefund(); //订单项移除可退款操作 $order_refund_model->removeOrderGoodsRefundAction([ [ 'order_id', '=', $order_id ] ]); //订单完成 $message_model = new Message(); $message_model->sendMessage([ 'keywords' => 'ORDER_COMPLETE', 'order_id' => $order_id, 'site_id' => $order_info[ 'site_id' ] ]); // 买家订单完成通知商家 $message_model->sendMessage([ 'keywords' => 'BUYER_ORDER_COMPLETE', 'order_id' => $order_id, 'site_id' => $order_info[ 'site_id' ] ]); return $this->success($res); } ~~~