index.vue 7.7 KB

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