Преглед изворни кода

feat(user/update-email)

現在用戶可以自行在後臺“資料編輯”處更改自己的賬戶郵箱了
兩足大貓貓 пре 5 година
родитељ
комит
c1e71397f5
3 измењених фајлова са 77 додато и 0 уклоњено
  1. 1 0
      app/routes.php
  2. 41 0
      resources/views/material/user/edit.tpl
  3. 35 0
      src/Controllers/UserController.php

+ 1 - 0
app/routes.php

@@ -66,6 +66,7 @@ return function (SlimApp $app) {
         $this->post('/buy_invite',              App\Controllers\UserController::class . ':buyInvite');
         $this->post('/custom_invite',           App\Controllers\UserController::class . ':customInvite');
         $this->get('/edit',                     App\Controllers\UserController::class . ':edit');
+        $this->post('/email',                    App\Controllers\UserController::class . ':updateEmail');
         $this->post('/password',                App\Controllers\UserController::class . ':updatePassword');
         $this->post('/wechat',                  App\Controllers\UserController::class . ':updateWechat');
         $this->post('/ssr',                     App\Controllers\UserController::class . ':updateSSR');

+ 41 - 0
resources/views/material/user/edit.tpl

@@ -140,6 +140,23 @@
                 </div>
             </div>
             <div class="col-xx-12 col-sm-6">
+                <div class="card margin-bottom-no">
+                    <div class="card-main">
+                        <div class="card-inner">
+                            <div class="card-inner">
+                                <div class="cardbtn-edit">
+                                    <div class="card-heading">账户邮箱修改</div>
+                                    <button class="btn btn-flat" id="email-update"><span class="icon">check</span>&nbsp;
+                                    </button>
+                                </div>
+                                <div class="form-group form-group-label">
+                                    <label class="floating-label" for="newemail">新邮箱</label>
+                                    <input class="form-control maxwidth-edit" id="newemail" type="text">
+                                </div>
+                            </div>
+                        </div>
+                    </div>
+                </div>
                 <div class="card margin-bottom-no">
                     <div class="card-main">
                         <div class="card-inner">
@@ -581,6 +598,30 @@
         })
     })
 </script>
+<script>
+    $(document).ready(function () {
+        $("#email-update").click(function () {
+            $.ajax({
+                type: "POST",
+                url: "email",
+                dataType: "json",
+                data: {
+                    newemail: $$getValue('newemail')
+                },
+                success: (data) => {
+                    $("#result").modal();
+                    $$.getElementById('msg').innerHTML = data.msg;
+                },
+                error: (jqXHR) => {
+                    $("#result").modal();
+                    $$.getElementById('msg').innerHTML = `${
+                            data.msg
+                            } 出现了一些错误`;
+                }
+            })
+        })
+    })
+</script>
 {/literal}
 <script>
     var ga_qrcode = '{$user->getGAurl()}',

+ 35 - 0
src/Controllers/UserController.php

@@ -38,6 +38,7 @@ use App\Utils\{
     Pay,
     URL,
     Hash,
+    Check,
     QQWry,
     Tools,
     Radius,
@@ -635,6 +636,40 @@ class UserController extends BaseController
         return $this->echoJson($response, $res);
     }
 
+    public function updateEmail($request, $response, $args)
+    {
+        $user = $this->user;
+        $newemail = $request->getParam('newemail');
+        $oldemail = $user->email;
+        $otheruser = User::where('email', $newemail)->first();
+        if ($newemail == '') {
+            $res['ret'] = 0;
+            $res['msg'] = '未填写邮箱';
+            return $response->getBody()->write(json_encode($res));
+        }
+        if (!Check::isEmailLegal($newemail)) {
+            $res['ret'] = 0;
+            $res['msg'] = '邮箱无效';
+            return $response->getBody()->write(json_encode($res));
+        }
+        if ($otheruser != null) {
+            $res['ret'] = 0;
+            $res['msg'] = '邮箱已经被使用了';
+            return $response->getBody()->write(json_encode($res));
+        }
+        if ($newemail == $oldemail) {
+            $res['ret'] = 0;
+            $res['msg'] = '新邮箱不能和旧邮箱一样';
+            return $response->getBody()->write(json_encode($res));
+        }
+        $user->email = $newemail;
+        $user->save();
+
+        $res['ret'] = 1;
+        $res['msg'] = '修改成功';
+        return $this->echoJson($response, $res);
+    }
+
     public function updateHide($request, $response, $args)
     {
         $hide = $request->getParam('hide');