帖子

微信小程序对接语音识别报10004无效的checkbox

[复制链接]

该用户从未签到

38601 176****3188 发表于 2018-12-29 17:22:59 1#
const express = require("express");
const app = express();
const multiparty = require("multiparty");
const ffmpeg = require("fluent-ffmpeg");
var fs = require('fs');
var http = require('http');
const crypto = require('crypto');

app.get("/ceshi",(req,res)=>{
  res.end("hello");
});


app.post("/smart_order", (req, res) => {
  var form = new multiparty.Form();
  form.parse(req, (err, fields, files) => {
    var file = files.wx_record[0];
    // file.path 完整路径 如 c://a/d/c.m4a
    // file.originalFilename c.m4a => uploads/c.wav
    var newPath = "uploads/" + file.originalFilename.slice(0, file.originalFilename.indexOf(".")) + ".wav";
    ffmpeg()
      .addInput(file.path)
      .save(newPath)
      .on("end", () => {
        // 将录音啊发送到讯飞
        webiat(newPath, res);
      })
      .on("error", (e) => {
        console.log("err", e)
      });
  });
});


var webiat = function (newPath, httpRes) {
  //讯飞开放平台注册申请应用的应用ID(APPID)
  var xAppid = "5c25dad3";
  // console.log('X-Appid:' + xAppid);
  var timestamp = Date.parse(new Date());
  var curTime = timestamp / 1000;
  // console.log('X-CurTime:' + curTime);
  var xParam = {
    "auf": "16k",
    "aue": "raw",
    "scene": "main",
    "userid": xAppid
  }
  xParam = JSON.stringify(xParam);
  const buf1 = Buffer.from(xParam)
  var xParamBase64 = buf1.toString('base64');
  console.log('X-Param:' + xParamBase64);
  //音频文件
  var fileData = fs.readFileSync(newPath);
  const buf2 = Buffer.from(fileData)
  var fileBase64 = buf2.toString('base64');
  var bodyData = "data=" + fileBase64;
  //ApiKey创建应用时自动生成
  var apiKey = "xxxxxxxxxxxxxxxxxxx";
  var token = apiKey + curTime + xParamBase64 + bodyData;
  var hash = crypto.createHash('md5');
  var xCheckSum = hash.update(token).digest('hex');
  console.log('X-CheckSum:' + xCheckSum);
  var options = {
    hostname: 'api.xfyun.cn',
    port: 80,
    path: '/v1/aiui/v1/voice_semantic',
    method: 'POST',
    headers: {
      "X-Appid": xAppid,
      "X-CurTime": curTime,
      "X-Param": xParamBase64,
      "X-CheckSum": xCheckSum,
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
  };

  var datas = [];
  var req = http.request(options, function (res) {
    res.setEncoding('utf-8');
    res.on('data', function (rersult) {
      // console.info('result body :' + rersult);
      datas.push(rersult);
    });
    res.on('end', function () {
      // console.log('No more data in response.');
      httpRes.end(datas.join(''));
    });
  });
  req.on('error', function (err) {
    console.error(err);
  });
  req.write(bodyData);
  req.end();
};

var server = app.listen(80, () => {
  var port = server.address().port;
  console.log(`开启成功 端口号为${port}`);
});
讯飞2017hj 发表于 2019-1-2 09:33:46
2#
您测试一下文档下面的demo看是否正常。
使用道具 举报 回复