index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <div class="login-container">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. >
  9. <div class="title-container">
  10. <h3 class="title">智慧校园公寓管理端</h3>
  11. </div>
  12. <el-form-item prop="username">
  13. <span class="svg-container">
  14. <div class="user"></div>
  15. </span>
  16. <el-input
  17. v-model="loginForm.username"
  18. placeholder="用户名"
  19. type="text"
  20. />
  21. </el-form-item>
  22. <el-form-item prop="password">
  23. <span class="svg-container">
  24. <div class="password"></div>
  25. </span>
  26. <el-input
  27. ref="password"
  28. v-model="loginForm.password"
  29. :type="passwordType"
  30. placeholder="密码"
  31. @keyup.enter.native="handleLogin"
  32. />
  33. <span class="show-pwd" @click="showPwd">
  34. <div :class="passwordType === 'password' ? 'eye_close' : 'eye'"></div>
  35. </span>
  36. </el-form-item>
  37. <el-button
  38. :loading="loading"
  39. type="primary"
  40. style="width: 100%; margin-bottom: 30px"
  41. @click="handleLogin"
  42. >登录</el-button
  43. >
  44. </el-form>
  45. </div>
  46. </template>
  47. <script>
  48. import { JSEncrypt } from "jsencrypt";
  49. export default {
  50. name: "Login",
  51. data() {
  52. // 自定义校验规则
  53. const validateUsername = (rule, value, callback) => {
  54. if (!value) {
  55. callback(new Error("请输入用户名"));
  56. } else {
  57. callback();
  58. }
  59. };
  60. const validatePassword = (rule, value, callback) => {
  61. if (value.length < 6) {
  62. callback(new Error("密码不少于6位"));
  63. } else {
  64. callback();
  65. }
  66. };
  67. return {
  68. // 表格绑定数据
  69. loginForm: {
  70. username: "",
  71. password: "",
  72. },
  73. // 校验规则
  74. loginRules: {
  75. username: [
  76. { required: true, trigger: "blur", validator: validateUsername },
  77. ],
  78. password: [
  79. { required: true, trigger: "blur", validator: validatePassword },
  80. ],
  81. },
  82. // 加载中效果
  83. loading: false,
  84. // 密码显示隐藏控制
  85. passwordType: "password",
  86. redirect: undefined,
  87. };
  88. },
  89. watch: {
  90. // 监听路由地址变化
  91. $route: {
  92. handler: function (route) {
  93. this.redirect = route.query && route.query.redirect;
  94. },
  95. immediate: true,
  96. },
  97. },
  98. methods: {
  99. // 密码显示隐藏控制函数
  100. showPwd() {
  101. if (this.passwordType === "password") {
  102. this.passwordType = "";
  103. } else {
  104. this.passwordType = "password";
  105. }
  106. // 重新渲染后使密码框聚焦
  107. this.$nextTick(() => {
  108. this.$refs.password.focus();
  109. });
  110. },
  111. // 登录函数
  112. handleLogin() {
  113. // 校验用户名密码是否符合验证规则
  114. this.$refs.loginForm.validate((valid) => {
  115. // 符合验证规则
  116. if (valid) {
  117. this.loading = true;
  118. let publicKey =
  119. "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMOcPB06u5yKyQsPjfVWiWgbEIrd14kiXNNihciaVKb6HnkQvq7zpQuZ80WEX94spnUMI3iOAl/GmIvHrpGwcbB4hJbznm+PajiwnUSPuCCXA68YJF640cJKb/8KeM7WVz69OFkIEPHhVxOy4FFF5QWe/kt6zOZ19HmE+ak+5x/QIDAQAB";
  120. let encryptor = new JSEncrypt(); // 新建JSEncrypt对象
  121. encryptor.setPublicKey(publicKey); // 设置公钥
  122. let rsaPassWord = encryptor.encrypt(this.loginForm.password); // 对密码进行加密
  123. console.log(rsaPassWord);
  124. this.$store
  125. .dispatch("user/login", {
  126. username: this.loginForm.username,
  127. password: rsaPassWord,
  128. })
  129. .then(() => {
  130. this.$router.push({ path: this.redirect || "/home" });
  131. this.loading = false;
  132. })
  133. .catch(() => {
  134. this.loading = false;
  135. });
  136. }
  137. // 不符合验证规则
  138. else {
  139. console.log("提交失败!!");
  140. return false;
  141. }
  142. });
  143. },
  144. },
  145. };
  146. </script>
  147. <style lang="scss">
  148. /* 修复input 背景不协调 和光标变色 */
  149. $bg: #283443;
  150. $light_gray: #fff;
  151. $cursor: #fff;
  152. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  153. .login-container .el-input input {
  154. color: $bg;
  155. }
  156. }
  157. /* 重置 element-ui css */
  158. .login-container {
  159. .el-input {
  160. display: inline-block;
  161. height: 47px;
  162. width: 85%;
  163. input {
  164. background: transparent;
  165. border: 0px;
  166. -webkit-appearance: none;
  167. border-radius: 0px;
  168. padding: 12px 5px 12px 15px;
  169. color: $bg;
  170. height: 47px;
  171. caret-color: $bg;
  172. &:-webkit-autofill {
  173. box-shadow: 0 0 0px 1000px #e5e5e5 inset !important;
  174. -webkit-text-fill-color: $bg !important;
  175. }
  176. }
  177. }
  178. .el-form-item {
  179. border: 1px solid rgba(255, 255, 255, 0.1);
  180. background: rgba(0, 0, 0, 0.1);
  181. border-radius: 5px;
  182. color: #454545;
  183. }
  184. }
  185. </style>
  186. <style lang="scss" scoped>
  187. $bg: #2d3a4b;
  188. $dark_gray: #889aa4;
  189. .login-container {
  190. width: 100vw;
  191. height: 100vh;
  192. min-width: 1280px;
  193. min-height: 800px;
  194. background: url(../../assets/images/bg.png);
  195. background-size: 100% 100%;
  196. overflow: hidden;
  197. .login-form {
  198. width: 520px;
  199. margin-top: 310px;
  200. margin-left: 1080px;
  201. }
  202. .svg-container {
  203. padding: 6px 5px 6px 15px;
  204. color: $bg;
  205. vertical-align: middle;
  206. width: 30px;
  207. display: inline-block;
  208. .user {
  209. width: 20px;
  210. height: 20px;
  211. background: url(../../assets/images/user.png);
  212. background-size: 100% 100%;
  213. }
  214. .password {
  215. width: 20px;
  216. height: 20px;
  217. background: url(../../assets/images/password.png);
  218. background-size: 100% 100%;
  219. }
  220. }
  221. .title-container {
  222. position: relative;
  223. .title {
  224. font-size: 28px;
  225. margin: 0px auto 40px auto;
  226. text-align: center;
  227. font-weight: bold;
  228. }
  229. }
  230. .show-pwd {
  231. position: absolute;
  232. right: 10px;
  233. top: 7px;
  234. font-size: 16px;
  235. color: $bg;
  236. cursor: pointer;
  237. user-select: none;
  238. .eye_close {
  239. margin-top: 7px;
  240. width: 20px;
  241. height: 20px;
  242. background: url(../../assets/images/eye_close.png);
  243. background-size: 100% 100%;
  244. }
  245. .eye {
  246. margin-top: 7px;
  247. width: 20px;
  248. height: 20px;
  249. background: url(../../assets/images/eye.png);
  250. background-size: 100% 100%;
  251. }
  252. }
  253. }
  254. </style>