laiguoran 5 роки тому
батько
коміт
8938835169

+ 17 - 3
app/controller/profile_controller.js

@@ -129,6 +129,12 @@ module.exports = app => {
                 const rule = { mobile: { type: 'mobile', allowEmpty: false } };
                 ctx.helper.validate(rule);
 
+                // 查找是否有重复的认证手机
+                const accountData = await ctx.service.projectAccount.getDataByCondition({ project_id: ctx.session.sessionProject.id, auth_mobile: mobile });
+                if (accountData !== null) {
+                    throw '此手机号码已被使用,请重新输入!';
+                }
+
                 const result = await ctx.service.projectAccount.setSMSCode(sessionUser.accountId, mobile);
                 if (!result) {
                     throw '获取验证码失败';
@@ -148,22 +154,30 @@ module.exports = app => {
          * @return {void}
          */
         async bindMobile(ctx) {
+            const response = {
+                err: 0,
+                msg: '',
+            };
             try {
                 const rule = ctx.service.projectAccount.rule('bindMobile');
                 ctx.helper.validate(rule);
 
                 const sessionUser = ctx.session.sessionUser;
+
                 const result = await ctx.service.projectAccount.bindMobile(sessionUser.accountId, ctx.request.body, ctx.session.sessionProject.id);
 
                 if (!result) {
                     throw '绑定手机失败!';
                 }
-                this.setMessage('绑定成功', this.messageType.SUCCESS);
+                // this.setMessage('绑定成功', this.messageType.SUCCESS);
+                response.msg = '绑定成功';
+                response.url = ctx.request.header.referer;
             } catch (error) {
                 console.log(error);
-                this.setMessage(error.toString(), this.messageType.ERROR);
+                response.err = 1;
+                response.msg = error.toString();
             }
-            ctx.redirect(ctx.request.header.referer);
+            ctx.body = response;
         }
 
         /**

+ 1 - 1
app/middleware/session_auth.js

@@ -24,7 +24,7 @@ module.exports = options => {
             }
             // 校验session
             const sessionToken = crypto.createHmac('sha1', sessionUser.loginTime + '')
-                .update(sessionUser.account).digest().toString('base64');
+                .update(sessionUser.account).digest('hex').toString('base64');
             if (sessionToken !== sessionUser.sessionToken) {
                 throw 'session数据错误';
             }

Різницю між файлами не показано, бо вона завелика
+ 3 - 3
app/public/css/bootstrap/bootstrap.min.css


+ 16 - 0
app/public/js/profile.js

@@ -78,6 +78,9 @@ $(document).ready(function() {
         $("#bind-btn").click(function() {
             const code = $("input[name='code']").val();
             const mobile = $("input[name='auth_mobile']").val();
+            if ($(this).hasClass('disabled')) {
+                return false;
+            }
             if (!(/^1[3456789]\d{9}$/.test(mobile))) {
                 toast('请填写正确的手机号码', 'error');
                 return false;
@@ -87,6 +90,19 @@ $(document).ready(function() {
                 toast('请填写正确的验证码', 'error');
                 return false;
             }
+            $.ajax({
+                url: '/profile/bind?_csrf=' + csrf,
+                type: 'post',
+                data: { auth_mobile: mobile, code: code },
+                dataTye: 'json',
+                success: function(response) {
+                    if (response.err === 0) {
+                        window.location.href = response.data.url;
+                    } else {
+                        toast(response.msg, 'error');
+                    }
+                }
+            });
         });
         // 修改手机
         $('#change-mobile').click(function () {

+ 3 - 3
app/service/project_account.js

@@ -168,7 +168,7 @@ module.exports = app => {
                     const currentTime = new Date().getTime() / 1000;
                     // 加密token
                     const sessionToken = crypto.createHmac('sha1', currentTime + '').update(accountData.account)
-                        .digest().toString('base64');
+                        .digest('hex').toString('base64');
 
                     if (loginType === 2) {
                         const updateData = {
@@ -426,13 +426,13 @@ module.exports = app => {
             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;
+                throw '验证码错误!';
             }
 
             // 查找是否有重复的认证手机
             const accountData = await this.getDataByCondition({ project_id: pid, auth_mobile: data.auth_mobile });
             if (accountData !== null) {
-                throw '已存在对应的手机';
+                throw '此手机号码已被使用,请重新输入!';
             }
 
             const updateData = { id: accountId, auth_mobile: data.auth_mobile };

+ 2 - 2
app/view/profile/sms.ejs

@@ -22,7 +22,7 @@
                     </div>
                     <% } %>
                     <!--绑定手机-->
-                    <form id="mobile-form" method="post" action="/profile/bind" <% if (accountData.auth_mobile !== '') { %>style="display: none" <% } %>>
+                    <form id="mobile-form" <% if (accountData.auth_mobile !== '') { %>style="display: none" <% } %>>
                         <div class="form-group">
                             <label>认证手机(用于 找回密码、接收通知)</label>
                             <div class="input-group mb-3">
@@ -38,7 +38,7 @@
                                 <input type="hidden" name="_csrf" value="<%= ctx.csrf %>">
                             </div>
                         </div>
-                        <button type="submit" class="btn btn-secondary disabled" id="bind-btn">确认绑定</button>
+                        <button type="button" class="btn btn-secondary disabled" id="bind-btn">确认绑定</button>
                     </form>
                     <% if (accountData.auth_mobile !== '') { %>
                     <!--短信通知开关(已有认证手机后显示)-->