search.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. function setLastSearchedSpace(item) {
  2. if (!(item && item.spaceId)) {
  3. return Promise.reject()
  4. }
  5. return new Promise((resolve, reject) => {
  6. wx.setStorage({
  7. key: "lastSearchedSpace",
  8. data: {
  9. spaceId: item.spaceId
  10. },
  11. success: () => {
  12. resolve()
  13. },
  14. fail: () => {
  15. reject()
  16. }
  17. });
  18. })
  19. }
  20. function setHistorySearchedSpaceList(historyList, item) {
  21. // 去重
  22. historyList.find((history, index) => {
  23. if (history.spaceId == item.spaceId && history.value == item.value) {
  24. historyList.splice(index, 1);
  25. return true;
  26. }
  27. return false;
  28. });
  29. historyList.unshift(item);
  30. const maxLength = 10;
  31. // 最多记录10条数据
  32. if (historyList.length > maxLength) {
  33. historyList.splice(maxLength, historyList.length - maxLength);
  34. }
  35. wx.setStorage({
  36. key: "historySearchedSpaceList",
  37. data: {
  38. historyList: historyList,
  39. },
  40. });
  41. return historyList;
  42. }
  43. function getHistorySearchedSpaceList() {
  44. return new Promise((resolve, reject) => {
  45. wx.getStorage({
  46. key: "historySearchedSpaceList",
  47. success: (res) => {
  48. if (res && res.data) {
  49. const historyList = res.data.historyList || [];
  50. resolve(historyList);
  51. }
  52. },
  53. fail: () => {
  54. resolve([]);
  55. }
  56. });
  57. })
  58. }
  59. export {
  60. setLastSearchedSpace,
  61. getHistorySearchedSpaceList,
  62. setHistorySearchedSpaceList
  63. }