searchWeb1.js 4.8 KB

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