提交 b47d5cc9 编写于 作者: 雪洛's avatar 雪洛

refactor: config utils

上级 17c1aaca
......@@ -163,25 +163,6 @@ function coverMobile (mobile) {
return mobile.slice(0, 3) + '****' + mobile.slice(7)
}
function getOauthConfig (params = {}) {
const {
config,
oatuhProivder,
clientPlatform,
requiredItem = []
} = params
if (!config.oauth || !config.oauth[oatuhProivder]) {
throw new Error(`Missing config param: ${clientPlatform}.${oatuhProivder}`)
}
const oauthConfig = config.oauth[oatuhProivder]
requiredItem.forEach((item) => {
if (!oauthConfig[item]) {
throw new Error(`Missing config param: ${clientPlatform}.${oatuhProivder}.${item}`)
}
})
return oauthConfig
}
module.exports = {
getType,
isValidString,
......@@ -196,6 +177,5 @@ module.exports = {
snake2camelJson,
getExtension,
getVerifyCode,
coverMobile,
getOauthConfig
coverMobile
}
......@@ -102,10 +102,10 @@ module.exports = {
})
// 包含uni-id配置合并等功能的工具集
this.config = new ConfigUtils({
appId: clientInfo.appId,
platform: clientPlatform
}).getPlatformConfig()
this.configUtils = new ConfigUtils({
context: this
})
this.config = this.configUtils.getPlatformConfig()
this.validator = new Validator()
/**
......
......@@ -4,54 +4,31 @@ const AliAccount = require('./alipay/account/index')
const AppleAccount = require('./apple/account/index')
const createApi = require('./share/create-api')
const {
getOauthConfig
} = require('../../common/utils')
module.exports = {
initWeixin: function () {
const oauthConfig = getOauthConfig({
config: this.config,
oatuhProivder: 'weixin',
clientPlatform: this.clientPlatform,
requiredItem: ['appid', 'appsecret']
})
const oauthConfig = this.configUtils.getOauthConfig({ provider: 'weixin' })
return createApi(WxAccount, {
appId: oauthConfig.appid,
secret: oauthConfig.appsecret
})
},
initQQ: function () {
const oauthConfig = getOauthConfig({
config: this.config,
oatuhProivder: 'qq',
clientPlatform: this.clientPlatform,
requiredItem: ['appid', 'appsecret']
})
const oauthConfig = this.configUtils.getOauthConfig({ provider: 'qq' })
return createApi(QQAccount, {
appId: oauthConfig.appid,
secret: oauthConfig.appsecret
})
},
initAlipay: function () {
const oauthConfig = getOauthConfig({
config: this.config,
oatuhProivder: 'alipay',
clientPlatform: this.clientPlatform,
requiredItem: ['appid', 'privateKey']
})
const oauthConfig = this.configUtils.getOauthConfig({ provider: 'alipay' })
return createApi(AliAccount, {
appId: oauthConfig.appid,
privateKey: oauthConfig.privateKey
})
},
initApple: function () {
const oauthConfig = getOauthConfig({
config: this.config,
oatuhProivder: 'apple',
clientPlatform: this.clientPlatform,
requiredItem: ['bundleId']
})
const oauthConfig = this.configUtils.getOauthConfig({ provider: 'apple' })
return createApi(AppleAccount, {
bundleId: oauthConfig.bundleId
})
......
const {
getWeixinPlatform
} = require('./weixin')
const requiredConfig = {
'web.h5-weixin': ['appid', 'appsecret'],
'web.web-weixin': ['appid', 'appsecret'],
'app.weixin': ['appid', 'appsecret'],
'mp-weixin.weixin': ['appid', 'appsecret'],
'app.qq': ['appid', 'appsecret'],
'mp-alipay.alipay': ['appid', 'privateKey'],
'app.apple': ['bundleId']
}
class ConfigUtils {
constructor ({
appId,
platform
context
} = {}) {
this.context = context
this.clientInfo = context.getClientInfo()
const {
appId,
uniPlatform
} = this.clientInfo
this.appId = appId
this.platform = platform
switch (uniPlatform) {
case 'app':
case 'app-plus':
this.platform = 'app'
break
case 'web':
case 'h5':
this.platform = 'web'
break
default:
this.platform = uniPlatform
break
}
}
getConfigArray () {
......@@ -47,6 +77,41 @@ class ConfigUtils {
}
return Object.assign(defaultConfig, appConfig, appConfig[platform])
}
getOauthProvider ({
provider
} = {}) {
const clientPlatform = this.platform
let oatuhProivder = provider
if (provider === 'weixin' && clientPlatform === 'web') {
const weixinPlatform = getWeixinPlatform.call(this.context)
if (weixinPlatform === 'h5' || weixinPlatform === 'web') {
oatuhProivder = weixinPlatform + '-weixin' // h5-weixin 公众号,web-weixin pc端
}
}
return oatuhProivder
}
getOauthConfig ({
provider
} = {}) {
const config = this.getPlatformConfig()
const clientPlatform = this.platform
const oatuhProivder = this.getOauthConfig({
provider
})
const requireConfigKey = requiredConfig[`${clientPlatform}.${oatuhProivder}`] || []
if (!config.oauth || !config.oauth[oatuhProivder]) {
throw new Error(`Config param required: ${clientPlatform}.${oatuhProivder}`)
}
const oauthConfig = config.oauth[oatuhProivder]
requireConfigKey.forEach((item) => {
if (!oauthConfig[item]) {
throw new Error(`Config param required: ${clientPlatform}.${oatuhProivder}.${item}`)
}
})
return oauthConfig
}
}
module.exports = ConfigUtils
......@@ -5,19 +5,13 @@ const {
const {
ERROR
} = require('../../common/error')
const {
getOauthConfig
} = require('../../common/utils')
function decryptWeixinData ({
encryptedData,
sessionKey,
iv
} = {}) {
const oauthConfig = getOauthConfig({
config: this.config,
oatuhProivder: 'weixin',
clientPlatform: this.clientPlatform,
requiredItem: ['appid']
const oauthConfig = this.configUtils.getOauthConfig({
provider: 'weixin'
})
const decipher = crypto.createDecipheriv(
'aes-128-cbc',
......
function isMobileCodeSupported (config) {
function isMobileCodeSupported () {
const config = this.config
return !!(config.service && config.service.sms && config.service.sms.smsKey)
}
function isUniverifySupport (config) {
function isUniverifySupport () {
const config = this.config
return !!(config.service && config.service.univerify && config.service.univerify.apiKey)
}
function isWeixinSupported (config) {
return !!(config.oauth && config.oauth.weixin && config.oauth.weixin.appsecret)
function isWeixinSupported () {
this.configUtils.getOauthConfig({
provider: 'weixin'
})
return true
}
function isQQSupported (config) {
return !!(config.oauth && config.oauth.qq && config.oauth.qq.appsecret)
function isQQSupported () {
this.configUtils.getOauthConfig({
provider: 'qq'
})
return true
}
function isAppleSupported (config) {
return !!(config.oauth && config.oauth.apple && config.oauth.apple.bundleId)
function isAppleSupported () {
this.configUtils.getOauthConfig({
provider: 'apple'
})
return true
}
function isAlipaySupported (config) {
return !!(config.oauth && config.oauth.alipay && config.oauth.alipay.privateKey)
function isAlipaySupported () {
this.configUtils.getOauthConfig({
provider: 'alipay'
})
return true
}
const loginTypeTester = {
......@@ -31,49 +45,23 @@ const loginTypeTester = {
alipay: isAlipaySupported
}
const ConfigUtils = require('../../lib/utils/config')
/**
* 获取支持的登录方式
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type
* @param {Object} params
* @param {String} params.appId 应用AppId
* @param {String} params.platform 应用平台
* @returns
*/
module.exports = function (params = {}) {
const schema = {
appId: {
type: 'string',
required: false
},
platform: {
type: 'string',
required: false
}
}
this.middleware.validate(params, schema)
const {
appId,
platform
} = params
const {
appId: currentAppId
} = this.getClientInfo()
const currentPlatform = this.clientPlatform
const config = new ConfigUtils({
appId: appId || currentAppId,
platform: platform || currentPlatform
}).getPlatformConfig()
module.exports = function () {
const supportedLoginType = [
'username-password',
'mobile-password',
'email-password'
]
for (const type in loginTypeTester) {
if (loginTypeTester[type](config)) {
supportedLoginType.push(type)
}
try {
if (loginTypeTester[type].call(this)) {
supportedLoginType.push(type)
}
} catch (error) {}
}
return {
errCode: 0,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册