search.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. },
  18. //输入数据显示搜索推荐
  19. showResult(){
  20. this.setData({
  21. show: true
  22. })
  23. },
  24. getInputValue(e){
  25. const {value, cursor, keyCode} = e.detail;
  26. this.setData({
  27. inputValue: value
  28. })
  29. if(this.data.inputValue != "") {
  30. this.getSearchGoods()
  31. }
  32. },
  33. putInputValue(e){
  34. this.setData({
  35. inputValue: e.currentTarget.dataset.postname
  36. })
  37. },
  38. //开始搜索
  39. searchBegin(e){
  40. let _this = this;
  41. var data = e.currentTarget.dataset.searchdata;
  42. console.log(data);
  43. // _this.data.replaceValue = e.currentTarget.dataset.postname;
  44. wx.setStorageSync(
  45. 'historyStorage',
  46. _this.data.historyStorage.concat(_this.data.inputValue)
  47. )
  48. },
  49. //删除搜索数据
  50. deleteInput(){
  51. this.setData({
  52. inputValue: ""
  53. })
  54. },
  55. //清空历史记录
  56. remove(){
  57. var _this = this
  58. wx.showModal({
  59. title: '提示',
  60. content: '确认清除所有历史记录?',
  61. success: function(res) {
  62. if(res.confirm) {
  63. wx.removeStorageSync(
  64. 'historyStorage'
  65. ),
  66. _this.setData({
  67. historyStorage: []
  68. })
  69. } else {
  70. console.log("点击取消");
  71. }
  72. }
  73. })
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad(options) {
  79. //异步获取数据
  80. var x = wx.getStorageSync(
  81. 'historyStorage',
  82. )
  83. //将之前的数据和现在得到的异步数据拼接渲染
  84. this.setData({
  85. historyStorage: [...this.data.historyStorage ,...x]
  86. })
  87. },
  88. getSearchGoods() {
  89. searchGoods(this.data).then((res) => {
  90. console.log(res);
  91. }).catch((err) => {
  92. console.log(err);
  93. })
  94. },
  95. /**
  96. * 生命周期函数--监听页面初次渲染完成
  97. */
  98. onReady() {
  99. },
  100. /**
  101. * 生命周期函数--监听页面显示
  102. */
  103. onShow() {
  104. if(this.data.inputValue != "") {
  105. this.getSearchGoods()
  106. }
  107. },
  108. /**
  109. * 生命周期函数--监听页面隐藏
  110. */
  111. onHide() {
  112. },
  113. /**
  114. * 生命周期函数--监听页面卸载
  115. */
  116. onUnload() {
  117. },
  118. /**
  119. * 页面相关事件处理函数--监听用户下拉动作
  120. */
  121. onPullDownRefresh() {
  122. },
  123. /**
  124. * 页面上拉触底事件的处理函数
  125. */
  126. onReachBottom() {
  127. },
  128. /**
  129. * 用户点击右上角分享
  130. */
  131. onShareAppMessage() {
  132. }
  133. })