index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (C) 2018-2019
  3. * All rights reserved, Designed By www.joolun.com
  4. * 注意:
  5. * 本软件为www.joolun.com开发研制,项目使用请保留此说明
  6. */
  7. const app = getApp()
  8. Page({
  9. data: {
  10. searchHistory: [],
  11. goodsList: []
  12. },
  13. onShow() {
  14. this.setData({
  15. searchHistory: wx.getStorageSync('searchHistory') ? wx.getStorageSync('searchHistory') : []
  16. })
  17. },
  18. onLoad(options) {
  19. app.initPage()
  20. .then(res => {
  21. this.goodsPage()
  22. })
  23. },
  24. searchHandle(e) {
  25. let value
  26. if (e.detail.value) {
  27. value = e.detail.value
  28. } else if (e.currentTarget.dataset.name){
  29. value = e.currentTarget.dataset.name
  30. }
  31. let searchHistory = this.data.searchHistory
  32. searchHistory.forEach(function (item, index) {
  33. let i = 9 //最多缓存10条
  34. if (item.name == value){
  35. searchHistory.splice(index, 1)
  36. i++
  37. }
  38. if (index >= i){
  39. searchHistory.splice(index, 1)
  40. }
  41. })
  42. searchHistory.unshift({
  43. name: value
  44. })
  45. wx.setStorageSync('searchHistory', searchHistory)
  46. wx.navigateTo({
  47. url: '/pages/goods/goods-list/index?name=' + value
  48. })
  49. },
  50. clearSearchHistory(){
  51. let that = this
  52. wx.showModal({
  53. content: '确认删除全部历史记录?',
  54. cancelText: '我再想想',
  55. confirmColor: '#ff0000',
  56. success(res) {
  57. if (res.confirm) {
  58. that.setData({
  59. searchHistory: []
  60. })
  61. wx.setStorageSync('searchHistory', [])
  62. }
  63. }
  64. })
  65. },
  66. goodsPage() {
  67. app.api.goodsPage({
  68. searchCount: false,
  69. current: 1,
  70. size: 10,
  71. ascs: '',//升序字段
  72. descs: 'sale_num'
  73. })
  74. .then(res => {
  75. let goodsList = res.data.records
  76. this.setData({
  77. goodsList: goodsList
  78. })
  79. })
  80. },
  81. })