項目中有用到斗圖表情,需接入Tenor斗圖庫,
此僅做記錄,便於今后回憶,
如能給你程序之旅帶來些許幫助,
不勝榮幸,
大神請繞道,
廢話不多說,
1、獲取秘鑰 或使用提供的秘鑰
1 var apikey = 'LIVDSRZULELA';
2、通過給定秘鑰ajax請求返回指定anon_id
1 // 獲取tenor anonid 2 3 getAnonId: function () { 4 5 var url = 'https://api.tenor.com/v1/anonid?key=' + 'LIVDSRZULELA'; 6 7 this.$ajax({ 8 9 method: 'GET', 10 11 url: url 12 13 }).then(res => { 14 15 this.anonid = res.data.anon_id; 16 17 }); 18 19 },
3、通過給定的anon_id,請求返回gif數據
p(searchTerm )—搜索關鍵字
lmt – 獲取的gif數組數量
1 // ajax加載gif方法 2 3 getTenorGif: function (searchText, num) { 4 5 var searchResultList = []; 6 7 var apikey = 'LIVDSRZULELA'; 8 9 var lmt = num; 10 11 var searchTerm = searchText; 12 13 var searchUrl = 'https://api.tenor.com/v1/search?tag=' + searchTerm + '&key=' + apikey + '&limit=' + lmt + '&anon_id=' + this.anonid; 14 15 this.$ajax({ 16 17 method: 'GET', 18 19 url: searchUrl 20 21 }).then(response => { 22 23 // 處理獲取的數據 24 25 for (var i = 0; i < response.data.results.length; i++) { 26 27 for (var j = 0; j < response.data.results[i].media.length; j++) { 28 29 var json = {nanogif: response.data.results[i].media[j].nanogif.url, tinygif: response.data.results[i].media[j].tinygif.url}; 30 31 searchResultList.push(json); 32 33 }; 34 35 }; 36 37 this.searchResult = searchResultList; 38 39 }); 40 41 }
Tenor api地址: https://tenor.com/gifapi/documentation#quickstart