login.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <div class="title-container">
  5. <h3 class="title">{{ $t('login.title') }}</h3>
  6. <lang-select class="set-language" />
  7. </div>
  8. <el-form-item prop="username">
  9. <el-input v-model="loginForm.username" type="text" auto-complete="off" :placeholder="$t('login.username')">
  10. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  11. </el-input>
  12. </el-form-item>
  13. <el-form-item prop="password">
  14. <el-input
  15. v-model="loginForm.password"
  16. type="password"
  17. auto-complete="off"
  18. :placeholder="$t('login.password')"
  19. @keyup.enter.native="handleLogin"
  20. >
  21. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  22. </el-input>
  23. </el-form-item>
  24. <!-- <el-form-item prop="code">-->
  25. <!-- <el-input-->
  26. <!-- v-model="loginForm.code"-->
  27. <!-- auto-complete="off"-->
  28. <!-- :placeholder="$t('login.code')"-->
  29. <!-- style="width: 63%"-->
  30. <!-- @keyup.enter.native="handleLogin"-->
  31. <!-- >-->
  32. <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
  33. <!-- </el-input>-->
  34. <!-- <div class="login-code">-->
  35. <!-- <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
  36. <!-- </div>-->
  37. <!-- </el-form-item>-->
  38. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">{{ $t('login.rememberPassword') }}</el-checkbox>
  39. <el-form-item style="width:100%;">
  40. <el-button
  41. :loading="loading"
  42. size="medium"
  43. type="primary"
  44. style="width:100%;"
  45. @click.native.prevent="handleLogin"
  46. >
  47. <span v-if="!loading"> {{ $t('login.logIn') }}</span>
  48. <span v-else>{{ $t('login.loading') }}</span>
  49. </el-button>
  50. </el-form-item>
  51. </el-form>
  52. <!-- 底部 -->
  53. <div class="el-login-footer">
  54. <span>Copyright © 2020-2022 Seashore.ept All Rights Reserved.</span>
  55. </div>
  56. </div>
  57. </template>
  58. <script>
  59. import { getCodeImg } from "@/api/login";
  60. import Cookies from "js-cookie";
  61. import { encrypt, decrypt } from '@/utils/jsencrypt'
  62. import LangSelect from '@/components/LangSelect'
  63. export default {
  64. name: "Login",
  65. components: { LangSelect },
  66. data() {
  67. return {
  68. codeUrl: "",
  69. cookiePassword: "",
  70. loginForm: {
  71. username: "",
  72. password: "",
  73. rememberMe: false,
  74. code: "",
  75. uuid: ""
  76. },
  77. loginRules: {
  78. username: [
  79. { required: true, trigger: "blur", message: this.$t('login.usernameNotEmpty') }
  80. ],
  81. password: [
  82. { required: true, trigger: "blur", message: this.$t('login.passwordNotEmpty')}
  83. ],
  84. code: [{ required: true, trigger: "change", message: this.$t('login.codeNotEmpty') }]
  85. },
  86. loading: false,
  87. redirect: undefined
  88. };
  89. },
  90. watch: {
  91. $route: {
  92. handler: function(route) {
  93. this.redirect = route.query && route.query.redirect;
  94. },
  95. immediate: true
  96. }
  97. },
  98. created() {
  99. this.getCode();
  100. this.getCookie();
  101. if (!this.$store.getters.language){
  102. console.log("默认中文")
  103. this.$i18n.locale = 'zh'
  104. this.$store.dispatch('app/setLanguage', 'zh')
  105. }
  106. },
  107. methods: {
  108. getCode() {
  109. getCodeImg().then(res => {
  110. this.codeUrl = "data:image/gif;base64," + res.img;
  111. this.loginForm.uuid = res.uuid;
  112. });
  113. },
  114. getCookie() {
  115. const username = Cookies.get("username");
  116. const password = Cookies.get("password");
  117. const rememberMe = Cookies.get('rememberMe')
  118. this.loginForm = {
  119. username: username === undefined ? this.loginForm.username : username,
  120. password: password === undefined ? this.loginForm.password : decrypt(password),
  121. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  122. };
  123. },
  124. handleLogin() {
  125. this.$refs.loginForm.validate(valid => {
  126. if (valid) {
  127. this.loading = true;
  128. if (this.loginForm.rememberMe) {
  129. Cookies.set("username", this.loginForm.username, { expires: 30 });
  130. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  131. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  132. } else {
  133. Cookies.remove("username");
  134. Cookies.remove("password");
  135. Cookies.remove('rememberMe');
  136. }
  137. this.$store
  138. .dispatch("Login", this.loginForm)
  139. .then(() => {
  140. this.$router.push({ path: this.redirect || "/404" });
  141. })
  142. .catch(() => {
  143. this.loading = false;
  144. this.getCode();
  145. });
  146. }
  147. });
  148. }
  149. }
  150. };
  151. </script>
  152. <style rel="stylesheet/scss" lang="scss">
  153. .login {
  154. display: flex;
  155. justify-content: center;
  156. align-items: center;
  157. height: 100%;
  158. //background-image: url("../assets/image/CPMS20210107.jpg");
  159. background-image: url("../assets/image/cpms-test.jpg");
  160. background-size: cover;
  161. }
  162. .title {
  163. margin: 0px auto 15px auto;
  164. text-align: center;
  165. color: #ffffff;
  166. }
  167. .login-form {
  168. position:absolute;
  169. top:50%;
  170. left:50%;
  171. transform:translate(-50%,-50%);
  172. /*实现块元素百分比下居中*/
  173. width:450px;
  174. padding:50px;
  175. background: #2a8db9db;
  176. box-sizing:border-box;
  177. box-shadow: 0px 15px 25px rgba(0,0,0,.5);
  178. border-radius:15px;
  179. .el-input {
  180. height: 38px;
  181. input {
  182. height: 38px;
  183. }
  184. }
  185. .input-icon {
  186. height: 39px;
  187. width: 14px;
  188. margin-left: 2px;
  189. }
  190. }
  191. .login-tip {
  192. font-size: 13px;
  193. text-align: center;
  194. color: #bfbfbf;
  195. }
  196. .login-code {
  197. width: 33%;
  198. height: 38px;
  199. float: right;
  200. img {
  201. cursor: pointer;
  202. vertical-align: middle;
  203. }
  204. }
  205. .el-dropdown {
  206. color: #ffffff;
  207. }
  208. .el-checkbox {
  209. color: #ffffff;
  210. }
  211. .el-login-footer {
  212. height: 40px;
  213. line-height: 40px;
  214. position: fixed;
  215. bottom: 0;
  216. width: 100%;
  217. text-align: center;
  218. color: #fff;
  219. font-family: Arial;
  220. font-size: 12px;
  221. letter-spacing: 1px;
  222. }
  223. .login-code-img {
  224. height: 38px;
  225. }
  226. </style>
  227. <style scoped>
  228. .el-button--primary {
  229. color: #FFFFFF;
  230. background-color: #40a9ff;
  231. border-color: #40a9ff;
  232. }
  233. .el-button:hover, .el-button:focus {
  234. border-color: #6abfff;
  235. background-color: #6abfff;
  236. }
  237. </style>