58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
'use strict'
|
|
|
|
const { validePhone, checkCode } = require("../util/utils");
|
|
const { registerService, loginService } = require('../service/user/index')
|
|
|
|
const login = async function (fastify, request, reply) {
|
|
const body = request.body
|
|
const { tel, pwd } = body
|
|
console.log(tel, pwd)
|
|
// if (!validePhone(tel)) {
|
|
// console.log('tel is valid', tel)
|
|
// return { 'errno': 600, 'message': '手机号不正确'}
|
|
// }
|
|
let new_pwd = pwd.trim()
|
|
// if (new_pwd.length === 0) {
|
|
// console.log('pwd.length === 0')
|
|
// return { 'errno': 601, 'message': '密码不能为空或者都是空格'}
|
|
// }
|
|
const res = await loginService(fastify, tel, new_pwd)
|
|
console.log(res)
|
|
if (res.errno !== 0) {
|
|
return { errno: 602, 'message': res.message }
|
|
}
|
|
return { errno: 0, 'message': '成功', token: res.token}
|
|
}
|
|
|
|
const register = async function(fastify, request, reply) {
|
|
const body = request.body
|
|
const { tel, code, pwd } = body
|
|
console.log(tel, code, pwd)
|
|
const { ec, err } = await checkCode(fastify, tel, code)
|
|
console.log(ec, err)
|
|
if (ec !== 0) {
|
|
return { 'errno': 600, 'message': err }
|
|
}
|
|
let new_pwd = pwd.trim()
|
|
if (new_pwd.length === 0) {
|
|
console.log('pwd.length === 0')
|
|
return { 'errno': 601, 'message': '密码不能为空或者都是空格'}
|
|
}
|
|
if (!validePhone(tel)) {
|
|
console.log('tel is valid', tel)
|
|
return { 'errno': 602, 'message': '手机号不正确'}
|
|
}
|
|
|
|
const res = await registerService(fastify, tel, new_pwd)
|
|
console.log(res)
|
|
if (res.err !== 0) {
|
|
return { 'errno': 603, 'message': res.message }
|
|
}
|
|
return { 'errno': 0, 'message': '成功'}
|
|
}
|
|
|
|
module.exports = {
|
|
login,
|
|
register
|
|
}
|