web端--斗图Tenor api 接入

项目中有用到斗图表情,需接入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

posted @ 2018-06-30 17:50  riven.lcs  阅读(1387)  评论(0编辑  收藏  举报