app.status.repo.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * 客户端App状态库。
  3. *
  4. * 客户端App状态对每个用户来讲只保存一个,不区分设备。
  5. * 因此当用户从iOS切换至Android平台时,先前的状态将被新的替换。
  6. *
  7. * author: Sand
  8. * since: 12/14/2016
  9. */
  10. "use strict";
  11. var imDb = require("./db/im.db.js");
  12. class AppStatusRepo {
  13. constructor() {
  14. }
  15. /**
  16. * 查找客户端App状态。
  17. *
  18. * @param userId
  19. * @param handler
  20. */
  21. static findOne(userId, handler) {
  22. let sql = "SELECT platform, token, client_id, app_in_bg, last_login_time " +
  23. "FROM app_status WHERE user_id = ?";
  24. imDb.execQuery({
  25. "sql": sql,
  26. "args": [userId],
  27. "handler": handler
  28. });
  29. };
  30. /**
  31. * 保存App的最新状态。
  32. *
  33. * @param userId
  34. * @param token
  35. * @param client_id
  36. * @param platform
  37. * @param handler
  38. */
  39. static save(userId, token, client_id, platform, handler) {
  40. imDb.execQuery({
  41. "sql": "INSERT INTO app_status (user_id, platform, token, client_id, app_in_bg, last_login_time) VALUES (?,?,?,?,1,1) ON" +
  42. " DUPLICATE KEY UPDATE token=?,client_id=?,platform=?,is_online=1,status=1",
  43. "args": [userId, token, client_id, platform, token, client_id, platform],
  44. "handler": handler
  45. });
  46. };
  47. /**
  48. * 更新App前后台状态。
  49. *
  50. * @param userId
  51. * @param appInBg
  52. * @param handler
  53. */
  54. static updateStatus(userId, appInBg, handler) {
  55. imDb.execQuery({
  56. "sql": "UPDATE user SET app_in_bg = ? WHERE user_id = ",
  57. "args": [appInBg, userId],
  58. "handler": handler
  59. });
  60. };
  61. static delete(token, handler) {
  62. imDb.execQuery({
  63. "sql": "DELETE FROM user WHERE token=?",
  64. "args": [token],
  65. "handler": handler
  66. });
  67. };
  68. }
  69. module.exports = AppStatusRepo;