登录
Android、iOS还未提供此功能,请在插件市场搜索替代方案。详情
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| x | 4.41 | 5.08 | 5.08 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | LoginOptions | 是 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errMsg | string | 否 | - | 描述信息 | |||||||||||||||||||||||||||||||||||||
| authResult | any | 是 | - | 登录服务商提供的登录信息,服务商不同返回的结果不完全相同 | |||||||||||||||||||||||||||||||||||||
| code | string | 是 | - | 小程序用户临时登录凭证 | |||||||||||||||||||||||||||||||||||||
| anonymousCode | string | 否 | - | 头条小程序当前设备标识 | |||||||||||||||||||||||||||||||||||||
| authCode | string | 否 | - | 支付宝小程序授权码 | |||||||||||||||||||||||||||||||||||||
| authErrorScope | any | 否 | - | 支付宝小程序登录失败的授权类型,key是授权失败的 scope,value 是对应的错误码 | |||||||||||||||||||||||||||||||||||||
| authSucessScope | Array<string> | 否 | - | 支付宝小程序登录成功的授权 scope | |||||||||||||||||||||||||||||||||||||
| appleInfo | AppleLoginAppleInfo | 否 | - | 苹果登录成功返回的信息 | |||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | 微信登录错误码 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| data | any | 否 | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | |||||||||||||||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||
注意
"querySchemes": ["weixin"] 请勿配置 wxopensdk。已在 @tencent/wechat_open_sdk@1.0.15 实测配置 wxopensdk 后登录无法回调"actions": [ "action.system.home", "wxentity.action.open" ]UniOAuthWeixinProvider(微信登录)继承自 UniProvider
| 名称 | 类型 | 必备 | 默认值 | 描述 |
|---|---|---|---|---|
| isWeChatInstalled | boolean | 是 | - | 判断微信是否安装 |
背景:目前uni-app x引擎已经内置了微信登录。但登录SDK还有很多,比如京东、支付宝登录。
以往这些SDK可以通过独立插件的方式集成到uni-app x中,但需要提供单独的API给开发者使用。
uni-app x从4.25起,开放了provider自接入机制,让三方SDK可以以provider方式被开发者集成。
开发一个UTS插件,对接uni规范化的API、错误信息描述等实现自己的登录插件,这样插件使用者就可以通过uni的标准API使用三方SDK。
举个例子,开发者想使用uni.login()的方式调用XX登录,但是内置登录api不支持,
那只需要按照下面四个步骤实现即可:
第一步,新建一个UTS插件,在interface.uts 中定义接口,UniOAuthProvider,代码如下
export interface UniOAuthWeixinProvider extends UniOAuthProvider{}
第二步,在app-android或者app-ios的index.uts中实现接口,代码如下
import { UniOAuthProvider } from '../interface.uts'
export class UniOAuthWeixinProviderImpl implements UniOAuthWeixinProvider {
override id : String = "XX" // id必须有插件作者前缀,避免冲突,避免不同插件作者的插件id重名
override description : String = "XX的描述"
override isAppExist : boolean | null = null
constructor(){}
override login(options : LoginOptions) {
//todo 具体逻辑,接收uni规范的入参,进行业务处理,返回uni规范的返回值。如遇到错误,按uni的规范返回错误码
}
}
第三步,在manifest.json中配置
"app": {
"distribute": {
/* android打包配置 */
"modules": {
"uni-oauth":{
"XX":{}
}
}
}
}
第四步,打包自定义基座然后运行
获取用户信息
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| x | 4.41 | x | x | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | GetUserInfoOptions | 是 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| userInfo | UserInfo | 是 | - | 用户信息对象,不包含 openid 等敏感信息 | |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| rawData | string | 否 | - | 不包括敏感信息的原始数据字符串,用于计算签名。 | |||||||||||||||||||||||||||||||||||||||||||||||||
| signature | string | 否 | - | 使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息,仅微信小程序生效。 | |||||||||||||||||||||||||||||||||||||||||||||||||
| encryptedData | string | 否 | - | 包括敏感数据在内的完整用户信息的加密数据,详细见加密数据解密算法,仅微信小程序生效。 | |||||||||||||||||||||||||||||||||||||||||||||||||
| iv | string | 否 | - | 加密算法的初始向量,详细见加密数据解密算法,仅微信小程序生效。 | |||||||||||||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | 描述信息 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errCode | number | 是 | - | 统一错误码 | |
| errSubject | string | 是 | - | 统一错误主题(模块)名称 | |
| data | any | 否 | - | 错误信息中包含的数据 | |
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1">
<!-- #endif -->
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-container">
<view class="uni-center">登录状态</view>
<view v-if="userInfo == null">
<template v-if="logging">
<view class="uni-center uni-common-mt">登录中...</view>
</template>
<template v-else>
<view class="uni-center uni-common-mt">未登录</view>
<view class="uni-center uni-common-mt">请点击下面按钮登录</view>
</template>
</view>
<view v-else>
<view class="uni-center uni-common-mt">
<image :src="userInfo!.avatarUrl" style="width: 60px;height: 60px;border-radius: 30px;"></image>
</view>
<view class="uni-center uni-common-mt">Hello, {{userInfo!.nickName}}</view>
</view>
</view>
<view class="uni-btn-v uni-common-mt">
<!-- #ifdef APP-HARMONY -->
<button type="primary" @click="hwLogin">华为登录</button>
<!-- #endif -->
<button class="uni-common-mt" type="primary" @click="wxLogin">微信登录</button>
<button class="uni-common-mt" @click="clear">清空</button>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
import { state, setUserInfo, UserInfo } from '@/store/index.uts'
const title = ref('OAuth')
const logging = ref(false)
const userInfo = computed(() : UserInfo | null => state.userInfo)
const testUserInfo = ref<UserInfo | null>(null)
const clear = () => {
setUserInfo(null)
testUserInfo.value = null
logging.value = false
}
const hwLogin = () => {
logging.value = true
if (userInfo.value != null) return
uni.login({
provider: 'huawei',
success() {
uni.getUserInfo({
provider: 'huawei',
success(res) {
console.log('获取用户信息成功')
const info : UserInfo = {
nickName: res.userInfo.nickName,
avatarUrl: res.userInfo.avatarUrl
}
testUserInfo.value = info
setUserInfo(info)
logging.value = false
},
fail(err) {
clear()
console.log('获取用户信息错误: ', JSON.stringify(err));
}
})
},
fail(err) {
clear()
console.log('获取用户信息错误: ', JSON.stringify(err));
}
})
}
const wxLogin = () => {
logging.value = true;
if (userInfo.value != null) return;
uni.login({
provider: 'weixin',
success(res) {
uniCloud.importObject('uni-login-test-co').loginByWeixin({
"code": res.code,
"appid": "wxd569c7238830733b"
}).then((res : UTSJSONObject) => {
console.log(JSON.stringify(res));
const info : UserInfo = {
nickName: res.getString('nickname') ?? '',
avatarUrl: res.getString('headimgurl') ?? ''
};
testUserInfo.value = info;
setUserInfo(info);
logging.value = false;
}).catch(err => {
console.error(JSON.stringify(err));
uni.showModal({
title: '微信登录失败',
content: (err as Error).message
});
clear();
});
},
fail(err) {
uni.showModal({
title: '微信登录失败',
content: err.errMsg
});
clear();
}
})
}
function getTestUserInfo() : UserInfo | null {
return testUserInfo.value
}
defineExpose({
getTestUserInfo,
hwLogin,
setUserInfo
})
</script>
<style>
</style>
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | 错误信息 |