Session.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import TableHelper from './lib/TableHelper'
  2. class Session extends TableHelper {
  3. constructor () {
  4. super('session')
  5. }
  6. async getSortList () {
  7. let objTable = await this.getObjTable()
  8. if (!objTable) return []
  9. let cursor = await objTable.index('pin_time_int').openCursor(null, 'prev')
  10. let list = []
  11. // let list2 = []
  12. while (cursor && cursor.value) {
  13. let value = cursor.value
  14. // 跳过已经删除的会话
  15. if (!value.delete_time_int || value.delete_time_int < value.update_time_int) {
  16. list.push(value)
  17. }
  18. cursor = await cursor.continue()
  19. }
  20. return list
  21. }
  22. async updateItem (sessionId, newData) {
  23. this.updateObject(newData, {
  24. session_id: sessionId
  25. })
  26. }
  27. async setUnread (sessionId, unreadNum) {
  28. this.updateItem(sessionId, {
  29. unread: unreadNum
  30. })
  31. }
  32. async setPin (sessionId, isPin) {
  33. let pinTime = isPin ? (new Date()).getTime() : 0
  34. this.updateItem(sessionId, {
  35. is_pin: isPin,
  36. pin_time_int: pinTime
  37. })
  38. }
  39. async setMute (sessionId, isMute) {
  40. this.updateItem(sessionId, {
  41. is_mute: isMute
  42. })
  43. }
  44. async clearData () {
  45. let objTable = await this.getObjTable()
  46. if (!objTable) return
  47. let cursor = await objTable.openCursor(null, 'prev')
  48. // let list2 = []
  49. while (cursor && cursor.value) {
  50. let value = cursor.value
  51. // 跳过已经删除的会话
  52. if (!value.delete_time_int) {
  53. objTable.delete(value.session_id)
  54. } else {
  55. // console.log('skip session_id:' + value.session_id)
  56. }
  57. cursor = await cursor.continue()
  58. }
  59. }
  60. async replaceSession (list) {
  61. let objTable = await this.getObjTable()
  62. if (!objTable) return list
  63. // 要合并已经被删除的会话
  64. for (let key in list) {
  65. let sessionId = list[key]['session_id']
  66. if (await objTable.get(sessionId)) {
  67. this.updateObject(list[key], { session_id: sessionId })
  68. } else {
  69. objTable.put(list[key])
  70. }
  71. }
  72. return this.getSortList()
  73. }
  74. async remove (sessionId) {
  75. let objTable = await this.getObjTable()
  76. if (!objTable) return
  77. // objTable.delete(sessionId)
  78. let item = await objTable.get(sessionId)
  79. if (item) {
  80. this.updateItem(sessionId, {
  81. delete_time_int: item.update_time_int
  82. })
  83. }
  84. }
  85. async recover (sessionId) {
  86. this.updateItem(sessionId, {
  87. delete_time_int: 0
  88. })
  89. }
  90. }
  91. export default Session