index.ux 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <import name="book-block" src="../../components/book/book.ux"></import>
  2. <import name="x-loading" src="../../components/loading/loading.ux"></import>
  3. <template>
  4. <div class="serch-wrap">
  5. <div class="search-top">
  6. <input
  7. type="text"
  8. id="search"
  9. class="serach-input"
  10. value="{{inputValue}}"
  11. @change="changeValue"
  12. enterkeytype="search"
  13. placeholder="请输入书名"
  14. @enterkeyclick="searchBook"
  15. style="placeholder-color:#D9D9D9"
  16. />
  17. </div>
  18. <div class="search-content" show="{{showHistory}}">
  19. <div class="top-wrap">
  20. <text>热门搜索</text>
  21. <div @click="nextHot">
  22. <text class="changed">换一换</text>
  23. <image
  24. src="../../assets/imgs/change_other.png"
  25. @click="changed"
  26. ></image>
  27. </div>
  28. </div>
  29. <div class="history-list" if="{{hotlist.length}}">
  30. <text
  31. class="history-item"
  32. for="{{hotlist}}"
  33. @click="serachItem($item.keyword)"
  34. >
  35. {{ $item.keyword }}
  36. </text>
  37. </div>
  38. </div>
  39. <div class="search-content" show="{{showHistory}}">
  40. <div class="top-wrap">
  41. <text>搜索历史</text>
  42. <image
  43. src="../../assets/imgs/delete.png"
  44. @click="deleteHistory"
  45. ></image>
  46. </div>
  47. <div class="history-list" if="{{historyList.length}}">
  48. <text
  49. class="history-item"
  50. for="{{historyList}}"
  51. @click="serachItem($item)"
  52. >
  53. {{ $item }}
  54. </text>
  55. </div>
  56. </div>
  57. <div class="search-content" if="{{!list.length&&!showHistory}}">
  58. <text class="isempty">{{ loadingText }}</text>
  59. </div>
  60. <list
  61. class="list-content"
  62. @scrollbottom="onReachBottom"
  63. if="{{list.length&&!showHistory}}"
  64. id="list"
  65. >
  66. <list-item type="reslut">
  67. <text class="reslut-title">为您搜索出以下内容</text>
  68. </list-item>
  69. <block for="list">
  70. <list-item
  71. type="books-item"
  72. class="books-item__wrap {{$idx === 0 ? 'book-item__wrap--first' : ''}}"
  73. >
  74. <book-block multi="{{true}}" width="{{150}}" book="{{$item}}">
  75. <div class="book-info">
  76. <text class="name">{{ $item.book_name }}</text>
  77. <text class="intro">{{ $item.book_summary }}</text>
  78. </div>
  79. </book-block>
  80. </list-item>
  81. <<list-item onappear="getBooksList" ></list-item>
  82. </block>
  83. </list>
  84. </div>
  85. </template>
  86. <script>
  87. import { booksList, hotWords, getUserInfo } from "../../api";
  88. import { pageLoad } from "../../helper";
  89. import storage from '@system.storage';
  90. import prompt from '@system.prompt';
  91. export default {
  92. private: {
  93. inputValue: null,
  94. showHistory: true,
  95. meta: {},
  96. hotlist: [],
  97. empty: false,
  98. list: [],
  99. historyList: [],
  100. loadingText: "努力搜索ing...",
  101. uid: '',
  102. filter: {
  103. key: "",
  104. page_size: 5,
  105. page: 1,
  106. status: 1,
  107. },
  108. },
  109. computed: {
  110. },
  111. nextHot() {
  112. if (this.meta.last_page == 1) {
  113. return;
  114. }
  115. if (this.meta.current_page == this.meta.last_page) {
  116. this.getData();
  117. } else {
  118. this.getData(this.meta.current_page + 1);
  119. }
  120. },
  121. async onInit() {
  122. let user = await getUserInfo();
  123. this.uid = user.id
  124. this.getData();
  125. let list = (await storage.get({ key: "history" })).data;
  126. let sets = JSON.parse(list)
  127. if (sets) this.historyList = sets;
  128. },
  129. async getData(page = 1) {
  130. let data = await hotWords(page);
  131. this.hotlist = data.list;
  132. this.meta = data.meta;
  133. },
  134. searchBook(iptValue) {
  135. if (!iptValue.value) {
  136. prompt.showToast({ message: "请输入书名" });
  137. return false;
  138. }
  139. this.filter.key = iptValue.value;
  140. this.getBooksList(false);
  141. this.$element('search').focus({ focus: false });
  142. this.showHistory = false;
  143. },
  144. serachItem(key) {
  145. this.filter.key = key;
  146. this.inputValue = key;
  147. this.getBooksList(false);
  148. this.showHistory = false;
  149. },
  150. deleteHistory() {
  151. let _self = this;
  152. prompt.showDialog({
  153. title: '重要提醒',
  154. message: '确认要删除历史记录吗~',
  155. buttons: [
  156. {
  157. text: '删除记录',
  158. color: '#EF5952'
  159. },
  160. {
  161. text: '我再想想',
  162. color: '#999'
  163. }
  164. ],
  165. success: function (data) {
  166. if (data.index == 0) {
  167. storage.delete({
  168. key: 'history',
  169. success: function (data) {
  170. _self.historyList = [];
  171. prompt.showToast({ message: "删除成功" });
  172. },
  173. })
  174. }
  175. },
  176. cancel: function () {
  177. console.log('取消前往')
  178. },
  179. })
  180. },
  181. changeValue(text) {
  182. if (!text.value) {
  183. this.showHistory = true;
  184. }
  185. },
  186. async getBooksList(isLoad = true) {
  187. this.empty = false;
  188. this.loadingText = "加载ing...";
  189. this.filter.page = (this.meta.current_page || 0) + 1;
  190. if (!isLoad) {
  191. this.filter.page = 1;
  192. this.meta = {};
  193. }
  194. let params = this.filter;
  195. params.uid = this.uid;
  196. console.log(params);
  197. pageLoad(this.meta, booksList, params).then(ret => {
  198. let newList = isLoad ? this.list : [];
  199. let dataList = ret.list;
  200. newList.push(...dataList);
  201. this.meta = ret.meta;
  202. this.list = newList;
  203. if (!this.list.length) this.loadingText = "没有更多内容了", this.empty = true;
  204. if (!isLoad) {
  205. this.$element('list') && this.$element('list').scrollTo({ index: 0 });
  206. if (this.filter.key && !this.historyList.includes(this.filter.key)) {
  207. this.historyList.push(this.filter.key);
  208. if (this.historyList.length > 10) {
  209. this.historyList.shift()
  210. }
  211. storage.set({
  212. key: 'history',
  213. value: JSON.stringify(this.historyList),
  214. success: function (data) {
  215. console.log('handling success')
  216. },
  217. })
  218. }
  219. }
  220. }).catch(e => {
  221. console.log("e", e);
  222. })
  223. },
  224. onReachBottom() {
  225. // 请求更多数据
  226. this.getBooksList();
  227. },
  228. }
  229. </script>
  230. <style lang="less">
  231. @import '../../assets/less/serach.less';
  232. </style>