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

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

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