提交 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,219 +40,256 @@ ...@@ -30,219 +40,256 @@
</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: {
authButton: { authButton: {
"title": "本机号码一键绑定", // 授权按钮文案 "title": "本机号码一键绑定", // 授权按钮文案
},
otherLoginButton: {
"title": "其他号码绑定",
}
},
userInfo: {
mobile:'',
nickname:''
}, },
hasLogin: false, otherLoginButton: {
hasPwd:false, "title": "其他号码绑定",
showLoginManage:false//通过页面传参隐藏登录&退出登录按钮 }
} },
userInfo: {
mobile: '',
nickname: ''
},
hasLogin: false,
hasPwd: false,
showLoginManage: false//通过页面传参隐藏登录&退出登录按钮
}
},
async onShow() {
this.univerifyStyle.authButton.title = "本机号码一键绑定"
this.univerifyStyle.otherLoginButton.title = "其他号码绑定"
},
async onLoad(e) {
if (e.showLoginManage) {
this.showLoginManage = true//通过页面传参隐藏登录&退出登录按钮
}
this.getUserInfo()
//判断当前用户是否有密码,否则就不显示密码修改功能
let res = await uniIdCo.getAccountInfo()
this.hasPwd = res.isPasswordSet
},
methods: {
login() {
uni.navigateTo({
url: '/uni_modules/uni-id-pages/pages/login/login-withoutpwd',
complete: (e) => {
console.log(e);
}
})
}, },
async onShow() { logout: common.logout,
this.univerifyStyle.authButton.title = "本机号码一键绑定" changePassword() {
this.univerifyStyle.otherLoginButton.title = "其他号码绑定" uni.navigateTo({
url: '/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd',
complete: (e) => {
console.log(e);
}
})
}, },
async onLoad(e) { getUserInfo(e) {
if(e.showLoginManage){ uni.showLoading({
this.showLoginManage = true//通过页面传参隐藏登录&退出登录按钮 mask: true
} });
this.getUserInfo() 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 });
let res = await uniIdCo.getAccountInfo() this.userInfo = res.result.data[0]
this.hasPwd = res.isPasswordSet console.log('this.userInfo', this.userInfo);
this.hasLogin = true
}).catch(e => {
this.userInfo = {}
this.hasLogin = false
console.log(e.message, e.errCode);
}).finally(e => {
// console.log(e);
uni.hideLoading()
})
},
bindMobile() {
// #ifdef APP-PLUS
uni.preLogin({
provider: 'univerify',
success: this.univerify(), //预登录成功
fail: (res) => { // 预登录失败
// 不显示一键登录选项(或置灰)
console.log(res)
this.bindMobileBySmsCode()
}
})
// #endif
// #ifdef MP-WEIXIN
this.$refs['bind-mobile-by-sms'].open()
// #endif
// #ifdef H5
//...去用验证码绑定
this.bindMobileBySmsCode()
// #endif
}, },
methods: { univerify() {
login() { uni.login({
uni.navigateTo({ "provider": 'univerify',
url: '/uni_modules/uni-id-pages/pages/login/login-withoutpwd', "univerifyStyle": this.univerifyStyle,
complete: (e) => { success: async e => {
console.log(e.authResult);
uniIdCo.bindMobileByUniverify(e.authResult).then(res => {
console.log(res);
this.getUserInfo()
}).catch(e => {
console.log(e); console.log(e);
} }).finally(e => {
})
},
logout:common.logout,
changePassword(){
uni.navigateTo({
url: '/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd',
complete: (e) => {
console.log(e); console.log(e);
} uni.closeAuthView()
}) })
}, },
getUserInfo(e) { fail: (err) => {
uni.showLoading({ console.log(err);
mask: true if (err.code == '30002' || err.code == '30001') {
});
usersTable.where("'_id' == $cloudEnv_uid").field('mobile,nickname,email').get().then(res => {
console.log({res});
this.userInfo = res.result.data[0]
console.log('this.userInfo', this.userInfo);
this.hasLogin = true
}).catch(e => {
this.userInfo = {}
this.hasLogin = false
console.log(e.message, e.errCode);
}).finally(e => {
// console.log(e);
uni.hideLoading()
})
},
bindMobile() {
// #ifdef APP-PLUS
uni.preLogin({
provider: 'univerify',
success: this.univerify(), //预登录成功
fail: (res) => { // 预登录失败
// 不显示一键登录选项(或置灰)
console.log(res)
this.bindMobileBySmsCode() this.bindMobileBySmsCode()
} }
}
})
},
bindMobileBySmsCode() {
uni.navigateTo({
url: './bind-mobile/bind-mobile',
events: {
getUserInfo: () => {
this.getUserInfo()
}
},
complete(e) {
console.log(e);
}
})
},
setNickname(nickname) {
console.log(nickname);
if (nickname) {
usersTable.where('_id==$env.uid').update({
nickname
}).then(e => {
console.log(e);
if (e.result.updated) {
uni.showToast({
title: "更新成功",
icon: 'none'
});
this.userInfo.nickname = nickname
} else {
uni.showToast({
title: "没有改变",
icon: 'none'
});
}
}) })
// #endif this.$refs.dialog.close()
} else {
// #ifdef MP-WEIXIN this.$refs.dialog.open()
this.$refs['bind-mobile-by-sms'].open() }
// #endif },
deactivate() {
uni.navigateTo({
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()]
// #ifdef H5 if (this.userInfo[bindField]) {
//...去用验证码绑定 await uniIdCo['unbind' + provider]()
this.bindMobileBySmsCode() await this.getUserInfo()
// #endif } else {
},
univerify() {
uni.login({ uni.login({
"provider": 'univerify', provider: provider.toLowerCase(),
"univerifyStyle": this.univerifyStyle, onlyAuthorize: true,
success: async e => { success: async e => {
console.log(e.authResult); const res = await uniIdCo['bind' + provider]({
uniIdCo.bindMobileByUniverify(e.authResult).then(res => { code: e.code
console.log(res);
this.getUserInfo()
}).catch(e => {
console.log(e);
}).finally(e=>{
console.log(e);
uni.closeAuthView()
}) })
}, if (res.errCode) {
fail: (err) => { uni.showToast({
console.log(err); title: res.errMsg || '绑定失败'
if (err.code == '30002' || err.code == '30001') { })
this.bindMobileBySmsCode()
}
}
})
},
bindMobileBySmsCode() {
uni.navigateTo({
url: './bind-mobile/bind-mobile',
events: {
getUserInfo: () => {
this.getUserInfo()
} }
await this.getUserInfo()
}, },
complete(e) { fail: async (err) => {
console.log(e); console.log(err);
uni.hideLoading()
} }
}) })
},
setNickname(nickname) {
console.log(nickname);
if (nickname) {
usersTable.where('_id==$env.uid').update({
nickname
}).then(e => {
console.log(e);
if (e.result.updated) {
uni.showToast({
title: "更新成功",
icon: 'none'
});
this.userInfo.nickname = nickname
} else {
uni.showToast({
title: "没有改变",
icon: 'none'
});
}
})
this.$refs.dialog.close()
} else {
this.$refs.dialog.open()
}
},
deactivate(){
uni.navigateTo({
url:"/uni_modules/uni-id-pages/pages/userinfo/deactivate/deactivate"
})
} }
} }
} }
}
</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 {
padding: 0;
}
/* #ifndef APP-NVUE */
view {
display: flex;
box-sizing: border-box;
flex-direction: column;
}
@media screen and (min-width: 690px) {
.uni-content { .uni-content {
padding: 0; padding: 0;
max-width: 690px;
margin-left: calc(50% - 345px);
border: none;
max-height: none;
border-radius: 0;
box-shadow: none;
} }
}
/* #ifndef APP-NVUE */ /* #endif */
view { .avatar {
display: flex; align-items: center;
box-sizing: border-box; justify-content: center;
flex-direction: column; margin: 22px 0;
} width: 100%;
@media screen and (min-width: 690px) { }
.uni-content {
padding: 0;
max-width: 690px;
margin-left: calc(50% - 345px);
border: none;
max-height: none;
border-radius: 0;
box-shadow: none;
}
}
/* #endif */
.avatar {
align-items: center;
justify-content: center;
margin: 22px 0;
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.
先完成此消息的编辑!
想要评论请 注册