searchWeb1.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. Page({
  5. data: {
  6. ListOther: [{
  7. Key: '愛', Type: 'zici', TypeName: '字词'
  8. }, {
  9. Key: '饕餮', Type: 'zici', TypeName: '字词'
  10. }, {
  11. Key: '鸿鹄之志', Type: 'zici', TypeName: '字词'
  12. }, {
  13. Key: '咏鹅', Type: 'shici', Author: '骆宾王', Dynasty: '唐', TypeName: '诗词'
  14. }, {
  15. Key: '天净沙·秋思', Type: 'shici', Author: '马致远', Dynasty: '元', TypeName: '诗词'
  16. }, {
  17. Key: 'success', Type: 'zici', TypeName: '字词'
  18. },{
  19. Key: 'Study hard and make progress every day.', Type: 'eng', TypeName: '翻译'
  20. },
  21. ]
  22. },
  23. onLoad: function () {
  24. wx.hideShareMenu();
  25. var that = this;
  26. that.setData({
  27. Containnerheight: main.getWindowHeight(),
  28. IsStart: true,
  29. });
  30. common.getStorageValue(that, "SearchTextList", [], function () {
  31. });
  32. },
  33. onKeyInput: function (e) {
  34. var search = e.detail.value;
  35. var that = this;
  36. that.setData({
  37. SearchInfo: search,
  38. });
  39. },
  40. onBindfocus:function(){
  41. this.setData({
  42. IsStart:true,
  43. List:[],
  44. });
  45. wx.setNavigationBarTitle({
  46. title: "搜索中"
  47. });
  48. },
  49. onSearch: function (e) {
  50. var that = this;
  51. if (e.currentTarget.dataset.search)
  52. this.data.SearchInfo = e.currentTarget.dataset.search;
  53. if (this.data.SearchInfo && this.data.SearchInfo.length > 0) {
  54. var search = this.data.SearchInfo;
  55. that.searchResult(search);
  56. setTimeout(function(){
  57. var arr = that.data.SearchTextList;
  58. for (var i = 0; i < arr.length; i++) {
  59. if (arr[i] == search) {
  60. arr.splice(i, 1);
  61. break;
  62. }
  63. }
  64. arr.unshift(search);
  65. while (arr.length > 10){
  66. arr.pop();
  67. }
  68. wx.setStorageSync("SearchTextList", arr);
  69. that.setData({
  70. SearchTextList: arr,
  71. });
  72. },2000);
  73. }
  74. else {
  75. wx.showToast({
  76. title: '请输搜索内容',
  77. })
  78. }
  79. },
  80. onSearchHistory: function (e) {
  81. var index = e.currentTarget.dataset.index;
  82. var listtype = e.currentTarget.dataset.type;
  83. var list = this.data.List;
  84. if (listtype=="null")
  85. list=this.data.ListOther;
  86. var obj={};
  87. for(var i=0;i<list.length;i++){
  88. if (index==i){
  89. obj=list[i];
  90. break;
  91. }
  92. }
  93. this.searchResult(obj.Key,obj.Type,obj.Author,obj.Url);
  94. },
  95. close: function (e) {
  96. wx.navigateBack({
  97. delta: 1,
  98. });
  99. },
  100. deleteItem: function (e) {
  101. var that = this;
  102. that.setData({
  103. SearchTextList: [],
  104. });
  105. wx.removeStorageSync("SearchTextList");
  106. },
  107. searchResult: function (search, searchType, author,shiciUrl) {
  108. wx.showLoading({
  109. title: '查询中',
  110. });
  111. var that = this;
  112. var url = 'GetMiaoguoAISearch?UserID=' + app.globalData.userInfo.UserID;
  113. url += "&Word=" + search;
  114. if (searchType)
  115. url += "&SearchType=" + searchType;
  116. if (author)
  117. url += "&Author=" + author;
  118. if (shiciUrl)
  119. url += "&ShiciUrl=" + shiciUrl;
  120. main.getData(url, function (data) {
  121. wx.hideLoading();
  122. wx.setNavigationBarTitle({
  123. title: "搜索结果"
  124. });
  125. searchType = "";
  126. author = "";
  127. if (data) {
  128. //console.log(data);
  129. if (data.List) {
  130. var len = 26;
  131. var list = data.List;
  132. for (var i = 0; i < list.length; i++) {
  133. var item = list[i];
  134. if (item.Content && item.Content.length > len)
  135. item.Content = item.Content.substr(0, len) + "...";
  136. }
  137. that.setData({
  138. List: list,
  139. IsStart: false,
  140. });
  141. }
  142. else {
  143. var obj = {};
  144. obj.Key=search;
  145. obj.Value = data;
  146. if (data.CHN && data.CHN.Author)
  147. obj.Author = data.CHN.Author;
  148. if (data.CHN && data.CHN.Dynasty)
  149. obj.Dynasty = data.CHN.Dynasty;
  150. if (data.CHN && data.CHN.PeomContent) {
  151. obj.Type = "shici";
  152. obj.TypeName = "诗词";
  153. obj.Content = data.CHN.PeomContent.join("").substr(0, 26);
  154. obj.ShiciUrl=shiciUrl;
  155. }
  156. else if (data.CHN){
  157. obj.Type = "zici";
  158. }
  159. if (data.ENG && !data.CHN) {
  160. obj.Type = "eng";
  161. obj.TypeName ="翻译";
  162. }
  163. main.updateSearchList(obj);
  164. wx.navigateTo({
  165. url: './searchWeb2',
  166. });
  167. }
  168. }
  169. else {
  170. that.setData({
  171. List: [],
  172. IsStart: false,
  173. });
  174. }
  175. });
  176. },
  177. onShareAppMessage: function () {
  178. return {
  179. title: app.globalData.ShareTitle,
  180. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  181. imageUrl: app.globalData.ShareImage,
  182. }
  183. },
  184. })