user.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const db = require('../config/db');
  2. const getNowFormatTime = require('../util/time-processer');
  3. const User = db.import('../schema/user.js');
  4. User.sync();
  5. class UserModel {
  6. /**
  7. * 按手机号码查询
  8. * @param phone
  9. * @returns {Promise.<*>}
  10. */
  11. static async findByPhone(phone){
  12. return User.findOne({
  13. where: {phone:phone}
  14. });
  15. }
  16. /**
  17. * 创建用户
  18. * @param name 昵称
  19. * @param password 密码
  20. * @param phone 手机号码
  21. * @returns {Promise.<*>}
  22. */
  23. static async create(name, password, phone, email ){
  24. let user = await User.findOne({
  25. where: {phone:phone}
  26. });
  27. if(user){
  28. return {code: -2};
  29. }
  30. user = await User.findOne({
  31. where: {name:name}
  32. });
  33. if(user){
  34. return {code: -1};
  35. }
  36. user = await User.create({name: name, password: password, phone: phone, email: email, createdTime: getNowFormatTime()});
  37. return {code: 0, data: user};
  38. }
  39. static async findByUid(id){
  40. return User.findOne({
  41. where: {id:id}
  42. });
  43. }
  44. static async updateName(id, newName){
  45. let user = await User.findOne({
  46. where: {name: newName}
  47. });
  48. if(user){
  49. return {code: -1};
  50. }
  51. await User.update({name: newName}, {where: {id: id}});
  52. return {code: 0};
  53. }
  54. static async updateEmail(id, newEmail){
  55. await User.update({email: newEmail}, {where: {id: id}});
  56. return {code: 0};
  57. }
  58. static async updateSignature(id, newSignature){
  59. await User.update({signature: newSignature}, {where: {id: id}});
  60. return {code: 0};
  61. }
  62. }
  63. module.exports = UserModel;