login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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="tenantName" v-if="tenantEnable">
  6. <el-input v-model="loginForm.tenantName" type="text" auto-complete="off" placeholder='租户'>
  7. <svg-icon slot="prefix" icon-class="tree" class="el-input__icon input-icon" />
  8. </el-input>
  9. </el-form-item>
  10. <el-form-item prop="username">
  11. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  12. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
  17. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  18. </el-input>
  19. </el-form-item>
  20. <el-form-item prop="code" v-if="captchaEnable">
  21. <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
  22. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  23. </el-input>
  24. <div class="login-code">
  25. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  26. </div>
  27. </el-form-item>
  28. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  29. <el-form-item style="width:100%;">
  30. <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
  31. <span v-if="!loading">登 录</span>
  32. <span v-else>登 录 中...</span>
  33. </el-button>
  34. </el-form-item>
  35. <el-form-item style="width:100%;">
  36. <div class="oauth-login" style="display:flex">
  37. <div class="oauth-login-item" v-for="item in SysUserSocialTypeEnum" :key="item.type" @click="doSocialLogin(item)">
  38. <img :src="item.img" height="25px" width="25px" alt="登录" >
  39. <span>{{item.title}}</span>
  40. </div>
  41. </div>
  42. </el-form-item>
  43. </el-form>
  44. <!-- 底部 -->
  45. <div class="el-login-footer">
  46. <span>Copyright © 2020-2021 iocoder.cn All Rights Reserved.</span>
  47. </div>
  48. </div>
  49. </template>
  50. <script>
  51. import { getCodeImg,socialAuthRedirect } from "@/api/login";
  52. import { getTenantIdByName } from "@/api/system/tenant";
  53. import Cookies from "js-cookie";
  54. import { encrypt, decrypt } from '@/utils/jsencrypt'
  55. import {SystemUserSocialTypeEnum} from "@/utils/constants";
  56. import { getTenantEnable } from "@/utils/ruoyi";
  57. export default {
  58. name: "Login",
  59. data() {
  60. return {
  61. codeUrl: "",
  62. captchaEnable: true,
  63. tenantEnable: true,
  64. loginForm: {
  65. username: "admin",
  66. password: "admin123",
  67. rememberMe: false,
  68. code: "",
  69. uuid: "",
  70. tenantName: "芋道源码",
  71. },
  72. loginRules: {
  73. username: [
  74. { required: true, trigger: "blur", message: "用户名不能为空" }
  75. ],
  76. password: [
  77. { required: true, trigger: "blur", message: "密码不能为空" }
  78. ],
  79. code: [{ required: true, trigger: "change", message: "验证码不能为空" }],
  80. tenantName: [
  81. { required: true, trigger: "blur", message: "租户不能为空" },
  82. {
  83. validator: (rule, value, callback) => {
  84. getTenantIdByName(value).then(res => {
  85. const tenantId = res.data;
  86. if (tenantId && tenantId >= 0) {
  87. // 设置租户
  88. Cookies.set("tenantId", tenantId);
  89. callback();
  90. } else {
  91. callback('租户不存在');
  92. }
  93. });
  94. },
  95. trigger: 'blur'
  96. }
  97. ],
  98. },
  99. loading: false,
  100. redirect: undefined,
  101. // 枚举
  102. SysUserSocialTypeEnum: SystemUserSocialTypeEnum,
  103. };
  104. },
  105. // watch: {
  106. // $route: {
  107. // handler: function(route) {
  108. // this.redirect = route.query && route.query.redirect;
  109. // },
  110. // immediate: true
  111. // }
  112. // },
  113. created() {
  114. // 租户开关
  115. this.tenantEnable = getTenantEnable();
  116. // 重定向地址
  117. this.redirect = this.$route.query.redirect;
  118. this.getCode();
  119. this.getCookie();
  120. },
  121. methods: {
  122. getCode() {
  123. // 只有开启的状态,才加载验证码。默认开启
  124. if (!this.captchaEnable) {
  125. return;
  126. }
  127. // 请求远程,获得验证码
  128. getCodeImg().then(res => {
  129. res = res.data;
  130. this.captchaEnable = res.enable;
  131. if (this.captchaEnable) {
  132. this.codeUrl = "data:image/gif;base64," + res.img;
  133. this.loginForm.uuid = res.uuid;
  134. }
  135. });
  136. },
  137. getCookie() {
  138. const username = Cookies.get("username");
  139. const password = Cookies.get("password");
  140. const rememberMe = Cookies.get('rememberMe')
  141. const tenantName = Cookies.get('tenantName');
  142. this.loginForm = {
  143. username: username === undefined ? this.loginForm.username : username,
  144. password: password === undefined ? this.loginForm.password : decrypt(password),
  145. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  146. tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName,
  147. };
  148. },
  149. handleLogin() {
  150. this.$refs.loginForm.validate(valid => {
  151. if (valid) {
  152. this.loading = true;
  153. // 设置 Cookie
  154. if (this.loginForm.rememberMe) {
  155. Cookies.set("username", this.loginForm.username, { expires: 30 });
  156. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  157. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  158. Cookies.set('tenantName', this.loginForm.tenantName, { expires: 30 });
  159. } else {
  160. Cookies.remove("username");
  161. Cookies.remove("password");
  162. Cookies.remove('rememberMe');
  163. Cookies.remove('tenantName');
  164. }
  165. // 发起登陆
  166. this.$store.dispatch("Login", this.loginForm).then(() => {
  167. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  168. }).catch(() => {
  169. this.loading = false;
  170. this.getCode();
  171. });
  172. }
  173. });
  174. },
  175. doSocialLogin(socialTypeEnum) {
  176. // 设置登录中
  177. this.loading = true;
  178. // 计算 redirectUri
  179. const redirectUri = location.origin + '/social-login?type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/"); // 重定向不能丢
  180. // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
  181. // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
  182. // 进行跳转
  183. socialAuthRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
  184. // console.log(res.url);
  185. window.location.href = res.data;
  186. });
  187. }
  188. }
  189. };
  190. </script>
  191. <style rel="stylesheet/scss" lang="scss">
  192. .login {
  193. display: flex;
  194. justify-content: center;
  195. align-items: center;
  196. height: 100%;
  197. background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  198. background-size: cover;
  199. }
  200. .title {
  201. margin: 0px auto 30px auto;
  202. text-align: center;
  203. color: #707070;
  204. }
  205. .login-form {
  206. border-radius: 6px;
  207. background: #ffffff;
  208. width: 500px;
  209. padding: 25px 25px 5px 25px;
  210. .el-input {
  211. height: 38px;
  212. input {
  213. height: 38px;
  214. }
  215. }
  216. .input-icon {
  217. height: 39px;
  218. width: 14px;
  219. margin-left: 2px;
  220. }
  221. }
  222. .login-tip {
  223. font-size: 13px;
  224. text-align: center;
  225. color: #bfbfbf;
  226. }
  227. .login-code {
  228. width: 33%;
  229. height: 38px;
  230. float: right;
  231. img {
  232. cursor: pointer;
  233. vertical-align: middle;
  234. }
  235. }
  236. .el-login-footer {
  237. height: 40px;
  238. line-height: 40px;
  239. position: fixed;
  240. bottom: 0;
  241. width: 100%;
  242. text-align: center;
  243. color: #fff;
  244. font-family: Arial;
  245. font-size: 12px;
  246. letter-spacing: 1px;
  247. }
  248. .login-code-img {
  249. height: 38px;
  250. }
  251. .oauth-login {
  252. display: flex;
  253. cursor:pointer;
  254. }
  255. .oauth-login-item {
  256. display: flex;
  257. align-items: center;
  258. margin-right: 10px;
  259. }
  260. .oauth-login-item img {
  261. height: 25px;
  262. width: 25px;
  263. }
  264. .oauth-login-item span:hover {
  265. text-decoration: underline red;
  266. color: red;
  267. }
  268. </style>