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

feat: 移除一键登录、短信的调用凭据

上级 dcb60caa
const {
setMobileVerifyCode
} = require('./verify-code')
const {
getVerifyCode
} = require('../../common/utils')
/**
* 发送短信
* @param {object} param
* @param {string} param.mobile 手机号
* @param {object} param.code 可选,验证码
* @param {object} param.scene 短信场景
* @param {object} param.templateId 可选,短信模板id
* @returns
*/
async function sendSmsCode ({
mobile,
code,
scene,
templateId
} = {}) {
const requiredParams = [
'name',
'smsKey',
'smsSecret',
'codeExpiresIn'
]
const smsConfig = (this.config.service && this.config.service.sms) || {}
for (let i = 0; i < requiredParams.length; i++) {
const key = requiredParams[i]
if (!smsConfig[key]) {
throw new Error(`Missing config param: service.sms.${key}`)
}
}
if (!code) {
code = getVerifyCode()
}
let action
switch (scene) {
case 'login-by-sms':
action = this.t('login')
break
default:
action = this.t('verify-mobile')
break
}
const sceneConfig = (smsConfig.scene || {})[scene] || {}
if (!templateId) {
templateId = sceneConfig.templateId
}
if (!templateId) {
throw new Error('"templateId" is required')
}
const codeExpiresIn = sceneConfig.codeExpiresIn || smsConfig.codeExpiresIn
await setMobileVerifyCode.call(this, {
mobile,
code,
expiresIn: codeExpiresIn,
scene
})
await uniCloud.sendSms({
smsKey: smsConfig.smsKey,
smsSecret: smsConfig.smsSecret,
phone: mobile,
templateId,
data: {
name: smsConfig.name,
code,
action,
expMinute: '' + Math.round(codeExpiresIn / 60)
}
})
return {
errCode: 0
}
}
module.exports = {
sendSmsCode
}
const {
setMobileVerifyCode
} = require('./verify-code')
const {
getVerifyCode
} = require('../../common/utils')
/**
* 发送短信
* @param {object} param
* @param {string} param.mobile 手机号
* @param {object} param.code 可选,验证码
* @param {object} param.scene 短信场景
* @param {object} param.templateId 可选,短信模板id
* @returns
*/
async function sendSmsCode ({
mobile,
code,
scene,
templateId
} = {}) {
const requiredParams = [
'name',
'codeExpiresIn'
]
const smsConfig = (this.config.service && this.config.service.sms) || {}
for (let i = 0; i < requiredParams.length; i++) {
const key = requiredParams[i]
if (!smsConfig[key]) {
throw new Error(`Missing config param: service.sms.${key}`)
}
}
if (!code) {
code = getVerifyCode()
}
let action
switch (scene) {
case 'login-by-sms':
action = this.t('login')
break
default:
action = this.t('verify-mobile')
break
}
const sceneConfig = (smsConfig.scene || {})[scene] || {}
if (!templateId) {
templateId = sceneConfig.templateId
}
if (!templateId) {
throw new Error('"templateId" is required')
}
const codeExpiresIn = sceneConfig.codeExpiresIn || smsConfig.codeExpiresIn
await setMobileVerifyCode.call(this, {
mobile,
code,
expiresIn: codeExpiresIn,
scene
})
await uniCloud.sendSms({
smsKey: smsConfig.smsKey,
smsSecret: smsConfig.smsSecret,
phone: mobile,
templateId,
data: {
name: smsConfig.name,
code,
action,
expMinute: '' + Math.round(codeExpiresIn / 60)
}
})
return {
errCode: 0
}
}
module.exports = {
sendSmsCode
}
async function getPhoneNumber ({
// eslint-disable-next-line camelcase
access_token,
openid
} = {}) {
const requiredParams = ['apiKey', 'apiSecret']
const univerifyConfig = (this.config.service && this.config.service.univerify) || {}
for (let i = 0; i < requiredParams.length; i++) {
const key = requiredParams[i]
if (!univerifyConfig[key]) {
throw new Error(`Missing config param: service.univerify.${key}`)
}
}
return uniCloud.getPhoneNumber({
provider: 'univerify',
appid: this.getUniversalClientInfo().appId,
apiKey: univerifyConfig.apiKey,
apiSecret: univerifyConfig.apiSecret,
// eslint-disable-next-line camelcase
access_token,
openid
})
}
module.exports = {
getPhoneNumber
}
async function getPhoneNumber ({
// eslint-disable-next-line camelcase
access_token,
openid
} = {}) {
const requiredParams = []
const univerifyConfig = (this.config.service && this.config.service.univerify) || {}
for (let i = 0; i < requiredParams.length; i++) {
const key = requiredParams[i]
if (!univerifyConfig[key]) {
throw new Error(`Missing config param: service.univerify.${key}`)
}
}
return uniCloud.getPhoneNumber({
provider: 'univerify',
appid: this.getUniversalClientInfo().appId,
apiKey: univerifyConfig.apiKey,
apiSecret: univerifyConfig.apiSecret,
// eslint-disable-next-line camelcase
access_token,
openid
})
}
module.exports = {
getPhoneNumber
}
function isMobileCodeSupported () {
const config = this.config
return !!(config.service && config.service.sms && config.service.sms.smsKey)
}
function isUniverifySupport () {
const config = this.config
return !!(config.service && config.service.univerify && config.service.univerify.apiKey)
}
function isWeixinSupported () {
this.configUtils.getOauthConfig({
provider: 'weixin'
})
return true
}
function isQQSupported () {
this.configUtils.getOauthConfig({
provider: 'qq'
})
return true
}
function isAppleSupported () {
this.configUtils.getOauthConfig({
provider: 'apple'
})
return true
}
function isAlipaySupported () {
this.configUtils.getOauthConfig({
provider: 'alipay'
})
return true
}
const loginTypeTester = {
'mobile-code': isMobileCodeSupported,
univerify: isUniverifySupport,
weixin: isWeixinSupported,
qq: isQQSupported,
apple: isAppleSupported,
alipay: isAlipaySupported
}
/**
* 获取支持的登录方式
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type
* @returns
*/
module.exports = async function () {
const supportedLoginType = [
'username-password',
'mobile-password',
'email-password'
]
for (const type in loginTypeTester) {
try {
if (loginTypeTester[type].call(this)) {
supportedLoginType.push(type)
}
} catch (error) { }
}
return {
errCode: 0,
errMsg: '',
supportedLoginType
}
}
function isMobileCodeSupported () {
const config = this.config
return !!(config.service && config.service.sms && config.service.sms.smsKey)
}
function isUniverifySupport () {
return true
}
function isWeixinSupported () {
this.configUtils.getOauthConfig({
provider: 'weixin'
})
return true
}
function isQQSupported () {
this.configUtils.getOauthConfig({
provider: 'qq'
})
return true
}
function isAppleSupported () {
this.configUtils.getOauthConfig({
provider: 'apple'
})
return true
}
function isAlipaySupported () {
this.configUtils.getOauthConfig({
provider: 'alipay'
})
return true
}
const loginTypeTester = {
'mobile-code': isMobileCodeSupported,
univerify: isUniverifySupport,
weixin: isWeixinSupported,
qq: isQQSupported,
apple: isAppleSupported,
alipay: isAlipaySupported
}
/**
* 获取支持的登录方式
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type
* @returns
*/
module.exports = async function () {
const supportedLoginType = [
'username-password',
'mobile-password',
'email-password'
]
for (const type in loginTypeTester) {
try {
if (loginTypeTester[type].call(this)) {
supportedLoginType.push(type)
}
} catch (error) { }
}
return {
errCode: 0,
errMsg: '',
supportedLoginType
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册