search.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // pages/search/search.js
  2. const { searchGoods } = require('../../API/appraise')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. historyStorage: [],//历史搜索
  9. historyStorageShow: false,//清空历史记录
  10. flag: true,//换一批推荐
  11. inputValue: "",
  12. replaceValue: "",
  13. searchResult: [{title: "短裤"}, {title: "短裙"}, {title: "连衣裙"}, {title: "卫衣"}, {title: "外套"}],
  14. search: false,
  15. page: 1,
  16. pageSize: 6,
  17. searchTo: []
  18. },
  19. //输入数据显示搜索推荐
  20. showResult(){
  21. this.setData({
  22. show: true
  23. })
  24. },
  25. getInputValue(e){
  26. const {value, cursor, keyCode} = e.detail;
  27. this.setData({
  28. inputValue: value
  29. })
  30. // 边输入边请求获取数据
  31. // if(this.data.inputValue != "") {
  32. // this.getSearchGoods()
  33. // }
  34. },
  35. putInputValue(e){
  36. this.setData({
  37. inputValue: e.currentTarget.dataset.postname
  38. })
  39. },
  40. //开始搜索
  41. searchBegin(e){
  42. let _this = this;
  43. var data = e.currentTarget.dataset.searchdata;
  44. console.log(data);
  45. wx.setStorageSync(
  46. 'historyStorage',
  47. _this.data.historyStorage.concat(_this.data.inputValue)
  48. )
  49. this.getSearchGoods()
  50. setTimeout(() => {
  51. console.log(this.data.searchTo);
  52. var new_search = JSON.stringify(this.data.searchTo)
  53. let new_arr = encodeURIComponent(new_search)
  54. wx.navigateTo({
  55. url: '../../pages/com_search/com_search?comList=' + new_arr,
  56. })
  57. }, 100)
  58. },
  59. //删除搜索数据
  60. deleteInput(){
  61. this.setData({
  62. inputValue: ""
  63. })
  64. },
  65. //清空历史记录
  66. remove(){
  67. var _this = this
  68. wx.showModal({
  69. title: '提示',
  70. content: '确认清除所有历史记录?',
  71. success: function(res) {
  72. if(res.confirm) {
  73. wx.removeStorageSync(
  74. 'historyStorage'
  75. ),
  76. _this.setData({
  77. historyStorage: []
  78. })
  79. } else {
  80. console.log("点击取消");
  81. }
  82. }
  83. })
  84. },
  85. //点击搜索历史复制到input框
  86. copyHistory(e) {
  87. var his = e.currentTarget.dataset.history
  88. this.setData({
  89. inputValue: his
  90. })
  91. },
  92. /**
  93. * 生命周期函数--监听页面加载
  94. */
  95. onLoad(options) {
  96. //异步获取数据
  97. var x = wx.getStorageSync(
  98. 'historyStorage',
  99. )
  100. //将之前的数据和现在得到的异步数据拼接渲染
  101. this.setData({
  102. historyStorage: [...this.data.historyStorage ,...x]
  103. })
  104. },
  105. getSearchGoods() {
  106. searchGoods(this.data).then((res) => {
  107. const {code, message, data} = res.data.data;
  108. console.log(data);
  109. this.setData({
  110. searchTo: data
  111. })
  112. }).catch((err) => {
  113. console.log(err);
  114. })
  115. },
  116. /**
  117. * 生命周期函数--监听页面初次渲染完成
  118. */
  119. onReady() {
  120. },
  121. /**
  122. * 生命周期函数--监听页面显示
  123. */
  124. onShow() {
  125. if(this.data.inputValue != "") {
  126. this.getSearchGoods()
  127. }
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide() {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload() {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh() {
  143. },
  144. /**
  145. * 页面上拉触底事件的处理函数
  146. */
  147. onReachBottom() {
  148. },
  149. /**
  150. * 用户点击右上角分享
  151. */
  152. onShareAppMessage() {
  153. }
  154. })