searchWeb1.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. var arr = that.data.SearchTextList;
  57. for (var i = 0; i < arr.length; i++) {
  58. if (arr[i] == search) {
  59. arr.splice(i, 1);
  60. break;
  61. }
  62. }
  63. arr.unshift(search);
  64. wx.setStorageSync("SearchTextList", arr);
  65. that.setData({
  66. SearchTextList: arr,
  67. });
  68. }
  69. else {
  70. wx.showToast({
  71. title: '请输搜索内容',
  72. })
  73. }
  74. },
  75. onSearchHistory: function (e) {
  76. var index = e.currentTarget.dataset.index;
  77. var listtype = e.currentTarget.dataset.type;
  78. var list = this.data.List;
  79. if (listtype=="null")
  80. list=this.data.ListOther;
  81. var obj={};
  82. for(var i=0;i<list.length;i++){
  83. if (index==i){
  84. obj=list[i];
  85. break;
  86. }
  87. }
  88. this.searchResult(obj.Key,obj.Type,obj.Author);
  89. },
  90. close: function (e) {
  91. wx.navigateBack({
  92. delta: 1,
  93. });
  94. },
  95. deleteItem: function (e) {
  96. var that = this;
  97. var index = e.currentTarget.dataset.id;
  98. that.data.SearchTextList.splice(index, 1);
  99. that.setData({
  100. SearchTextList: that.data.SearchTextList,
  101. });
  102. wx.setStorageSync("SearchTextList", that.data.SearchTextList);
  103. },
  104. searchResult: function (search, searchType, author) {
  105. wx.showLoading({
  106. title: '查询中',
  107. });
  108. var that = this;
  109. var url = 'GetMiaoguoAISearch?UserID=' + app.globalData.userInfo.UserID;
  110. url += "&Word=" + search;
  111. if (searchType)
  112. url += "&SearchType=" + searchType;
  113. if (author)
  114. url += "&Author=" + author;
  115. main.getData(url, function (data) {
  116. wx.hideLoading();
  117. wx.setNavigationBarTitle({
  118. title: "搜索结果"
  119. });
  120. searchType = "";
  121. author = "";
  122. if (data) {
  123. //console.log(data);
  124. if (data.List) {
  125. var len = 26;
  126. var list = data.List;
  127. for (var i = 0; i < list.length; i++) {
  128. var item = list[i];
  129. if (item.Content && item.Content.length > len)
  130. item.Content = item.Content.substr(0, len) + "...";
  131. }
  132. that.setData({
  133. List: list,
  134. IsStart: false,
  135. });
  136. }
  137. else {
  138. var obj = {};
  139. obj.Key=search;
  140. obj.Value = data;
  141. if (data.CHN && data.CHN.Author)
  142. obj.Author = data.CHN.Author;
  143. if (data.CHN && data.CHN.Dynasty)
  144. obj.Dynasty = data.CHN.Dynasty;
  145. if (data.CHN && data.CHN.PeomContent) {
  146. obj.Type = "shici";
  147. obj.TypeName = "诗词";
  148. obj.Content = data.CHN.PeomContent.join("").substr(0, 26);
  149. }
  150. else if (data.CHN){
  151. obj.Type = "zici";
  152. }
  153. if (data.ENG && !data.CHN) {
  154. obj.Type = "eng";
  155. obj.TypeName="外语";
  156. }
  157. main.updateSearchList(obj);
  158. wx.navigateTo({
  159. url: './searchWeb2',
  160. });
  161. }
  162. }
  163. else {
  164. that.setData({
  165. List: [],
  166. IsStart: false,
  167. });
  168. }
  169. });
  170. },
  171. onShareAppMessage: function () {
  172. return {
  173. title: app.globalData.ShareTitle,
  174. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  175. imageUrl: app.globalData.ShareImage,
  176. }
  177. },
  178. })