應(yīng)用情景,有新的訂單生成,后臺(tái)進(jìn)行消息提醒并播放音頻(這里用到輪詢簡(jiǎn)單實(shí)現(xiàn)):下面附代碼
1.找到laravel-admin 中的 index.blade.php 文件進(jìn)行編輯
{{-- 音頻通知 --}} <audio style="display:none; height: 0" id="bg-music" preload="auto" src="http://fjdx.sc.chinaz.com/Files/DownLoad/sound1/201511/6571.mp3" loop="loop"></audio> <script> function LA() {} LA.token = "{{ csrf_token() }}"; var getting = { url:'/admin/sendNotice', dataType:'json', success: function (res) { console.log(res); if(res.code == 200){ toastr.options.onclick = function(){ location.href='/admin/order'; // 點(diǎn)擊跳轉(zhuǎn)頁(yè)面 }; toastr.options.timeOut=120000; // 保存2分鐘 toastr.warning(res.msg); // 提示文字 var audio = document.getElementById('bg-music'); // 啟用音頻通知 audio.play(); setTimeout(function(){ audio.load(); // 1.5秒后關(guān)閉音頻通知 },1500); } }, error: function (res) { console.log(res); } }; //關(guān)鍵在這里,Ajax定時(shí)訪問(wèn)服務(wù)端,不斷獲取數(shù)據(jù) ,這里是5秒請(qǐng)求一次。 window.setInterval(function(){$.ajax(getting)},5000); </script>
2.請(qǐng)求路由控制器
/** * 輪詢判斷有沒(méi)有新訂單 * * @return void */ public function sendNotice() { // 處理邏輯 $count = 0; // 獲取的結(jié)果 $getCount = session()->get('count'); if ($count > $getCount) { session()->put('count', $count); // 存session return ['code' => 200, 'msg' => '您有新的訂單請(qǐng)及時(shí)處理']; } // 不成立的話則存最新的值 session()->put('count', $count); return ['code' => 201]; }
實(shí)現(xiàn)效果:
注意:
使用Google 瀏覽器如果無(wú)法播放提示音?
1、進(jìn)入 chrome://flags/#autoplay-policy
2、找到 Autoplay policy
3、修改Default
總結(jié)
以上所述是小編給大家介紹的Laravel-admin 消息提醒、播放音頻功能的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com