百度地址识别接口: https://ai.baidu.com/ai-doc/NLP/vk6z52h5n
先获取access_token:
百度获取access_token接口文档: https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu#%E4%BD%BF%E7%94%A8access-key-idsecret-access-key%E7%9A%84%E5%BC%80%E5%8F%91%E8%80%85%E6%B3%A8%E6%84%8F
请求URL数据格式
向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:
grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key;
通过API Key和Secret Key获取的access_token:
创建应用来获取应用的API Key,应用的Secret Key
(https://console.bce.baidu.com/ai/#/ai/nlp/overview/index)
<div class="editExpress-area">
<van-field v-model="message" :border="false" rows="2" autosize label=""
placeholder="智**,18*****4107,杭州市西湖区西溪银泰3号楼****" type="textarea"></van-field>
<div class="editExpress-opreate">
<span class="editExpress-clear">
<span @click="handleClear">清空</span>
</span>
<span class="editExpress-analysis" :class="message ? 'active' : ''" @click="handleAnalysis">识别解析</span>
</div>
</div>
import axios from 'axios'
export default {
name: 'editExpress',
data () {
return {
message: '', // 智*,18*****4107,杭州市西湖区西溪银泰3号楼****
loading: false
}
},
methods: {
handleClear () {
this.message = ''
},
handleAnalysis () {
if (!this.message) {
this.$toast('请填写地址')
return false
}
const baidu_server = '/baiduApi/oauth/2.0/token?'
const grant_type= 'client_credentials'
const client_id= 'wFQ9ZzkvTrkr2tWcSelnUpR8' // 应用的API Key
const client_secret= '8ByGQ7FG2dABTuRpBbFp4pFW7ZqY3H8*****' // 应用的Secret Key
const url = baidu_server + 'grant_type=' + grantType + '&client_id=' + client_id+ '&client_secret=' + client_secret
this.loading = true
axios.get(url).then(res => { // 获取access_token
if (res.status === 200) {
// 地址识别接口
axios.post('/baiduApi/rpc/2.0/nlp/v1/address?access_token=' + res.data.access_token, {
text: this.message,
confidence: 100
}).then(res => {
if (res.status === 200) {
/* 百度返回的结果
{
city: "杭州市"
city_code: "330100"
county: "西湖区"
county_code: "330106"
detail: "西溪****"
lat: 30.298562
lng: 120.080382
log_id: 1366594722142879700
person: "智*"
phonenum: "18*****4107"
province: "浙江省"
province_code: "330000"
text: "智*,18******107,杭州市西湖区*********"
town: "蒋村街道"
town_code: "330106012"
}
*/
}
}).then(() => {
this.loading = false
}).catch(() => {
this.loading = false
})
}
}).catch(() => {
this.loading = false
})
},
}
}