socialLogin.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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="username">
  6. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  7. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  8. </el-input>
  9. </el-form-item>
  10. <el-form-item prop="password">
  11. <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
  12. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item style="width:100%;">
  16. <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
  17. <span v-if="!loading">提 交</span>
  18. <span v-else>提 交 中...</span>
  19. </el-button>
  20. </el-form-item>
  21. </el-form>
  22. <!-- 底部 -->
  23. <div class="el-login-footer">
  24. <span>Copyright © 2020-2021 iocoder.cn All Rights Reserved.</span>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import { socialLogin } from "@/api/login";
  30. import Cookies from "js-cookie";
  31. import { encrypt, decrypt } from '@/utils/jsencrypt'
  32. export default {
  33. name: "ThirdLogin",
  34. data() {
  35. return {
  36. loginForm: {
  37. username: "admin",
  38. password: "admin123",
  39. rememberMe: false, // TODO 芋艿:后面看情况,去掉这块
  40. },
  41. loginRules: {
  42. username: [
  43. { required: true, trigger: "blur", message: "用户名不能为空" }
  44. ],
  45. password: [
  46. { required: true, trigger: "blur", message: "密码不能为空" }
  47. ],
  48. },
  49. loading: false,
  50. redirect: undefined,
  51. // 社交登陆相关
  52. type: undefined,
  53. code: undefined,
  54. state: undefined,
  55. };
  56. },
  57. // watch: {
  58. // $route: {
  59. // handler: function(route) {
  60. // this.redirect = route.query && route.query.redirect;
  61. // },
  62. // immediate: true
  63. // }
  64. // },
  65. created() {
  66. this.getCookie();
  67. // 重定向地址
  68. this.redirect = this.$route.query.redirect;
  69. debugger
  70. // 社交登陆相关
  71. this.type = this.$route.query.type;
  72. this.code = this.$route.query.code;
  73. this.state = this.$route.query.state;
  74. this.$store.dispatch("SocialLogin", {
  75. code: this.code,
  76. state: this.state,
  77. type: this.type
  78. }).then(() => {
  79. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  80. }).catch(() => {
  81. this.loading = false;
  82. });
  83. },
  84. methods: {
  85. getCookie() {
  86. const username = Cookies.get("username");
  87. const password = Cookies.get("password");
  88. const rememberMe = Cookies.get('rememberMe')
  89. this.loginForm = {
  90. username: username === undefined ? this.loginForm.username : username,
  91. password: password === undefined ? this.loginForm.password : decrypt(password),
  92. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  93. };
  94. },
  95. handleLogin() {
  96. this.$refs.loginForm.validate(valid => {
  97. if (valid) {
  98. this.loading = true;
  99. if (this.loginForm.rememberMe) {
  100. Cookies.set("username", this.loginForm.username, { expires: 30 });
  101. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  102. } else {
  103. Cookies.remove("username");
  104. Cookies.remove("password");
  105. }
  106. this.$store.dispatch("SocialLogin2", {
  107. code: this.code,
  108. state: this.state,
  109. type: this.type,
  110. username: this.loginForm.username,
  111. password: this.loginForm.password
  112. }).then(() => {
  113. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  114. }).catch(() => {
  115. this.loading = false;
  116. });
  117. }
  118. });
  119. }
  120. }
  121. };
  122. </script>
  123. <style rel="stylesheet/scss" lang="scss">
  124. .login {
  125. display: flex;
  126. justify-content: center;
  127. align-items: center;
  128. height: 100%;
  129. background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  130. background-size: cover;
  131. }
  132. .title {
  133. margin: 0px auto 30px auto;
  134. text-align: center;
  135. color: #707070;
  136. }
  137. .login-form {
  138. border-radius: 6px;
  139. background: #ffffff;
  140. width: 400px;
  141. padding: 25px 25px 5px 25px;
  142. .el-input {
  143. height: 38px;
  144. input {
  145. height: 38px;
  146. }
  147. }
  148. .input-icon {
  149. height: 39px;
  150. width: 14px;
  151. margin-left: 2px;
  152. }
  153. }
  154. .login-tip {
  155. font-size: 13px;
  156. text-align: center;
  157. color: #bfbfbf;
  158. }
  159. .login-code {
  160. width: 33%;
  161. height: 38px;
  162. float: right;
  163. img {
  164. cursor: pointer;
  165. vertical-align: middle;
  166. }
  167. }
  168. .el-login-footer {
  169. height: 40px;
  170. line-height: 40px;
  171. position: fixed;
  172. bottom: 0;
  173. width: 100%;
  174. text-align: center;
  175. color: #fff;
  176. font-family: Arial;
  177. font-size: 12px;
  178. letter-spacing: 1px;
  179. }
  180. .login-code-img {
  181. height: 38px;
  182. }
  183. </style>