|
|
@@ -7,6 +7,7 @@ const aliClient = require('../util/aliClient');
|
|
|
const getTokenUser = require('../util/tokenProcessor');
|
|
|
|
|
|
|
|
|
+
|
|
|
class UserController {
|
|
|
/**
|
|
|
* 登录
|
|
|
@@ -62,38 +63,122 @@ class UserController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 注册
|
|
|
- * @param ctx
|
|
|
- * @returns {Promise.<void>}
|
|
|
- */
|
|
|
- static async register(ctx) {
|
|
|
- const myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
|
|
|
- const myReg2=/^\w+$/;
|
|
|
- const data = ctx.request.body;
|
|
|
|
|
|
- if(!ctx.session.verification_phone || !ctx.session.verification_code){
|
|
|
- ctx.body = Response.failed('请重新获取验证码');
|
|
|
- }else if(ctx.session.verification_code !== data.identifyCode || ctx.session.verification_phone !== data.phone){
|
|
|
- ctx.body = Response.failed('验证码错误');
|
|
|
- }else if(!myReg.test(data.email)){
|
|
|
- ctx.body = Response.failed('邮箱格式有误');
|
|
|
- }else if(data.name.length<1 || data.name.length>10 || data.password.length<6 || data.password.length>20 || data.phone.length !== 11 || !myReg2.test(data.name)){
|
|
|
- ctx.body = Response.failed('参数非法');
|
|
|
- }else{
|
|
|
- const hashPassword = bcrypt.hashSync(data.password, 10);
|
|
|
- const result = await userModel.create(data.name, hashPassword, data.phone, data.email);
|
|
|
- ctx.session.verification_phone = undefined;
|
|
|
- ctx.session.verification_code = undefined;
|
|
|
- if (result.code === 0) {
|
|
|
- ctx.body = Response.success({name: result.data.name});
|
|
|
- } else if(result.code === -2){
|
|
|
- ctx.body = Response.failed('此手机号已注册');
|
|
|
- } else if(result.code === -1){
|
|
|
- ctx.body = Response.failed('此用户名已使用');
|
|
|
- }
|
|
|
- }
|
|
|
+ static canManageUser(ctx){
|
|
|
+ let curUser = getTokenUser(ctx);
|
|
|
+ if (curUser === null){// 没有身份,不可以
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 管理员或者老师可以管理
|
|
|
+ return curUser.role === "ADMIN" || curUser.role === "TEACHER";
|
|
|
+
|
|
|
}
|
|
|
+ /*
|
|
|
+ 批量注册用户
|
|
|
+ 接收一个数组,返回一个map
|
|
|
+ */
|
|
|
+ static async batchRegister(ctx){
|
|
|
+ if (!UserController.canManageUser(ctx)){
|
|
|
+ return "Method Not Allowed."
|
|
|
+ }
|
|
|
+
|
|
|
+ const usersToRegister = ctx.request.body;
|
|
|
+
|
|
|
+ for (const user of usersToRegister) {
|
|
|
+ const {name, password, phone, email} = user
|
|
|
+ try {
|
|
|
+ const result = await UserController.createUser(name, password, phone, email);
|
|
|
+ user.result = "创建成功"
|
|
|
+ }catch (e){
|
|
|
+ user.result = e.message
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(usersToRegister)
|
|
|
+ ctx.body = Response.success(usersToRegister);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册用户
|
|
|
+ * @param ctx
|
|
|
+ * @returns {Promise.<void>}
|
|
|
+ */
|
|
|
+ static async register(ctx) {
|
|
|
+ const { name, password, phone, email, identifyCode } = ctx.request.body;
|
|
|
+ let curUser = await getTokenUser(ctx);
|
|
|
+ try {
|
|
|
+ if (curUser === null || curUser.role === "STUDENT"){ // 普通用户注册流程
|
|
|
+ UserController.validateIdentifyCode(ctx,identifyCode,phone) //需要验证手机验证码
|
|
|
+ ctx.session.verification_phone = undefined;
|
|
|
+ ctx.session.verification_code = undefined;
|
|
|
+ }
|
|
|
+ const result = await UserController.createUser(name, password, phone, email)
|
|
|
+ ctx.body = Response.success({name:result.data.name})
|
|
|
+ }catch (e) {
|
|
|
+ ctx.body = Response.failed(e.message)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建用户
|
|
|
+ static async createUser(name, password, phone, email){
|
|
|
+ UserController.validateRegistrationData(name, password, phone, email);
|
|
|
+ //注册逻辑
|
|
|
+ const hashPassword = bcrypt.hashSync(password, 10);
|
|
|
+ const result = await userModel.create(name, hashPassword, phone, email);
|
|
|
+
|
|
|
+ if (result.code === 0) {
|
|
|
+ return result
|
|
|
+ } else if (result.code === -2) {
|
|
|
+ throw new Error('此手机号已注册');
|
|
|
+ } else if (result.code === -1) {
|
|
|
+ throw new Error('此用户名已使用');
|
|
|
+ }else {
|
|
|
+ throw new Error("未知错误")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static validateIdentifyCode(ctx,idetifyCode,phone){
|
|
|
+ if (!ctx.session.verification_phone || !ctx.session.verification_code) {
|
|
|
+ throw new Error('请重新获取验证码')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ctx.session.verification_code !== identifyCode || ctx.session.verification_phone !== phone) {
|
|
|
+ throw new Error('验证码错误');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证注册数据是否合法
|
|
|
+ static validateRegistrationData(name, password, phone, email) {
|
|
|
+ const emailRegex = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
|
|
|
+ const usernameRegex = /^\w+$/; //由字母、数字、下划线组成且不包含其他字符的字符串
|
|
|
+
|
|
|
+ if (!emailRegex.test(email)) {
|
|
|
+ throw new Error('邮箱格式有误');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (name.length < 1 || name.length > 10) {
|
|
|
+ throw new Error('用户名长度应在1到10之间');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (password.length < 6 || password.length > 20) {
|
|
|
+ throw new Error('密码长度应在6到20之间');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (phone.length !== 11) {
|
|
|
+ throw new Error('手机号码长度应为11位');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!usernameRegex.test(name)) {
|
|
|
+ throw new Error('用户名只能包含字母、数字和下划线');
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
static async sendMessage(ctx){
|
|
|
let {phone} = ctx.request.body;
|
|
|
@@ -105,8 +190,8 @@ class UserController {
|
|
|
}
|
|
|
let params = {
|
|
|
PhoneNumbers: phone,
|
|
|
- SignName: 'seeccoder',
|
|
|
- TemplateCode: 'SMS_181555598',
|
|
|
+ SignName: 'SEECODER',
|
|
|
+ TemplateCode: 'SMS_253920073',
|
|
|
TemplateParam: '{"code":"' + code + '"}'
|
|
|
};
|
|
|
let options = {
|
|
|
@@ -123,7 +208,7 @@ class UserController {
|
|
|
ctx.session.maxAge = 5 * 60 * 1000;
|
|
|
ctx.session.verification_code = code;
|
|
|
ctx.session.verification_phone = phone;
|
|
|
- // console.log('verification_code:', ctx.session.verification_code);
|
|
|
+ console.log('verification_code:', ctx.session.verification_code);
|
|
|
ctx.body = Response.success({message: '发送成功'});
|
|
|
}else{
|
|
|
console.log("error:", result.message);
|