AudioControllerApi.java 9.07 KB
package com.cnlive.shenhe.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.entity.ShyAudio;
import com.cnlive.shenhe.entity.ShyYidun;
import com.cnlive.shenhe.enums.TextLabels;
import com.cnlive.shenhe.service.AudioService;
import com.cnlive.shenhe.service.YiDunService;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.YiDunAntiFraudUtil;
import com.google.common.collect.EvictingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @Author: xujincheng
 * @Date: 2020/6/5 13:33
 * @Description: 音频送审接口
 */
@Controller
@RequestMapping("/api/audio")
public class AudioControllerApi {

    private static Logger logger = LoggerFactory.getLogger(AudioControllerApi.class);

    private static EvictingQueue<String> queue = EvictingQueue.create(50);

    @Autowired
    AudioService audioService;
    @Autowired
    YiDunService yiDunService;
    @Autowired
    AuditPass pass;

    /**
     * 音频入库
     *
     * @param
     * @multiplecategory 获取栏目分类//
     * @audioClipsName 获取音频上传人的id
     * @soure 音频来源
     * @audioId 音频id
     * @audioTitle 音频标题
     * @audioUrl 音频url
     * @audioCoverUrl 封面图
     * @audioIntro 音频简介
     * @audioTag 音频标签
     * @audioKeyword 音频关键字
     * @uploadTime 音频上传时间
     * @priority 优先级(1-热点 0-非热点)
     * @callback 回调函数
     * @spId spId
     * @bucketName 存储空间名
     * @bucketType 金山或七牛
     * @duration 时长
     * @contactInfo 音频上传人联系方式
     * @extendField 扩展字段
     */
    @PostMapping("/import")
    @ResponseBody
    public ResponseBean importAudio(String param) {
        logger.info("音频入库,param=====" + param);
        try {
            ShyAudio shyAudio = JSONObject.parseObject(param, ShyAudio.class);
            ShyAudio audioMsg = audioService.getAudioMsg(shyAudio.getAudioId());
            if (audioMsg == null) {
                synchronized (AudioControllerApi.class) {
                    if (!queue.contains(shyAudio.getAudioId())) {
                        queue.add(shyAudio.getAudioId());
                        shyAudio.setCreateTime(new Date());
                        shyAudio.setState(0);
                        //获取审核类型
                        audioService.getShenheType(shyAudio);
                        shyAudio.setReceive(0);
                        audioService.importAudioMsg(shyAudio);
                    }
                }
            } else {
                shyAudio.setId(audioMsg.getId());
                shyAudio.setUpdateTime(new Date());
                audioService.updateAudioMsg(shyAudio);
                return new ResponseBean(ErrorEnum.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(ErrorEnum.ERROR);
        }
        return new ResponseBean(ErrorEnum.SUCCESS);
    }


    /**
     * 易盾音频检测结果回调
     *
     * @param
     */
    @PostMapping("/callbackAudio")
    @ResponseBody
    public void callbackYiDunAudio(String secretId, String businessId, String signature, String callbackData) {
        logger.info("易盾音频检测结果回调信息secretId:{},businessId:{},signature:{},callbackData:{}", secretId, businessId, signature, callbackData);
        boolean flag = YiDunAntiFraudUtil.verifySignature(secretId, businessId, signature, callbackData);
        logger.info("易盾音频检测校验签名结果result:{}", flag);
        if (flag) {
            try {
                JSONObject jsonObject = JSONObject.parseObject(callbackData);
                JSONArray antispamArray = jsonObject.getJSONArray("antispam");
                if (antispamArray != null && antispamArray.size() > 0) {
                    for (Object obj : antispamArray) {
                        ShyYidun newYiDunMsg = new ShyYidun();
                        ShyAudio newshyAudio = new ShyAudio();
                        try {
                            JSONObject objectAntispam = (JSONObject) obj;
                            //0:通过,1:嫌疑,2:不通过
                            int action = objectAntispam.getIntValue("action");
                            //2:检测中,3:检测完成,4:检测失败
                            int asrStatus = objectAntispam.getIntValue("asrStatus");
                            String taskId = objectAntispam.getString("taskId");
                            ShyYidun yiDunMsg = yiDunService.getYiDunMsg(taskId);
                            if (yiDunMsg == null) {
                                continue;
                            }
                            newYiDunMsg.setId(yiDunMsg.getId());
                            newYiDunMsg.setDuration(-1);
                            newYiDunMsg.setUpdate_time(new Date());
                            ShyAudio shyAudio = audioService.selectByPrimaryKey(Integer.parseInt(yiDunMsg.getContent_id()));
                            newshyAudio.setId(shyAudio.getId());
                            if (action == 0 && asrStatus == 3) {
                                newYiDunMsg.setDesc_msg("易盾点播音频审核通过了");
                                newYiDunMsg.setCheck_status(0);
                                newshyAudio.setMsg("易盾点播音频审核通过了");
                                newshyAudio.setUploader("机审通过");
                                newshyAudio.setState(1);
                                newshyAudio.setShenhe_type(CommonConst.AUDIT_TYPE_MACHANE);
                                newshyAudio.setReceive(CommonConst.RECEIVE_AUDIT);
                                pass.audioPass(shyAudio.getAudioId(), 3, shyAudio.getAttr_tags(), "易盾点播音频审核通过了", shyAudio.getCallback());
                            } else {
                                StringBuilder labelMsg = new StringBuilder();
                                if (action != 0 && asrStatus == 3) {
                                    JSONArray labels = objectAntispam.getJSONArray("labels");
                                    labelMsg = new StringBuilder("(");
                                    if (labels != null && labels.size() > 0) {
                                        for (Object labelObj : labels) {
                                            JSONObject jsonObjectone = (JSONObject) labelObj;
                                            Integer label = jsonObjectone.getInteger("label");
                                            String name = TextLabels.getName(label);
                                            labelMsg.append(name);

                                        }
                                    }
                                }
                                if (action == 1 && asrStatus == 3) {
                                    newYiDunMsg.setDesc_msg("易盾点播音频审核音频嫌疑" + labelMsg + ")");
                                    newshyAudio.setMsg("易盾点播音频审核音频嫌疑" + labelMsg + ")");
                                }
                                if (action == 2 && asrStatus == 3) {
                                    newYiDunMsg.setDesc_msg("易盾点播音频审核音频违规" + labelMsg + ")");
                                    newshyAudio.setMsg("易盾点播音频审核音频违规" + labelMsg + ")");

                                }
                                newshyAudio.setState(0);
                                newshyAudio.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
                                newshyAudio.setReceive(CommonConst.RECEIVE_BEFORE);
                                //审核失败
                                newYiDunMsg.setCheck_status(2);
                            }
                            newYiDunMsg.setResult_type(String.valueOf(action));
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                            //审核类型改为人工审核
                            newshyAudio.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
                            //拒绝原因
                            newshyAudio.setMsg("易盾点播音频审核其它问题需排查");
                            newYiDunMsg.setDesc_msg("易盾点播音频审核其它问题需排查");
                        }
                        yiDunService.updateYiDunMsg(newYiDunMsg);
                        audioService.updateAudioMsg(newshyAudio);
                    }
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

}