user.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const Schema = require('mongoose').Schema;
  3. const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
  4. // 用户表
  5. const user = {
  6. openid: { type: String, required: false, zh: '微信id' }, //
  7. user_id: { type: String, required: false, zh: '用户id', ref: 'Base.User' }, //
  8. type: { type: String, required: false, default: '0', zh: '用户类别' }, // 0:普通用户;-1:超级管理员;1比赛管理员;2裁判
  9. parent_id: { type: String, required: false, zh: '所属id' }, // 裁判需要填写管理员的数据id
  10. };
  11. const schema = new Schema(user, { toJSON: { getters: true, virtuals: true } });
  12. schema.index({ id: 1 });
  13. schema.index({ 'meta.createdAt': 1 });
  14. schema.index({ openid: 1 });
  15. schema.index({ user_id: 1 });
  16. schema.index({ type: 1 });
  17. schema.index({ parent_id: 1 });
  18. schema.plugin(metaPlugin);
  19. const source = 'race';
  20. module.exports = app => {
  21. const is_multiple = app.mongooseDB.clients;
  22. let model;
  23. if (is_multiple) {
  24. const conn = app.mongooseDB.get(source);
  25. model = conn.model('User', schema, 'user');
  26. } else {
  27. const { mongoose } = app;
  28. model = mongoose.model('User', schema, 'user');
  29. }
  30. return model;
  31. };