login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <div class="container">
  3. <div class="logo"></div>
  4. <!-- 登录区域 -->
  5. <div class="content">
  6. <!-- 配图 -->
  7. <div class="pic"></div>
  8. <!-- 表单 -->
  9. <div class="field">
  10. <!-- [移动端]标题 -->
  11. <h2 class="mobile-title">
  12. <h3 class="title">芋道后台管理系统</h3>
  13. </h2>
  14. <!-- 表单 -->
  15. <div class="form-cont">
  16. <el-tabs class="form" v-model="loginForm.loginType" style=" float:none;">
  17. <el-tab-pane label="账号密码登录" name="uname">
  18. </el-tab-pane>
  19. <el-tab-pane label="短信验证码登录" name="sms">
  20. </el-tab-pane>
  21. </el-tabs>
  22. <div>
  23. <el-form ref="loginForm" :model="loginForm" :rules="LoginRules" class="login-form">
  24. <el-form-item prop="tenantName" v-if="tenantEnable">
  25. <el-input v-model="loginForm.tenantName" type="text" auto-complete="off" placeholder='租户'>
  26. <svg-icon slot="prefix" icon-class="tree" class="el-input__icon input-icon"/>
  27. </el-input>
  28. </el-form-item>
  29. <!-- 账号密码登录 -->
  30. <div v-if="loginForm.loginType === 'uname'">
  31. <el-form-item prop="username">
  32. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  33. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
  34. </el-input>
  35. </el-form-item>
  36. <el-form-item prop="password">
  37. <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
  38. @keyup.enter.native="handleLogin">
  39. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
  40. </el-input>
  41. </el-form-item>
  42. <el-form-item prop="code" v-if="captchaEnable">
  43. <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
  44. @keyup.enter.native="handleLogin">
  45. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
  46. </el-input>
  47. <div class="login-code">
  48. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  49. </div>
  50. </el-form-item>
  51. <el-checkbox v-model="loginForm.rememberMe" style="margin:0 0 25px 0;">记住密码</el-checkbox>
  52. </div>
  53. <!-- 短信验证码登录 -->
  54. <div v-if="loginForm.loginType === 'sms'">
  55. <el-form-item prop="mobile">
  56. <el-input v-model="loginForm.mobile" type="text" auto-complete="off" placeholder="请输入手机号">
  57. <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon"/>
  58. </el-input>
  59. </el-form-item>
  60. <el-form-item prop="mobileCode">
  61. <el-input v-model="loginForm.mobileCode" type="text" auto-complete="off" placeholder="短信验证码"
  62. @keyup.enter.native="handleLogin">
  63. <template slot="icon">
  64. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
  65. </template>
  66. <template slot="append">
  67. <span v-if="mobileCodeTimer <= 0" class="getMobileCode" @click="getSmsCode" style="cursor: pointer;">获取验证码</span>
  68. <span v-if="mobileCodeTimer > 0" class="getMobileCode">{{ mobileCodeTimer }}秒后可重新获取</span>
  69. </template>
  70. </el-input>
  71. </el-form-item>
  72. </div>
  73. <!-- 下方的登录按钮 -->
  74. <el-form-item style="width:100%;">
  75. <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
  76. @click.native.prevent="handleLogin">
  77. <span v-if="!loading">登 录</span>
  78. <span v-else>登 录 中...</span>
  79. </el-button>
  80. </el-form-item>
  81. <!-- 社交登录 -->
  82. <el-form-item style="width:100%;">
  83. <div class="oauth-login" style="display:flex">
  84. <div class="oauth-login-item" v-for="item in SysUserSocialTypeEnum" :key="item.type" @click="doSocialLogin(item)">
  85. <img :src="item.img" height="25px" width="25px" alt="登录" >
  86. <span>{{item.title}}</span>
  87. </div>
  88. </div>
  89. </el-form-item>
  90. </el-form>
  91. </div>
  92. </div>
  93. </div>
  94. </div>
  95. <!-- footer -->
  96. <div class="footer">
  97. Copyright © 2020-2022 iocoder.cn All Rights Reserved.
  98. </div>
  99. </div>
  100. </template>
  101. <script>
  102. import {getCodeImg, sendSmsCode, socialAuthRedirect} from "@/api/login";
  103. import {getTenantIdByName} from "@/api/system/tenant";
  104. import Cookies from "js-cookie";
  105. import {decrypt, encrypt} from '@/utils/jsencrypt'
  106. import {SystemUserSocialTypeEnum} from "@/utils/constants";
  107. import {getTenantEnable} from "@/utils/ruoyi";
  108. export default {
  109. name: "Login",
  110. data() {
  111. return {
  112. codeUrl: "",
  113. captchaEnable: true,
  114. tenantEnable: true,
  115. mobileCodeTimer: 0,
  116. loginForm: {
  117. loginType: "uname",
  118. username: "admin",
  119. password: "admin123",
  120. mobile: "",
  121. mobileCode: "",
  122. rememberMe: false,
  123. code: "",
  124. uuid: "",
  125. tenantName: "芋道源码",
  126. },
  127. scene: 21,
  128. LoginRules: {
  129. username: [
  130. {required: true, trigger: "blur", message: "用户名不能为空"}
  131. ],
  132. password: [
  133. {required: true, trigger: "blur", message: "密码不能为空"}
  134. ],
  135. code: [{required: true, trigger: "change", message: "验证码不能为空"}],
  136. mobile: [
  137. {required: true, trigger: "blur", message: "手机号不能为空"},
  138. {
  139. validator: function (rule, value, callback) {
  140. if (/^1[0-9]\d{9}$/.test(value) == false) {
  141. callback(new Error("手机号格式错误"));
  142. } else {
  143. callback();
  144. }
  145. }, trigger: "blur"
  146. }
  147. ],
  148. tenantName: [
  149. {required: true, trigger: "blur", message: "租户不能为空"},
  150. {
  151. validator: (rule, value, callback) => {
  152. // debugger
  153. getTenantIdByName(value).then(res => {
  154. const tenantId = res.data;
  155. if (tenantId && tenantId >= 0) {
  156. // 设置租户
  157. Cookies.set("tenantId", tenantId);
  158. callback();
  159. } else {
  160. callback('租户不存在');
  161. }
  162. });
  163. },
  164. trigger: 'blur'
  165. }
  166. ]
  167. },
  168. loading: false,
  169. redirect: undefined,
  170. // 枚举
  171. SysUserSocialTypeEnum: SystemUserSocialTypeEnum,
  172. };
  173. },
  174. // watch: {
  175. // $route: {
  176. // handler: function(route) {
  177. // this.redirect = route.query && route.query.redirect;
  178. // },
  179. // immediate: true
  180. // }
  181. // },
  182. created() {
  183. // 租户开关
  184. this.tenantEnable = getTenantEnable();
  185. // 重定向地址
  186. this.redirect = this.$route.query.redirect;
  187. this.getCode();
  188. this.getCookie();
  189. },
  190. methods: {
  191. getCode() {
  192. // 只有开启的状态,才加载验证码。默认开启
  193. if (!this.captchaEnable) {
  194. return;
  195. }
  196. // 请求远程,获得验证码
  197. getCodeImg().then(res => {
  198. res = res.data;
  199. this.captchaEnable = res.enable;
  200. if (this.captchaEnable) {
  201. this.codeUrl = "data:image/gif;base64," + res.img;
  202. this.loginForm.uuid = res.uuid;
  203. }
  204. });
  205. },
  206. getCookie() {
  207. const username = Cookies.get("username");
  208. const password = Cookies.get("password");
  209. const rememberMe = Cookies.get('rememberMe')
  210. const tenantName = Cookies.get('tenantName');
  211. const mobile = Cookies.get('mobile');
  212. const mobileCode = Cookies.get('mobileCode');
  213. const loginType = Cookies.get('loginType');
  214. this.loginForm = {
  215. username: username === undefined ? this.loginForm.username : username,
  216. password: password === undefined ? this.loginForm.password : decrypt(password),
  217. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  218. tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName,
  219. mobile: mobile === undefined ? this.loginForm.mobile : mobile,
  220. mobileCode: mobileCode === undefined ? this.loginForm.mobileCode : mobileCode,
  221. loginType: loginType === undefined ? this.loginForm.loginType : loginType,
  222. };
  223. },
  224. handleLogin() {
  225. this.$refs.loginForm.validate(valid => {
  226. if (valid) {
  227. this.loading = true;
  228. // 设置 Cookie
  229. if (this.loginForm.rememberMe) {
  230. Cookies.set("username", this.loginForm.username, {expires: 30});
  231. Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
  232. Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
  233. Cookies.set('tenantName', this.loginForm.tenantName, {expires: 30});
  234. } else {
  235. Cookies.remove("username");
  236. Cookies.remove("password");
  237. Cookies.remove('rememberMe');
  238. Cookies.remove('tenantName');
  239. }
  240. // 发起登陆
  241. console.log("发起登录", this.loginForm);
  242. this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => {
  243. this.$router.push({path: this.redirect || "/"}).catch(() => {
  244. });
  245. }).catch(() => {
  246. this.loading = false;
  247. this.getCode();
  248. });
  249. }
  250. });
  251. },
  252. doSocialLogin(socialTypeEnum) {
  253. // 设置登录中
  254. this.loading = true;
  255. // 计算 redirectUri
  256. const redirectUri = location.origin + '/social-login?type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/"); // 重定向不能丢
  257. // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
  258. // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
  259. // 进行跳转
  260. socialAuthRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
  261. // console.log(res.url);
  262. window.location.href = res.data;
  263. });
  264. },
  265. /** ========== 以下为升级短信登录 ========== */
  266. getSmsCode() {
  267. if (this.mobileCodeTimer > 0) return;
  268. this.$refs.loginForm.validate(valid => {
  269. if (!valid) return;
  270. sendSmsCode(this.loginForm.mobile, this.scene, this.loginForm.uuid, this.loginForm.code).then(res => {
  271. this.$modal.msgSuccess("获取验证码成功")
  272. this.mobileCodeTimer = 60;
  273. let msgTimer = setInterval(() => {
  274. this.mobileCodeTimer = this.mobileCodeTimer - 1;
  275. if (this.mobileCodeTimer <= 0) {
  276. clearInterval(msgTimer);
  277. }
  278. }, 1000);
  279. });
  280. });
  281. }
  282. }
  283. };
  284. </script>
  285. <style lang="scss" scoped>
  286. @import "~@/assets/styles/login.scss";
  287. .oauth-login {
  288. display: flex;
  289. align-items: cen;
  290. cursor:pointer;
  291. }
  292. .oauth-login-item {
  293. display: flex;
  294. align-items: center;
  295. margin-right: 10px;
  296. }
  297. .oauth-login-item img {
  298. height: 25px;
  299. width: 25px;
  300. }
  301. .oauth-login-item span:hover {
  302. text-decoration: underline red;
  303. color: red;
  304. }
  305. </style>