提交 11898e55 编写于 作者: DCloud-yinjiacheng's avatar DCloud-yinjiacheng

新增一键登录示例

上级 1a8ee3b2
...@@ -556,6 +556,13 @@ ...@@ -556,6 +556,13 @@
"navigationBarTitleText": "globalProperties", "navigationBarTitleText": "globalProperties",
"enablePullDownRefresh": false "enablePullDownRefresh": false
} }
},
{
"path": "pages/API/uni-verify/uni-verify",
"style": {
"navigationBarTitleText": "uni-verify",
"enablePullDownRefresh": false
}
}, },
{ {
"path": "pages/tabBar/CSS", "path": "pages/tabBar/CSS",
......
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-btn-v uni-common-mt">
<button type="primary" @click="verify">一键登录</button>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: '一键登录',
univerifyManager: null as UniverifyManager | null
}
},
onLoad() {
this.univerifyManager = uni.getUniverifyManager();
// 预登录
this.univerifyManager?.preLogin({
success() {
console.log("pre login success");
},
fail(err : PreLoginFail) {
console.error("pre login fail => " + JSON.stringify(err));
}
} as PreLoginOptions);
},
methods: {
verify() {
// 校验预登录是否有效
const isPreLoginValid = this.univerifyManager?.isPreLoginValid() ?? false;
if (isPreLoginValid) {
// 预登录有效,执行登录
this.login();
} else {
// 预登录无效,执行预登录
this.univerifyManager?.preLogin({
success() {
console.log("pre login success");
this.login();
},
fail(err : PreLoginFail) {
console.error("pre login fail => " + JSON.stringify(err));
}
} as PreLoginOptions);
}
},
login() {
this.univerifyManager?.login({
// 登录页样式
univerifyStyle: {
fullScreen: false,
backgroundColor: "#FFFFFF",
loginBtnText: "一键登录",
logoPath: "/static/logo.png"
} as UniverifyStyle,
success(res : LoginSuccess) {
console.log("login success => " + JSON.stringify(res));
// 云函数取号
uniCloud.callFunction({
name: 'univerify',
data: {
access_token: res.accessToken, // 客户端一键登录接口返回的access_token
openid: res.openId // 客户端一键登录接口返回的openid
}
}).then(res => {
uni.showModal({
title: '取号成功',
content: res.result.getJSON("res")?.getString("phoneNumber"),
showCancel: false,
success(_) {
// 关闭登录页
this.univerifyManager?.close();
}
});
}).catch(err => {
console.error(JSON.stringify(err));
// 关闭登录页
this.univerifyManager?.close();
});
},
fail(err : LoginFail) {
console.error("login fail => " + err);
uni.showToast({
title: "登录失败",
icon: "error"
});
}
} as LoginOptions);
}
}
}
</script>
<style>
</style>
...@@ -430,6 +430,10 @@ ...@@ -430,6 +430,10 @@
name: '实人认证', name: '实人认证',
url: 'facial-recognition-verify', url: 'facial-recognition-verify',
}, },
{
name: '一键登录',
url: 'uni-verify',
}
] as Page[], ] as Page[],
}, },
/* { /* {
......
'use strict';
exports.main = async function(event) {
const res = await uniCloud.getPhoneNumber({
appid: '__UNI__3584C99', // 替换成自己开通一键登录的应用的DCloud appid
provider: 'univerify',
access_token: event.access_token,
openid: event.openid
})
// 执行入库等操作,正常情况下不要把完整手机号返回给前端
return {
code: 0,
message: '获取手机号成功',
res: res
}
}
{
"name": "univerify",
"dependencies": {},
"extensions": {
"uni-cloud-verify": {}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册