adminLogin.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <div class="login">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. >
  9. <h3 class="title">配网应用管理平台</h3>
  10. <el-form-item prop="username">
  11. <el-input
  12. v-model="loginForm.username"
  13. type="text"
  14. auto-complete="off"
  15. placeholder="账号"
  16. >
  17. <svg-icon
  18. slot="prefix"
  19. icon-class="user"
  20. class="el-input__icon input-icon"
  21. />
  22. </el-input>
  23. </el-form-item>
  24. <el-form-item prop="password">
  25. <el-input
  26. v-model="loginForm.password"
  27. type="password"
  28. auto-complete="off"
  29. placeholder="密码"
  30. @keyup.enter.native="handleLogin"
  31. >
  32. <svg-icon
  33. slot="prefix"
  34. icon-class="password"
  35. class="el-input__icon input-icon"
  36. />
  37. </el-input>
  38. </el-form-item>
  39. <el-form-item prop="code" v-if="captchaEnabled">
  40. <el-input
  41. v-model="loginForm.code"
  42. auto-complete="off"
  43. placeholder="验证码"
  44. style="width: 63%"
  45. @keyup.enter.native="handleLogin"
  46. >
  47. <svg-icon
  48. slot="prefix"
  49. icon-class="validCode"
  50. class="el-input__icon input-icon"
  51. />
  52. </el-input>
  53. <div class="login-code">
  54. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  55. </div>
  56. </el-form-item>
  57. <el-checkbox
  58. v-model="loginForm.rememberMe"
  59. style="margin: 0px 0px 25px 0px"
  60. >记住密码</el-checkbox
  61. >
  62. <el-form-item style="width: 100%">
  63. <!-- :loading="loading" -->
  64. <el-button
  65. size="medium"
  66. type="primary"
  67. style="width: 100%"
  68. @click.native.prevent="handleLogin"
  69. >
  70. <span v-if="!loading">登 录</span>
  71. <span v-else>登 录 中...</span>
  72. </el-button>
  73. <div style="float: right" v-if="register">
  74. <router-link class="link-type" :to="'/register'"
  75. >立即注册</router-link
  76. >
  77. </div>
  78. </el-form-item>
  79. </el-form>
  80. <!-- 底部 -->
  81. <!-- <div class="el-login-footer">
  82. <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
  83. </div> -->
  84. </div>
  85. </template>
  86. <script>
  87. import { getCodeImg, getPublicKey } from "@/api/login";
  88. import Cookies from "js-cookie";
  89. import { encrypt, decrypt } from "@/utils/jsencrypt";
  90. export default {
  91. name: "Login",
  92. data() {
  93. return {
  94. codeUrl: "",
  95. loginForm: {
  96. username: "",
  97. password: "",
  98. rememberMe: false,
  99. code: "",
  100. uuid: "",
  101. },
  102. loginRules: {
  103. username: [
  104. { required: true, trigger: "blur", message: "请输入您的账号" },
  105. ],
  106. password: [
  107. { required: true, trigger: "blur", message: "请输入您的密码" },
  108. ],
  109. code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  110. },
  111. loading: false,
  112. // 验证码开关
  113. captchaEnabled: true,
  114. // 注册开关
  115. register: false,
  116. redirect: undefined,
  117. };
  118. },
  119. watch: {
  120. $route: {
  121. handler: function (route) {
  122. this.redirect = route.query && route.query.redirect;
  123. },
  124. immediate: true,
  125. },
  126. },
  127. created() {
  128. this.getCode();
  129. this.getCookie();
  130. this.setDevLogin();
  131. },
  132. methods: {
  133. getPublicKey() {
  134. return new Promise((resolve, reject) => {
  135. getPublicKey()
  136. .then((res) => {
  137. resolve(res);
  138. })
  139. .catch((error) => {
  140. reject(error);
  141. });
  142. });
  143. },
  144. getCode() {
  145. getCodeImg().then((res) => {
  146. this.captchaEnabled =
  147. res.captchaEnabled === undefined ? true : res.captchaEnabled;
  148. if (this.captchaEnabled) {
  149. this.codeUrl = "data:image/gif;base64," + res.img;
  150. this.loginForm.uuid = res.uuid;
  151. }
  152. });
  153. },
  154. getCookie() {
  155. const username = Cookies.get("username");
  156. const password = Cookies.get("password");
  157. const rememberMe = Cookies.get("rememberMe");
  158. this.loginForm = {
  159. username: username === undefined ? this.loginForm.username : username,
  160. password:
  161. password === undefined ? this.loginForm.password : decrypt(password),
  162. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  163. };
  164. },
  165. handleLogin() {
  166. this.$refs.loginForm.validate((valid) => {
  167. if (valid) {
  168. this.loading = true;
  169. if (this.loginForm.rememberMe) {
  170. Cookies.set("username", this.loginForm.username, { expires: 30 });
  171. Cookies.set("password", encrypt(this.loginForm.password), {
  172. expires: 30,
  173. });
  174. Cookies.set("rememberMe", this.loginForm.rememberMe, {
  175. expires: 30,
  176. });
  177. } else {
  178. Cookies.remove("username");
  179. Cookies.remove("password");
  180. Cookies.remove("rememberMe");
  181. }
  182. this.getPublicKey().then((res) => {
  183. let publicKey = res.publicKey;
  184. this.loginForm.password = encrypt(
  185. this.loginForm.password,
  186. publicKey
  187. );
  188. this.$store
  189. .dispatch("Login", this.loginForm)
  190. .then(() => {
  191. this.$router
  192. .push({ path: this.redirect || "/" })
  193. .catch((error) => {});
  194. })
  195. .catch(() => {
  196. this.loginForm.password = null;
  197. this.loading = false;
  198. if (this.captchaEnabled) {
  199. this.getCode();
  200. }
  201. });
  202. });
  203. }
  204. });
  205. },
  206. setDevLogin() {
  207. if (process.env.NODE_ENV === "development") {
  208. this.loginForm.username = "admin";
  209. this.loginForm.password = "admin123";
  210. }
  211. },
  212. },
  213. };
  214. </script>
  215. <style rel="stylesheet/scss" lang="scss">
  216. .login {
  217. display: flex;
  218. justify-content: center;
  219. align-items: center;
  220. height: 100%;
  221. background-image: url("../assets/images/login-background.jpg");
  222. background-size: cover;
  223. }
  224. .title {
  225. margin: 0px auto 30px auto;
  226. text-align: center;
  227. color: #707070;
  228. }
  229. .login-form {
  230. border-radius: 6px;
  231. background: #ffffff;
  232. width: 400px;
  233. padding: 25px 25px 5px 25px;
  234. .el-input {
  235. height: 38px;
  236. input {
  237. height: 38px;
  238. }
  239. }
  240. .input-icon {
  241. height: 39px;
  242. width: 14px;
  243. margin-left: 2px;
  244. }
  245. }
  246. .login-tip {
  247. font-size: 13px;
  248. text-align: center;
  249. color: #bfbfbf;
  250. }
  251. .login-code {
  252. width: 33%;
  253. height: 38px;
  254. float: right;
  255. img {
  256. cursor: pointer;
  257. vertical-align: middle;
  258. }
  259. }
  260. .el-login-footer {
  261. height: 40px;
  262. line-height: 40px;
  263. position: fixed;
  264. bottom: 0;
  265. width: 100%;
  266. text-align: center;
  267. color: #fff;
  268. font-family: Arial;
  269. font-size: 12px;
  270. letter-spacing: 1px;
  271. }
  272. .login-code-img {
  273. height: 38px;
  274. }
  275. </style>