提交 62c2c41f 编写于 作者: C chenruilong

feat: 增加解绑第三方账号接口

上级 7ba8f0f0
...@@ -14,6 +14,16 @@ ...@@ -14,6 +14,16 @@
<uni-list-item v-if="hasPwd" class="item" @click="changePassword" title="修改密码" link> <uni-list-item v-if="hasPwd" class="item" @click="changePassword" title="修改密码" link>
</uni-list-item> </uni-list-item>
</uni-list> </uni-list>
<uni-list class="mt10">
<uni-list-item @click="bindThirdAccount('Weixin')" title="微信" link
:rightText="userInfo.wx_openid ? '已绑定': '未绑定' "></uni-list-item>
<uni-list-item @click="bindThirdAccount('QQ')" title="QQ" link
:rightText="userInfo.qq_openid ? '已绑定': '未绑定' "></uni-list-item>
<uni-list-item @click="bindThirdAccount('Alipay')" title="支付宝" link
:rightText="userInfo.ali_openid ? '已绑定': '未绑定' "></uni-list-item>
<uni-list-item @click="bindThirdAccount('Apple')" title="Apple" link
:rightText="userInfo.apple_openid ? '已绑定': '未绑定' "></uni-list-item>
</uni-list>
<uni-list class="mt10"> <uni-list class="mt10">
<uni-list-item @click="deactivate" title="注销账号" link="navigateTo"></uni-list-item> <uni-list-item @click="deactivate" title="注销账号" link="navigateTo"></uni-list-item>
</uni-list> </uni-list>
...@@ -30,11 +40,11 @@ ...@@ -30,11 +40,11 @@
</view> </view>
</template> </template>
<script> <script>
const db = uniCloud.database(); const db = uniCloud.database();
const usersTable = db.collection('uni-id-users') const usersTable = db.collection('uni-id-users')
const uniIdCo = uniCloud.importObject("uni-id-co") const uniIdCo = uniCloud.importObject("uni-id-co")
import common from '@/uni_modules/uni-id-pages/common/common.js'; import common from '@/uni_modules/uni-id-pages/common/common.js';
export default { export default {
data() { data() {
return { return {
univerifyStyle: { univerifyStyle: {
...@@ -46,12 +56,12 @@ ...@@ -46,12 +56,12 @@
} }
}, },
userInfo: { userInfo: {
mobile:'', mobile: '',
nickname:'' nickname: ''
}, },
hasLogin: false, hasLogin: false,
hasPwd:false, hasPwd: false,
showLoginManage:false//通过页面传参隐藏登录&退出登录按钮 showLoginManage: false//通过页面传参隐藏登录&退出登录按钮
} }
}, },
async onShow() { async onShow() {
...@@ -59,7 +69,7 @@ ...@@ -59,7 +69,7 @@
this.univerifyStyle.otherLoginButton.title = "其他号码绑定" this.univerifyStyle.otherLoginButton.title = "其他号码绑定"
}, },
async onLoad(e) { async onLoad(e) {
if(e.showLoginManage){ if (e.showLoginManage) {
this.showLoginManage = true//通过页面传参隐藏登录&退出登录按钮 this.showLoginManage = true//通过页面传参隐藏登录&退出登录按钮
} }
this.getUserInfo() this.getUserInfo()
...@@ -76,8 +86,8 @@ ...@@ -76,8 +86,8 @@
} }
}) })
}, },
logout:common.logout, logout: common.logout,
changePassword(){ changePassword() {
uni.navigateTo({ uni.navigateTo({
url: '/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd', url: '/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd',
complete: (e) => { complete: (e) => {
...@@ -89,8 +99,8 @@ ...@@ -89,8 +99,8 @@
uni.showLoading({ uni.showLoading({
mask: true mask: true
}); });
usersTable.where("'_id' == $cloudEnv_uid").field('mobile,nickname,email').get().then(res => { usersTable.where("'_id' == $cloudEnv_uid").field('mobile,nickname,email,wx_openid,wx_unionid,ali_openid,apple_open,qq_openid,qq_unionid').get().then(res => {
console.log({res}); console.log({ res });
this.userInfo = res.result.data[0] this.userInfo = res.result.data[0]
console.log('this.userInfo', this.userInfo); console.log('this.userInfo', this.userInfo);
this.hasLogin = true this.hasLogin = true
...@@ -136,7 +146,7 @@ ...@@ -136,7 +146,7 @@
this.getUserInfo() this.getUserInfo()
}).catch(e => { }).catch(e => {
console.log(e); console.log(e);
}).finally(e=>{ }).finally(e => {
console.log(e); console.log(e);
uni.closeAuthView() uni.closeAuthView()
}) })
...@@ -187,28 +197,64 @@ ...@@ -187,28 +197,64 @@
this.$refs.dialog.open() this.$refs.dialog.open()
} }
}, },
deactivate(){ deactivate() {
uni.navigateTo({ uni.navigateTo({
url:"/uni_modules/uni-id-pages/pages/userinfo/deactivate/deactivate" url: "/uni_modules/uni-id-pages/pages/userinfo/deactivate/deactivate"
})
},
async bindThirdAccount(provider) {
const uniIdCo = uniCloud.importObject("uni-id-co")
const bindField = {
weixin: 'wx_openid',
alipay: 'ali_openid',
apple: 'apple_openid',
qq: 'qq_openid'
}[provider.toLowerCase()]
if (this.userInfo[bindField]) {
await uniIdCo['unbind' + provider]()
await this.getUserInfo()
} else {
uni.login({
provider: provider.toLowerCase(),
onlyAuthorize: true,
success: async e => {
const res = await uniIdCo['bind' + provider]({
code: e.code
})
if (res.errCode) {
uni.showToast({
title: res.errMsg || '绑定失败'
})
}
await this.getUserInfo()
},
fail: async (err) => {
console.log(err);
uni.hideLoading()
}
}) })
} }
} }
} }
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@import url("/uni_modules/uni-id-pages/common/login-page.scss"); @import url("/uni_modules/uni-id-pages/common/login-page.scss");
.uni-content { .uni-content {
padding: 0; padding: 0;
} }
/* #ifndef APP-NVUE */ /* #ifndef APP-NVUE */
view { view {
display: flex; display: flex;
box-sizing: border-box; box-sizing: border-box;
flex-direction: column; flex-direction: column;
} }
@media screen and (min-width: 690px) {
@media screen and (min-width: 690px) {
.uni-content { .uni-content {
padding: 0; padding: 0;
max-width: 690px; max-width: 690px;
...@@ -218,31 +264,32 @@ ...@@ -218,31 +264,32 @@
border-radius: 0; border-radius: 0;
box-shadow: none; box-shadow: none;
} }
} }
/* #endif */
.avatar { /* #endif */
.avatar {
align-items: center; align-items: center;
justify-content: center; justify-content: center;
margin: 22px 0; margin: 22px 0;
width: 100%; width: 100%;
} }
.item { .item {
flex: 1; flex: 1;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
} }
button { button {
margin: 10%; margin: 10%;
margin-top: 40px; margin-top: 40px;
border-radius: 0; border-radius: 0;
background-color: #FFFFFF; background-color: #FFFFFF;
width: 80%; width: 80%;
} }
.mt10{ .mt10 {
margin-top: 10px; margin-top: 10px;
} }
</style> </style>
...@@ -52,7 +52,11 @@ const LOG_TYPE = { ...@@ -52,7 +52,11 @@ const LOG_TYPE = {
BIND_WEIXIN: 'bind-weixin', BIND_WEIXIN: 'bind-weixin',
BIND_QQ: 'bind-qq', BIND_QQ: 'bind-qq',
BIND_APPLE: 'bind-apple', BIND_APPLE: 'bind-apple',
BIND_ALIPAY: 'bind-alipay' BIND_ALIPAY: 'bind-alipay',
UNBIND_WEIXIN: 'unbind-weixin',
UNBIND_QQ: 'unbind-qq',
UNBIND_ALIPAY: 'unbind-alipay',
UNBIND_APPLE: 'unbind-apple'
} }
const SMS_SCENE = { const SMS_SCENE = {
......
...@@ -30,7 +30,11 @@ const ERROR = { ...@@ -30,7 +30,11 @@ const ERROR = {
SET_INVITE_CODE_FAILED: 'uni-id-set-invite-code-failed', SET_INVITE_CODE_FAILED: 'uni-id-set-invite-code-failed',
INVALID_INVITE_CODE: 'uni-id-invalid-invite-code', INVALID_INVITE_CODE: 'uni-id-invalid-invite-code',
CHANGE_INVITER_FORBIDDEN: 'uni-id-change-inviter-forbidden', CHANGE_INVITER_FORBIDDEN: 'uni-id-change-inviter-forbidden',
BIND_CONFLICT: 'uni-id-bind-conflict' BIND_CONFLICT: 'uni-id-bind-conflict',
UNBIND_FAIL: 'uni-id-unbind-failed',
UNBIND_NOT_SUPPORTED: 'uni-id-unbind-not-supported',
UNBIND_UNIQUE_LOGIN: 'uni-id-unbind-unique-login'
} }
function isUniIdError (errCode) { function isUniIdError (errCode) {
......
...@@ -62,5 +62,17 @@ module.exports = { ...@@ -62,5 +62,17 @@ module.exports = {
}, },
getAccountInfo: { getAccountInfo: {
auth: true auth: true
},
unbindWeixin: {
auth: true
},
unbindAlipay: {
auth: true
},
unbindQQ: {
auth: true
},
unbindApple: {
auth: true
} }
} }
...@@ -42,7 +42,11 @@ const { ...@@ -42,7 +42,11 @@ const {
bindAlipay, bindAlipay,
bindApple, bindApple,
bindQQ, bindQQ,
bindWeixin bindWeixin,
unbindWeixin,
unbindAlipay,
unbindQQ,
unbindApple
} = require('./module/relate/index') } = require('./module/relate/index')
const { const {
updatePwd, updatePwd,
...@@ -543,5 +547,30 @@ module.exports = { ...@@ -543,5 +547,30 @@ module.exports = {
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type
* @returns * @returns
*/ */
getSupportedLoginType getSupportedLoginType,
/**
* 解绑微信
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-weixin
* @returns
*/
unbindWeixin,
/**
* 解绑支付宝
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-alipay
* @returns
*/
unbindAlipay,
/**
* 解绑QQ
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-qq
* @returns
*/
unbindQQ,
/**
* 解绑Apple
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-apple
* @returns
*/
unbindApple
} }
...@@ -36,7 +36,10 @@ const sentence = { ...@@ -36,7 +36,10 @@ const sentence = {
'uni-id-invalid-invite-code': 'Invalid invite code', 'uni-id-invalid-invite-code': 'Invalid invite code',
'uni-id-change-inviter-forbidden': 'Change inviter is not allowed', 'uni-id-change-inviter-forbidden': 'Change inviter is not allowed',
'uni-id-bind-conflict': 'This account has been bound', 'uni-id-bind-conflict': 'This account has been bound',
'uni-id-admin-exist-in-other-apps': 'Administrator is registered in other consoles' 'uni-id-admin-exist-in-other-apps': 'Administrator is registered in other consoles',
'uni-id-unbind-failed': 'Please bind first and then unbind',
'uni-id-unbind-not-supported': 'Unbinding is not supported',
'uni-id-unbind-unique-login': 'This is the only way to login at the moment, please bind your phone number and then try to unbind'
} }
module.exports = { module.exports = {
......
...@@ -36,7 +36,10 @@ const sentence = { ...@@ -36,7 +36,10 @@ const sentence = {
'uni-id-invalid-invite-code': '邀请码不可用', 'uni-id-invalid-invite-code': '邀请码不可用',
'uni-id-change-inviter-forbidden': '禁止修改邀请人', 'uni-id-change-inviter-forbidden': '禁止修改邀请人',
'uni-id-bind-conflict': '此账号已被绑定', 'uni-id-bind-conflict': '此账号已被绑定',
'uni-id-admin-exist-in-other-apps': '超级管理员已在其他控制台注册' 'uni-id-admin-exist-in-other-apps': '超级管理员已在其他控制台注册',
'uni-id-unbind-failed': '请先绑定后再解绑',
'uni-id-unbind-not-supported': '不支持解绑',
'uni-id-unbind-unique-login': '这是当前唯一登录方式,请绑定手机号后再尝试解绑'
} }
module.exports = { module.exports = {
......
...@@ -5,8 +5,15 @@ const { ...@@ -5,8 +5,15 @@ const {
ERROR ERROR
} = require('../../common/error') } = require('../../common/error')
const { const {
userCollection userCollection, dbCmd, USER_IDENTIFIER
} = require('../../common/constants') } = require('../../common/constants')
const {
getUserIdentifier
} = require('../../lib/utils/account')
const {
batchFindObjctValue
} = require('../../common/utils')
const merge = require('lodash.merge') const merge = require('lodash.merge')
/** /**
...@@ -56,7 +63,90 @@ async function postBind ({ ...@@ -56,7 +63,90 @@ async function postBind ({
} }
} }
async function preUnBind ({
uid,
unBindAccount,
logType
}) {
const notUnBind = ['username', 'mobile', 'email']
const userIdentifier = getUserIdentifier(unBindAccount)
const condition = Object.keys(userIdentifier).reduce((res, key) => {
if (userIdentifier[key]) {
if (notUnBind.includes(key)) {
throw {
errCode: ERROR.UNBIND_NOT_SUPPORTED
}
}
res.push({
[key]: userIdentifier[key]
})
}
return res
}, [])
const currentUnBindAccount = Object.keys(userIdentifier).reduce((res, key) => {
if (userIdentifier[key]) {
res.push(key)
}
return res
}, [])
const { data: users } = await userCollection.where(dbCmd.and(
{ _id: uid },
dbCmd.or(condition)
)).get()
if (users.length <= 0) {
await this.middleware.uniIdLog({
data: {
user_id: uid
},
type: logType,
success: false
})
throw {
errCode: ERROR.UNBIND_FAIL
}
}
const [user] = users
const otherAccounts = batchFindObjctValue(user, Object.keys(USER_IDENTIFIER).filter(key => !notUnBind.includes(key) && !currentUnBindAccount.includes(key)))
let hasOtherAccountBind = false
for (const key in otherAccounts) {
if (otherAccounts[key]) {
hasOtherAccountBind = true
break
}
}
if (!hasOtherAccountBind && !user.mobile) {
throw {
errCode: ERROR.UNBIND_UNIQUE_LOGIN
}
}
}
async function postUnBind ({
uid,
unBindAccount,
logType
}) {
await userCollection.doc(uid).update(unBindAccount)
await this.middleware.uniIdLog({
data: {
user_id: uid
},
type: logType
})
return {
errCode: 0
}
}
module.exports = { module.exports = {
preBind, preBind,
postBind postBind,
preUnBind,
postUnBind
} }
...@@ -47,7 +47,7 @@ module.exports = async function (params = {}) { ...@@ -47,7 +47,7 @@ module.exports = async function (params = {}) {
} = getAlipayAccountResult } = getAlipayAccountResult
const bindAccount = { const bindAccount = {
apple_openid: openid ali_openid: openid
} }
await preBind.call(this, { await preBind.call(this, {
uid, uid,
......
...@@ -5,5 +5,9 @@ module.exports = { ...@@ -5,5 +5,9 @@ module.exports = {
bindAlipay: require('./bind-alipay'), bindAlipay: require('./bind-alipay'),
bindApple: require('./bind-apple'), bindApple: require('./bind-apple'),
bindQQ: require('./bind-qq'), bindQQ: require('./bind-qq'),
bindWeixin: require('./bind-weixin') bindWeixin: require('./bind-weixin'),
unbindWeixin: require('./unbind-weixin'),
unbindAlipay: require('./unbind-alipay'),
unbindQQ: require('./unbind-qq'),
unbindApple: require('./unbind-apple')
} }
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
/**
* 解绑支付宝
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-alipay
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
await preUnBind.call(this, {
uid,
unBindAccount: {
ali_openid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_ALIPAY
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
ali_openid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_ALIPAY
})
}
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
/**
* 解绑apple
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-apple
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
await preUnBind.call(this, {
uid,
unBindAccount: {
apple_openid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_APPLE
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
apple_openid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_APPLE
})
}
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
const {
getQQPlatform
} = require('../../lib/utils/qq')
/**
* 解绑QQ
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-qq
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
const { appId } = this.getClientInfo()
const qqPlatform = getQQPlatform.call(this)
await preUnBind.call(this, {
uid,
unBindAccount: {
qq_openid: dbCmd.or([
{
[qqPlatform]: dbCmd.exists(true)
},
{
[`${qqPlatform}_${appId}`]: dbCmd.exists(true)
}
]),
qq_unionid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_QQ
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
qq_openid: dbCmd.remove(),
qq_unionid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_QQ
})
}
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
const {
getWeixinPlatform
} = require('../../lib/utils/weixin')
/**
* 解绑微信
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-weixin
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
const weixinPlatform = getWeixinPlatform.call(this)
await preUnBind.call(this, {
uid,
unBindAccount: {
wx_openid: {
[weixinPlatform]: dbCmd.exists(true)
},
wx_unionid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_WEIXIN
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
wx_openid: dbCmd.remove(),
wx_unionid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_WEIXIN
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册