searchWeb1.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if (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);
  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) {
  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. main.getData(url, function (data) {
  119. wx.hideLoading();
  120. wx.setNavigationBarTitle({
  121. title: "搜索结果"
  122. });
  123. searchType = "";
  124. author = "";
  125. if (data) {
  126. //console.log(data);
  127. if (data.List) {
  128. var len = 26;
  129. var list = data.List;
  130. for (var i = 0; i < list.length; i++) {
  131. var item = list[i];
  132. if (item.Content && item.Content.length > len)
  133. item.Content = item.Content.substr(0, len) + "...";
  134. }
  135. that.setData({
  136. List: list,
  137. IsStart: false,
  138. });
  139. }
  140. else {
  141. var obj = {};
  142. obj.Key=search;
  143. obj.Value = data;
  144. if (data.CHN && data.CHN.Author)
  145. obj.Author = data.CHN.Author;
  146. if (data.CHN && data.CHN.Dynasty)
  147. obj.Dynasty = data.CHN.Dynasty;
  148. if (data.CHN && data.CHN.PeomContent) {
  149. obj.Type = "shici";
  150. obj.TypeName = "诗词";
  151. obj.Content = data.CHN.PeomContent.join("").substr(0, 26);
  152. }
  153. else if (data.CHN){
  154. obj.Type = "zici";
  155. }
  156. if (data.ENG && !data.CHN) {
  157. obj.Type = "eng";
  158. obj.TypeName ="翻译";
  159. }
  160. main.updateSearchList(obj);
  161. wx.navigateTo({
  162. url: './searchWeb2',
  163. });
  164. }
  165. }
  166. else {
  167. that.setData({
  168. List: [],
  169. IsStart: false,
  170. });
  171. }
  172. });
  173. },
  174. onShareAppMessage: function () {
  175. return {
  176. title: app.globalData.ShareTitle,
  177. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  178. imageUrl: app.globalData.ShareImage,
  179. }
  180. },
  181. })