后台 语音提示 百度语音合成

本文介绍了一种使用JS和PHP实现的商品库存预警系统。通过JS进行语音播放提示,配合PHP后端查询库存状态,当库存低于预警值时,系统会生成语音文件并播放,及时通知管理员。

想写一个后台 商品库存提示的  

js 代码 

/**
*播放语音
*/
function voice (){
 
	$.ajax({
			type:"get",
			url:"{:url('goods_limit')}",
			async:true,
			success:function(res){
			if(res.code=='1'){
				 var html = '<div class="tip" style="width:200px;height:80px;background: #106cb6;position:fixed;right:100px;bottom:100px;color:#fff;text-align:center;line-height:80px;border-radius:10px;"><div>'+res.msg+'</div></div>';
				  $('embed').remove();
				  $('body').append('<embed src="'+window.location.protocol+"//"+window.location.host+'/public_html/mp3/audio.mp3" autostart="true" hidden="true" loop="false">');
				  $("#tip").html(html);
			}
			 
			},
			error:function(){

			}
		})
	//提示 取消 
	setTimeout("voice_none()", 4000)
}

php 代码  调用的是百度的 语音合成 

$this->APP_ID, $this->API_KEY, $this->SECRET_KEY 等参数 填写自己的  并且 引用 百度的 demo 

require_once '../extend/Voice/AipSpeech.php';
//查询
	public function goods_limit(){
		// $goods_name_arr = session('goods_name_arr');
		$warning = db('xitong')->where('id',1)->value('warning');
		$goods_name_arr = db('goods')->where(['goods_number'=>['<=',$warning],'onsale'=>1])->column('goods_name');
		if($goods_name_arr){
			session('goods_name_arr',$goods_name_arr);
			$ret['code']=1;
			if(count($goods_name_arr)>1){
				$ret['msg'] = '多件商品库存预警';
			}else{
				$ret['msg'] = $goods_name_arr[0].'商品库存预警';
			}
			$this->goods_voice($ret['msg']);
			return json($ret);
		}else{
			$ret['code']=0;
			return json($ret);
		}
	}
	
	//语音文件生成 
	public function goods_voice($msg='商品库存预警提示'){
		$client = new \AipSpeech($this->APP_ID, $this->API_KEY, $this->SECRET_KEY);
		$result = $client->synthesis($msg, 'zh', 1, array(
			'vol' => 5,
			'dev_pid' => 1537,//播报声音
		));
		// 识别正确返回语音二进制 错误则返回json 参照下面错误码
		if(!is_array($result)){
			file_put_contents('mp3/audio.mp3', $result);
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值