login.vue 8.3 KB

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