searchWeb1.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: '字词', Remark: 'ài', Content:'对人或事物有很深的感情'
  8. }, {
  9. Key: '饕餮', Type: 'zici', TypeName: '字词', Remark: 'tāo tiè', Content: '传说中的一种凶恶贪食的野兽'
  10. }, {
  11. Key: '鸿鹄之志', Type: 'zici', TypeName: '字词', Remark: 'hóng hú zhī zhì', Content:'比喻远大的志向'
  12. }, {
  13. Key: '咏鹅', Type: 'shici', Author: '骆宾王', Dynasty: '唐', TypeName: '诗词', Content: '鹅鹅鹅,曲项向天歌。白毛浮绿水,红掌拨清波。'
  14. }, {
  15. Key: '天净沙·秋思', Type: 'shici', Author: '马致远', Dynasty: '元', TypeName: '诗词', Content: '枯藤老树昏鸦,小桥流水人家,古道西风瘦马......'
  16. }, {
  17. Key: 'success', Type: 'eng', TypeName: '翻译', Remark:'[səkˈses]', Content: 'n.成功,成就'
  18. },{
  19. Key: 'Nice to meet you', Type: 'eng', TypeName: '翻译', Content: '很高兴见到你!'
  20. },
  21. ]
  22. },
  23. onLoad: function () {
  24. var that = this;
  25. that.setData({
  26. Containnerheight: main.getWindowHeight(),
  27. IsStart: true,
  28. });
  29. common.getStorageValue(that, "SearchTextList", [], function () {
  30. });
  31. },
  32. onKeyInput: function (e) {
  33. var search = e.detail.value;
  34. var that = this;
  35. that.setData({
  36. SearchInfo: search,
  37. });
  38. },
  39. onBindfocus:function(){
  40. this.setData({
  41. IsStart:true,
  42. List:[],
  43. });
  44. wx.setNavigationBarTitle({
  45. title: "搜索中"
  46. });
  47. },
  48. onSearch: function (e) {
  49. var that = this;
  50. if (e.currentTarget.dataset.search)
  51. this.data.SearchInfo = e.currentTarget.dataset.search;
  52. if (this.data.SearchInfo && this.data.SearchInfo.length > 0) {
  53. var search = this.data.SearchInfo;
  54. that.searchResult(search);
  55. setTimeout(function(){
  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. while (arr.length > 10){
  65. arr.pop();
  66. }
  67. wx.setStorageSync("SearchTextList", arr);
  68. that.setData({
  69. SearchTextList: arr,
  70. });
  71. },2000);
  72. }
  73. else {
  74. wx.showToast({
  75. title: '请输搜索内容',
  76. })
  77. }
  78. },
  79. onSearchHistory: function (e) {
  80. var index = e.currentTarget.dataset.index;
  81. var listtype = e.currentTarget.dataset.type;
  82. var list = this.data.List;
  83. if (listtype=="null")
  84. list=this.data.ListOther;
  85. var obj={};
  86. for(var i=0;i<list.length;i++){
  87. if (index==i){
  88. obj=list[i];
  89. break;
  90. }
  91. }
  92. this.searchResult(obj.Key,obj.Type,obj.Author,obj.Url);
  93. },
  94. close: function (e) {
  95. wx.navigateBack({
  96. delta: 1,
  97. });
  98. },
  99. deleteItem: function (e) {
  100. var that = this;
  101. that.setData({
  102. SearchTextList: [],
  103. });
  104. wx.removeStorageSync("SearchTextList");
  105. },
  106. searchResult: function (search,searchType,author,shiciUrl) {
  107. wx.showLoading({
  108. title: '查询中',
  109. });
  110. setTimeout(function () {
  111. wx.hideLoading();
  112. }, 5000);
  113. var that = this;
  114. var url = 'GetMiaoguoAISearch2?UserID=' + app.globalData.userInfo.UserID;
  115. url += "&Word=" + search;
  116. if (searchType)
  117. url += "&SearchType=" + searchType;
  118. if (author)
  119. url += "&Author=" + author;
  120. if (shiciUrl)
  121. url += "&ShiciUrl=" + shiciUrl;
  122. main.getData(url, function (data) {
  123. wx.hideLoading();
  124. wx.setNavigationBarTitle({
  125. title: "搜索结果"
  126. });
  127. searchType = "";
  128. author = "";
  129. if (data) {
  130. //console.log(data);
  131. if (data.List) {
  132. var len = 26;
  133. var list = data.List;
  134. for (var i = 0; i < list.length; i++) {
  135. var item = list[i];
  136. if (item.Content && item.Content.length > len)
  137. item.Content = item.Content.substr(0, len) + "...";
  138. }
  139. that.setData({
  140. List: list,
  141. IsStart: false,
  142. });
  143. }
  144. else {
  145. var obj = {};
  146. obj.Key=search;
  147. obj.Value = data;
  148. if (data.CHN && data.CHN.Author)
  149. obj.Author = data.CHN.Author;
  150. if (data.CHN && data.CHN.Dynasty)
  151. obj.Dynasty = data.CHN.Dynasty;
  152. if (data.CHN && data.CHN.PeomContent) {
  153. obj.Type = "shici";
  154. obj.TypeName = "诗词";
  155. obj.Content = data.CHN.PeomContent.join("").substr(0, 26);
  156. obj.ShiciUrl=shiciUrl;
  157. }
  158. else if (data.CHN){
  159. obj.Type = "zici";
  160. if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].pinyin)
  161. obj.Remark = data.CHN.PinYin[0].pinyin;
  162. if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].explain){
  163. obj.Content = data.CHN.PinYin[0].explain.substr(0, 26);
  164. obj.Content = obj.Content.replace("<p>", "");
  165. obj.Content = obj.Content.replace("</p>", "");
  166. if (obj.Content.length>=26)
  167. obj.Content += "..."
  168. }
  169. }
  170. if (data.ENG && !data.CHN) {
  171. obj.Type = "eng";
  172. obj.TypeName ="翻译";
  173. if (data.ENG.Soundmark && data.ENG.Soundmark.Eng)
  174. obj.Remark = data.ENG.Soundmark.Eng;
  175. if (data.ENG.Paraphrase.length>0){
  176. if (common.checkIsArray(data.ENG.Paraphrase) && data.ENG.Paraphrase[0].ParaphraseList)
  177. obj.Content = data.ENG.Paraphrase[0].ParaphraseList.join("; ").substr(0, 26);
  178. else
  179. obj.Content = data.ENG.Paraphrase.substr(0, 26)+"...";
  180. }
  181. }
  182. main.updateSearchList(obj);
  183. wx.navigateTo({
  184. url: './searchWeb2',
  185. });
  186. }
  187. }
  188. else {
  189. that.setData({
  190. List: [],
  191. IsStart: false,
  192. });
  193. }
  194. });
  195. },
  196. onShareAppMessage: function () {
  197. return {
  198. title: app.globalData.ShareTitle,
  199. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  200. imageUrl: app.globalData.ShareImage,
  201. }
  202. },
  203. })