Commit cd205a6061ea6ca63d2efbca04d92c28279cf96d

Authored by wangwenbin10
1 parent 4a23a696

前端接入绑定社交平台的逻辑

yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java
@@ -107,7 +107,7 @@ public class SysSocialServiceImpl implements SysSocialService { @@ -107,7 +107,7 @@ public class SysSocialServiceImpl implements SysSocialService {
107 // 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新 107 // 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新
108 SysSocialUserDO socialUser = CollUtil.findOneByField(socialUsers, "openid", authUser.getUuid()); 108 SysSocialUserDO socialUser = CollUtil.findOneByField(socialUsers, "openid", authUser.getUuid());
109 SysSocialUserDO saveSocialUser = SysSocialUserDO.builder() // 新增和更新的通用属性 109 SysSocialUserDO saveSocialUser = SysSocialUserDO.builder() // 新增和更新的通用属性
110 - .token(authUser.getToken().getAccessToken()).rawUserInfo(toJsonString(authUser.getToken())) 110 + .token(authUser.getToken().getAccessToken()).rawTokenInfo(toJsonString(authUser.getToken()))
111 .nickname(authUser.getNickname()).avatar(authUser.getAvatar()).rawUserInfo(toJsonString(authUser.getRawUserInfo())) 111 .nickname(authUser.getNickname()).avatar(authUser.getAvatar()).rawUserInfo(toJsonString(authUser.getRawUserInfo()))
112 .build(); 112 .build();
113 if (socialUser == null) { 113 if (socialUser == null) {
@@ -129,7 +129,7 @@ public class SysSocialServiceImpl implements SysSocialService { @@ -129,7 +129,7 @@ public class SysSocialServiceImpl implements SysSocialService {
129 } 129 }
130 // 校验,是否解绑的是非自己的 130 // 校验,是否解绑的是非自己的
131 socialUsers.forEach(socialUser -> { 131 socialUsers.forEach(socialUser -> {
132 - if (Objects.equals(socialUser.getUserId(), userId)) { 132 + if (!Objects.equals(socialUser.getUserId(), userId)) {
133 throw exception(SOCIAL_UNBIND_NOT_SELF); 133 throw exception(SOCIAL_UNBIND_NOT_SELF);
134 } 134 }
135 }); 135 });
yudao-admin-ui/src/api/login.js
@@ -74,3 +74,28 @@ export function socialLogin2(type, code, state, username, password) { @@ -74,3 +74,28 @@ export function socialLogin2(type, code, state, username, password) {
74 } 74 }
75 }) 75 })
76 } 76 }
  77 +
  78 +// 社交绑定,使用 code 授权码
  79 +export function socialBind(type, code, state) {
  80 + return request({
  81 + url: '/social-bind',
  82 + method: 'post',
  83 + data: {
  84 + type,
  85 + code,
  86 + state,
  87 + }
  88 + })
  89 +}
  90 +
  91 +// 取消社交绑定
  92 +export function socialUnbind(type, unionId) {
  93 + return request({
  94 + url: '/social-unbind',
  95 + method: 'delete',
  96 + data: {
  97 + type,
  98 + unionId
  99 + }
  100 + })
  101 +}
