@aliedu/autologin

3.2.5 • Public • Published

非常重要 !!!!!

出于安全因素,3.0.5 以上不再直接返回 token,而是返回 cubeUserId,需要改造为后端之间通过 cubeUserId+AK/SK 的方式获取用户登录态

教育魔方登录 sdk

集成 sdk

执行集成登录模块

npm install --save @aliedu/autologin

获取魔方用户 id

浙里办/浙里办小程序/钉钉获取 cubeUserId

特别注意!!! 调用该方法前,请先联系费司添加域名白名单,否则会因为跨域而无法调用成功。!!!!!!!!!

在发送需要登录态的请求前需要获取用户信息,前端通过 sdk 获取:

import { getCubeUserId } from "@aliedu/autologin"

let accessKey = "xxxxxxxxxxxxxxxxx"
async function yourfunction(accessKey, isDebug, isZXM) {
  try {
    const cubeUserId = await getCubeUserId(accessKey, isDebug, isZXM)
    // 自己项目中的网络请求
    xxxRequest(cubeUserId)
  } catch (error) {
    console.log("出错了")
    console.log(error)
  }
}

yourfunction(accessKey, isDebug, isZXM)

注意:

  • 获取 cubeUserId 是一个异步操作,请确保异步完成后再进行需要登录态的网络请求

  • getCubeUserId 参数:

    • accessKey: 应用的 AK.

    • isDebug: 是否是调试状态,是的话走日常环境,不是的话走线上环境。默认是线上环境

    • isZXM: 是否是浙学码应用,浙学码应用会走浙学码注册流程,否则未注册魔方用户时会报错

短信验证码获取 cubeUserId

其他环境下,可以通过短信验证码获取 token。

获取短信验证码:

import { getSmsCode } from "@aliedu/autologin"

async function yourfunction(isDebug, mobile) {
  try {
    const res = await getSmsCode(mobile, isDebug)
    if (res.success) {
      // 成功的处理逻辑
    } else {
      // 失败的处理逻辑
    }
  } catch (error) {
    console.log("出错了")
    console.log(error)
  }
}

yourfunction(isDebug, mobile)

短信验证码换取 token:

import { getCubeUserIdByPhone } from "@aliedu/autologin"

async function yourfunction(isDebug, idCard, mobile, smsCode, name, isZXM) {
  try {
    const sessionId = await getCubeUserIdByPhone(
      idCard,
      mobile,
      smsCode,
      isZXM,
      name,
      isDebug
    )
    // 自己项目中的网络请求
    xxxRequest(token)
  } catch (error) {
    console.log("出错了")
    console.log(error)
  }
}

yourfunction(isDebug, idCard, mobile, smsCode, name, isZXM)

注意:

  • getCubeUserIdByPhone 参数:

    • idCard: 身份证信息 必填

    • mobile: 手机号 必填

    • smsCode: 验证码 必填

    • isZXM: 是否是浙学码应用 选填,默认是否

    • name: 用户姓名 选填,isZXM 为 true 时建议填入

    • isDebug: 是否是调试状态,是的话走立场环境,不是的话走线上环境

是否授权

获取是否已授权状态。未授权会跳转授权界面,授权成功后,返回结果为 true。 注意,仅钉钉支持(浙里办里无法授权)。

import { authUserInfo } from "@aliedu/autologin"

async function yourfunction(isDebug, token) {
  try {
    const res = await authUserInfo(isDebug)
    if (res) {
      // 去获取身份信息
    }
  } catch (error) {
    console.log("出错了")
    console.log(error)
  }
}

yourfunction(isDebug, token)

注意:

  • authUserInfo 参数:
    • isDebug: 是否是调试状态,是的话走立场环境,不是的话走线上环境
  • 授权成功 res 为 true,否则通过 catch 获取 error 信息

浙学码集成

集成

浙学码通过 iframe 嵌入的方式集成。

<iframe
  id="ifra"
  src="{qrCodeUrl}"
  frameborder="0"
  scrolling="no"
  width="350px"
/>

qrCodeUrl

非常重要,qrCodeUrl 改为了 v2 版本

动态高度

iframe 高度动态获取:

window.addEventListener("message", function (e) {
  if (e.data.type && e.data.type === "zhexuema" && e.data.data.frame) {
    console.log("外侧--- 接收到 frame 信息")
    console.log(e.data)
    var frame = document.getElementById("ifra")
    frame.style.height = e.data.data.frame.height + "px"
    frame.style.width = e.data.data.frame.width + "px"
  }
})

浙学码将在 DOMContentLoaded 时发送消息,请先于此监听,否则将无法获取到高度信息

登录态获取成功的通知

在调用 getCubeUserId 方法后,浙学码引擎会自动获取到登录态。第一次调用 getCubeUserId 后要通知浙学码引擎刷新页面。

通常情况下不需要传递空的 userInfo 对象,即可刷新页面

<iframe id="ifra" src="{qrCodeUrl}" frameborder="0" scrolling="no" />

<script>
  var frame = document.getElementById("ifra")
  frame.contentWindow.postMessage(
    {
      type: "zhexuema",
      data: {
        userInfo: {},
      },
    },
    "*"
  )
</script>

如果是家长代亮码的场景,需要传递参数:

  • 是否代亮码: represent
  • 学生 id: studentUserId
<iframe id="ifra" src="{qrCodeUrl}" frameborder="0" scrolling="no" />

<script>
  var frame = document.getElementById("ifra")
  frame.contentWindow.postMessage(
    {
      type: "zhexuema",
      data: {
        userInfo: {
          represent: true,
          studentUserId: "123456",
        },
      },
    },
    "*"
  )
</script>

同样需要注意此方法的调用时机,需要等到 iframe 中的监听注册完毕才可发送,否则内部无法获取到 token 信息。建议在获取到 哲学吗所在 iframe 发出的动态高度信息的回调中发送 token。接受到 token 后,将请求亮码接口

Dependencies (2)

Dev Dependencies (23)

Package Sidebar

Install

npm i @aliedu/autologin

Weekly Downloads

2

Version

3.2.5

License

none

Unpacked Size

60 kB

Total Files

3

Last publish

Collaborators

  • jianting.bjt
  • zch759740844
  • zhaojian6516