mock平台

user.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const baseModel = require('./base.js');
  2. class userModel extends baseModel {
  3. getName() {
  4. return 'user';
  5. }
  6. getSchema() {
  7. return {
  8. username: {
  9. type: String,
  10. required: true
  11. },
  12. password: {
  13. type: String,
  14. required: true
  15. },
  16. email: {
  17. type: String,
  18. required: true
  19. },
  20. passsalt: String,
  21. study: { type: Boolean, default: false },
  22. role: String,
  23. add_time: Number,
  24. up_time: Number,
  25. type: { type: String, enum: ['site', 'third'], default: 'site' } //site用户是网站注册用户, third是第三方登录过来的用户
  26. };
  27. }
  28. save(data) {
  29. let user = new this.model(data);
  30. return user.save();
  31. }
  32. checkRepeat(email) {
  33. return this.model.countDocuments({
  34. email: email
  35. });
  36. }
  37. list() {
  38. return this.model
  39. .find()
  40. .select('_id username email role type add_time up_time study')
  41. .exec(); //显示id name email role
  42. }
  43. findByUids(uids) {
  44. return this.model
  45. .find({
  46. _id: { $in: uids }
  47. })
  48. .select('_id username email role type add_time up_time study')
  49. .exec();
  50. }
  51. listWithPaging(page, limit) {
  52. page = parseInt(page);
  53. limit = parseInt(limit);
  54. return this.model
  55. .find()
  56. .sort({ _id: -1 })
  57. .skip((page - 1) * limit)
  58. .limit(limit)
  59. .select('_id username email role type add_time up_time study')
  60. .exec();
  61. }
  62. listCount() {
  63. return this.model.countDocuments();
  64. }
  65. findByEmail(email) {
  66. return this.model.findOne({ email: email });
  67. }
  68. findById(id) {
  69. return this.model.findOne({
  70. _id: id
  71. });
  72. }
  73. del(id) {
  74. return this.model.remove({
  75. _id: id
  76. });
  77. }
  78. update(id, data) {
  79. return this.model.update(
  80. {
  81. _id: id
  82. },
  83. data
  84. );
  85. }
  86. search(keyword) {
  87. return this.model
  88. .find(
  89. {
  90. $or: [{ email: new RegExp(keyword, 'i') }, { username: new RegExp(keyword, 'i') }]
  91. },
  92. {
  93. passsalt: 0,
  94. password: 0
  95. }
  96. )
  97. .limit(10);
  98. }
  99. }
  100. module.exports = userModel;