GetLoginUrl.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const API = require("../../../lib/API")
  2. const axios = require('axios')
  3. const config = require('../../../config.json')
  4. const { BaseStdResponse } = require("../../../BaseStdResponse");
  5. const https = require("https")
  6. class GetLoginUrl extends API {
  7. constructor() {
  8. super();
  9. this.setPath('/UniLogin/GetLoginUrl')
  10. this.setMethod('GET')
  11. }
  12. async onRequest(req, res) {
  13. let { type } = req.query
  14. const uniConfig = config.unilogin
  15. let url = `${uniConfig.url}/connect.php?act=login&appid=${uniConfig.appid}&appkey=${uniConfig.appkey}&type=${type || 'qq'}&redirect_uri=${encodeURIComponent(uniConfig.return_url)}`
  16. try {
  17. const r = await axios.get(url, {
  18. proxy: false,
  19. httpsAgent: new https.Agent({
  20. rejectUnauthorized: false
  21. })
  22. })
  23. if (!r || r.data?.code !== 0 || !r.data?.url) {
  24. this.logger.error(`获取聚合登录链接失败!${r.data?.msg || 'api接口错误'}`)
  25. return res.json({
  26. ...BaseStdResponse.ERR,
  27. msg: '请尝试其他登录方式或稍后再试'
  28. })
  29. }
  30. res.json({
  31. ...BaseStdResponse.OK,
  32. data: r.data.url
  33. })
  34. } catch (error) {
  35. this.logger.error(`获取聚合登录链接失败!${error.message || 'api接口错误'}`)
  36. return res.json({
  37. ...BaseStdResponse.ERR,
  38. msg: '请尝试其他登录方式或稍后再试!'
  39. })
  40. }
  41. }
  42. }
  43. module.exports.GetLoginUrl = GetLoginUrl;