login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // console.log("开始Oauth登录...%o", socialTypeEnum.code);
  177. // 设置登录中
  178. this.loading = true;
  179. // 计算 redirectUri
  180. const redirectUri = location.origin + '/social-login?type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/"); // 重定向不能丢
  181. // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
  182. // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
  183. // 进行跳转
  184. socialAuthRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
  185. // console.log(res.url);
  186. window.location.href = res.data;
  187. });
  188. }
  189. }
  190. };
  191. </script>
  192. <style rel="stylesheet/scss" lang="scss">
  193. .login {
  194. display: flex;
  195. justify-content: center;
  196. align-items: center;
  197. height: 100%;
  198. background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  199. background-size: cover;
  200. }
  201. .title {
  202. margin: 0px auto 30px auto;
  203. text-align: center;
  204. color: #707070;
  205. }
  206. .login-form {
  207. border-radius: 6px;
  208. background: #ffffff;
  209. width: 500px;
  210. padding: 25px 25px 5px 25px;
  211. .el-input {
  212. height: 38px;
  213. input {
  214. height: 38px;
  215. }
  216. }
  217. .input-icon {
  218. height: 39px;
  219. width: 14px;
  220. margin-left: 2px;
  221. }
  222. }
  223. .login-tip {
  224. font-size: 13px;
  225. text-align: center;
  226. color: #bfbfbf;
  227. }
  228. .login-code {
  229. width: 33%;
  230. height: 38px;
  231. float: right;
  232. img {
  233. cursor: pointer;
  234. vertical-align: middle;
  235. }
  236. }
  237. .el-login-footer {
  238. height: 40px;
  239. line-height: 40px;
  240. position: fixed;
  241. bottom: 0;
  242. width: 100%;
  243. text-align: center;
  244. color: #fff;
  245. font-family: Arial;
  246. font-size: 12px;
  247. letter-spacing: 1px;
  248. }
  249. .login-code-img {
  250. height: 38px;
  251. }
  252. .oauth-login {
  253. display: flex;
  254. cursor:pointer;
  255. }
  256. .oauth-login-item {
  257. display: flex;
  258. align-items: center;
  259. margin-right: 10px;
  260. }
  261. .oauth-login-item img {
  262. height: 25px;
  263. width: 25px;
  264. }
  265. .oauth-login-item span:hover {
  266. text-decoration: underline red;
  267. color: red;
  268. }
  269. </style>