Message.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import TableHelper from './lib/TableHelper'
  2. import User from './User'
  3. import { decryptoMsg } from '@/util/util.js'
  4. class Message extends TableHelper {
  5. constructor () {
  6. super('message')
  7. }
  8. async getMsg (sessionId, limit = 100) {
  9. if (!sessionId) return null
  10. let objTable = await this.getObjTable()
  11. var lowerBound = [sessionId, 0]
  12. var upperBound = [sessionId, 999999999999999]
  13. let keyRangeValue = IDBKeyRange.bound(lowerBound, upperBound)
  14. let cursor = await objTable.index('session_id').openCursor(keyRangeValue, 'prev')
  15. let list = []
  16. let userIds = {}
  17. // let list2 = []
  18. while (cursor && cursor.value) {
  19. let value = cursor.value
  20. if (list.length < limit && value['session_id'] === sessionId) {
  21. if (value.msg_type == 4) {
  22. // 针对红包类型
  23. try {
  24. if (typeof value.msg === 'string') {
  25. value.msg = JSON.parse(decryptoMsg(value.msg))
  26. }
  27. } catch (ex) {
  28. console.error(ex)
  29. }
  30. } else {
  31. value.msg = decryptoMsg(value.msg)
  32. }
  33. list.unshift(value)
  34. userIds[value.from] = value.from
  35. if (value.to) {
  36. userIds[value.to] = value.to
  37. }
  38. cursor = await cursor.continue()
  39. } else {
  40. break
  41. }
  42. }
  43. let objUser = new User()
  44. let userMap = await objUser.getUserMap(userIds)
  45. return { list, userMap }
  46. }
  47. async updatePackInfo (hash, type, value) {
  48. let objTable = await this.getObjTable()
  49. let obj = await objTable.get(hash)
  50. obj.ext[type] = value
  51. objTable.put(obj)
  52. }
  53. }
  54. export default Message