searchWeb1.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 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. setTimeout(function () {
  112. wx.hideLoading();
  113. }, 5000);
  114. var that = this;
  115. var url = 'GetMiaoguoAISearch?UserID=' + app.globalData.userInfo.UserID;
  116. url += "&Word=" + search;
  117. if (searchType)
  118. url += "&SearchType=" + searchType;
  119. if (author)
  120. url += "&Author=" + author;
  121. if (shiciUrl)
  122. url += "&ShiciUrl=" + shiciUrl;
  123. main.getData(url, function (data) {
  124. wx.hideLoading();
  125. wx.setNavigationBarTitle({
  126. title: "搜索结果"
  127. });
  128. searchType = "";
  129. author = "";
  130. if (data) {
  131. //console.log(data);
  132. if (data.List) {
  133. var len = 26;
  134. var list = data.List;
  135. for (var i = 0; i < list.length; i++) {
  136. var item = list[i];
  137. if (item.Content && item.Content.length > len)
  138. item.Content = item.Content.substr(0, len) + "...";
  139. }
  140. that.setData({
  141. List: list,
  142. IsStart: false,
  143. });
  144. }
  145. else {
  146. var obj = {};
  147. obj.Key=search;
  148. obj.Value = data;
  149. if (data.CHN && data.CHN.Author)
  150. obj.Author = data.CHN.Author;
  151. if (data.CHN && data.CHN.Dynasty)
  152. obj.Dynasty = data.CHN.Dynasty;
  153. if (data.CHN && data.CHN.PeomContent) {
  154. obj.Type = "shici";
  155. obj.TypeName = "诗词";
  156. obj.Content = data.CHN.PeomContent.join("").substr(0, 26);
  157. obj.ShiciUrl=shiciUrl;
  158. }
  159. else if (data.CHN){
  160. obj.Type = "zici";
  161. if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].pinyin)
  162. obj.Remark = data.CHN.PinYin[0].pinyin;
  163. if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].explain){
  164. obj.Content = data.CHN.PinYin[0].explain.substr(0, 26);
  165. obj.Content = obj.Content.replace("<p>", "");
  166. obj.Content = obj.Content.replace("</p>", "");
  167. if (obj.Content.length>=26)
  168. obj.Content += "..."
  169. }
  170. }
  171. if (data.ENG && !data.CHN) {
  172. obj.Type = "eng";
  173. obj.TypeName ="翻译";
  174. if (data.ENG.Soundmark && data.ENG.Soundmark.Eng)
  175. obj.Remark = data.ENG.Soundmark.Eng;
  176. if (data.ENG.Paraphrase.length>0){
  177. if (common.checkIsArray(data.ENG.Paraphrase) && data.ENG.Paraphrase[0].ParaphraseList)
  178. obj.Content = data.ENG.Paraphrase[0].ParaphraseList.join("; ").substr(0, 26);
  179. else
  180. obj.Content = data.ENG.Paraphrase.substr(0, 26)+"...";
  181. }
  182. }
  183. main.updateSearchList(obj);
  184. wx.navigateTo({
  185. url: './searchWeb2',
  186. });
  187. }
  188. }
  189. else {
  190. that.setData({
  191. List: [],
  192. IsStart: false,
  193. });
  194. }
  195. });
  196. },
  197. onShareAppMessage: function () {
  198. return {
  199. title: app.globalData.ShareTitle,
  200. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  201. imageUrl: app.globalData.ShareImage,
  202. }
  203. },
  204. })