| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- const db = require('../config/db');
- const getNowFormatTime = require('../util/time-processer');
- const User = db.import('../schema/user.js');
- const seq = require('sequelize');
- const Op = seq.Op;
- User.sync();
- class UserModel {
- /**
- * 按手机号码查询
- * @param phone
- * @returns {Promise.<*>}
- */
- static async findByPhone(phone){
- return User.findOne({
- where: {phone:phone}
- });
- }
- /**
- * 创建用户
- * @param name 昵称
- * @param password 密码
- * @param phone 手机号码
- * @returns {Promise.<*>}
- */
- static async create(name, password, phone, email ){
- let user = await User.findOne({
- where: {phone:phone}
- });
- if(user){
- return {code: -2};
- }
- user = await User.findOne({
- where: {name:name}
- });
- if(user){
- return {code: -1};
- }
- user = await User.create({name: name, password: password, phone: phone, email: email, createdTime: getNowFormatTime()});
- return {code: 0, data: user};
- }
- static async findByUid(id){
- return User.findOne({
- where: {id:id}
- });
- }
- static async updateName(id, newName){
- let user = await User.findOne({
- where: {name: newName}
- });
- if(user){
- return {code: -1};
- }
- await User.update({name: newName}, {where: {id: id}});
- return {code: 0};
- }
- static async updateEmail(id, newEmail){
- await User.update({email: newEmail}, {where: {id: id}});
- return {code: 0};
- }
- static async updateSignature(id, newSignature){
- await User.update({signature: newSignature}, {where: {id: id}});
- return {code: 0};
- }
- static async identifyTeacher(uid){
- await User.update({role: 'TEACHER'}, {where: {id: uid}});
- return true;
- }
- static async resetPassword(phone, newPassword){
- let result = await User.update({password: newPassword}, {where: {phone: phone}});
- return result[0]> 0;
- }
- static async getUsers(){
- return User.findAll({
- where: {role: {[Op.ne]: 'ADMIN'}}
- });
- }
- static async updateIsValid(id, isValid){
- await User.update({isValid: isValid}, {where: {id: id}});
- return true;
- }
- }
- module.exports = UserModel;
|