login.vue 6.8 KB

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