db.js 923 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import * as common from './common.js'
  2. //取值
  3. function get(key, sync = true) {
  4. try {
  5. if (sync) {
  6. return uni.getStorageSync(key);
  7. } else {
  8. let data = '';
  9. uni.getStorage({
  10. key: key,
  11. success: function(res) {
  12. data = res.data;
  13. }
  14. });
  15. return data;
  16. }
  17. } catch (e) {
  18. return false;
  19. }
  20. }
  21. //赋值
  22. function set(key, value, sync = true) {
  23. try {
  24. if (sync) {
  25. return uni.setStorageSync(key, value);
  26. } else {
  27. uni.setStorage({
  28. key: key,
  29. data: value
  30. });
  31. }
  32. } catch (e) {
  33. }
  34. }
  35. //移除
  36. function del(key, sync = true) {
  37. try {
  38. if (sync) {
  39. return uni.removeStorageSync(key);
  40. } else {
  41. uni.removeStorage({
  42. key: key
  43. });
  44. }
  45. } catch (e) {
  46. return false;
  47. }
  48. }
  49. //清空
  50. function clear(sync = true) {
  51. try {
  52. if (sync) {
  53. return uni.clearStorageSync();
  54. } else {
  55. uni.clearStorage();
  56. }
  57. } catch (e) {
  58. return false;
  59. }
  60. }
  61. export {
  62. get,
  63. set,
  64. del,
  65. clear
  66. }