login.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <h3 class="title">芋道后台管理系统</h3>
  5. <el-form-item prop="username">
  6. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  7. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  8. </el-input>
  9. </el-form-item>
  10. <el-form-item prop="password">
  11. <el-input
  12. v-model="loginForm.password"
  13. type="password"
  14. auto-complete="off"
  15. placeholder="密码"
  16. @keyup.enter.native="handleLogin"
  17. >
  18. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  19. </el-input>
  20. </el-form-item>
  21. <el-form-item prop="code">
  22. <el-input
  23. v-model="loginForm.code"
  24. auto-complete="off"
  25. placeholder="验证码"
  26. style="width: 63%"
  27. @keyup.enter.native="handleLogin"
  28. >
  29. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  30. </el-input>
  31. <div class="login-code">
  32. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  33. </div>
  34. </el-form-item>
  35. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  36. <el-form-item style="width:100%;">
  37. <el-button
  38. :loading="loading"
  39. size="medium"
  40. type="primary"
  41. style="width:100%;"
  42. @click.native.prevent="handleLogin"
  43. >
  44. <span v-if="!loading">登 录</span>
  45. <span v-else>登 录 中...</span>
  46. </el-button>
  47. </el-form-item>
  48. <el-form-item style="width:100%;">
  49. <div class="oauth-login" style="display:flex">
  50. <div class="oauth-login-item" v-for="item in oauthProviders" :key="item.code" @click="doAuth2Login(item)">
  51. <img :src=item.img height="25px" width="25px" alt="登录" >
  52. <span>{{item.title}}</span>
  53. </div>
  54. </div>
  55. </el-form-item>
  56. </el-form>
  57. <!-- 底部 -->
  58. <div class="el-login-footer">
  59. <span>Copyright © 2020-2021 iocoder.cn All Rights Reserved.</span>
  60. </div>
  61. </div>
  62. </template>
  63. <script>
  64. import { getCodeImg,oAuthLogin } from "@/api/login";
  65. import Cookies from "js-cookie";
  66. import { encrypt, decrypt } from '@/utils/jsencrypt'
  67. export default {
  68. name: "Login",
  69. data() {
  70. return {
  71. codeUrl: "",
  72. cookiePassword: "",
  73. loginForm: {
  74. username: "admin",
  75. password: "admin123",
  76. rememberMe: false,
  77. code: "",
  78. uuid: ""
  79. },
  80. oauthProviders: [
  81. {
  82. img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/gitee.png",
  83. title: "码云",
  84. code: "gitee"
  85. },
  86. {
  87. img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/wechat.png",
  88. title: "微信",
  89. code: "weixin"
  90. },
  91. {
  92. img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/qq.png",
  93. title: "QQ",
  94. code: "qq"
  95. }
  96. ],
  97. loginRules: {
  98. username: [
  99. { required: true, trigger: "blur", message: "用户名不能为空" }
  100. ],
  101. password: [
  102. { required: true, trigger: "blur", message: "密码不能为空" }
  103. ],
  104. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  105. },
  106. loading: false,
  107. redirect: undefined
  108. };
  109. },
  110. watch: {
  111. $route: {
  112. handler: function(route) {
  113. this.redirect = route.query && route.query.redirect;
  114. },
  115. immediate: true
  116. }
  117. },
  118. created() {
  119. this.getCode();
  120. this.getCookie();
  121. },
  122. methods: {
  123. getCode() {
  124. getCodeImg().then(res => {
  125. res = res.data;
  126. this.codeUrl = "data:image/gif;base64," + res.img;
  127. this.loginForm.uuid = res.uuid;
  128. });
  129. },
  130. getCookie() {
  131. const username = Cookies.get("username");
  132. const password = Cookies.get("password");
  133. const rememberMe = Cookies.get('rememberMe')
  134. this.loginForm = {
  135. username: username === undefined ? this.loginForm.username : username,
  136. password: password === undefined ? this.loginForm.password : decrypt(password),
  137. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  138. };
  139. },
  140. handleLogin() {
  141. this.$refs.loginForm.validate(valid => {
  142. if (valid) {
  143. this.loading = true;
  144. if (this.loginForm.rememberMe) {
  145. Cookies.set("username", this.loginForm.username, { expires: 30 });
  146. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  147. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  148. } else {
  149. Cookies.remove("username");
  150. Cookies.remove("password");
  151. Cookies.remove('rememberMe');
  152. }
  153. this.$store.dispatch("Login", this.loginForm).then(() => {
  154. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  155. }).catch(() => {
  156. this.loading = false;
  157. this.getCode();
  158. });
  159. }
  160. });
  161. },
  162. doAuth2Login(provider) {
  163. console.log("开始Oauth登录...%o", provider.code);
  164. this.loading = true;
  165. oAuthLogin(provider.code).then((res) => {
  166. console.log(res.url);
  167. window.location.href = res.url;
  168. });
  169. }
  170. }
  171. };
  172. </script>
  173. <style rel="stylesheet/scss" lang="scss">
  174. .login {
  175. display: flex;
  176. justify-content: center;
  177. align-items: center;
  178. height: 100%;
  179. background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  180. background-size: cover;
  181. }
  182. .title {
  183. margin: 0px auto 30px auto;
  184. text-align: center;
  185. color: #707070;
  186. }
  187. .login-form {
  188. border-radius: 6px;
  189. background: #ffffff;
  190. width: 400px;
  191. padding: 25px 25px 5px 25px;
  192. .el-input {
  193. height: 38px;
  194. input {
  195. height: 38px;
  196. }
  197. }
  198. .input-icon {
  199. height: 39px;
  200. width: 14px;
  201. margin-left: 2px;
  202. }
  203. }
  204. .login-tip {
  205. font-size: 13px;
  206. text-align: center;
  207. color: #bfbfbf;
  208. }
  209. .login-code {
  210. width: 33%;
  211. height: 38px;
  212. float: right;
  213. img {
  214. cursor: pointer;
  215. vertical-align: middle;
  216. }
  217. }
  218. .el-login-footer {
  219. height: 40px;
  220. line-height: 40px;
  221. position: fixed;
  222. bottom: 0;
  223. width: 100%;
  224. text-align: center;
  225. color: #fff;
  226. font-family: Arial;
  227. font-size: 12px;
  228. letter-spacing: 1px;
  229. }
  230. .login-code-img {
  231. height: 38px;
  232. }
  233. .oauth-login {
  234. display: flex;
  235. cursor:pointer;
  236. }
  237. .oauth-login-item {
  238. display: flex;
  239. align-items: center;
  240. margin-right: 10px;
  241. }
  242. .oauth-login-item img {
  243. height: 25px;
  244. width: 25px;
  245. }
  246. .oauth-login-item span:hover {
  247. text-decoration: underline red;
  248. color: red;
  249. }
  250. </style>