app.status.repo.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * 客户端App状态库。
  3. *
  4. * 客户端App状态对每个用户来讲只保存一个,不区分设备。
  5. * 因此当用户从iOS切换至Android平台时,先前的状态将被新的替换。
  6. *
  7. * author: Sand
  8. * since: 12/14/2016
  9. */
  10. "use strict";
  11. let 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 deviceToken
  35. * @param client_id
  36. * @param platform
  37. * @param handler
  38. */
  39. static save(userId, deviceToken, client_id, platform, handler) {
  40. let sql = "INSERT INTO app_status (user_id, platform, token, client_id, app_in_bg, last_login_time) " +
  41. "VALUES (?, ?, ?, ?, 0, now()) " +
  42. "ON DUPLICATE KEY UPDATE platform=?, token=?, client_id=?, app_in_bg=0,last_login_time=now()";
  43. ImDb.execQuery({
  44. sql: sql,
  45. args: [userId, platform, deviceToken, client_id, platform, deviceToken, client_id],
  46. handler: function(err,res){
  47. handler(err,res);
  48. }
  49. });
  50. };
  51. /**
  52. * 更新App前后台状态。
  53. *
  54. * @param userId
  55. * @param appInBg
  56. * @param handler
  57. */
  58. static updateStatus(userId, appInBg, handler) {
  59. ImDb.execQuery({
  60. "sql": "UPDATE app_status SET app_in_bg = ? WHERE user_id = ",
  61. "args": [appInBg, userId],
  62. "handler": handler
  63. });
  64. };
  65. /**
  66. * 销毁用户状态
  67. *
  68. * @param userId
  69. * @param handler
  70. */
  71. static destroy(userId, handler) {
  72. ImDb.execQuery({
  73. "sql": "DELETE FROM app_status WHERE user_id = ?",
  74. "args": [userId],
  75. "handler": handler
  76. });
  77. };
  78. }
  79. module.exports = AppStatusRepo;