login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // debugger
  85. getTenantIdByName(value).then(res => {
  86. const tenantId = res.data;
  87. if (tenantId >= 0) {
  88. // 设置租户
  89. Cookies.set("tenantId", tenantId);
  90. callback();
  91. } else {
  92. callback('租户不存在');
  93. }
  94. });
  95. },
  96. trigger: 'blur'
  97. }
  98. ],
  99. },
  100. loading: false,
  101. redirect: undefined,
  102. // 枚举
  103. SysUserSocialTypeEnum: SystemUserSocialTypeEnum,
  104. };
  105. },
  106. // watch: {
  107. // $route: {
  108. // handler: function(route) {
  109. // this.redirect = route.query && route.query.redirect;
  110. // },
  111. // immediate: true
  112. // }
  113. // },
  114. created() {
  115. // 租户开关
  116. this.tenantEnable = getTenantEnable();
  117. // 重定向地址
  118. this.redirect = this.$route.query.redirect;
  119. this.getCode();
  120. this.getCookie();
  121. },
  122. methods: {
  123. getCode() {
  124. // 只有开启的状态,才加载验证码。默认开启
  125. if (!this.captchaEnable) {
  126. return;
  127. }
  128. // 请求远程,获得验证码
  129. getCodeImg().then(res => {
  130. res = res.data;
  131. this.captchaEnable = res.enable;
  132. if (this.captchaEnable) {
  133. this.codeUrl = "data:image/gif;base64," + res.img;
  134. this.loginForm.uuid = res.uuid;
  135. }
  136. });
  137. },
  138. getCookie() {
  139. const username = Cookies.get("username");
  140. const password = Cookies.get("password");
  141. const rememberMe = Cookies.get('rememberMe')
  142. const tenantName = Cookies.get('tenantName');
  143. this.loginForm = {
  144. username: username === undefined ? this.loginForm.username : username,
  145. password: password === undefined ? this.loginForm.password : decrypt(password),
  146. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  147. tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName,
  148. };
  149. },
  150. handleLogin() {
  151. this.$refs.loginForm.validate(valid => {
  152. if (valid) {
  153. this.loading = true;
  154. // 设置 Cookie
  155. if (this.loginForm.rememberMe) {
  156. Cookies.set("username", this.loginForm.username, { expires: 30 });
  157. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  158. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  159. Cookies.set('tenantName', this.loginForm.tenantName, { expires: 30 });
  160. } else {
  161. Cookies.remove("username");
  162. Cookies.remove("password");
  163. Cookies.remove('rememberMe');
  164. Cookies.remove('tenantName');
  165. }
  166. // 发起登陆
  167. this.$store.dispatch("Login", this.loginForm).then(() => {
  168. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  169. }).catch(() => {
  170. this.loading = false;
  171. this.getCode();
  172. });
  173. }
  174. });
  175. },
  176. doSocialLogin(socialTypeEnum) {
  177. // console.log("开始Oauth登录...%o", socialTypeEnum.code);
  178. // 设置登录中
  179. this.loading = true;
  180. // 计算 redirectUri
  181. const redirectUri = location.origin + '/social-login?type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/"); // 重定向不能丢
  182. // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
  183. // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
  184. // 进行跳转
  185. socialAuthRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
  186. // console.log(res.url);
  187. window.location.href = res.data;
  188. });
  189. }
  190. }
  191. };
  192. </script>
  193. <style rel="stylesheet/scss" lang="scss">
  194. .login {
  195. display: flex;
  196. justify-content: center;
  197. align-items: center;
  198. height: 100%;
  199. background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  200. background-size: cover;
  201. }
  202. .title {
  203. margin: 0px auto 30px auto;
  204. text-align: center;
  205. color: #707070;
  206. }
  207. .login-form {
  208. border-radius: 6px;
  209. background: #ffffff;
  210. width: 500px;
  211. padding: 25px 25px 5px 25px;
  212. .el-input {
  213. height: 38px;
  214. input {
  215. height: 38px;
  216. }
  217. }
  218. .input-icon {
  219. height: 39px;
  220. width: 14px;
  221. margin-left: 2px;
  222. }
  223. }
  224. .login-tip {
  225. font-size: 13px;
  226. text-align: center;
  227. color: #bfbfbf;
  228. }
  229. .login-code {
  230. width: 33%;
  231. height: 38px;
  232. float: right;
  233. img {
  234. cursor: pointer;
  235. vertical-align: middle;
  236. }
  237. }
  238. .el-login-footer {
  239. height: 40px;
  240. line-height: 40px;
  241. position: fixed;
  242. bottom: 0;
  243. width: 100%;
  244. text-align: center;
  245. color: #fff;
  246. font-family: Arial;
  247. font-size: 12px;
  248. letter-spacing: 1px;
  249. }
  250. .login-code-img {
  251. height: 38px;
  252. }
  253. .oauth-login {
  254. display: flex;
  255. cursor:pointer;
  256. }
  257. .oauth-login-item {
  258. display: flex;
  259. align-items: center;
  260. margin-right: 10px;
  261. }
  262. .oauth-login-item img {
  263. height: 25px;
  264. width: 25px;
  265. }
  266. .oauth-login-item span:hover {
  267. text-decoration: underline red;
  268. color: red;
  269. }
  270. </style>