storage.js 825 B

12345678910111213141516171819202122232425262728293031
  1. const {
  2. isUndefined,
  3. isString,
  4. isObject,
  5. } = require('./base.js');
  6. const updataStorageData = (key, value) => {
  7. try {
  8. if (!isString(key) || key === '') {
  9. return console.error('[$updateStorageData] key 不能为空!'); // eslint-disable-line
  10. }
  11. let data = wx.getStorageSync(key);
  12. // 只有key情况下,直接返回data
  13. if (isUndefined(value)) return data;
  14. // Object合并
  15. if (isObject(value) && isObject(data)) {
  16. let info = Object.assign({}, data, value);
  17. wx.setStorageSync(key, info);
  18. return info;
  19. }
  20. // 其他数据直接覆盖
  21. wx.setStorageSync(key, value);
  22. return value;
  23. } catch (e) {
  24. console.error(`[ERROR]: ${value ? 'UPDATE' : 'GET'} Storage ${key} : `, e.stack); // eslint-disable-line
  25. }
  26. };
  27. module.exports = {
  28. updataStorageData
  29. };