| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- const axios = require('axios')
- const https = require('https')
- const config = require('../config.json')
- const VALID_SOCIAL_TYPES = ['qq', 'wx']
- function normalizeSocialType(type) {
- const socialType = type || 'qq'
- return VALID_SOCIAL_TYPES.includes(socialType) ? socialType : null
- }
- async function fetchUniLoginProfile(type, code) {
- const socialType = normalizeSocialType(type)
- if (!socialType)
- throw new Error('不支持的第三方登录类型')
- const uniConfig = config.unilogin
- const url = `${uniConfig.url}/connect.php?act=callback&appid=${uniConfig.appid}&appkey=${uniConfig.appkey}&type=${socialType}&code=${code}`
- const r = await axios.get(url, {
- httpsAgent: new https.Agent({
- rejectUnauthorized: false
- }),
- proxy: false
- })
- if (!r || r.data?.code !== 0)
- throw new Error(r.data?.msg || 'api接口错误')
- return {
- ...r.data,
- social_type: socialType
- }
- }
- module.exports = {
- VALID_SOCIAL_TYPES,
- normalizeSocialType,
- fetchUniLoginProfile
- }
|