|
利用聚合數(shù)據(jù)API快速寫出小程序,過程簡(jiǎn)單。 1、申請(qǐng)小程序賬號(hào) 2、進(jìn)入開發(fā) 3、調(diào)用API。比如“蘇州實(shí)時(shí)公交”小程序,選擇的是蘇州實(shí)時(shí)公交API。 蘇州實(shí)時(shí)公交API文檔:https://www./docs/api/id/31
如下,是“蘇州實(shí)時(shí)公交”小程序調(diào)用代碼: var url = "https://apis./szbusline/bus"; //為了您的密鑰安全,建議使用服務(wù)端代碼中轉(zhuǎn)請(qǐng)求,事例代碼可參考 https://code./ var apiKey = "yourKey"; //輸入自己的key Page({ data: { inputValue: '', restation: [], condition: true }, //獲取輸入框的值 bindInput: function(e) { var that = this; that.setData({ inputValue: e.detail.value }); }, //點(diǎn)擊搜索按鈕調(diào)用的函數(shù) search:function(e){ var that = this; //數(shù)據(jù)加載完成之前,顯示加載中提示框 wx.showToast({ title: '加載中。。。', icon: 'loading', duration: 10000 }); //輸入框沒有輸入的判斷 if(that.data.inputValue == ''){ wx.hideToast(); return; } //發(fā)起請(qǐng)求,注意 wx.request發(fā)起的是 HTTPS 請(qǐng)求 wx.request({ url: url + "?station=" + that.data.inputValue + "&key=" + apiKey, data: {}, header: { 'content-type': 'application/json' }, success: function(res) { var data = res.data; //將數(shù)據(jù)從邏輯層發(fā)送到視圖層,同時(shí)改變對(duì)應(yīng)的 this.data 的值 that.setData({ restation: data.result, condition: false }); //數(shù)據(jù)加載成功后隱藏加載中彈框 wx.hideToast(); } }) } })
|
|
|