index.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <html lang="zh">
  2. <head>
  3. <title>登录</title>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  6. <script src="jquery.min.js"></script>
  7. <script src="vue.js"></script>
  8. <script src="elementui.js"></script>
  9. <script src="js.cookie.min.js"></script>
  10. <link rel="stylesheet" type="text/css" href="elementui.css">
  11. <!--<script src="vconsole.min.js"></script>
  12. <script>
  13. window.vConsole = new window.VConsole({
  14. defaultPlugins: ['system', 'network', 'element', 'storage'], // 可以在此设定要默认加载的面板
  15. maxLogNumber: 1000,
  16. // disableLogScrolling: true,
  17. onReady: function() {
  18. console.log('vConsole is ready.');
  19. },
  20. onClearLog: function() {
  21. console.log('on clearLog');
  22. }
  23. });
  24. </script>-->
  25. </head>
  26. <body>
  27. <div id="app">
  28. <div>
  29. <el-card class="box-card login-card">
  30. <div slot="header" class="clearfix">
  31. <span>登录</span>
  32. <el-button style="float: right; padding: 3px 0;" type="text" @click="toRegister">
  33. 注册
  34. </el-button>
  35. </div>
  36. <el-input :disabled="isLoading" minlength="1" maxlength="18" @keydown.native.enter="login" style="margin-top:20px;" v-model="userInfo.name" placeholder="用户名"></el-input>
  37. <el-input :disabled="isLoading" minlength="6" maxlength="18" @keydown.native.enter="login" style="margin-top:20px;" v-model="userInfo.password" placeholder="密码" type="password"></el-input>
  38. <el-button @click="login" style="margin-top:20px;display: block;margin-left: auto;margin-right: auto;" type="primary" plain :loading="isLoading">登录</el-button>
  39. </el-card>
  40. </div>
  41. </div>
  42. <script>
  43. var app;
  44. var vue = new Vue({
  45. el: '#app',
  46. data: {
  47. apiUrl: '/api',
  48. visible: false,
  49. isLoading: false,
  50. userInfo: {
  51. name: "",
  52. password: ""
  53. }
  54. },
  55. methods: {
  56. req(url, method, data) {
  57. data = data ? JSON.stringify(data) : "";
  58. return new Promise(function(resolve, reject) {
  59. $.ajax({
  60. type: method,
  61. url: url,
  62. data: data ? data : "",
  63. contentType: data ? "application/json" : "",
  64. success: function(data) {
  65. resolve(data);
  66. },
  67. error: function(err) {
  68. reject(err);
  69. }
  70. });
  71. });
  72. },
  73. toRegister() {
  74. location.href = "/register.html";
  75. },
  76. login() {
  77. var name = this.userInfo.name;
  78. var password = this.userInfo.password;
  79. if (name.length < 1 || name.length > 18) {
  80. this.$message.closeAll();
  81. this.$message({
  82. type: 'error',
  83. showClose: true,
  84. message: "用户名长度需在1-18位"
  85. });
  86. return;
  87. }
  88. if (password.length < 6 || password.length > 18) {
  89. this.$message.closeAll();
  90. this.$message({
  91. type: 'error',
  92. showClose: true,
  93. message: "密码长度需在6-18位"
  94. });
  95. return;
  96. }
  97. this.isLoading = true;
  98. this.req("/api/auth/login", "POST", { "name": name, "password": password }).then((data) => {
  99. if (data["success"] === 0) {
  100. this.$message.closeAll();
  101. this.$message({
  102. type: 'error',
  103. showClose: true,
  104. message: data["msg"]
  105. });
  106. this.isLoading = false;
  107. }
  108. else if (data["success"] === 1) {
  109. this.$message.closeAll();
  110. this.$message({
  111. type: 'success',
  112. showClose: true,
  113. message: "登录成功"
  114. });
  115. this.isLoading = false;
  116. location.href = "/home.html";
  117. }
  118. else {
  119. this.$message.closeAll();
  120. this.$message({
  121. type: 'error',
  122. showClose: true,
  123. message: "未知错误1"
  124. });
  125. this.isLoading = false;
  126. }
  127. }).catch((err) => {
  128. this.$message.closeAll();
  129. this.$message({
  130. type: 'error',
  131. showClose: true,
  132. message: "服务器错误1"
  133. });
  134. this.isLoading = false;
  135. });
  136. }
  137. },
  138. mounted: function() {
  139. app = this;
  140. var token = Cookies.get("token");
  141. if (token) {
  142. this.isLoading = true;
  143. this.req(this.apiUrl + "/auth/login/cookie", "GET", null).then((data) => {
  144. console.log(data);
  145. this.isLoading = false;
  146. if (data && data["success"]) {
  147. location.href = "/home.html";
  148. }
  149. else {
  150. Cookies.set("token", "");
  151. }
  152. }).catch(() => {
  153. this.isLoading = false;
  154. Cookies.set("token", "");
  155. });
  156. }
  157. }
  158. });
  159. </script>
  160. <style>
  161. .login-card {
  162. width: 350px;
  163. margin: 20px auto 0 auto;
  164. }
  165. </style>
  166. </body>
  167. </html>