TP5.0+PHP +百度敏感词

wxml

 <form catchsubmit="formSubmit" catchreset="formReset">
     <input class="weui-input" name="input" placeholder="这是一个输入框" />
 <button style="margin: 30rpx 0" type="primary" bindtap="Clik" formType="submit">评论</button>
    </form>
  

js

  formSubmit(e) {
        // console.log('form发生了submit事件,携带数据为:', e.detail.value)
        let that=this
        this.setData({
            comment: e.detail.value.input
        })

      },
       Clik:tool.debounce(function(evt) {
        console.log(evt)
           let that=this
           let comment=this.data.comment
           let token=wx.getStorageSync('token');
           wx.request({
               url: 'http://www.zc.com/baidu', //仅为示例,并非真实的接口地址
               method:"post",
               data:{comment},
               header:{token},
               success (res) {
                 console.log(res)
                if(res.data.code==1){
                    wx.reLaunch({
                      url: '/pages/details/details',
                    })
                }else{
                    wx.reLaunch({
                      url: '/pages/details/details',
                    })
                }
               }
             })
       }),

comment


function curlPost($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}

//百度过滤敏感词 获取token
function getBaiduToken(){
    $url = 'https://aip.baidubce.com/oauth/2.0/token';
    $post_data=[
        'grant_type'=>'client_credentials',
        'client_id'=>'evtcQOPlk165cOHSPmIiDLCn',
        'client_secret'=>'vmnLZAPEGGEGNcGebOkLC5RLuToOakxM'
    ];
    $o = "";
    foreach ($post_data as $k => $v) {
        $o .= "$k=" . urlencode($v) . "&";
    }
    $post_data = substr($o, 0, -1);

    $res =  curlPost($url, $post_data);
    //进行把返回结果转成数组
    $res = json_decode($res, true);
    if (isset($res['error'])) {
        exit('API Key或者Secret Key不正确');
    }
    $accessToken = $res['access_token'];
    return $accessToken;
}
//百度过滤敏感词 返回过滤结果
function testText($data){
    $bodys = array(
        'text' => $data['comment']
    );
    $token=getBaiduToken();
    $url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=".$token;
    $res = curlPost($url,$bodys);
    $res = json_decode($res, true);
    return  $res;
}

控制器调用

    public function BaiDu(Request $request){
        $param=$request->post('comment');
        $arr=['comment'=>$param];
        $res=testText($arr);
        if(in_array('不合规',$res)){
            return json(['code'=>0,'data'=>$res,'msg'=>'审核失败']);
        }else{
            $data=Comment::create($arr);
            if(empty($data)){
                return json(['code'=>0,'data'=>$res,'msg'=>'评论失败']);
            }else{
                return json(['code'=>1,'data'=>$res,'msg'=>'评论成功']);
            }

        }
    }

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.海上月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值