|
@@ -11,6 +11,7 @@
|
|
|
// 加密类
|
|
|
const crypto = require('crypto');
|
|
|
const SSO = require('../lib/sso');
|
|
|
+const SMS = require('../lib/sms');
|
|
|
module.exports = app => {
|
|
|
|
|
|
class ProjectAccount extends app.BaseService {
|
|
@@ -57,6 +58,19 @@ module.exports = app => {
|
|
|
telephone: { type: 'string', allowEmpty: true, max: 12 },
|
|
|
};
|
|
|
break;
|
|
|
+ case 'modifyPassword':
|
|
|
+ rule = {
|
|
|
+ password: { type: 'string', required: true, min: 6 },
|
|
|
+ new_password: { type: 'string', required: true, min: 6 },
|
|
|
+ confirm_password: { type: 'string', required: true, min: 6 },
|
|
|
+ };
|
|
|
+ break;
|
|
|
+ case 'bindMobile':
|
|
|
+ rule = {
|
|
|
+ code: { type: 'string', required: true, min: 6 },
|
|
|
+ auth_mobile: { type: 'mobile', allowEmpty: false },
|
|
|
+ };
|
|
|
+ break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
@@ -241,6 +255,93 @@ module.exports = app => {
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改密码
|
|
|
+ *
|
|
|
+ * @param {Number} accountId - 账号id
|
|
|
+ * @param {String} password - 旧密码
|
|
|
+ * @param {String} newPassword - 新密码
|
|
|
+ * @return {Boolean} - 返回修改结果
|
|
|
+ */
|
|
|
+ async modifyPassword(accountId, password, newPassword) {
|
|
|
+ // 查找账号
|
|
|
+ const accountData = await this.getDataByCondition({ id: accountId });
|
|
|
+ if (accountData.password === undefined) {
|
|
|
+ throw '不存在对应用户';
|
|
|
+ }
|
|
|
+ // 判断是否为sso账号,如果是则不能在此系统修改(后续通过接口修改?)
|
|
|
+ if (accountData.password === 'SSO password') {
|
|
|
+ throw 'SSO用户请到SSO系统修改密码';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加密密码
|
|
|
+ const encryptPassword = crypto.createHmac('sha1', accountData.account).update(password)
|
|
|
+ .digest().toString('base64');
|
|
|
+ if (encryptPassword !== accountData.password) {
|
|
|
+ throw '密码错误';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通过密码验证后修改数据
|
|
|
+ const encryptNewPassword = crypto.createHmac('sha1', accountData.account).update(newPassword)
|
|
|
+ .digest().toString('base64');
|
|
|
+ const updateData = { password: encryptNewPassword };
|
|
|
+ const result = await this.save(updateData, accountId);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置短信验证码
|
|
|
+ *
|
|
|
+ * @param {Number} accountId - 账号id
|
|
|
+ * @param {String} mobile - 电话号码
|
|
|
+ * @return {Boolean} - 设置结果
|
|
|
+ */
|
|
|
+ async setSMSCode(accountId, mobile) {
|
|
|
+ const cacheKey = 'smsCode:' + accountId;
|
|
|
+ const randString = this.ctx.helper.generateRandomString(6, 2);
|
|
|
+ // 缓存15分钟(拼接电话,防止篡改)
|
|
|
+ this.cache.set(cacheKey, randString + mobile, 'EX', 900);
|
|
|
+ let result = false;
|
|
|
+
|
|
|
+ // 发送短信
|
|
|
+ try {
|
|
|
+ const sms = new SMS(this.ctx);
|
|
|
+ const content = '【纵横计量支付】验证码:' + randString + ',15分钟有效。';
|
|
|
+ result = await sms.send(mobile, content);
|
|
|
+ } catch (error) {
|
|
|
+ result = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绑定认证手机
|
|
|
+ *
|
|
|
+ * @param {Number} accountId - 账号id
|
|
|
+ * @param {Object} data - post过来的数据
|
|
|
+ * @return {Boolean} - 绑定结果
|
|
|
+ */
|
|
|
+ async bindMobile(accountId, data) {
|
|
|
+ const cacheKey = 'smsCode:' + accountId;
|
|
|
+ const cacheCode = await this.cache.get(cacheKey);
|
|
|
+ if (cacheCode === null || data.code === undefined || cacheCode !== (data.code + data.auth_mobile)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找是否有重复的认证手机
|
|
|
+ const accountData = await this.getDataByCondition({ auth_mobile: data.auth_mobile });
|
|
|
+ if (accountData !== null) {
|
|
|
+ throw '已存在对应的手机';
|
|
|
+ }
|
|
|
+
|
|
|
+ const updateData = { auth_mobile: data.auth_mobile };
|
|
|
+
|
|
|
+ return this.save(updateData, accountId);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
return ProjectAccount;
|