完整完成钉钉的三方接入

This commit is contained in:
YunaiV
2021-10-02 23:55:41 +08:00
parent e05d90590e
commit 9be00d6035
23 changed files with 401 additions and 94 deletions

View File

@ -39,18 +39,18 @@ export function getCodeImg() {
})
}
// 三方登陆的跳转
export function thirdLoginRedirect(type, redirectUri) {
// 社交登陆的跳转
export function socialLoginRedirect(type, redirectUri) {
return request({
url: '/third-login-redirect?type=' + type + '&redirectUri=' + redirectUri,
url: '/social-login-redirect?type=' + type + '&redirectUri=' + redirectUri,
method: 'get'
})
}
// 三方登陆,使用 code 授权码
export function thirdLogin(type, code, state) {
// 社交登陆,使用 code 授权码
export function socialLogin(type, code, state) {
return request({
url: '/third-login',
url: '/social-login',
method: 'post',
data: {
type,
@ -59,3 +59,18 @@ export function thirdLogin(type, code, state) {
}
})
}
// 社交登陆,使用 code 授权码 + + 账号密码
export function socialLogin2(type, code, state, username, password) {
return request({
url: '/social-login2',
method: 'post',
data: {
type,
code,
state,
username,
password
}
})
}

View File

@ -7,7 +7,7 @@ import { getToken } from '@/utils/auth'
NProgress.configure({ showSpinner: false })
const whiteList = ['/login', '/third-login', '/auth-redirect', '/bind', '/register', '/oauthLogin/gitee']
const whiteList = ['/login', '/social-login', '/auth-redirect', '/bind', '/register', '/oauthLogin/gitee']
router.beforeEach((to, from, next) => {
NProgress.start()

View File

@ -44,8 +44,8 @@ export const constantRoutes = [
hidden: true
},
{
path: '/third-login',
component: (resolve) => require(['@/views/thirdLogin'], resolve),
path: '/social-login',
component: (resolve) => require(['@/views/socialLogin'], resolve),
hidden: true
},
{

View File

@ -1,4 +1,4 @@
import { login, logout, getInfo } from '@/api/login'
import {login, logout, getInfo, socialLogin, socialLogin2} from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
@ -47,6 +47,42 @@ const user = {
})
},
// 社交登陆
SocialLogin({ commit }, userInfo) {
const code = userInfo.code
const state = userInfo.state
const type = userInfo.type
return new Promise((resolve, reject) => {
socialLogin(type, code, state).then(res => {
res = res.data;
setToken(res.token)
commit('SET_TOKEN', res.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
// 社交登陆
SocialLogin2({ commit }, userInfo) {
const code = userInfo.code
const state = userInfo.state
const type = userInfo.type
const username = userInfo.username.trim()
const password = userInfo.password
return new Promise((resolve, reject) => {
socialLogin2(type, code, state, username, password).then(res => {
res = res.data;
setToken(res.token)
commit('SET_TOKEN', res.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
// 获取用户信息
GetInfo({ commit, state }) {
return new Promise((resolve, reject) => {

View File

@ -45,7 +45,7 @@
</template>
<script>
import { getCodeImg,thirdLoginRedirect } from "@/api/login";
import { getCodeImg,socialLoginRedirect } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
@ -143,11 +143,11 @@ export default {
// 设置登陆中
this.loading = true;
// 计算 redirectUri
// const redirectUri = location.origin + '/third-login';
const redirectUri = location.origin + '/social-login';
// const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
// const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
// 进行跳转
thirdLoginRedirect(provider.type, redirectUri).then((res) => {
socialLoginRedirect(provider.type, redirectUri).then((res) => {
// console.log(res.url);
window.location.href = res.data;
});

View File

@ -28,7 +28,7 @@
</template>
<script>
import { thirdLogin } from "@/api/login";
import { socialLogin } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
@ -39,7 +39,7 @@ export default {
loginForm: {
username: "admin",
password: "admin123",
rememberMe: false,
rememberMe: false, // TODO
},
loginRules: {
username: [
@ -68,11 +68,17 @@ export default {
created() {
this.getCookie();
//
this.type = 10;
this.type = 20;
this.code = this.$route.query.code;
this.state = this.$route.query.state;
thirdLogin(this.type, this.code, this.state).then(res => {
debugger
this.$store.dispatch("SocialLogin", {
code: this.code,
state: this.state,
type: this.type
}).then(() => {
this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
}).catch(() => {
this.loading = false;
});
},
methods: {
@ -97,7 +103,13 @@ export default {
Cookies.remove("username");
Cookies.remove("password");
}
this.$store.dispatch("Login", this.loginForm).then(() => {
this.$store.dispatch("SocialLogin2", {
code: this.code,
state: this.state,
type: this.type,
username: this.loginForm.username,
password: this.loginForm.password
}).then(() => {
this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
}).catch(() => {
this.loading = false;