Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qianlong66
uni-app
提交
77ddcff3
U
uni-app
项目概览
qianlong66
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
77ddcff3
编写于
6月 25, 2021
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(App): oauth
上级
1729a464
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
278 addition
and
0 deletion
+278
-0
packages/uni-api/src/index.ts
packages/uni-api/src/index.ts
+1
-0
packages/uni-api/src/protocols/plugin/oauth.ts
packages/uni-api/src/protocols/plugin/oauth.ts
+50
-0
packages/uni-app-plus/src/service/api/index.ts
packages/uni-app-plus/src/service/api/index.ts
+1
-0
packages/uni-app-plus/src/service/api/plugin/oauth.ts
packages/uni-app-plus/src/service/api/plugin/oauth.ts
+226
-0
未找到文件。
packages/uni-api/src/index.ts
浏览文件 @
77ddcff3
...
...
@@ -77,6 +77,7 @@ export * from './protocols/ui/tabBar'
export
*
from
'
./protocols/ui/window
'
export
*
from
'
./protocols/plugin/getProvider
'
export
*
from
'
./protocols/plugin/oauth
'
// helpers
export
{
defineOnApi
,
...
...
packages/uni-api/src/protocols/plugin/oauth.ts
0 → 100644
浏览文件 @
77ddcff3
export
const
API_LOGIN
=
'
login
'
export
type
API_TYPE_LOGIN
=
typeof
uni
.
login
export
const
LoginProtocol
:
ApiProtocol
<
API_TYPE_LOGIN
>
=
{
provider
:
String
as
any
,
scopes
:
[
String
,
Array
]
as
any
,
timeout
:
Number
,
univerifyStyle
:
Object
as
any
,
}
export
const
API_GET_USER_INFO
=
'
getUserInfo
'
export
type
API_TYPE_GET_USER_INFO
=
typeof
uni
.
getUserInfo
export
const
GetUserInfoProtocol
:
ApiProtocol
<
API_TYPE_GET_USER_INFO
>
=
{
provider
:
String
as
any
,
withCredentials
:
Boolean
,
timeout
:
Number
,
lang
:
String
,
}
export
const
API_GET_USER_PROFILE
=
'
ggetUserProfilegetUserProfile
'
export
type
API_TYPE_GET_USER_PROFILE
=
typeof
uni
.
getUserProfile
export
const
GgetUserProfileProtocol
:
ApiProtocol
<
API_TYPE_GET_USER_PROFILE
>
=
{
provider
:
String
as
any
,
withCredentials
:
Boolean
,
timeout
:
Number
,
lang
:
String
as
any
,
}
export
const
API_PRE_LOGIN
=
'
preLogin
'
export
type
API_TYPE_PRE_LOGIN
=
typeof
uni
.
preLogin
const
provider
=
{
UNIVERIFY
:
'
univerify
'
,
}
export
const
PreLoginOptions
:
ApiOptions
<
API_TYPE_PRE_LOGIN
>
=
{
formatArgs
:
{
provider
(
value
,
parmas
)
{
if
(
Object
.
values
(
provider
).
indexOf
(
String
(
value
))
<
0
)
{
return
'
provider error
'
}
},
},
}
export
const
PreLoginProtocol
:
ApiProtocol
<
API_TYPE_PRE_LOGIN
>
=
{
provider
:
{
type
:
String
as
any
,
required
:
true
,
},
}
export
const
API_CLOSE_AUTH_VIEW
=
'
closeAuthView
'
export
type
API_TYPE_CLOSE_AUTH_VIEW
=
typeof
uni
.
closeAuthView
packages/uni-app-plus/src/service/api/index.ts
浏览文件 @
77ddcff3
...
...
@@ -32,3 +32,4 @@ export * from './ui/popup/showActionSheet'
export
*
from
'
./ui/popup/showToast
'
export
*
from
'
./plugin/getProvider
'
export
*
from
'
./plugin/oauth
'
packages/uni-app-plus/src/service/api/plugin/oauth.ts
0 → 100644
浏览文件 @
77ddcff3
import
{
API_LOGIN
,
API_TYPE_LOGIN
,
LoginProtocol
,
API_GET_USER_INFO
,
API_TYPE_GET_USER_INFO
,
GetUserInfoProtocol
,
API_GET_USER_PROFILE
,
API_TYPE_GET_USER_PROFILE
,
GgetUserProfileProtocol
,
API_PRE_LOGIN
,
API_TYPE_PRE_LOGIN
,
PreLoginOptions
,
PreLoginProtocol
,
API_CLOSE_AUTH_VIEW
,
API_TYPE_CLOSE_AUTH_VIEW
,
defineAsyncApi
,
}
from
'
@dcloudio/uni-api
'
import
{
isPlainObject
}
from
'
@vue/shared
'
import
{
warpPlusSuccessCallback
,
warpPlusErrorCallback
,
}
from
'
../../../helpers/plus
'
type
Provider
=
PlusOauthAuthService
function
getService
(
provider
:
string
):
Promise
<
Provider
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
plus
.
oauth
.
getServices
((
services
)
=>
{
const
service
=
services
.
find
(({
id
})
=>
id
===
provider
)
service
?
resolve
(
service
)
:
reject
(
new
Error
(
'
provider not find
'
))
},
reject
)
})
}
/**
* 微信登录
*/
const
baseLogin
=
(
params
:
UniApp
.
LoginOptions
,
{
resolve
,
reject
,
}:
{
resolve
:
(
res
:
any
)
=>
void
reject
:
(
errMsg
:
string
,
errRes
?:
any
)
=>
void
}
)
=>
{
const
provider
=
params
.
provider
||
'
weixin
'
const
errorCallback
=
warpPlusErrorCallback
(
reject
)
getService
(
provider
)
.
then
((
service
)
=>
{
function
login
()
{
service
.
login
(
(
res
)
=>
{
const
authResult
=
res
.
target
.
authResult
resolve
({
code
:
authResult
.
code
,
authResult
:
authResult
,
})
},
errorCallback
,
provider
===
'
apple
'
?
{
scope
:
'
email
'
}
:
{
univerifyStyle
:
univerifyButtonsClickHandling
(
params
.
univerifyStyle
!
,
errorCallback
),
}
||
{}
)
}
// 先注销再登录
// apple登录logout之后无法重新触发获取email,fullname;一键登录无logout
if
(
provider
===
'
apple
'
||
provider
===
'
univerify
'
)
{
login
()
}
else
{
service
.
logout
(
login
,
login
)
}
})
.
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
provider
=
params
.
provider
||
'
weixin
'
const
errorCallback
=
warpPlusErrorCallback
(
reject
)
getService
(
provider
)
.
then
((
loginService
)
=>
{
loginService
.
getUserInfo
((
res
)
=>
{
let
userInfo
:
(
PlusOauthAppleInfo
|
PlusOauthUserInfo
)
&
{
nickName
?:
string
openId
?:
string
avatarUrl
?:
string
openid
?:
PlusOauthUserInfo
[
'
openid
'
]
nickname
?:
PlusOauthUserInfo
[
'
nickname
'
]
gender
?:
PlusOauthUserInfo
[
'
sex
'
]
headimgurl
?:
PlusOauthUserInfo
[
'
headimgurl
'
]
}
=
{
nickName
:
''
}
if
(
provider
===
'
weixin
'
)
{
const
wechatUserInfo
=
loginService
.
userInfo
if
(
wechatUserInfo
)
userInfo
=
{
openId
:
wechatUserInfo
.
openid
,
nickName
:
wechatUserInfo
.
nickname
,
gender
:
wechatUserInfo
.
sex
,
city
:
wechatUserInfo
.
city
,
province
:
wechatUserInfo
.
province
,
country
:
wechatUserInfo
.
country
,
avatarUrl
:
wechatUserInfo
.
headimgurl
,
// @ts-ignore
unionId
:
wechatUserInfo
.
unionid
,
}
}
else
if
(
provider
===
'
apple
'
)
{
const
appleInfo
=
loginService
.
appleInfo
if
(
appleInfo
)
userInfo
=
{
openId
:
appleInfo
.
user
,
fullName
:
appleInfo
.
fullName
,
email
:
appleInfo
.
email
,
authorizationCode
:
appleInfo
.
authorizationCode
,
identityToken
:
appleInfo
.
identityToken
,
realUserStatus
:
appleInfo
.
realUserStatus
,
}
}
else
{
userInfo
=
loginService
.
userInfo
!
if
(
userInfo
)
{
userInfo
.
openId
=
userInfo
.
openId
||
userInfo
.
openid
||
loginService
.
authResult
!
.
openid
userInfo
.
nickName
=
userInfo
.
nickName
||
userInfo
.
nickname
userInfo
.
avatarUrl
=
userInfo
.
avatarUrl
||
userInfo
.
headimgurl
}
}
let
result
:
Data
=
{}
// @ts-ignore
if
(
params
.
data
&&
params
.
data
.
api_name
===
'
webapi_getuserinfo
'
)
{
result
.
data
=
{
data
:
JSON
.
stringify
(
userInfo
),
rawData
:
''
,
signature
:
''
,
encryptedData
:
''
,
iv
:
''
,
}
}
else
{
result
.
userInfo
=
userInfo
}
resolve
(
result
as
unknown
as
UniApp
.
GetUserProfileRes
)
},
errorCallback
)
})
.
catch
(()
=>
{
reject
(
'
请先调用 uni.login
'
)
})
},
GetUserInfoProtocol
)
/**
* 获取用户信息-兼容
*/
export
const
getUserProfile
=
defineAsyncApi
<
API_TYPE_GET_USER_PROFILE
>
(
API_GET_USER_PROFILE
,
(
params
,
{
resolve
,
reject
})
=>
{
return
baseLogin
(
params
,
{
resolve
,
reject
})
},
GgetUserProfileProtocol
)
export
const
preLogin
=
defineAsyncApi
<
API_TYPE_PRE_LOGIN
>
(
API_PRE_LOGIN
,
(
params
,
{
resolve
,
reject
})
=>
{
const
successCallback
=
warpPlusSuccessCallback
(
resolve
)
const
errorCallback
=
warpPlusErrorCallback
(
reject
)
getService
(
params
.
provider
as
string
)
.
then
((
service
)
=>
service
.
preLogin
(
successCallback
,
errorCallback
))
.
catch
(
errorCallback
)
},
PreLoginProtocol
,
PreLoginOptions
)
const
_closeAuthView
=
()
=>
getService
(
'
univerify
'
).
then
((
service
)
=>
service
.
closeAuthView
())
export
const
closeAuthView
=
defineAsyncApi
<
API_TYPE_CLOSE_AUTH_VIEW
>
(
API_CLOSE_AUTH_VIEW
,
_closeAuthView
)
/**
* 一键登录自定义登陆按钮点击处理
*/
function
univerifyButtonsClickHandling
(
univerifyStyle
:
PlusOauthUniverifyStyles
,
errorCallback
:
Function
)
{
if
(
univerifyStyle
&&
isPlainObject
(
univerifyStyle
)
&&
univerifyStyle
.
buttons
&&
Object
.
prototype
.
toString
.
call
(
univerifyStyle
.
buttons
.
list
)
===
'
[object Array]
'
&&
univerifyStyle
.
buttons
.
list
!
.
length
>
0
)
{
univerifyStyle
.
buttons
.
list
!
.
forEach
((
button
,
index
)
=>
{
univerifyStyle
.
buttons
!
.
list
!
[
index
].
onclick
=
function
()
{
_closeAuthView
().
then
(()
=>
{
errorCallback
({
code
:
'
30008
'
,
message
:
'
用户点击了自定义按钮
'
,
index
,
provider
:
button
.
provider
,
})
})
}
})
}
return
univerifyStyle
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录