search.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // pages/search/search.js
  2. import httpRequestApi from '../../utils/APIRequest';
  3. export const searchInit = (that) => {
  4. that.setData({
  5. searchData: {
  6. searchList: [],
  7. recommendList: []
  8. }
  9. })
  10. //获取输入值,并请求接口
  11. that.focus = ({detail}) => {
  12. if (detail.value) {
  13. httpRequestApi.getCourse({
  14. title: detail.value
  15. }).success((res)=>{
  16. console.log('搜索结果', res)
  17. that.data.searchData.searchList = res.data.data.list;
  18. that.data.searchData.recommendList = [];
  19. that.setData({
  20. searchData: that.data.searchData
  21. })
  22. })
  23. }else {
  24. that.data.searchData.searchList = [];
  25. that.data.searchData.recommendList = [];
  26. that.setData({
  27. searchData: that.data.searchData
  28. })
  29. }
  30. }
  31. //跳转到详情页
  32. that.details = ({ currentTarget }) => {
  33. const id = currentTarget.dataset.id;
  34. wx.navigateTo({
  35. url: '/pages/details/details?id=' + id
  36. })
  37. }
  38. httpRequestApi.getCategoryRecommend().success((res)=>{
  39. console.log('热门搜', res)
  40. that.data.searchData.recommendList = res.data.data;
  41. that.setData({
  42. searchData: that.data.searchData
  43. })
  44. })
  45. }