Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello_uni-id-pages
提交
509c9442
H
hello_uni-id-pages
项目概览
DCloud
/
hello_uni-id-pages
通知
1054
Star
31
Fork
43
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
2
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello_uni-id-pages
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
509c9442
编写于
10月 26, 2022
作者:
雪洛
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'dev'
上级
15dff20b
4cbe46d6
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
171 addition
and
19 deletion
+171
-19
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/constants.js
...ges/uniCloud/cloudfunctions/uni-id-co/common/constants.js
+3
-0
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js
...i-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js
+7
-2
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/utils.js
...ages/uniCloud/cloudfunctions/uni-id-co/lib/utils/utils.js
+18
-0
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js
...ges/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js
+45
-9
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/login/login-by-weixin-mobile.js
...unctions/uni-id-co/module/login/login-by-weixin-mobile.js
+1
-4
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/login/login-by-weixin.js
.../cloudfunctions/uni-id-co/module/login/login-by-weixin.js
+17
-2
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/utils/index.js
...s/uniCloud/cloudfunctions/uni-id-co/module/utils/index.js
+2
-1
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/utils/secure-network-handshake-by-weixin.js
...-id-co/module/utils/secure-network-handshake-by-weixin.js
+73
-0
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/package.json
...i-id-pages/uniCloud/cloudfunctions/uni-id-co/package.json
+5
-1
未找到文件。
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/constants.js
浏览文件 @
509c9442
...
@@ -6,6 +6,8 @@ const verifyCollectionName = 'opendb-verify-codes'
...
@@ -6,6 +6,8 @@ const verifyCollectionName = 'opendb-verify-codes'
const
verifyCollection
=
db
.
collection
(
verifyCollectionName
)
const
verifyCollection
=
db
.
collection
(
verifyCollectionName
)
const
deviceCollectionName
=
'
uni-id-device
'
const
deviceCollectionName
=
'
uni-id-device
'
const
deviceCollection
=
db
.
collection
(
deviceCollectionName
)
const
deviceCollection
=
db
.
collection
(
deviceCollectionName
)
const
openDataCollectionName
=
'
opendb-open-data
'
const
openDataCollection
=
db
.
collection
(
openDataCollectionName
)
const
USER_IDENTIFIER
=
{
const
USER_IDENTIFIER
=
{
username
:
'
username
'
,
username
:
'
username
'
,
...
@@ -78,6 +80,7 @@ module.exports = {
...
@@ -78,6 +80,7 @@ module.exports = {
userCollection
,
userCollection
,
verifyCollection
,
verifyCollection
,
deviceCollection
,
deviceCollection
,
openDataCollection
,
USER_IDENTIFIER
,
USER_IDENTIFIER
,
USER_STATUS
,
USER_STATUS
,
CAPTCHA_SCENE
,
CAPTCHA_SCENE
,
...
...
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js
浏览文件 @
509c9442
...
@@ -63,7 +63,8 @@ const {
...
@@ -63,7 +63,8 @@ const {
}
=
require
(
'
./module/verify/index
'
)
}
=
require
(
'
./module/verify/index
'
)
const
{
const
{
refreshToken
,
refreshToken
,
setPushCid
setPushCid
,
secureNetworkHandshakeByWeixin
}
=
require
(
'
./module/utils/index
'
)
}
=
require
(
'
./module/utils/index
'
)
const
{
const
{
getInvitedUser
,
getInvitedUser
,
...
@@ -576,5 +577,9 @@ module.exports = {
...
@@ -576,5 +577,9 @@ module.exports = {
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-apple
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-apple
* @returns
* @returns
*/
*/
unbindApple
unbindApple
,
/**
* 安全网络握手,目前仅处理微信小程序安全网络握手
*/
secureNetworkHandshakeByWeixin
}
}
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/utils.js
0 → 100644
浏览文件 @
509c9442
let
redisEnable
=
null
function
getRedisEnable
()
{
// 未用到的时候不调用redis接口,节省一些连接数
if
(
redisEnable
!==
null
)
{
return
redisEnable
}
try
{
uniCloud
.
redis
()
redisEnable
=
true
}
catch
(
error
)
{
redisEnable
=
false
}
return
redisEnable
}
module
.
exports
=
{
getRedisEnable
}
\ No newline at end of file
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/weixin.js
浏览文件 @
509c9442
...
@@ -5,8 +5,14 @@ const {
...
@@ -5,8 +5,14 @@ const {
const
{
const
{
ERROR
ERROR
}
=
require
(
'
../../common/error
'
)
}
=
require
(
'
../../common/error
'
)
const
{
getRedisEnable
}
=
require
(
'
./utils
'
)
const
{
openDataCollection
}
=
require
(
'
../../common/constants
'
)
function
decryptWeixinData
({
function
decryptWeixinData
({
encryptedData
,
encryptedData
,
sessionKey
,
sessionKey
,
iv
iv
...
@@ -31,7 +37,7 @@ function decryptWeixinData ({
...
@@ -31,7 +37,7 @@ function decryptWeixinData ({
return
decoded
return
decoded
}
}
function
getWeixinPlatform
()
{
function
getWeixinPlatform
()
{
const
platform
=
this
.
clientPlatform
const
platform
=
this
.
clientPlatform
const
userAgent
=
this
.
getClientInfo
().
userAgent
const
userAgent
=
this
.
getClientInfo
().
userAgent
switch
(
platform
)
{
switch
(
platform
)
{
...
@@ -48,7 +54,7 @@ function getWeixinPlatform () {
...
@@ -48,7 +54,7 @@ function getWeixinPlatform () {
}
}
}
}
async
function
saveWeixinUserKey
({
async
function
saveWeixinUserKey
({
openid
,
openid
,
sessionKey
,
// 微信小程序用户sessionKey
sessionKey
,
// 微信小程序用户sessionKey
accessToken
,
// App端微信用户accessToken
accessToken
,
// App端微信用户accessToken
...
@@ -85,7 +91,36 @@ async function saveWeixinUserKey ({
...
@@ -85,7 +91,36 @@ async function saveWeixinUserKey ({
}
}
}
}
function
generateWeixinCache
({
async
function
saveSecureNetworkCache
({
code
,
openid
,
unionid
,
sessionKey
})
{
const
{
appId
}
=
this
.
getClientInfo
()
const
key
=
`uni-id:
${
appId
}
:weixin-mp:code:
${
code
}
:secure-network-cache`
const
value
=
JSON
.
stringify
({
openid
,
unionid
,
session_key
:
sessionKey
})
// 此处存储的是code的缓存,有效期两天即可
const
expiredSeconds
=
2
*
24
*
60
*
60
await
openDataCollection
.
doc
(
key
).
set
({
value
,
expired
:
Date
.
now
()
+
expiredSeconds
*
1000
})
const
isRedisEnable
=
getRedisEnable
()
if
(
isRedisEnable
)
{
const
redis
=
uniCloud
.
redis
()
await
redis
.
set
(
key
,
value
,
'
EX
'
,
expiredSeconds
)
}
}
function
generateWeixinCache
({
sessionKey
,
// 微信小程序用户sessionKey
sessionKey
,
// 微信小程序用户sessionKey
accessToken
,
// App端微信用户accessToken
accessToken
,
// App端微信用户accessToken
refreshToken
,
// App端微信用户refreshToken
refreshToken
,
// App端微信用户refreshToken
...
@@ -118,7 +153,7 @@ function generateWeixinCache ({
...
@@ -118,7 +153,7 @@ function generateWeixinCache ({
}
}
}
}
function
getWeixinOpenid
({
function
getWeixinOpenid
({
userRecord
userRecord
}
=
{})
{
}
=
{})
{
const
weixinPlatform
=
getWeixinPlatform
.
call
(
this
)
const
weixinPlatform
=
getWeixinPlatform
.
call
(
this
)
...
@@ -130,7 +165,7 @@ function getWeixinOpenid ({
...
@@ -130,7 +165,7 @@ function getWeixinOpenid ({
return
wxOpenidObj
[
`
${
weixinPlatform
}
_
${
appId
}
`
]
||
wxOpenidObj
[
weixinPlatform
]
return
wxOpenidObj
[
`
${
weixinPlatform
}
_
${
appId
}
`
]
||
wxOpenidObj
[
weixinPlatform
]
}
}
async
function
getWeixinCacheFallback
({
async
function
getWeixinCacheFallback
({
userRecord
,
userRecord
,
key
key
}
=
{})
{
}
=
{})
{
...
@@ -143,7 +178,7 @@ async function getWeixinCacheFallback ({
...
@@ -143,7 +178,7 @@ async function getWeixinCacheFallback ({
return
weixinCache
&&
weixinCache
[
key
]
return
weixinCache
&&
weixinCache
[
key
]
}
}
async
function
getWeixinCache
({
async
function
getWeixinCache
({
uid
,
uid
,
userRecord
,
userRecord
,
key
key
...
@@ -177,7 +212,7 @@ async function getWeixinCache ({
...
@@ -177,7 +212,7 @@ async function getWeixinCache ({
})
})
}
}
async
function
getWeixinAccessToken
()
{
async
function
getWeixinAccessToken
()
{
const
weixinPlatform
=
getWeixinPlatform
.
call
(
this
)
const
weixinPlatform
=
getWeixinPlatform
.
call
(
this
)
const
appId
=
this
.
getClientInfo
().
appId
const
appId
=
this
.
getClientInfo
().
appId
...
@@ -194,5 +229,6 @@ module.exports = {
...
@@ -194,5 +229,6 @@ module.exports = {
generateWeixinCache
,
generateWeixinCache
,
getWeixinCache
,
getWeixinCache
,
saveWeixinUserKey
,
saveWeixinUserKey
,
getWeixinAccessToken
getWeixinAccessToken
,
saveSecureNetworkCache
}
}
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/login/login-by-weixin-mobile.js
浏览文件 @
509c9442
...
@@ -2,10 +2,7 @@ const {
...
@@ -2,10 +2,7 @@ const {
initWeixin
initWeixin
}
=
require
(
'
../../lib/third-party/index
'
)
}
=
require
(
'
../../lib/third-party/index
'
)
const
{
const
{
getWeixinPlatform
,
getWeixinAccessToken
getWeixinAccessToken
,
generateWeixinCache
,
saveWeixinUserKey
}
=
require
(
'
../../lib/utils/weixin
'
)
}
=
require
(
'
../../lib/utils/weixin
'
)
const
{
const
{
ERROR
ERROR
...
...
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/login/login-by-weixin.js
浏览文件 @
509c9442
...
@@ -11,7 +11,8 @@ const {
...
@@ -11,7 +11,8 @@ const {
const
{
const
{
generateWeixinCache
,
generateWeixinCache
,
getWeixinPlatform
,
getWeixinPlatform
,
saveWeixinUserKey
saveWeixinUserKey
,
saveSecureNetworkCache
}
=
require
(
'
../../lib/utils/weixin
'
)
}
=
require
(
'
../../lib/utils/weixin
'
)
const
{
const
{
LOG_TYPE
LOG_TYPE
...
@@ -37,7 +38,9 @@ module.exports = async function (params = {}) {
...
@@ -37,7 +38,9 @@ module.exports = async function (params = {}) {
this
.
middleware
.
validate
(
params
,
schema
)
this
.
middleware
.
validate
(
params
,
schema
)
const
{
const
{
code
,
code
,
inviteCode
inviteCode
,
// 内部参数,暂不暴露
secureNetworkCache
=
false
}
=
params
}
=
params
const
{
const
{
appId
appId
...
@@ -81,6 +84,18 @@ module.exports = async function (params = {}) {
...
@@ -81,6 +84,18 @@ module.exports = async function (params = {}) {
expired
:
accessTokenExpired
// App端微信用户accessToken过期时间
expired
:
accessTokenExpired
// App端微信用户accessToken过期时间
}
=
getWeixinAccountResult
}
=
getWeixinAccountResult
if
(
secureNetworkCache
)
{
if
(
weixinPlatform
!==
'
mp
'
)
{
throw
new
Error
(
'
Unsupported weixin platform, expect mp-weixin
'
)
}
await
saveSecureNetworkCache
({
code
,
openid
,
unionid
,
sessionKey
})
}
const
{
const
{
type
,
type
,
user
user
...
...
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/utils/index.js
浏览文件 @
509c9442
module
.
exports
=
{
module
.
exports
=
{
refreshToken
:
require
(
'
./refresh-token
'
),
refreshToken
:
require
(
'
./refresh-token
'
),
setPushCid
:
require
(
'
./set-push-cid
'
)
setPushCid
:
require
(
'
./set-push-cid
'
),
secureNetworkHandshakeByWeixin
:
require
(
'
./secure-network-handshake-by-weixin
'
)
}
}
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/utils/secure-network-handshake-by-weixin.js
0 → 100644
浏览文件 @
509c9442
const
{
ERROR
}
=
require
(
'
../../common/error
'
)
const
{
initWeixin
}
=
require
(
'
../../lib/third-party/index
'
)
const
{
saveWeixinUserKey
,
saveSecureNetworkCache
}
=
require
(
'
../../lib/utils/weixin
'
)
const
loginByWeixin
=
require
(
'
../login/login-by-weixin
'
)
/**
* 微信安全网络握手
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
* @param {object} params
* @param {string} params.code 微信登录返回的code
* @param {boolean} params.callLoginByWeixin 是否同时调用一次微信登录
* @returns
*/
module
.
exports
=
async
function
(
params
=
{})
{
const
schema
=
{
code
:
'
string
'
,
callLoginByWeixin
:
{
type
:
'
boolean
'
,
required
:
false
}
}
this
.
middleware
.
validate
(
params
,
schema
)
let
platform
=
this
.
clientPlatform
if
(
platform
!==
'
mp-weixin
'
)
{
throw
new
Error
(
`[secureNetworkHandshake] platform
${
platform
}
is not supported`
)
}
const
{
code
,
callLoginByWeixin
=
false
}
=
params
if
(
callLoginByWeixin
)
{
return
loginByWeixin
.
call
(
this
,
{
code
,
secureNetworkCache
:
true
})
}
const
weixinApi
=
initWeixin
.
call
(
this
)
let
getWeixinAccountResult
try
{
getWeixinAccountResult
=
await
weixinApi
.
code2Session
(
code
)
}
catch
(
error
)
{
console
.
error
(
error
)
throw
{
errCode
:
ERROR
.
GET_THIRD_PARTY_ACCOUNT_FAILED
}
}
const
{
openid
,
unionid
,
sessionKey
// 微信小程序用户sessionKey
}
=
getWeixinAccountResult
await
saveSecureNetworkCache
.
call
(
this
,
{
code
,
openid
,
unionid
,
sessionKey
})
await
saveWeixinUserKey
.
call
(
this
,
{
openid
,
sessionKey
})
return
{
errCode
:
0
}
}
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/package.json
浏览文件 @
509c9442
...
@@ -14,6 +14,10 @@
...
@@ -14,6 +14,10 @@
"uni-open-bridge-common"
:
"file:../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common"
"uni-open-bridge-common"
:
"file:../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common"
},
},
"extensions"
:
{
"extensions"
:
{
"uni-cloud-sms"
:
{}
"uni-cloud-sms"
:
{},
"uni-cloud-redis"
:
{}
},
"cloudfunction-config"
:
{
"keepRunningAfterReturn"
:
false
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录