| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- const db = require('../config/db');
- const getNowFormatTime = require('../util/time-processer');
- const User = db.import('../schema/user.js');
- 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};
- }
- }
- module.exports = UserModel;
|