login.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <div class="login" style="">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <img id="switch" class="switch" ref="switch" :src="getImg()" @click="switchLogin()" style="cursor: pointer;width: 60px;height: 60px;position: absolute;right: 0;top: 0;">
  5. <h3 class="title">用户画像管理系统</h3>
  6. <div id="passLogin" v-if="isPassLogin">
  7. <el-form-item prop="username">
  8. <el-input
  9. v-model="loginForm.username"
  10. type="text"
  11. auto-complete="off"
  12. placeholder="账号"
  13. >
  14. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
  15. </el-input>
  16. </el-form-item>
  17. <el-form-item prop="password">
  18. <el-input
  19. v-model="loginForm.password"
  20. type="password"
  21. auto-complete="off"
  22. placeholder="密码"
  23. @keyup.enter.native="handleLogin"
  24. >
  25. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
  26. </el-input>
  27. </el-form-item>
  28. <el-form-item prop="code" v-if="captchaEnabled">
  29. <el-input
  30. v-model="loginForm.code"
  31. auto-complete="off"
  32. placeholder="验证码"
  33. style="width: 63%"
  34. @keyup.enter.native="handleLogin"
  35. >
  36. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
  37. </el-input>
  38. <div class="login-code">
  39. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  40. </div>
  41. </el-form-item>
  42. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  43. <el-form-item style="width:100%;">
  44. <el-button
  45. :loading="loading"
  46. size="medium"
  47. type="primary"
  48. style="width:100%;"
  49. @click.native.prevent="handleLogin"
  50. >
  51. <span v-if="!loading">登 录</span>
  52. <span v-else>登 录 中...</span>
  53. </el-button>
  54. <div style="float: right;" v-if="register">
  55. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  56. </div>
  57. </el-form-item>
  58. </div>
  59. <div id="codeLogin" v-if="!isPassLogin" style="height: 364px">
  60. <iframe id="dingtalk-qrcode" class="qrCode" :src="iframeSrc"
  61. style="display: block;margin: auto;height: 293px;border: 1px solid #cecece;border-radius: 10px">
  62. </iframe>
  63. <p class="text" style="display: flex;align-items: center;justify-content: center;margin-top: 25px;">
  64. <img :src="ddImg">
  65. <span style="margin-left: 8px;">钉钉扫码登录</span>
  66. </p>
  67. </div>
  68. </el-form>
  69. <!-- 底部 -->
  70. <div class="el-login-footer" style="display:none">
  71. <span>Copyright © 2018-2023 xzl.vip All Rights Reserved.</span>
  72. </div>
  73. <iframe id="authFrame" style="display: none"/>
  74. </div>
  75. </template>
  76. <script>
  77. import {getCodeImg} from "@/api/login";
  78. import Cookies from "js-cookie";
  79. import {encrypt, decrypt} from '@/utils/jsencrypt'
  80. export default {
  81. name: "Login",
  82. data() {
  83. return {
  84. codeUrl: "",
  85. loginForm: {
  86. username: "",
  87. password: "",
  88. rememberMe: false,
  89. code: "",
  90. uuid: ""
  91. },
  92. loginRules: {
  93. username: [
  94. {required: true, trigger: "blur", message: "请输入您的账号"}
  95. ],
  96. password: [
  97. {required: true, trigger: "blur", message: "请输入您的密码"}
  98. ],
  99. code: [{required: true, trigger: "change", message: "请输入验证码"}]
  100. },
  101. loading: false,
  102. // 验证码开关
  103. captchaEnabled: true,
  104. // 注册开关
  105. register: false,
  106. redirect: undefined,
  107. img2: require("@/assets/images/i2.png"),
  108. img4: require("@/assets/images/i4.png"),
  109. ddImg: require("@/assets/images/dd.png"),
  110. isPassLogin: true,
  111. dingTalkConfig: {
  112. appid: "dingr2toeanbqkmhitkx",//自己申请的appid
  113. redirectUrl: "http://10.70.192.122:9180/dingtalk",//这里是扫码成功后跳转的回调地址
  114. },
  115. iframeSrc: ""
  116. };
  117. },
  118. watch: {
  119. $route: {
  120. handler: function (route) {
  121. this.redirect = route.query && route.query.redirect;
  122. },
  123. immediate: true
  124. }
  125. },
  126. created() {
  127. this.getCode();
  128. this.getCookie();
  129. this.iframeSrc = this.generateDingTalkQrcode();
  130. this.addDingListener();
  131. },
  132. methods: {
  133. addDingListener() {
  134. let handleLoginTmpCode = (loginTmpCode) => {
  135. console.log('loginTmpCode : ' + loginTmpCode)
  136. window.location.href = this.getAuthUrl() + `&loginTmpCode=${loginTmpCode}`;
  137. };
  138. let handleMessage = (event) => {
  139. if (event.origin === "https://login.dingtalk.com") {
  140. handleLoginTmpCode(event.data);
  141. }
  142. };
  143. if (typeof window.addEventListener != "undefined") {
  144. window.addEventListener("message", handleMessage, false);
  145. } else if (typeof window.attachEvent != "undefined") {
  146. window.attachEvent("onmessage", handleMessage);
  147. }
  148. },
  149. getAuthUrl() {
  150. var redirectUrl = encodeURIComponent(this.dingTalkConfig.redirectUrl)
  151. return 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=' + this.dingTalkConfig.appid + '&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + redirectUrl;
  152. },
  153. generateDingTalkQrcode() {
  154. return 'https://login.dingtalk.com/login/qrcode.htm?goto=' + encodeURIComponent(this.getAuthUrl())
  155. },
  156. switch() {
  157. this.img = require("@/assets/images/i4.png");
  158. this.isPassLogin = !this.isPassLogin;
  159. },
  160. getCode() {
  161. getCodeImg().then(res => {
  162. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
  163. if (this.captchaEnabled) {
  164. this.codeUrl = "data:image/gif;base64," + res.img;
  165. this.loginForm.uuid = res.uuid;
  166. }
  167. });
  168. },
  169. getCookie() {
  170. const username = Cookies.get("username");
  171. const password = Cookies.get("password");
  172. const rememberMe = Cookies.get('rememberMe')
  173. this.loginForm = {
  174. username: username === undefined ? this.loginForm.username : username,
  175. password: password === undefined ? this.loginForm.password : decrypt(password),
  176. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  177. };
  178. },
  179. handleLogin() {
  180. this.$refs.loginForm.validate(valid => {
  181. if (valid) {
  182. this.loading = true;
  183. if (this.loginForm.rememberMe) {
  184. Cookies.set("username", this.loginForm.username, {expires: 30});
  185. Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
  186. Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
  187. } else {
  188. Cookies.remove("username");
  189. Cookies.remove("password");
  190. Cookies.remove('rememberMe');
  191. }
  192. this.$store.dispatch("Login", this.loginForm).then(() => {
  193. this.$router.push({path: this.redirect || "/"}).catch(() => {
  194. });
  195. }).catch(() => {
  196. this.loading = false;
  197. if (this.captchaEnabled) {
  198. this.getCode();
  199. }
  200. });
  201. }
  202. });
  203. },
  204. getImg() {
  205. if(this.isPassLogin) {
  206. return this.img4
  207. }else {
  208. return this.img2
  209. }
  210. },
  211. switchLogin() {
  212. this.isPassLogin = !this.isPassLogin
  213. }
  214. }
  215. };
  216. </script>
  217. <style rel="stylesheet/scss" lang="scss">
  218. .login {
  219. display: flex;
  220. justify-content: center;
  221. align-items: center;
  222. height: 100%;
  223. background-image: url("../assets/images/login-background.jpg");
  224. background-size: cover;
  225. }
  226. .title {
  227. margin: 0px auto 30px auto;
  228. text-align: center;
  229. color: #707070;
  230. }
  231. .login-form {
  232. position: absolute;
  233. border-radius: 6px;
  234. background: #ffffff;
  235. width: 400px;
  236. padding: 25px 25px 5px 25px;
  237. .el-input {
  238. height: 38px;
  239. input {
  240. height: 38px;
  241. }
  242. }
  243. .input-icon {
  244. height: 39px;
  245. width: 14px;
  246. margin-left: 2px;
  247. }
  248. }
  249. .login-tip {
  250. font-size: 13px;
  251. text-align: center;
  252. color: #bfbfbf;
  253. }
  254. .login-code {
  255. width: 33%;
  256. height: 38px;
  257. float: right;
  258. img {
  259. cursor: pointer;
  260. vertical-align: middle;
  261. }
  262. }
  263. .el-login-footer {
  264. height: 40px;
  265. line-height: 40px;
  266. position: fixed;
  267. bottom: 0;
  268. width: 100%;
  269. text-align: center;
  270. color: #fff;
  271. font-family: Arial;
  272. font-size: 12px;
  273. letter-spacing: 1px;
  274. }
  275. .login-code-img {
  276. height: 38px;
  277. }
  278. </style>