opendbOpenData.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const dbName = {
  2. openData: "opendb-open-data" // 数据库表名 - 缓存表
  3. }
  4. const db = uniCloud.database();
  5. const _ = db.command;
  6. var dao = {};
  7. /**
  8. * 获取
  9. let cacheInfo = await dao.opendbOpenData.get(key);
  10. */
  11. dao.get = async (key) => {
  12. let res = await db.collection(dbName.openData).doc(key).get();
  13. let cacheInfo = res.data && res.data.length > 0 ? res.data[0] : null;
  14. if (!cacheInfo) {
  15. // 缓存不存在
  16. return null;
  17. }
  18. if (cacheInfo.expired > 0 && Date.now() > cacheInfo.expired) {
  19. // 缓存过期了
  20. return null;
  21. }
  22. // 缓存存在且未过期
  23. let value;
  24. try {
  25. value = JSON.parse(cacheInfo.value);
  26. } catch (err) {
  27. value = cacheInfo.value;
  28. }
  29. return value;
  30. };
  31. /**
  32. * 设置缓存
  33. await dao.opendbOpenData.set(key, value, expired);
  34. */
  35. dao.set = async (key, value, expired = 0) => {
  36. if (expired > 0) {
  37. expired = Date.now() + expired * 1000;
  38. }
  39. let res = await db.collection(dbName.openData).doc(key).set({
  40. value: JSON.stringify(value),
  41. expired
  42. });
  43. return res.id ? res.id : null;
  44. };
  45. /**
  46. * 获取accessToken
  47. let cacheInfo = await dao.opendbOpenData.getAccessToken({
  48. appId: "wx123",
  49. platform: "weixin-mp"
  50. });
  51. */
  52. dao.getAccessToken = async (key = {}) => {
  53. let { appId, platform } = key;
  54. let cacheKey = `uni-id:${platform}:${appId}:access-token`;
  55. return await dao.get(cacheKey);
  56. };
  57. /**
  58. * 获取accessToken
  59. await dao.opendbOpenData.setAccessToken({
  60. appId: "wx123",
  61. platform: "weixin-mp"
  62. }, value, expired);
  63. */
  64. dao.setAccessToken = async (key, value, expired) => {
  65. let { appId, platform } = key;
  66. let cacheKey = `uni-id:${platform}:${appId}:access-token`;
  67. return await dao.set(cacheKey, value, expired);
  68. };
  69. /**
  70. * 获取sessionKey
  71. let cacheInfo = await dao.opendbOpenData.getSessionKey({
  72. appId: "wx123",
  73. platform: "weixin-mp",
  74. openid: "o123"
  75. });
  76. */
  77. dao.getSessionKey = async (key = {}) => {
  78. let { appId, platform, openid } = key;
  79. let cacheKey = `uni-id:${platform}:${appId}:${openid}:session-key`;
  80. return await dao.get(cacheKey);
  81. };
  82. /**
  83. * 设置sessionKey
  84. await dao.opendbOpenData.setSessionKey({
  85. appId: "wx123",
  86. platform: "weixin-mp",
  87. openid: "o123"
  88. }, value, expired);
  89. */
  90. dao.setSessionKey = async (key = {}, value, expired) => {
  91. let { appId, platform, openid } = key;
  92. let cacheKey = `uni-id:${platform}:${appId}:${openid}:session-key`;
  93. return await dao.set(cacheKey, value, expired);
  94. };
  95. module.exports = dao;