index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <template>
  2. <div class="login-container">
  3. <el-alert
  4. title="beautiful boys and girls欢迎加入vue-admin-beautifulQQ群:972435319"
  5. type="success"
  6. :closable="false"
  7. style="position: fixed"
  8. ></el-alert>
  9. <el-row>
  10. <el-col :xs="24" :sm="24" :md="12" :lg="16" :xl="16">
  11. <div style="color: transparent">占位符</div>
  12. </el-col>
  13. <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
  14. <el-form
  15. ref="form"
  16. :model="form"
  17. :rules="rules"
  18. class="login-form"
  19. label-position="left"
  20. >
  21. <div class="title">hello !</div>
  22. <div class="title-tips">欢迎来到{{ title }}!</div>
  23. <el-form-item style="margin-top: 40px" prop="username">
  24. <span class="svg-container svg-container-admin">
  25. <vab-icon :icon="['fas', 'user']" />
  26. </span>
  27. <el-input
  28. v-model.trim="form.username"
  29. v-focus
  30. placeholder="请输入用户名"
  31. tabindex="1"
  32. type="text"
  33. />
  34. </el-form-item>
  35. <el-form-item prop="password">
  36. <span class="svg-container">
  37. <vab-icon :icon="['fas', 'lock']" />
  38. </span>
  39. <el-input
  40. :key="passwordType"
  41. ref="password"
  42. v-model.trim="form.password"
  43. :type="passwordType"
  44. tabindex="2"
  45. placeholder="请输入密码"
  46. @keyup.enter.native="handleLogin"
  47. />
  48. <span
  49. v-if="passwordType === 'password'"
  50. class="show-password"
  51. @click="handlePassword"
  52. >
  53. <vab-icon :icon="['fas', 'eye-slash']"></vab-icon>
  54. </span>
  55. <span v-else class="show-password" @click="handlePassword">
  56. <vab-icon :icon="['fas', 'eye']"></vab-icon>
  57. </span>
  58. </el-form-item>
  59. <el-button
  60. :loading="loading"
  61. class="login-btn"
  62. type="primary"
  63. @click="handleLogin"
  64. >
  65. 登录
  66. </el-button>
  67. <router-link to="/register">
  68. <div style="margin-top: 20px">注册</div>
  69. </router-link>
  70. </el-form>
  71. </el-col>
  72. </el-row>
  73. </div>
  74. </template>
  75. <script>
  76. import { isPassword } from "@/utils/validate";
  77. export default {
  78. name: "Login",
  79. directives: {
  80. focus: {
  81. inserted(el) {
  82. el.querySelector("input").focus();
  83. },
  84. },
  85. },
  86. data() {
  87. const validateusername = (rule, value, callback) => {
  88. if ("" == value) {
  89. callback(new Error("用户名不能为空"));
  90. } else {
  91. callback();
  92. }
  93. };
  94. const validatePassword = (rule, value, callback) => {
  95. if (!isPassword(value)) {
  96. callback(new Error("密码不能少于6位"));
  97. } else {
  98. callback();
  99. }
  100. };
  101. return {
  102. nodeEnv: process.env.NODE_ENV,
  103. title: this.$baseTitle,
  104. form: {
  105. username: "",
  106. password: "",
  107. },
  108. rules: {
  109. username: [
  110. {
  111. required: true,
  112. trigger: "blur",
  113. validator: validateusername,
  114. },
  115. ],
  116. password: [
  117. {
  118. required: true,
  119. trigger: "blur",
  120. validator: validatePassword,
  121. },
  122. ],
  123. },
  124. loading: false,
  125. passwordType: "password",
  126. redirect: undefined,
  127. };
  128. },
  129. watch: {
  130. $route: {
  131. handler(route) {
  132. this.redirect = (route.query && route.query.redirect) || "/";
  133. },
  134. immediate: true,
  135. },
  136. },
  137. created() {
  138. document.body.style.overflow = "hidden";
  139. },
  140. beforeDestroy() {
  141. document.body.style.overflow = "auto";
  142. },
  143. mounted() {
  144. this.form.username = "admin";
  145. this.form.password = "123456";
  146. setTimeout(() => {
  147. this.handleLogin();
  148. }, 3000);
  149. },
  150. methods: {
  151. handlePassword() {
  152. this.passwordType === "password"
  153. ? (this.passwordType = "")
  154. : (this.passwordType = "password");
  155. this.$nextTick(() => {
  156. this.$refs.password.focus();
  157. });
  158. },
  159. handleLogin() {
  160. this.$refs.form.validate((valid) => {
  161. if (valid) {
  162. this.loading = true;
  163. this.$store
  164. .dispatch("user/login", this.form)
  165. .then(() => {
  166. const routerPath =
  167. this.redirect === "/404" || this.redirect === "/401"
  168. ? "/"
  169. : this.redirect;
  170. this.$router.push(routerPath).catch(() => {});
  171. this.loading = false;
  172. })
  173. .catch(() => {
  174. this.loading = false;
  175. });
  176. } else {
  177. return false;
  178. }
  179. });
  180. },
  181. },
  182. };
  183. </script>
  184. <style lang="scss" scoped>
  185. .login-container {
  186. height: 100vh;
  187. background: url("~@/assets/login_images/background.jpg") center center fixed
  188. no-repeat;
  189. background-size: cover;
  190. .title {
  191. font-size: 54px;
  192. font-weight: 500;
  193. color: rgba(14, 18, 26, 1);
  194. }
  195. .title-tips {
  196. margin-top: 29px;
  197. font-size: 26px;
  198. font-weight: 400;
  199. color: rgba(14, 18, 26, 1);
  200. text-overflow: ellipsis;
  201. white-space: nowrap;
  202. }
  203. .login-btn {
  204. display: inherit;
  205. width: 220px;
  206. height: 60px;
  207. margin-top: 5px;
  208. border: 0;
  209. &:hover {
  210. opacity: 0.9;
  211. }
  212. }
  213. .login-form {
  214. position: relative;
  215. max-width: 100%;
  216. margin: calc((100vh - 425px) / 2) 10% 10%;
  217. overflow: hidden;
  218. .forget-password {
  219. width: 100%;
  220. margin-top: 40px;
  221. text-align: left;
  222. .forget-pass {
  223. width: 129px;
  224. height: 19px;
  225. font-size: 20px;
  226. font-weight: 400;
  227. color: rgba(92, 102, 240, 1);
  228. }
  229. }
  230. }
  231. .tips {
  232. margin-bottom: 10px;
  233. font-size: $base-font-size-default;
  234. color: $base-color-white;
  235. span {
  236. &:first-of-type {
  237. margin-right: 16px;
  238. }
  239. }
  240. }
  241. .title-container {
  242. position: relative;
  243. .title {
  244. margin: 0 auto 40px auto;
  245. font-size: 34px;
  246. font-weight: bold;
  247. color: $base-color-blue;
  248. text-align: center;
  249. }
  250. }
  251. .svg-container {
  252. position: absolute;
  253. top: 14px;
  254. left: 15px;
  255. z-index: $base-z-index;
  256. font-size: 16px;
  257. color: #d7dee3;
  258. cursor: pointer;
  259. user-select: none;
  260. }
  261. .show-password {
  262. position: absolute;
  263. top: 14px;
  264. right: 25px;
  265. font-size: 16px;
  266. color: #d7dee3;
  267. cursor: pointer;
  268. user-select: none;
  269. }
  270. ::v-deep {
  271. .el-form-item {
  272. padding-right: 0;
  273. margin: 20px 0;
  274. color: #454545;
  275. background: transparent;
  276. border: 1px solid transparent;
  277. border-radius: 2px;
  278. &__content {
  279. min-height: $base-input-height;
  280. line-height: $base-input-height;
  281. }
  282. &__error {
  283. position: absolute;
  284. top: 100%;
  285. left: 18px;
  286. font-size: $base-font-size-small;
  287. line-height: 18px;
  288. color: $base-color-red;
  289. }
  290. }
  291. .el-input {
  292. box-sizing: border-box;
  293. input {
  294. height: 58px;
  295. padding-left: 45px;
  296. font-size: $base-font-size-default;
  297. line-height: 58px;
  298. color: $base-font-color;
  299. background: #f6f4fc;
  300. border: 0;
  301. caret-color: $base-font-color;
  302. }
  303. }
  304. }
  305. }
  306. </style>