|
| 1 | +const userModel = require("../model/user.js"); |
| 2 | +const config = require("../config/index.js"); |
| 3 | +const util = require("../util/index"); |
| 4 | +module.exports = { |
| 5 | + register: async (ctx, next) => { |
| 6 | + let { name, password, phone } = ctx.request.body; |
| 7 | + if (name && password) { |
| 8 | + password = util.createHash(password); |
| 9 | + const result = await new userModel({ |
| 10 | + name: name, |
| 11 | + password: password, |
| 12 | + phone: phone |
| 13 | + }).save(); |
| 14 | + console.log("register result is", result); |
| 15 | + if (!result) |
| 16 | + return (ctx.body = { |
| 17 | + code: "400", |
| 18 | + message: "register fail" |
| 19 | + }); |
| 20 | + else |
| 21 | + return (ctx.body = { |
| 22 | + code: "200", |
| 23 | + message: "register success!" |
| 24 | + }); |
| 25 | + } |
| 26 | + }, |
| 27 | + login: async (ctx, next) => { |
| 28 | + const data = ctx.request.body; |
| 29 | + if (!data.name || !data.password) { |
| 30 | + return (ctx.body = { |
| 31 | + code: "", |
| 32 | + data: null, |
| 33 | + message: "the usernumber or password can't be null" |
| 34 | + }); |
| 35 | + } |
| 36 | + data.password = util.createHash(data.password); |
| 37 | + const result = await userModel.find({ |
| 38 | + name: data.name, |
| 39 | + password: data.password |
| 40 | + }); |
| 41 | + if (result && result.length) { |
| 42 | + const token = util.sign(result); |
| 43 | + return (ctx.body = { |
| 44 | + code: "200", |
| 45 | + token: token, |
| 46 | + message: "login success" |
| 47 | + }); |
| 48 | + } else { |
| 49 | + return (ctx.body = { |
| 50 | + code: "400", |
| 51 | + data: null, |
| 52 | + message: "usernumber or password is error" |
| 53 | + }); |
| 54 | + } |
| 55 | + }, |
| 56 | + getuserinfo: async (ctx, next) => { |
| 57 | + const { name } = ctx.request.query; |
| 58 | + if (name) { |
| 59 | + const result = await userModel.findOne({ |
| 60 | + name: name |
| 61 | + }); |
| 62 | + console.log(result); |
| 63 | + result |
| 64 | + ? (ctx.body = { code: 200, data: result, message: "ok" }) |
| 65 | + : (ctx.body = { code: 200, data: null, message: "ok" }); |
| 66 | + } else { |
| 67 | + ctx.body = { |
| 68 | + code: 400, |
| 69 | + message: "name is null" |
| 70 | + }; |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
0 commit comments