user.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const db = require('../config/db');
  2. const getNowFormatTime = require('../util/time-processer');
  3. const User = db.import('../schema/user.js');
  4. const seq = require('sequelize');
  5. const Op = seq.Op;
  6. User.sync();
  7. class UserModel {
  8. /**
  9. * 按手机号码查询
  10. * @param phone
  11. * @returns {Promise.<*>}
  12. */
  13. static async findByPhone(phone){
  14. return User.findOne({
  15. where: {phone:phone}
  16. });
  17. }
  18. /**
  19. * 创建用户
  20. * @param name 昵称
  21. * @param password 密码
  22. * @param phone 手机号码
  23. * @returns {Promise.<*>}
  24. */
  25. static async create(name, password, phone, email ){
  26. let user = await User.findOne({
  27. where: {phone:phone}
  28. });
  29. if(user){
  30. return {code: -2};
  31. }
  32. user = await User.findOne({
  33. where: {name:name}
  34. });
  35. if(user){
  36. return {code: -1};
  37. }
  38. user = await User.create({name: name, password: password, phone: phone, email: email, createdTime: getNowFormatTime()});
  39. return {code: 0, data: user};
  40. }
  41. static async findByUid(id){
  42. return User.findOne({
  43. where: {id:id}
  44. });
  45. }
  46. static async updateName(id, newName){
  47. let user = await User.findOne({
  48. where: {name: newName}
  49. });
  50. if(user){
  51. return {code: -1};
  52. }
  53. await User.update({name: newName}, {where: {id: id}});
  54. return {code: 0};
  55. }
  56. static async updateEmail(id, newEmail){
  57. await User.update({email: newEmail}, {where: {id: id}});
  58. return {code: 0};
  59. }
  60. static async updateSignature(id, newSignature){
  61. await User.update({signature: newSignature}, {where: {id: id}});
  62. return {code: 0};
  63. }
  64. static async identifyTeacher(uid){
  65. await User.update({role: 'TEACHER'}, {where: {id: uid}});
  66. return true;
  67. }
  68. static async resetPassword(phone, newPassword){
  69. let result = await User.update({password: newPassword}, {where: {phone: phone}});
  70. return true;
  71. }
  72. static async getUsers(){
  73. return User.findAll({
  74. where: {role: {[Op.ne]: 'ADMIN'}}
  75. });
  76. }
  77. static async updateIsValid(id, isValid){
  78. await User.update({isValid: isValid}, {where: {id: id}});
  79. return true;
  80. }
  81. }
  82. module.exports = UserModel;