search.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // _this.data.replaceValue = e.currentTarget.dataset.postname;
  46. wx.setStorageSync(
  47. 'historyStorage',
  48. _this.data.historyStorage.concat(_this.data.inputValue)
  49. )
  50. this.getSearchGoods()
  51. setTimeout(() => {
  52. console.log(this.data.searchTo);
  53. var new_search = JSON.stringify(this.data.searchTo)
  54. let new_arr = encodeURIComponent(new_search)
  55. wx.navigateTo({
  56. url: '../../pages/com_search/com_search?comList=' + new_arr,
  57. })
  58. }, 1000)
  59. },
  60. //删除搜索数据
  61. deleteInput(){
  62. this.setData({
  63. inputValue: ""
  64. })
  65. },
  66. //清空历史记录
  67. remove(){
  68. var _this = this
  69. wx.showModal({
  70. title: '提示',
  71. content: '确认清除所有历史记录?',
  72. success: function(res) {
  73. if(res.confirm) {
  74. wx.removeStorageSync(
  75. 'historyStorage'
  76. ),
  77. _this.setData({
  78. historyStorage: []
  79. })
  80. } else {
  81. console.log("点击取消");
  82. }
  83. }
  84. })
  85. },
  86. //点击搜索历史复制到input框
  87. copyHistory(e) {
  88. var his = e.currentTarget.dataset.history
  89. this.setData({
  90. inputValue: his
  91. })
  92. },
  93. /**
  94. * 生命周期函数--监听页面加载
  95. */
  96. onLoad(options) {
  97. //异步获取数据
  98. var x = wx.getStorageSync(
  99. 'historyStorage',
  100. )
  101. //将之前的数据和现在得到的异步数据拼接渲染
  102. this.setData({
  103. historyStorage: [...this.data.historyStorage ,...x]
  104. })
  105. },
  106. getSearchGoods() {
  107. searchGoods(this.data).then((res) => {
  108. const {code, message, data} = res.data.data;
  109. console.log(data);
  110. this.setData({
  111. searchTo: data
  112. })
  113. }).catch((err) => {
  114. console.log(err);
  115. })
  116. },
  117. /**
  118. * 生命周期函数--监听页面初次渲染完成
  119. */
  120. onReady() {
  121. },
  122. /**
  123. * 生命周期函数--监听页面显示
  124. */
  125. onShow() {
  126. if(this.data.inputValue != "") {
  127. this.getSearchGoods()
  128. }
  129. },
  130. /**
  131. * 生命周期函数--监听页面隐藏
  132. */
  133. onHide() {
  134. },
  135. /**
  136. * 生命周期函数--监听页面卸载
  137. */
  138. onUnload() {
  139. },
  140. /**
  141. * 页面相关事件处理函数--监听用户下拉动作
  142. */
  143. onPullDownRefresh() {
  144. },
  145. /**
  146. * 页面上拉触底事件的处理函数
  147. */
  148. onReachBottom() {
  149. },
  150. /**
  151. * 用户点击右上角分享
  152. */
  153. onShareAppMessage() {
  154. }
  155. })