审查视图

application/mobile/controller/Almighty.php 25.7 KB
何书鹏 authored
1 2 3 4 5 6 7
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\Question;
何书鹏 authored
8
use app\mobile\model\Simulation;
何书鹏 authored
9
use app\mobile\model\Old;
何书鹏 authored
10
use app\mobile\model\Everyday;
何书鹏 authored
11
use app\mobile\model\Secret;
何书鹏 authored
12 13 14 15
use app\mobile\model\QuestionAnswer;
use app\mobile\model\QuestionNote;
use app\mobile\model\QuestionWrong;
use app\mobile\model\QuestionCollect;
何书鹏 authored
16
use addons\qiniu\library\Auth;
1  
何书鹏 authored
17
use app\common\model\Attachment;
何书鹏 authored
18 19 20

/**
 * 全能题库接口
何书鹏 authored
21
 * @ApiWeigh (98)
何书鹏 authored
22 23 24
 */
class Almighty extends Api
{
1  
何书鹏 authored
25
	protected $noNeedLogin = [''];
何书鹏 authored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (全能题库-题目列表)
     * @ApiSummary  (全能题库-题目列表)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="answer_type", type="string", required=true, description="作答类型:0=全部,1=已答题,2=未答题,3=错题")
     * @ApiParams (name="question_type", type="string", required=true, description="试题题型:0=全部,1=单选题,2=多选题,3=判断题,4=简答题")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599032660",
        "data": {
            "total": 1, //题目总数
            "list": [{ //题目列表
                "id": 1, //题目ID
                "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
                "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
                "type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
                "answer": "A", //答案
                "note": "笔记", //笔记
                "is_collect": "1" //是否收藏:0=否,1=是
            }]
        }
    })
     */
    public function questionList()
    {
        $user_id = $this->auth->id;
        $answer_type = $this->request->param('answer_type');
        $question_type = $this->request->param('question_type');
        $where['q.target_type'] = '1';
        switch ($answer_type) {
            case '1':
                $question_id_arr = QuestionAnswer::where('user_id',$user_id)->column('question_id');
                $where['q.id'] = !empty($question_id_arr) ? ['in',$question_id_arr] : 0;
                break;
            case '2':
                $question_id_arr = QuestionAnswer::where('user_id',$user_id)->column('question_id');
                if(!empty($question_id_arr)){
                    $where['q.id'] = ['notin',$question_id_arr];
                }
                break;
            case '3':
                $question_id_arr = QuestionAnswer::where('user_id',$user_id)->where('is_wrong','1')->column('question_id');
                $where['q.id'] = !empty($question_id_arr) ? ['in',$question_id_arr] : 0;
                break;
        }
        if(!empty($question_type)){
            $where['q.type'] = $question_type;
        }
        $list = Question::alias('q')
何书鹏 authored
87 88
            ->join('mobile_question_note qn','q.id = qn.question_id and qn.user_id='.$user_id,'left')
            ->join('mobile_question_collect qc','q.id = qc.question_id and qc.user_id='.$user_id,'left')
何书鹏 authored
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            ->where($where)
            ->field('
                q.id,
                q.title,
                q.option,
                q.type,
                q.answer,
                qn.content note,
                if(qc.id > 0,1,0) is_collect
            ')->orderRaw('rand()')->limit(100) // 随机100道题目
            ->select();
        $total = count($list);
        $this->success('成功',compact('total','list'));
    }

    /**
     * @ApiTitle    (全能题库-题目列表-详情)
     * @ApiSummary  (全能题库-题目列表-详情)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="question_id", type="int", required=true, description="题目ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599032660",
        "data": {
            "id": 1, //题目ID
            "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
            "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
            "type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
            "answer": "A", //答案
            "note": "笔记", //笔记
            "is_collect": "1", //是否收藏:0=否,1=是
            "status": "1" //状态:0=未答,1=答对,2=答错
            "my_answer": "1", //我的答案
        }
    })
     */
    public function questionInfo()
    {
        $user_id = $this->auth->id;
        $question_id = $this->request->param('question_id');
        empty($question_id) && $this->error('缺少必要参数');
        $info = Question::alias('q')
何书鹏 authored
136 137 138
            ->join('mobile_question_note qn','q.id = qn.question_id and qn.user_id='.$user_id,'left')
            ->join('mobile_question_collect qc','q.id = qc.question_id and qc.user_id='.$user_id,'left')
            ->join('mobile_question_answer qa','q.id = qa.question_id and qa.user_id='.$user_id,'left')
何书鹏 authored
139 140 141 142 143 144 145 146 147 148 149 150 151 152
            ->where('q.id',$question_id)
            ->field('
                q.id,
                q.title,
                q.option,
                q.type,
                q.answer,
                q.analysis_video,
                q.analysis_text,
                qn.content note,
                if(qc.id > 0,1,0) is_collect,
                if(qa.id > 0,if(qa.is_wrong="0",1,2),0) status,
                qa.content my_answer
            ')->find();
何书鹏 authored
153
        $this->success('成功',$info);
何书鹏 authored
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }

    /**
     * @ApiTitle    (全能题库-答题)
     * @ApiSummary  (全能题库-答题)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="question_id", type="int", required=true, description="题目ID")
     * @ApiParams (name="answer", type="string", required=true, description="回答内容")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599032660",
        "data": null
    })
     */
    public function answer()
    {
        $question_id = $this->request->param('question_id');
        $answer = $this->request->param('answer');
        if(empty($question_id) || empty($answer)){
            $this->error('缺少必要参数');
        }
        $question = Question::get($question_id);
        empty($question) && $this->error('题目信息不存在');
        $info = QuestionAnswer::get(['question_id'=>$question_id,'user_id'=>$this->auth->id]);
        if(empty($info)){
            $info = new QuestionAnswer();
        }
        // 记录答案
        $score = 0;
        $is_wrong = '0';
        if($question['type'] != '4'){
            if($question['answer'] == $answer){
                $score = $question['score'];
            }else{
                $is_wrong = '1';
            }
        }
        $info->allowField(true)->save([
            'question_id' => $question_id,
            'user_id' => $this->auth->id,
            'content' => $answer,
            'is_wrong' => $is_wrong,
            'get_score' => $score
        ]);
        $this->success('回答成功');
    }

    /**
     * @ApiTitle    (全能题库-查看解析)
     * @ApiSummary  (全能题库-查看解析)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="question_id", type="int", required=true, description="题目ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
何书鹏 authored
218
        "time": "1600675472",
何书鹏 authored
219
        "data": {
何书鹏 authored
220 221 222 223 224
            "analysis_video": { //解析视频
                "cover": "http://qizhibang.brotop.cn/uploads/20200921/Fkp1Dv0c4dyYfVzAFBjhTuv25BNv.mp4?vframe/jpg/offset/1/w/1280/h/720", //封面图
                "video": "http://qizhibang.brotop.cn/uploads/20200921/Fkp1Dv0c4dyYfVzAFBjhTuv25BNv.mp4" //视频
            },
            "analysis_text": "" //解析文字
何书鹏 authored
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        }
    })
     */
    public function analysis()
    {
        $question_id = $this->request->param('question_id');
        empty($question_id) && $this->error('缺少必要参数');      
        $info = Question::get($question_id);
        empty($info) && $this->error('题目信息不存在');
        $info->visible(['analysis_video','analysis_text']);
        $this->success('成功',$info);
    }

    /**
     * @ApiTitle    (全能题库-笔记)
     * @ApiSummary  (全能题库-笔记)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="question_id", type="int", required=true, description="题目ID")
     * @ApiParams (name="content", type="int", required=true, description="笔记内容")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599032660",
        "data": null
    })
     */
    public function note()
    {
        $question_id = $this->request->param('question_id');
        $content = $this->request->param('content');
        empty($question_id) && $this->error('缺少必要参数');
        empty($content) && $this->error('请填写笔记');
        $info = QuestionNote::get(['question_id'=>$question_id,'user_id'=>$this->auth->id]);
        if(empty($info)){
            $info = new QuestionNote();
        }
        $info->allowField(true)->save([
            'question_id' => $question_id,
            'user_id' => $this->auth->id,
            'content' => $content
        ]);
        $this->success('添加笔记成功');
    }

    /**
     * @ApiTitle    (全能题库-报错)
     * @ApiSummary  (全能题库-报错)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="question_id", type="int", required=true, description="题目ID")
何书鹏 authored
281
     * @ApiParams (name="type", type="string", required=true, description="报错类型:1=答案不正确,2=题目不正确,3=解析不完整,4=其他错误")
何书鹏 authored
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
     * @ApiParams (name="content", type="string", required=true, description="问题描述")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599032660",
        "data": null
    })
     */
    public function wrong()
    {
        $question_id = $this->request->param('question_id');
        $type = $this->request->param('type');
        $content = $this->request->param('content');
        empty($question_id) && $this->error('缺少必要参数');
        empty($type) && $this->error('请选择报错类型');
        empty($content) && $this->error('请填写问题描述');
        $info = QuestionWrong::get(['question_id'=>$question_id,'user_id'=>$this->auth->id]);
        if(empty($info)){
            $info = new QuestionWrong();
        }
        $info->allowField(true)->save([
            'question_id' => $question_id,
            'user_id' => $this->auth->id,
            'type' => $type,
            'content' => $content
        ]);
        $this->success('报错成功');
    }

    /**
     * @ApiTitle    (全能题库-收藏)
     * @ApiSummary  (全能题库-收藏)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="question_id", type="int", required=true, description="题目ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599032660",
        "data": null
    })
     */
    public function collect()
    {
        $question_id = $this->request->param('question_id');
        empty($question_id) && $this->error('缺少必要参数');
        $info = QuestionCollect::get(['question_id'=>$question_id,'user_id'=>$this->auth->id]);
        if(!empty($info)){
            $info->delete();
            $this->success('取消收藏成功');
        }
        QuestionCollect::create([
            'question_id' => $question_id,
            'user_id' => $this->auth->id,
        ]);
        $this->success('收藏成功');
    }

    /**
何书鹏 authored
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
     * @ApiTitle    (智能搜题-语音转文字)
     * @ApiSummary  (智能搜题-语音转文字)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="filename", type="string", required=true, description="文件地址")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599130815",
        "data": [{
            "id": 1, //题目ID
            "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
            "type": "4", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
            "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]" //题目选项
        }]
    })
     */
    public function getText()
    {
        $filename = $this->request->param('filename');
何书鹏 authored
368
        empty($filename) && $this->error('请输入音频地址');
何书鹏 authored
369
        $suffix = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
何书鹏 authored
370 371 372 373 374 375 376 377
        if (!in_array($suffix, ['pcm', 'wav', 'amr', 'm4a', 'mp3'])) {
            $this->error('不支持该音频格式');
        }
        if($suffix == 'mp3'){
            $filename = $this->mp3ToPcm($filename);
        }
        $result = $this->asrJson($filename);
        $this->success('成功',['text'=>$result]);
何书鹏 authored
378 379 380
    }

    /**
何书鹏 authored
381 382 383 384 385 386 387 388
     * @ApiTitle    (智能搜题)
     * @ApiSummary  (智能搜题)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams (name="keyword", type="string", required=true, description="关键词")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
何书鹏 authored
389
     * @ApiParams   (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
何书鹏 authored
390 391 392 393 394 395 396 397
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599130815",
        "data": [{
            "id": 1, //题目ID
            "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
何书鹏 authored
398
            "type": "4", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
何书鹏 authored
399 400 401 402 403 404 405
            "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]" //题目选项
        }]
    })
     */
    public function search()
    {
        $page = $this->request->param('page', 1, 'intval');
何书鹏 authored
406
        $page_num = $this->request->param('page_num', 10, 'intval');
何书鹏 authored
407 408 409 410
        $keyword = $this->request->param('keyword');
        empty($keyword) && $this->error('缺少必要参数');
        $list = Question::where('target_type','1')
            ->where('title','like','%'.$keyword.'%')
何书鹏 authored
411
            ->field('id,title,type,option')
何书鹏 authored
412
            ->page($page,$page_num)
何书鹏 authored
413 414 415 416 417 418 419 420 421 422 423 424 425 426
            ->select();
        $this->success('成功',$list);
    }

    /**
     * @ApiTitle    (错题训练)
     * @ApiSummary  (错题训练)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
何书鹏 authored
427
        "time": "1600678092",
何书鹏 authored
428
        "data": {
何书鹏 authored
429 430 431 432 433 434 435 436 437
            "banner": "http://www.enterprise.top/uploads/20200911/8894d62100f2f920ffb2f38063b63f2d.jpg", //广告图
            "list": [{ // 试卷列表
                "0": {
                    "id": 11,
                    "target_type": "1",
                    "target_id": 0,
                    "type": "3"
                },
                "pan": 1, //判断题数
何书鹏 authored
438
                "duo": 0, //多选题数
何书鹏 authored
439 440 441 442 443
                "dan": 0, //单选题数
                "title": "《全能题库》", //试卷标题
                "type": "1", //题目归属类型:1=全能题库,2=模拟试题,3=历年真题,4=每日一练,5=通关密卷
                "id": 0 //试卷ID(全能题库为0)
            }]
何书鹏 authored
444 445 446 447 448
        }
    })
     */
    public function wrongQuestion()
    {
何书鹏 authored
449 450
        $banner = Db::name('mobile_config')->where('id',1)->value('wrong_adver');
        $banner = !empty($banner) ? cdnurl($banner,true) : '';
何书鹏 authored
451
        $question_list = Question::alias('q')
何书鹏 authored
452
            ->join('mobile_question_answer qa','q.id = qa.question_id and user_id='.$this->auth->id,'left')
何书鹏 authored
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
            ->where('qa.is_wrong','1')
            ->field('q.id,q.target_type,q.target_id,q.type')
            ->select();
        $list = [];
        foreach ($question_list as $v) {
            $list[$v['target_id']][] = $v;
        }
        // 统计题目数
        foreach ($list as &$v) {
            $v['dan'] = $v['duo'] = $v['pan'] = 0;
            foreach ($v as &$value) {
                switch ($value['type']) {
                    case '1':
                        $v['dan']++;
                        break;
                    case '2':
                        $v['duo']++;
                        break;
                    case '3':
                        $v['pan']++;
                        break;
                }
            }
            switch ($v[0]['target_type']) {
                case '1':
                    $v['title'] = '《全能题库》';
                    break;
                case '2':
何书鹏 authored
481
                    $v['title'] = Simulation::where('id',$v[0]['target_id'])->value('title');
何书鹏 authored
482
                    break;
何书鹏 authored
483 484 485
                case '3':
                    $v['title'] = Old::where('id',$v[0]['target_id'])->value('title');
                    break;
何书鹏 authored
486 487 488
                case '4':
                    $v['title'] = Everyday::where('id',$v[0]['target_id'])->value('title');
                    break;
何书鹏 authored
489 490 491
                case '5':
                    $v['title'] = Secret::where('id',$v[0]['target_id'])->value('title');
                    break;
何书鹏 authored
492
            }
何书鹏 authored
493
            $v['type'] = $v[0]['target_type'];
何书鹏 authored
494 495
            $v['id'] = $v[0]['target_id'];
        }
何书鹏 authored
496
        $this->success('成功',compact('banner','list'));
何书鹏 authored
497
    }
何书鹏 authored
498 499

    /**
何书鹏 authored
500 501
     * mp3转pcm
     * @ApiInternal
何书鹏 authored
502 503 504 505 506 507 508
     */
    public function mp3ToPcm($filename)
    {
        $key = ltrim($filename,'/');
        $config = get_addon_config('qiniu');
        $auth = new Auth($config['app_key'], $config['secret_key']);
        //设置转码参数
何书鹏 authored
509
        $fops = "avthumb/s16le/acodec/pcm_s16le/ac/1/ar/16000";
何书鹏 authored
510 511
        //通过添加'|saveas'参数,指定处理后的文件保存的bucket和key 不指定默认保存在当前空间,bucket为目标空间,后一个参数为转码之后文件名
        $filename = str_replace(strrchr($key, "."),"",$key); 
1  
何书鹏 authored
512
        $entry = $config['bucket'] . ':' . $filename . '.pcm';
何书鹏 authored
513 514 515 516 517
        $saveas_key = $auth->base64_urlSafeEncode($entry);
 
        $fops = $fops.'|saveas/'.$saveas_key;

        $policy = array(
何书鹏 authored
518
            'persistentOps' => $fops,
1  
何书鹏 authored
519
            'persistentNotifyUrl' => $this->request->root(true) . '/mobile/notify/notifyQiniuAvthumb',
何书鹏 authored
520 521 522 523 524 525
        );
        $token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
        $multipart = [
            ['name' => 'token', 'contents' => $token],
            [
                'name'     => 'file',
1  
何书鹏 authored
526
                'contents' => fopen(ROOT_PATH . '/public/' .$key, 'r'),
何书鹏 authored
527 528 529 530 531 532 533 534 535 536 537
                'filename' => $key,
            ]
        ];
        try {
            $client = new \GuzzleHttp\Client();
            $res = $client->request('POST', $config['uploadurl'], [
                'multipart' => $multipart
            ]);
            $code = $res->getStatusCode();
            //成功不做任何操作
        } catch (\GuzzleHttp\Exception\ClientException $e) {
何书鹏 authored
538 539
            $this->error("上传失败");
        }
1  
何书鹏 authored
540
        // 返回文件地址
1  
何书鹏 authored
541 542 543 544 545
        $url = '/' . $filename . '.pcm';
        if($this->file_exists($url)){
            return cdnurl($url);
        }
        $this->error('接口错误');
1  
何书鹏 authored
546 547 548
    }

    /**
1  
何书鹏 authored
549
     * 判断文件是否存在
1  
何书鹏 authored
550 551
     * @ApiInternal
     */
1  
何书鹏 authored
552
    public function file_exists($url)
1  
何书鹏 authored
553
    {
1  
何书鹏 authored
554 555 556
        $info = Db::name('attachment')->where('url',$url)->field('id')->find();
        if(!$info){
            $this->file_exists($url);
1  
何书鹏 authored
557
        }else{
1  
何书鹏 authored
558
            return true;
1  
何书鹏 authored
559
        }
何书鹏 authored
560 561 562 563 564 565 566
    }

    /**
     * 语音转文字
     * @ApiInternal
     */
    public function asrJson($filename){
1  
何书鹏 authored
567
        dump($filename);
何书鹏 authored
568 569 570 571 572 573
        define('DEMO_CURL_VERBOSE', false); // 打印curl debug信息
        # 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx"
        $API_KEY =  "kVcnfD9iW2XVZSMaLMrtLYIz";
        # 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx"
        $SECRET_KEY = "O9o1O213UgG5LFn0bDGNtoRN3VWl2du6";
        # 需要识别的文件
1  
何书鹏 authored
574
        $AUDIO_FILE = preg_match("/^http(s)?:\\/\\/.+/",$filename) ? $filename : ROOT_PATH . '/public' . $filename;
何书鹏 authored
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
        # 文件格式
        $FORMAT = substr($AUDIO_FILE, -3); // 文件后缀 pcm/wav/amr 格式 极速版额外支持m4a 格式
        $CUID = "123456PHP";
        # 采样率
        $RATE = 16000;  // 固定值
        # 普通版
        $ASR_URL = "http://vop.baidu.com/server_api";
        # 根据文档填写PID,选择语言及识别模型
        $DEV_PID = 1537; //  1537 表示识别普通话,使用输入法模型。
        $SCOPE = 'audio_voice_assistant_get'; // 有此scope表示有语音识别普通版能力,没有请在网页里开通语音识别能力

        #测试自训练平台需要打开以下信息, 自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
        //$DEV_PID = 8001 ;   
        //$LM_ID = 1234 ;

        # 极速版需要打开以下信息 打开注释的话请填写自己申请的appkey appSecret ,并在网页中开通极速版(开通后可能会收费)
        //$ASR_URL = "http://vop.baidu.com/pro_api";
        //$DEV_PID = 80001; 
        //$SCOPE = 'brain_enhanced_asr';  // 有此scope表示有极速版能力,没有请在网页里开通极速版

        $SCOPE = false; // 部分历史应用没有加入scope,设为false忽略检查
        /** 公共模块获取token开始 */
        $auth_url = "http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$API_KEY."&client_secret=".$SECRET_KEY;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $auth_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 检查证书中是否设置域名,0不验证
        curl_setopt($ch, CURLOPT_VERBOSE, DEMO_CURL_VERBOSE);
        $res = curl_exec($ch);
        if(curl_errno($ch))
        {
            $this->error(curl_error($ch));
        }
        curl_close($ch);
        $response = json_decode($res, true);
        if (!isset($response['access_token'])){
何书鹏 authored
613
            $this->error('ERROR TO OBTAIN TOKEN');
何书鹏 authored
614 615
        }
        if (!isset($response['scope'])){
何书鹏 authored
616
            $this->error('ERROR TO OBTAIN scopes');
何书鹏 authored
617 618
        }
        if ($SCOPE && !in_array($SCOPE, explode(" ", $response['scope']))){
何书鹏 authored
619
            $this->error('CHECK SCOPE ERROR');// 请至网页上应用内开通语音识别权限
何书鹏 authored
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        }
        $token = $response['access_token'];
        /** 公共模块获取token结束 */

        /** 拼接参数开始 **/
        $audio = file_get_contents($AUDIO_FILE);
        $base_data = base64_encode($audio);
        $params = array(
            "dev_pid" => $DEV_PID,
            //"lm_id" => $LM_ID,    //测试自训练平台开启此项
            "format" => $FORMAT,
            "rate" => $RATE,
            "token" => $token,
            "cuid"=> $CUID,
            "speech" => $base_data,
            "len" => strlen($audio),
            "channel" => 1,
        );

        $json_array = json_encode($params);
        $headers[] = "Content-Length: ".strlen($json_array);
        $headers[] = 'Content-Type: application/json; charset=utf-8';
        /** 拼接参数结束 **/
            
        /** asr 请求开始 **/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $ASR_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 识别时长不超过原始音频
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_array);
        curl_setopt($ch, CURLOPT_VERBOSE, DEMO_CURL_VERBOSE);
        $res = curl_exec($ch);
        if(curl_errno($ch))
        {
            $this->error(curl_error($ch));
        }
        curl_close($ch);
何书鹏 authored
660
        /** asr 请求结束 **/
何书鹏 authored
661
何书鹏 authored
662 663 664 665 666 667 668
        // 解析结果
        $response = json_decode($res, true);

        if (isset($response['err_no']) && $response['err_no'] == 0){
            return $response['result'][0];
        }else{
            $this->error($response['err_msg']);
何书鹏 authored
669 670
        }
    }
何书鹏 authored
671
}