yudao-admin-ui/src/views/socialLogin.vue
@@ -69,7 +69,6 @@ export default { @@ -69,7 +69,6 @@ export default {
69 this.getCookie(); 69 this.getCookie();
70 // 重定向地址 70 // 重定向地址
71 this.redirect = this.$route.query.redirect; 71 this.redirect = this.$route.query.redirect;
72 - debugger  
73 // 社交登录相关 72 // 社交登录相关
74 this.type = this.$route.query.type; 73 this.type = this.$route.query.type;
75 this.code = this.$route.query.code; 74 this.code = this.$route.query.code;
yudao-admin-ui/src/views/system/user/profile/index.vue
@@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
33 </li> 33 </li>
34 <li class="list-group-item"> 34 <li class="list-group-item">
35 <svg-icon icon-class="peoples" />所属角色 35 <svg-icon icon-class="peoples" />所属角色
36 - <div class="pull-right">{{ user.roles.map(post => post.name).join(',') }}</div> 36 + <div class="pull-right" v-if="user.roles">{{ user.roles.map(role => role.name).join(',') }}</div>
37 </li> 37 </li>
38 <li class="list-group-item"> 38 <li class="list-group-item">
39 <svg-icon icon-class="date" />创建日期 39 <svg-icon icon-class="date" />创建日期
@@ -55,6 +55,9 @@ @@ -55,6 +55,9 @@
55 <el-tab-pane label="修改密码" name="resetPwd"> 55 <el-tab-pane label="修改密码" name="resetPwd">
56 <resetPwd :user="user" /> 56 <resetPwd :user="user" />
57 </el-tab-pane> 57 </el-tab-pane>
  58 + <el-tab-pane label="社交信息" name="userSocial">
  59 + <userSocial :user="user" :getUser="getUser" :setActiveTab="setActiveTab" />
  60 + </el-tab-pane>
58 </el-tabs> 61 </el-tabs>
59 </el-card> 62 </el-card>
60 </el-col> 63 </el-col>
@@ -66,11 +69,12 @@ @@ -66,11 +69,12 @@
66 import userAvatar from "./userAvatar"; 69 import userAvatar from "./userAvatar";
67 import userInfo from "./userInfo"; 70 import userInfo from "./userInfo";
68 import resetPwd from "./resetPwd"; 71 import resetPwd from "./resetPwd";
  72 +import userSocial from "./userSocial";
69 import { getUserProfile } from "@/api/system/user"; 73 import { getUserProfile } from "@/api/system/user";
70 74
71 export default { 75 export default {
72 name: "Profile", 76 name: "Profile",
73 - components: { userAvatar, userInfo, resetPwd }, 77 + components: { userAvatar, userInfo, resetPwd, userSocial },
74 data() { 78 data() {
75 return { 79 return {
76 user: {}, 80 user: {},
@@ -87,6 +91,9 @@ export default { @@ -87,6 +91,9 @@ export default {
87 getUserProfile().then(response => { 91 getUserProfile().then(response => {
88 this.user = response.data; 92 this.user = response.data;
89 }); 93 });
  94 + },
  95 + setActiveTab(activeTab) {
  96 + this.activeTab = activeTab
90 } 97 }
91 } 98 }
92 }; 99 };
yudao-admin-ui/src/views/system/user/profile/userSocial.vue 0 → 100644
  1 +<template>
  2 + <el-table :data="socialUsers" :show-header="false">
  3 + <el-table-column label="社交平台" align="left" width="120">
  4 + <template slot-scope="scope">
  5 + <img style="height:20px;vertical-align: middle;" :src="scope.row.img" /> {{ scope.row.title }}
  6 + </template>
  7 + </el-table-column>
  8 + <el-table-column label="操作" align="left" >
  9 + <template slot-scope="scope">
  10 + <div v-if="scope.row.unionId">
  11 + 已绑定
  12 + <el-button size="large" type="text" @click="unbind(scope.row)">(解绑)</el-button>
  13 + </div>
  14 + <div v-else>
  15 + 未绑定
  16 + <el-button size="large" type="text" @click="bind(scope.row)">(绑定)</el-button>
  17 + </div>
  18 + </template>
  19 + </el-table-column>
  20 + </el-table>
  21 +</template>
  22 +
  23 +<script>
  24 +
  25 +import {SysUserSocialTypeEnum} from "@/utils/constants";
  26 +import {socialAuthRedirect, socialBind, socialUnbind} from "@/api/login";
  27 +
  28 +export default {
  29 + props: {
  30 + user: {
  31 + type: Object
  32 + },
  33 + getUser: { // 刷新用户
  34 + type: Function
  35 + },
  36 + setActiveTab: { // 设置激活的
  37 + type: Function
  38 + }
  39 + },
  40 + data() {
  41 + return {
  42 + };
  43 + },
  44 + computed: {
  45 + socialUsers (){
  46 + const socialUsers = [];
  47 + for (const i in SysUserSocialTypeEnum) {
  48 + const socialUser = {...SysUserSocialTypeEnum[i]};
  49 + socialUsers.push(socialUser);
  50 + if (this.user.socialUsers) {
  51 + for (const j in this.user.socialUsers) {
  52 + if (socialUser.type === this.user.socialUsers[j].type) {
  53 + socialUser.unionId = this.user.socialUsers[j].unionId;
  54 + break;
  55 + }
  56 + }
  57 + }
  58 + }
  59 + return socialUsers;
  60 + }
  61 + },
  62 + created() {
  63 + // 社交绑定
  64 + const type = this.$route.query.type;
  65 + const code = this.$route.query.code;
  66 + const state = this.$route.query.state;
  67 + if (!code) {
  68 + return;
  69 + }
  70 + socialBind(type, code, state).then(resp => {
  71 + this.msgSuccess("绑定成功");
  72 + this.$router.replace('/user/profile');
  73 + // 调用父组件, 刷新
  74 + this.getUser();
  75 + this.setActiveTab('userSocial');
  76 + });
  77 + },
  78 + methods: {
  79 + bind(socialUser) {
  80 + // 计算 redirectUri
  81 + const redirectUri = location.origin + '/user/profile?type=' + socialUser.type;
  82 + // 进行跳转
  83 + socialAuthRedirect(socialUser.type, encodeURIComponent(redirectUri)).then((res) => {
  84 + // console.log(res.url);
  85 + window.location.href = res.data;
  86 + });
  87 + },
  88 + unbind(socialUser) {
  89 + socialUnbind(socialUser.type, socialUser.unionId).then(resp => {
  90 + this.msgSuccess("解绑成功");
  91 + socialUser.unionId = undefined;
  92 + });
  93 + },
  94 + close() {
  95 + this.$store.dispatch("tagsView/delView", this.$route);
  96 + this.$router.push({ path: "/index" });
  97 + }
  98 + }
  99 +};
  100 +</script>