1. import { Application } from 'egg';
    2. import * as uuidv4 from 'uuid/v4';
    3. /** 用户资料表,修改邮箱和手机号时,需要同步到userAuth表 */
    4. export default function(app: Application) {
    5. const { STRING, BOOLEAN, DATE, UUID, ARRAY } = app.Sequelize;
    6. const tableName = 'user';
    7. const User = app.model.define(
    8. tableName,
    9. {
    10. id: {
    11. type: UUID,
    12. unique: true,
    13. primaryKey: true,
    14. allowNull: false,
    15. defaultValue: () => {
    16. return uuidv4().replace(/-/g, '');
    17. },
    18. },
    19. username: {
    20. type: STRING(32),
    21. unique: true,
    22. allowNull: false,
    23. comment: '用户名',
    24. },
    25. password: {
    26. type: STRING(255),
    27. allowNull: false,
    28. comment: '密码',
    29. },
    30. email: {
    31. type: STRING(64),
    32. unique: true,
    33. allowNull: true,
    34. comment: '邮箱地址',
    35. },
    36. phone: {
    37. type: STRING(20),
    38. unique: true,
    39. allowNull: true,
    40. comment: '手机号码',
    41. },
    42. avatar: {
    43. type: STRING(150),
    44. allowNull: true,
    45. comment: '头像',
    46. },
    47. alias: {
    48. type: STRING(30),
    49. comment: '别名',
    50. },
    51. realName: {
    52. type: STRING(30),
    53. allowNull: true,
    54. comment: '真实姓名',
    55. },
    56. signature: {
    57. type: STRING(255),
    58. allowNull: true,
    59. comment: '签名',
    60. },
    61. status: {
    62. type: BOOLEAN,
    63. allowNull: false,
    64. defaultValue: 1,
    65. comment: '用户状态: 1 正常; 0 禁用',
    66. },
    67. lastActivedAt: DATE,
    68. createdAt: DATE,
    69. updatedAt: DATE,
    70. },
    71. {
    72. tableName,
    73. comment: '用户表',
    74. timestamps: true,
    75. underscored: false,
    76. }
    77. );
    78. class UserModal extends User {
    79. id: string;
    80. username: string;
    81. email: string;
    82. phone: string;
    83. avatar: string;
    84. alias: string;
    85. realName: string;
    86. signature: string;
    87. status: boolean;
    88. roles: any[];
    89. lastActivedAt: number;
    90. static associate() {
    91. app.model.User.hasMany(app.model.UserAuth, { as: 'userAuths', sourceKey: 'id', foreignKey: 'userId' });
    92. app.model.User.belongsToMany(app.model.Role, { as: 'roles', through: app.model.UserRoleRelation, foreignKey: 'userId' });
    93. }
    94. }
    95. return UserModal;
    96. }