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

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

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