如何利用微信开发者工具开发微信小程序对接百度AI文本纠错API

重在说明如何对接百度AI的API,所以这个简陋的架构请一眼带过。

<!-- index.wxml -->
<view>
<input placeholder="请输入" bindinput="bindKeyInput">{{text}}</input>
<view width='100rpx'><text></text>{{correct}}</view>
<button bindtap="push" type="primary">开始检测</button>
</view>

client_id: ‘你的API Key’,
client_secret: ‘你的Secret Key’——这两个参数需要我们在百度AI开放平台官网建立一个语音处理技术的应用来获取。

// index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '百度是一家人工只能公司',
    correct: '改正后的文本'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  bindKeyInput: function(e) {
    this.setData({
      text: e.detail.value
    })
  },
  push: function() {
    var that = this;
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token', //获取access_token
      data: {
        grant_type: 'client_credentials',
        client_id: '你的API Key',
        client_secret: '你的Secret Key'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data.access_token)获取到你的access_token,,输出到控制台,复制到下方对应位置
        that.check()
      }
    })
  },
  check: function(){
    var that = this
    wx.request({
      url: 'https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet?charset=UTF-8&access_token=你获取到的access_token', //文本纠错
      data: {
        text: that.data.text
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data.item.correct_query)
        that.setData({
          correct: res.data.item.correct_query  //将更正后的文本赋值给correct
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值