1.
4.
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const res = await uniCloud.getPhoneNumber({
appid: '__UNI__872BDC2', // 替换成自己开通一键登录的应用的DCloud appid,使用callFunction方式调用时可以不传(会自动取当前客户端的appid),如果使用云函数URL化的方式访问必须传此参数
provider: 'univerify',
access_token: event.access_token,
openid: event.openid
});
console.log(res); // res里的数据格式 { code: 0, success: true, phoneNumber: '186*****078' }
return res
};
login() {
let _this = this
uni.login({
provider: 'univerify',
univerifyStyle: { // 自定义登录框样式
"fullScreen": false,
"title": '快速登录',
"backgroundColor": "#ffffff",
"phoneNum": {
"color": "#000000", // 手机号文字颜色 默认值:#000000
"fontSize": "18" // 手机号字体大小 默认值:18
},
"slogan": {
"color": "#8a8b90", // slogan 字体颜色 默认值:#8a8b90
"fontSize": "12" // slogan 字体大小 默认值:12
},
"icon": {
"path": "/static/logo.png" // 自定义显示在授权框中的logo,仅支持本地图片 默认显示App logo
},
// "closeIcon": {
// "path": "/static/none.png" // 自定义关闭按钮,仅支持本地图片。 HBuilderX3.3.7+版本支持
// },
// 其他登录方式
"otherLoginButton": {
"visible": "true", // 是否显示其他登录按钮,默认值:true
"title": "短信验证码登录", // 其他登录方式按钮文字 默认值:“其他登录方式”
},
// 一键登录
"authButton": {
"normalColor": "#EF801B", // 授权按钮正常状态背景颜色 默认值:#3479f5
"highlightColor": "#EF801B", // 授权按钮按下状态背景颜色 默认值:#2861c5(仅ios支持)
"disabledColor": "#EF801B", // 授权按钮不可点击时背景颜色 默认值:#73aaf5(仅ios支持)
"textColor": "#ffffff", // 授权按钮文字颜色 默认值:#ffffff
"title": "本机号码一键登录" // 授权按钮文案 默认值:“本机号码一键登录”
},
"privacyTerms": {
"defaultCheckBoxState": false, // 条款勾选框初始状态 默认值: true
"prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
"suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
"textColor": "#666666",
"termsColor": "#2D93EC",
"privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
{
"url": "https://app.sheyuntai.com/automatic/m6190.agreement/content.html?id=1&version=m6190", // 点击跳转的协议详情页面
"title": "用户服务协议" // 协议名称
},
{
"url": "https://app.sheyuntai.com/automatic/m6190.agreement/content.html?id=2&version=m6190", // 点击跳转的协议详情页面
"title": "隐私政策" // 协议名称
}
]
},
},
success(res) { // 登录成功
// 在得到access_token后,通过callfunction调用云函数
uniCloud.callFunction({
name: 'getPhoneNumber', // 云函数名称
data: { //传给云函数的参数
'access_token': res.authResult.access_token, // 客户端一键登录接口返回的access_token
'openid': res.authResult.openid // 客户端一键登录接口返回的openid
},
success(callRes) {// 一键登录 这里拿到了手机号
_this.$api
.post(global.apiUrls.xxx, {
mobile: callRes.result.phoneNumber //获取到的手机号
})
.then((user) => {
//处理登录逻辑
}).catch(err => {
uni.closeAuthView()
setTimeout(() =>{
_this.$message.info(user.data.errMessage);
},100)
})
},
fail(callErr) {
console.log('调用云函数出错' + callErr)
uni.closeAuthView()
},
complete() {
// uni.closeAuthView() //关闭授权登录界面
console.log('complete--------');
}
})
},