login.vue 8.0 KB

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