socialLogin.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // 社交登录相关
  70. this.type = this.$route.query.type;
  71. this.code = this.$route.query.code;
  72. this.state = this.$route.query.state;
  73. this.$store.dispatch("SocialLogin", {
  74. code: this.code,
  75. state: this.state,
  76. type: this.type
  77. }).then(() => {
  78. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  79. }).catch(() => {
  80. this.loading = false;
  81. });
  82. },
  83. methods: {
  84. getCookie() {
  85. const username = Cookies.get("username");
  86. const password = Cookies.get("password");
  87. const rememberMe = Cookies.get('rememberMe')
  88. this.loginForm = {
  89. username: username === undefined ? this.loginForm.username : username,
  90. password: password === undefined ? this.loginForm.password : decrypt(password),
  91. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  92. };
  93. },
  94. handleLogin() {
  95. this.$refs.loginForm.validate(valid => {
  96. if (valid) {
  97. this.loading = true;
  98. if (this.loginForm.rememberMe) {
  99. Cookies.set("username", this.loginForm.username, { expires: 30 });
  100. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  101. } else {
  102. Cookies.remove("username");
  103. Cookies.remove("password");
  104. }
  105. this.$store.dispatch("SocialLogin2", {
  106. code: this.code,
  107. state: this.state,
  108. type: this.type,
  109. username: this.loginForm.username,
  110. password: this.loginForm.password
  111. }).then(() => {
  112. this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
  113. }).catch(() => {
  114. this.loading = false;
  115. });
  116. }
  117. });
  118. }
  119. }
  120. };
  121. </script>
  122. <style rel="stylesheet/scss" lang="scss">
  123. .login {
  124. display: flex;
  125. justify-content: center;
  126. align-items: center;
  127. height: 100%;
  128. background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  129. background-size: cover;
  130. }
  131. .title {
  132. margin: 0px auto 30px auto;
  133. text-align: center;
  134. color: #707070;
  135. }
  136. .login-form {
  137. border-radius: 6px;
  138. background: #ffffff;
  139. width: 400px;
  140. padding: 25px 25px 5px 25px;
  141. .el-input {
  142. height: 38px;
  143. input {
  144. height: 38px;
  145. }
  146. }
  147. .input-icon {
  148. height: 39px;
  149. width: 14px;
  150. margin-left: 2px;
  151. }
  152. }
  153. .login-tip {
  154. font-size: 13px;
  155. text-align: center;
  156. color: #bfbfbf;
  157. }
  158. .login-code {
  159. width: 33%;
  160. height: 38px;
  161. float: right;
  162. img {
  163. cursor: pointer;
  164. vertical-align: middle;
  165. }
  166. }
  167. .el-login-footer {
  168. height: 40px;
  169. line-height: 40px;
  170. position: fixed;
  171. bottom: 0;
  172. width: 100%;
  173. text-align: center;
  174. color: #fff;
  175. font-family: Arial;
  176. font-size: 12px;
  177. letter-spacing: 1px;
  178. }
  179. .login-code-img {
  180. height: 38px;
  181. }
  182. </style>