提交 95517b9b 编写于 作者: D DCloud_LXH

feat(App): login add onlyAuthorize param, uni.getCheckBoxState

上级 f38979ca
...@@ -5,6 +5,7 @@ export const LoginProtocol: ApiProtocol<API_TYPE_LOGIN> = { ...@@ -5,6 +5,7 @@ export const LoginProtocol: ApiProtocol<API_TYPE_LOGIN> = {
scopes: [String, Array] as any, scopes: [String, Array] as any,
timeout: Number, timeout: Number,
univerifyStyle: Object as any, univerifyStyle: Object as any,
onlyAuthorize: Boolean,
} }
export const API_GET_USER_INFO = 'getUserInfo' export const API_GET_USER_INFO = 'getUserInfo'
...@@ -48,3 +49,6 @@ export const PreLoginProtocol: ApiProtocol<API_TYPE_PRE_LOGIN> = { ...@@ -48,3 +49,6 @@ export const PreLoginProtocol: ApiProtocol<API_TYPE_PRE_LOGIN> = {
export const API_CLOSE_AUTH_VIEW = 'closeAuthView' export const API_CLOSE_AUTH_VIEW = 'closeAuthView'
export type API_TYPE_CLOSE_AUTH_VIEW = typeof uni.closeAuthView export type API_TYPE_CLOSE_AUTH_VIEW = typeof uni.closeAuthView
export const API_GET_CHECK_BOX_STATE = 'getCheckBoxState'
export type API_TYPE_GET_CHECK_BOX_STATE = typeof uni.getCheckBoxState
...@@ -14,7 +14,10 @@ import { ...@@ -14,7 +14,10 @@ import {
PreLoginProtocol, PreLoginProtocol,
API_CLOSE_AUTH_VIEW, API_CLOSE_AUTH_VIEW,
API_TYPE_CLOSE_AUTH_VIEW, API_TYPE_CLOSE_AUTH_VIEW,
API_GET_CHECK_BOX_STATE,
API_TYPE_GET_CHECK_BOX_STATE,
defineAsyncApi, defineAsyncApi,
defineSyncApi,
} from '@dcloudio/uni-api' } from '@dcloudio/uni-api'
import { isPlainObject, toTypeString } from '@vue/shared' import { isPlainObject, toTypeString } from '@vue/shared'
import { import {
...@@ -23,6 +26,10 @@ import { ...@@ -23,6 +26,10 @@ import {
} from '../../../helpers/plus' } from '../../../helpers/plus'
type Provider = PlusOauthAuthService type Provider = PlusOauthAuthService
type CallBack = {
resolve: (res: any) => void
reject: (errMsg: string) => void
}
function getService(provider: string): Promise<Provider> { function getService(provider: string): Promise<Provider> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -33,25 +40,35 @@ function getService(provider: string): Promise<Provider> { ...@@ -33,25 +40,35 @@ function getService(provider: string): Promise<Provider> {
}) })
} }
/** export const login = defineAsyncApi<API_TYPE_LOGIN>(
* 微信登录 API_LOGIN,
*/ (params, { resolve, reject }) => {
const baseLogin = (
params: UniApp.LoginOptions,
{
resolve,
reject,
}: {
resolve: (res: any) => void
reject: (errMsg: string, errRes?: any) => void
}
) => {
const provider = params.provider || 'weixin' const provider = params.provider || 'weixin'
const errorCallback = warpPlusErrorCallback(reject) const errorCallback = warpPlusErrorCallback(reject)
const authOptions =
provider === 'apple'
? { scope: 'email' }
: params.univerifyStyle
? {
univerifyStyle: univerifyButtonsClickHandling(
params.univerifyStyle,
errorCallback
),
}
: {}
getService(provider) getService(provider)
.then((service) => { .then((service) => {
function login() { function login() {
if (params.onlyAuthorize && provider === 'weixin') {
service.authorize(({ code }) => {
resolve({
code,
authResult: '',
})
}, errorCallback)
return
}
service.login( service.login(
(res) => { (res) => {
const authResult = res.target.authResult const authResult = res.target.authResult
...@@ -61,14 +78,7 @@ const baseLogin = ( ...@@ -61,14 +78,7 @@ const baseLogin = (
}) })
}, },
errorCallback, errorCallback,
provider === 'apple' authOptions
? { scope: 'email' }
: {
univerifyStyle: univerifyButtonsClickHandling(
params.univerifyStyle!,
errorCallback
),
} || {}
) )
} }
// 先注销再登录 // 先注销再登录
...@@ -80,16 +90,14 @@ const baseLogin = ( ...@@ -80,16 +90,14 @@ const baseLogin = (
} }
}) })
.catch(errorCallback) .catch(errorCallback)
} },
export const login = defineAsyncApi<API_TYPE_LOGIN>(
API_LOGIN,
baseLogin,
LoginProtocol LoginProtocol
) )
export const getUserInfo = defineAsyncApi<API_TYPE_GET_USER_INFO>( const baseGetUserInfo = (
API_GET_USER_INFO, params: UniApp.GetUserInfoOptions,
(params, { resolve, reject }) => { { resolve, reject }: CallBack
) => {
const provider = params.provider || 'weixin' const provider = params.provider || 'weixin'
const errorCallback = warpPlusErrorCallback(reject) const errorCallback = warpPlusErrorCallback(reject)
getService(provider) getService(provider)
...@@ -153,13 +161,17 @@ export const getUserInfo = defineAsyncApi<API_TYPE_GET_USER_INFO>( ...@@ -153,13 +161,17 @@ export const getUserInfo = defineAsyncApi<API_TYPE_GET_USER_INFO>(
} else { } else {
result.userInfo = userInfo result.userInfo = userInfo
} }
resolve(result as unknown as UniApp.GetUserProfileRes) resolve(result as unknown as UniApp.GetUserInfoRes)
}, errorCallback) }, errorCallback)
}) })
.catch(() => { .catch(() => {
reject('请先调用 uni.login') reject('请先调用 uni.login')
}) })
}, }
export const getUserInfo = defineAsyncApi<API_TYPE_GET_USER_INFO>(
API_GET_USER_INFO,
baseGetUserInfo,
GetUserInfoProtocol GetUserInfoProtocol
) )
...@@ -168,18 +180,16 @@ export const getUserInfo = defineAsyncApi<API_TYPE_GET_USER_INFO>( ...@@ -168,18 +180,16 @@ export const getUserInfo = defineAsyncApi<API_TYPE_GET_USER_INFO>(
*/ */
export const getUserProfile = defineAsyncApi<API_TYPE_GET_USER_PROFILE>( export const getUserProfile = defineAsyncApi<API_TYPE_GET_USER_PROFILE>(
API_GET_USER_PROFILE, API_GET_USER_PROFILE,
(params, { resolve, reject }) => { baseGetUserInfo,
return baseLogin(params, { resolve, reject })
},
GgetUserProfileProtocol GgetUserProfileProtocol
) )
export const preLogin = defineAsyncApi<API_TYPE_PRE_LOGIN>( export const preLogin = defineAsyncApi<API_TYPE_PRE_LOGIN>(
API_PRE_LOGIN, API_PRE_LOGIN,
(params, { resolve, reject }) => { ({ provider }, { resolve, reject }) => {
const successCallback = warpPlusSuccessCallback(resolve) const successCallback = warpPlusSuccessCallback(resolve)
const errorCallback = warpPlusErrorCallback(reject) const errorCallback = warpPlusErrorCallback(reject)
getService(params.provider as string) getService(provider as string)
.then((service) => service.preLogin(successCallback, errorCallback)) .then((service) => service.preLogin(successCallback, errorCallback))
.catch(errorCallback) .catch(errorCallback)
}, },
...@@ -189,11 +199,28 @@ export const preLogin = defineAsyncApi<API_TYPE_PRE_LOGIN>( ...@@ -189,11 +199,28 @@ export const preLogin = defineAsyncApi<API_TYPE_PRE_LOGIN>(
const _closeAuthView = () => const _closeAuthView = () =>
getService('univerify').then((service) => service.closeAuthView()) getService('univerify').then((service) => service.closeAuthView())
export const closeAuthView = defineAsyncApi<API_TYPE_CLOSE_AUTH_VIEW>( export const closeAuthView = defineSyncApi<API_TYPE_CLOSE_AUTH_VIEW>(
API_CLOSE_AUTH_VIEW, API_CLOSE_AUTH_VIEW,
_closeAuthView _closeAuthView
) )
export const getCheckBoxState = defineAsyncApi<API_TYPE_GET_CHECK_BOX_STATE>(
API_GET_CHECK_BOX_STATE,
(_, { resolve, reject }) => {
const successCallback = warpPlusSuccessCallback(resolve)
const errorCallback = warpPlusErrorCallback(reject)
try {
getService('univerify').then((service) => {
// @ts-expect-error
const state = service.getCheckBoxState()
successCallback({ state })
})
} catch (error) {
errorCallback(error)
}
}
)
/** /**
* 一键登录自定义登陆按钮点击处理 * 一键登录自定义登陆按钮点击处理
*/ */
......
...@@ -922,10 +922,10 @@ ...@@ -922,10 +922,10 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
"@dcloudio/types@^2.4.7": "@dcloudio/types@^2.5.0":
version "2.4.7" version "2.5.0"
resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.4.7.tgz#2fcf7098489e75c394a2a7c9f4e537a19269547e" resolved "https://registry.nlark.com/@dcloudio/types/download/@dcloudio/types-2.5.0.tgz#fe25f19166a9ba091213f72a8a07e1704d205134"
integrity sha512-qaBuauchKYBxU1XaSXDB81d0vizNiaaFfHGDuOdvIL8CkG0xI9tw0A3KUbmJ/UGEE2gSg3eE7hujdarspyJiXg== integrity sha1-/iXxkWapugkSE/cqigfhcE0gUTQ=
"@eslint/eslintrc@^0.4.3": "@eslint/eslintrc@^0.4.3":
version "0.4.3" version "0.4.3"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册