search.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const util = require('../../../utils/util.js');
  2. const app = getApp();
  3. Page({
  4. data: {
  5. hotLabel:[],
  6. record:[],
  7. dataList:[],
  8. userInfo:{},
  9. result:false,
  10. search:false,
  11. value:'',
  12. type:''
  13. },
  14. onLoad(options) {
  15. console.log(options)
  16. let record = options.type=='goods'?(wx.getStorageSync('_GOODSCORD_') || []):(wx.getStorageSync('_BBRECORD_') || []);
  17. let userInfo = app.globalData.userInfo;
  18. this.setData({record,userInfo,type:options.type})
  19. this.getHot(1);
  20. },
  21. bindinput(e){
  22. this.setData({value:e.detail.value})
  23. },
  24. // 清除记录
  25. clean(){
  26. this.setData({record:[]})
  27. wx.removeStorage({
  28. key: this.data.type=='goods'?'_GOODSCORD_':'_BBRECORD_'
  29. })
  30. },
  31. // 获取热门标签
  32. getHot(pageIndex){
  33. let that = this;
  34. util.ajax({
  35. func:"article/hot_keywords",
  36. data:{"pageIndex": pageIndex,"pageSize":10,"atype":that.data.type=='goods'?1:0}
  37. },function(res){
  38. if(res.code == 0){
  39. that.setData({hotLabel:res.data});
  40. }
  41. })
  42. },
  43. bindSearch(){
  44. this.search(this.data.value);
  45. },
  46. // 搜索
  47. search(text){
  48. var that = this,type=this.data.type;
  49. if(text == ''){
  50. util.showTips('请先输入您要搜索的关键词');
  51. return false;
  52. }
  53. let record = type=='goods'?(wx.getStorageSync('_GOODSCORD_') || []):(wx.getStorageSync('_BBRECORD_') || []);
  54. if(record.length){
  55. let judge = false;
  56. for(let i in record){
  57. if(record[i] == text){
  58. judge = true
  59. break ;
  60. }
  61. }
  62. if(!judge){
  63. record.splice(0,0,text);
  64. }
  65. }else{
  66. record.push(text);
  67. }
  68. wx.setStorage({
  69. key:type=='goods'?'_GOODSCORD_':"_BBRECORD_",
  70. data:record
  71. })
  72. util.ajax({
  73. func:"article/searching",
  74. data:{keyword:text,pageIndex:1,pageSize:50,atype:type=='goods'?1:0}
  75. },function(res){
  76. if(res.code == 0){
  77. let dataList = res.data.articles;
  78. that.setData({dataList,search:true})
  79. }else{
  80. util.showTips(res.reason);
  81. }
  82. })
  83. },
  84. choose(e){
  85. let aid = e.currentTarget.dataset.aid;
  86. wx.navigateTo({
  87. url: '/pages/product/activity/index?aid='+aid
  88. })
  89. },
  90. // 标签搜索
  91. bindLabel(e){
  92. this.search(e.currentTarget.dataset.text);
  93. this.setData({value:e.currentTarget.dataset.text});
  94. }
  95. })