storage.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // 定义key名
  2. const USER_INFO_KEY = 'wx_user_info'
  3. const TOKEN_KEY = 'wx_token'
  4. const SEARCH_HISTORY_KEY = 'search_history'
  5. const Collect_HISTORY_KEY = 'collect_goods_key'
  6. function saveStorage (key, value, expired = 0) {
  7. const storage = {
  8. value
  9. }
  10. // 设置过期时间
  11. if (expired) {
  12. storage.expired = new Date().getTime() + expired * 1000
  13. }
  14. try {
  15. return wx.setStorageSync(key, storage)
  16. } catch (e) {
  17. console.log(e, `save ${key} storage err`)
  18. }
  19. }
  20. function loadStorage (key) {
  21. try {
  22. const storage = wx.getStorageSync(key)
  23. // 过期
  24. if (storage.expired && storage.expired <= new Date().getTime) {
  25. storage.value = ''
  26. removeStorage(key)
  27. }
  28. return storage.value
  29. } catch (e) {
  30. console.log(e, `load ${key} storage err`)
  31. }
  32. }
  33. function removeStorage (key) {
  34. try {
  35. return wx.removeStorageSync(key)
  36. } catch (e) {
  37. console.log(e, `remove ${key} storage err`)
  38. }
  39. }
  40. export function saveUserInfo (userinfo) {
  41. return saveStorage(USER_INFO_KEY, userinfo, 86400 / 24)
  42. }
  43. export function loadUserInfo () {
  44. return loadStorage(USER_INFO_KEY)
  45. }
  46. export function removeUserInfo () {
  47. return removeStorage(USER_INFO_KEY)
  48. }
  49. export function saveToken (token) {
  50. return saveStorage(TOKEN_KEY, token, 86400 / 24)
  51. }
  52. export function loadToken () {
  53. return loadStorage(TOKEN_KEY)
  54. }
  55. export function removeToken () {
  56. return removeStorage(TOKEN_KEY)
  57. }
  58. export function saveSearchHistory (value) {
  59. let historys = loadSearchHistory() || []
  60. historys = historys.filter(item => item !== value)
  61. historys.unshift(value)
  62. return saveStorage(SEARCH_HISTORY_KEY, historys)
  63. }
  64. export function loadSearchHistory () {
  65. return loadStorage(SEARCH_HISTORY_KEY)
  66. }
  67. export function removeSearchHistory () {
  68. return removeStorage(SEARCH_HISTORY_KEY)
  69. }
  70. export function saveCollectHistory (goods) {
  71. let historys = loadCollectHistory() || []
  72. historys = historys.filter(item => item.id !== goods.id)
  73. historys.unshift(goods)
  74. return saveStorage(Collect_HISTORY_KEY, historys)
  75. }
  76. export function loadCollectHistory (goods_id) {
  77. let arr = loadStorage(Collect_HISTORY_KEY) ? loadStorage(Collect_HISTORY_KEY) : []
  78. let result = arr
  79. if (goods_id) {
  80. result = arr.find(item => item.id == goods_id)
  81. }
  82. return result
  83. }
  84. export function removeCollect (goods_id) {
  85. let historys = loadCollectHistory() || []
  86. historys = historys.filter(item => item.id !== goods_id)
  87. return saveStorage(Collect_HISTORY_KEY, historys)
  88. }