想写一个后台 商品库存提示的
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);
}
}