import BaseController from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
|
import { BaseSms } from "../../entity/basicInfo/user/baseSms";
|
import { SysUser } from "../../entity/sys/core/sysUser";
|
import moment = require("moment");
|
|
/**
|
* 用户中心
|
*/
|
export default class UserController extends BaseController {
|
//#region 修改密码发送验证码
|
/**
|
* 注册发送验证码
|
*/
|
@Post()
|
public async sendForGetValidateCode() {
|
try {
|
//#region 校验数据
|
//验证手机号不能为空
|
if (!this.body.mobile) {
|
this.info.result = false;
|
this.info.msg = "您没有输入手机号!";
|
this.ctx.body = this.info;
|
return;
|
}
|
//验证手机号格式是否正确
|
let tegexText = /^(134[012345678]\d{7}|1[345789][012356789]\d{8})$/g;
|
if (!tegexText.test(this.body.mobile)) {
|
this.info.result = false;
|
this.info.statusCode = 500;
|
this.info.state = "ConsignorApp_MobileFormatError";
|
this.info.msg = "手机号不正确";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
let clientInfo = await this.dbRead.findOne(BaseConsignor, {
|
mobile: this.body.mobile
|
});
|
if (!clientInfo) {
|
this.info.result = false;
|
this.info.msg = "您输入的手机号在系统中不存在!";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
//#endregion
|
|
// 发送验证码
|
let mobile = this.body.mobile;
|
//let templateId = "321297";//带路网验证码模板
|
let randKey = 999999 * Math.random() + 100000;
|
let params = {
|
"#code#": randKey
|
};
|
this.info = await this.ctx.service.utils.smsHelper.sendYPSMS(mobile, params, 11111);
|
|
if (this.info.result) {
|
let sms = new BaseSms();
|
sms.keyCode = this.body.mobile;
|
sms.validCode = randKey.toString();
|
sms.validType = "api验证";
|
sms.isUsed = 0;
|
//记录验证码
|
await this.dbWrite.save(sms);
|
this.info.result = true;
|
this.info.statusCode = 200;
|
this.info.state = "ValidateCodeSuccess";
|
this.info.msg = "发送成功";
|
} else {
|
this.info.result = false;
|
this.info.statusCode = 500;
|
this.info.state = "fail";
|
this.info.msg = "发送失败";
|
}
|
this.ctx.body = this.info;
|
return;
|
} catch (ex) {
|
let msg = "出现异常:" + ex.message;
|
this.info.result = false;
|
this.info.msg = msg;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
|
//#region 忘记密码-手机修改密码
|
@Post()
|
public async phoneUpdatePwd() {
|
try {
|
//校验数据
|
if (!this.body.mobile) {
|
this.info.result = false;
|
this.info.msg = "手机号不能为空!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!this.body.ValidateCode) {
|
this.info.result = false;
|
this.info.msg = "验证码不能为空!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!this.body.userPwd) {
|
this.info.result = false;
|
this.info.msg = "请输入密码!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!this.body.RepeatPassWord) {
|
this.info.result = false;
|
this.info.msg = "请确认密码!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (this.body.RepeatPassWord != this.body.userPwd) {
|
this.info.result = false;
|
this.info.msg = "两次密码不一致!";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
var userInfo = await this.dbRead.findOne(SysUser, {
|
mobile: this.body.mobile
|
});
|
if (userInfo == null) {
|
this.info.result = false;
|
this.info.msg = "手机号未注册!";
|
this.ctx.body = this.info;
|
return;
|
}
|
//解密验证码
|
|
var smsInfo = await this.dbRead.findOne(BaseSms, {
|
where: {
|
keyCode: this.body.mobile,
|
isUsed: 0
|
},
|
order: {
|
sms_Id: "DESC"
|
}
|
});
|
if (smsInfo != null && smsInfo.validCode == this.body.ValidateCode) {
|
smsInfo.isUsed = 1;
|
await this.dbWrite.save(smsInfo);
|
var span = moment(new Date()).diff(moment(smsInfo.createDate), "minutes");
|
if (span > 10) {
|
//十分钟后超时
|
this.info.result = false;
|
this.info.msg = "验证已失效!";
|
this.ctx.body = this.info;
|
return;
|
}
|
} else {
|
this.info.result = false;
|
this.info.msg = "验证不正确!";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
//修改密码
|
let userPwd = this.ctx.helper.md5EncodingSalt(this.body.userPwd);
|
await this.dbWrite.update(SysUser, userInfo.user_Id, {
|
userPwd: userPwd
|
});
|
this.info.result = true;
|
this.info.msg = "修改成功!";
|
this.ctx.body = this.info;
|
return;
|
} catch {
|
this.info.result = false;
|
this.info.msg = "修改失败!";
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
|
//#region 超级管理员后端修改密码
|
@Post()
|
public async modifyPwd() {
|
try {
|
//校验数据
|
if (!this.body.mobile) {
|
this.info.result = false;
|
this.info.msg = "手机号不能为空!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!this.body.userPwd) {
|
this.info.result = false;
|
this.info.msg = "请输入密码!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (!this.body.RepeatPassWord) {
|
this.info.result = false;
|
this.info.msg = "请确认密码!";
|
this.ctx.body = this.info;
|
return;
|
}
|
if (this.body.RepeatPassWord != this.body.userPwd) {
|
this.info.result = false;
|
this.info.msg = "两次密码不一致!";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
var userInfo = await this.dbRead.findOne(SysUser, {
|
mobile: this.body.mobile
|
});
|
if (userInfo == null) {
|
this.info.result = false;
|
this.info.msg = "手机号未注册!";
|
this.ctx.body = this.info;
|
return;
|
}
|
//修改密码
|
let userPwd = this.ctx.helper.md5EncodingSalt(this.body.userPwd);
|
await this.dbWrite.update(SysUser, userInfo.user_Id, {
|
userPwd: userPwd
|
});
|
this.info.result = true;
|
this.info.msg = "修改成功!";
|
this.ctx.body = this.info;
|
} catch {
|
this.info.result = false;
|
this.info.msg = "修改失败!";
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
}
|