消息提醒:您有新的订单,请及时处理

外卖吃多了,你都听到过这中声音,有好奇过是怎么实现的吗?

其实很简单,就是前端不断的请求后台接口,根据情况自动播放前端的mp3之类的早就准备好的音频。

前端核心代码:

  


   
public function search()
    {
        if (!$this->validate()) {
            return $this->errorResponse;
        }
        $query = OrderMessage::find()->alias('om')->where([
            'om.store_id' => $this->store_id,
            'om.shop_id' => 0,
            'om.is_delete' => 0,
            'om.order_type' => [0, 1, 2, 3, 4]
        ]);
        if ($this->is_read) {
            $query->andWhere(['or',['om.is_read' => 0],['om.is_sound'=>0]]);
        }
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit]);

        $list = $query->orderBy(['om.addtime' => SORT_DESC])
            ->limit($pagination->limit)
            ->offset($pagination->offset)
            ->asArray()
            ->all();

        $urlManager = \Yii::$app->urlManager;
        // 此处考虑在 cache 或 setting 内做缓存,存储 order_message 表的更新时间,以及查询结果
        // 插入 order_message 时,在 beforeInsert 内清空缓存
        // -- wi1dcard
        foreach ($list as $index => &$value) {
            switch ($value['order_type']) {
                case 3:
                    $yy_order = YyOrder::findOne(['id' => $value['order_id'], 'store_id' => $this->store_id]);
                    if ($value['type'] == 0) {
                        $value['url'] = $urlManager->createUrl(['mch/book/order/index', 'status' => 1]);
                    } else {
                        $value['url'] = $urlManager->createUrl(['mch/book/order/index', 'status' => 3]);
                    }
                    $user = User::findOne(['id' => $yy_order->user_id]);
                    $order['order_no'] = $yy_order->order_no;
                    $order['name'] = $user->nickname;
                    break;
                case 4:
                    $mch_goods = Goods::findOne(['id' => $value['order_id']]);
                    $mch = Mch::findOne(['id' => $mch_goods->mch_id]);
                    $value['url'] = $urlManager->createUrl(['mch/mch/goods/goods', 'keyword' => $mch_goods->name]);
                    $order['order_no'] = $mch_goods->name;
                    $order['name'] = $mch->name;
                    break;
                default:
                    if ($value['type'] == 0) {
                        $value['url'] = $urlManager->createUrl(['mch/order/index', 'status' => 1]);
                    } else {
                        $value['url'] = $urlManager->createUrl(['mch/order/refund']);
                    }
                    switch ($value['order_type']) {
                        case 0:
                            $class = "app\models\Order";
                            break;
                        case 1:
                            $class = "app\models\MsOrder";
                            $value['url'] = $urlManager->createUrl(['mch/miaosha/order/index', 'status' => 1]);
                            break;
                        case 2:
                            $class = "app\models\PtOrder";
                            $value['url'] = $urlManager->createUrl(['mch/group/order/index', 'status' => 1]);
                            break;
                        default:
                            break;
                    }
                    $order = $class ? $class::findOne(['id' => $value['order_id'], 'store_id' => $this->store_id]) : [];
                    //砍价订单
                    if($value['order_type']==0 && $order->type==2){
                        $value['url'] = $urlManager->createUrl(['mch/bargain/order/index']);
                        $value['is_bargain'] = 1;
                    }
            }
            $value['order_no'] = $order['order_no'];
            $value['name'] = $order['name'];
            $value['platform'] = User::find()->where(['id' => $order['user_id']])->select('platform')->scalar();

            $time = time() - $value['addtime'];

            if ($time < 60) {
                $value['time'] = $time . '秒前';
            } elseif ($time < 3600) {
                $value['time'] = ceil($time / 60) . '分钟前';
            } elseif ($time < 86400) {
                $value['time'] = ceil($time / 3600) . '小时前';
            } else {
                $value['time'] = ceil($time / 86400) . '天前';
            }
        }
        return [
            'list' => $list,
            'pagination' => $pagination
        ];
    }

 

你可能感兴趣的:(php)