search.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // parents/pages/search/search.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { SearchIndex, SearchBookList, ThemeSearch, AddWishBooks, GoodIndexSearch, IndexSearchHis, ClearSearchHis} from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, thumbnail, qrcodeInvalidToastText, borrowToastText, reservationsToastText, wishToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. thumbnail,
  13. baseImgUrl,
  14. borrowToastText,
  15. reservationsToastText,
  16. wishToastText,
  17. isLoaded: 1,
  18. pageSize: 10,
  19. pageNo: 1,
  20. isAll: 0,
  21. isShow: 0,
  22. list:[],
  23. itemIndex: 0,
  24. items: ['按周阅读量排序', '按借阅热度排序', '按借阅量TOP10排序'],
  25. content: '',
  26. isReturn: 0,
  27. isType:false,
  28. isRecord:true,
  29. nodataArray: {
  30. text: "搜索无结果"
  31. }
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`)
  38. options.libId = getGlobalVal('library').id || getGlobalVal('list')[0].libId
  39. this.setData({ options })
  40. },
  41. /**
  42. * 生命周期函数--监听页面初次渲染完成
  43. */
  44. onReady: function () {
  45. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  46. for (const i in obj) {
  47. this[i] = obj[i]
  48. }
  49. },
  50. /**
  51. * 生命周期函数--监听页面显示
  52. */
  53. onShow: function () {
  54. this.getIndexSearchHis()
  55. // this.getData(res => {
  56. // this.setData({ isLoaded: 1 })
  57. // })
  58. },
  59. /**
  60. * 生命周期函数--监听页面隐藏
  61. */
  62. onHide: function () {
  63. },
  64. /**
  65. * 生命周期函数--监听页面卸载
  66. */
  67. onUnload: function () {
  68. },
  69. returnBack:function(){
  70. wx.navigateBack({
  71. })
  72. },
  73. /**
  74. * 页面相关事件处理函数--监听用户下拉动作
  75. */
  76. onPullDownRefresh: function () {
  77. const { isAll, options } = this.data
  78. const { type = 1 } = options
  79. // if (type == 1) return
  80. this.setData({ pageNo: 1 })
  81. this.getData(res => {
  82. wx.stopPullDownRefresh()
  83. })
  84. },
  85. /**
  86. * 页面上拉触底事件的处理函数
  87. */
  88. onReachBottom: function () {
  89. const { isAll, options } = this.data
  90. const { type = 1 } = options
  91. if (isAll) return
  92. this.getData()
  93. },
  94. /**
  95. * 用户点击右上角分享
  96. */
  97. onShareAppMessage: function () {
  98. if (isFn(sharePage)) return sharePage()
  99. },
  100. /**
  101. * 获取历史记录
  102. */
  103. getIndexSearchHis: function (cb) {
  104. const continuousFn = { fn: this.getIndexSearchHis, param: { ...arguments } }
  105. IndexSearchHis({ data: {}, continuousFn }).then(res => {
  106. const { searchHis } = res.data
  107. this.setData({ contentList: searchHis })
  108. })
  109. },
  110. /**
  111. * 获取搜索数据
  112. */
  113. getData: function (cb) {
  114. const continuousFn = { fn: this.getData, param: { ...arguments } }
  115. const { content, options, pageNo, pageSize, itemIndex } = this.data
  116. const { libId, type, child_theme } = options
  117. var that=this;
  118. wx.showLoading({
  119. title: '加载中',
  120. })
  121. const Fn = [GoodIndexSearch, SearchBookList, ThemeSearch]
  122. const data = [{ libId, searchTitle:content, pageNo, pageSize }, { libId, content, pageNo, pageSize, type: itemIndex + 1 }, { libId, content, pageNo, pageSize, childTheme: child_theme }]
  123. Fn[type - 1]({ data: data[type - 1], continuousFn}).then(res => {
  124. wx.hideLoading()
  125. if (type == 1) {
  126. const { list } = res.data
  127. const oneTemp = (res => {
  128. for (let i in res) {
  129. if (res[i].summary.length > 30) {
  130. res[i].summary = res[i].summary.slice(0, 30) + '...'
  131. } else {
  132. res[i].summary = res[i].summary
  133. }
  134. }
  135. return res
  136. })(list)
  137. let listTemp = []
  138. if (pageNo == 1) {
  139. listTemp = [].concat(oneTemp)
  140. } else {
  141. listTemp = [].concat(that.data.list, oneTemp)
  142. }
  143. that.setData({ list: listTemp, pageNo: list.length == pageSize ? pageNo + 1 : pageNo, isAll: list.length < pageSize,isType:true })
  144. } else if (type ==2) {
  145. const { list } = res.data
  146. let listTemp = []
  147. if (pageNo == 1) {
  148. listTemp = [].concat(list)
  149. } else {
  150. listTemp = [].concat(that.data.list, list)
  151. }
  152. that.setData({ list: listTemp, pageNo: list.length == pageSize ? pageNo + 1 : pageNo, isAll: list.length < pageSize })
  153. } else if (type == 3) {
  154. const { bookList } = res.data
  155. let listTemp = []
  156. if (pageNo == 1) {
  157. listTemp = [].concat(bookList)
  158. } else {
  159. listTemp = [].concat(that.data.list, bookList)
  160. }
  161. that.setData({ list: listTemp, pageNo: list.length == pageSize ? pageNo + 1 : pageNo, isAll: list.length < pageSize })
  162. }
  163. cb && cb(res)
  164. }).catch(res => {
  165. cb && cb(res)
  166. })
  167. },
  168. /**
  169. * 输入搜索值
  170. */
  171. inputSearchCtl: function (e) {
  172. const { value } = e.detail
  173. const { content } = this.data
  174. if (content == value) {
  175. return
  176. }
  177. // var contentList = wx.getStorageSync('contentList')||[];
  178. // contentList.unshift(content)
  179. // wx.setStorageSync('contentList', contentList)
  180. this.setData({ content: value, pageNo: 1,isRecord:false})
  181. this.getData()
  182. },
  183. /**
  184. * 清除搜索值
  185. */
  186. clearSearchCtl: function () {
  187. this.setData({ content: '' })
  188. },
  189. /**
  190. * 提交搜索值
  191. */
  192. submitSearchCtl: function () {
  193. this.setData({ isReturn: 1, pageNo: 1 })
  194. this.getData()
  195. },
  196. /**
  197. * 删除
  198. */
  199. deleteTips:function(){
  200. const continuousFn = { fn: this.deleteTips, param: { ...arguments } }
  201. ClearSearchHis({ data: {}, continuousFn }).then(res => {
  202. wx.showToast({
  203. title: '删除成功'
  204. })
  205. this.setData({ contentList: [] })
  206. })
  207. },
  208. /**
  209. * 展示选择框
  210. */
  211. showItemCtl: function () {
  212. const { isShow } = this.data
  213. if (isShow) {
  214. return
  215. }
  216. this.setData({ isShow: 1 })
  217. },
  218. /**
  219. * 展示选择框
  220. */
  221. selectItemCtl: function (e) {
  222. const { index } = e.currentTarget.dataset
  223. const { itemIndex, items } = this.data
  224. if (index == itemIndex) {
  225. return
  226. }
  227. this.setData({ itemIndex: index, isShow: 0, pageNo: 1 })
  228. this.getData()
  229. },
  230. /**
  231. * 借阅 预约 加入心愿单
  232. */
  233. submitCtl: function (e) {
  234. const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
  235. if (!hasVipService() || !hasLibraryService()) return
  236. const { type, number } = e.currentTarget.dataset
  237. const { vipId, libId } = this.data.options
  238. AddWishBooks({ data: { isbn13: number, libId, type, vipId }, continuousFn }).then(res => {
  239. wx.showToast({
  240. title: `${type == 1 ? '借阅' : type == 2 ? '加入心愿' : type == 3 ? '预约' : ''}成功`
  241. })
  242. this.getData()
  243. })
  244. }
  245. })