0.2.5 • Published 3 years ago

@jk998/identity v0.2.5

Weekly downloads
2
License
-
Repository
-
Last release
3 years ago

CTOC

@jk998/identity

统一认证授权库,集成 Auth & Socket

前置依赖

yarn add @jk998/library

安装

yarn add @jk998/identity

初始化

import Vue from 'vue'

// 加载前置依赖 Library
import Peace from '@jk998/library'
Vue.use(Peace, {
  config: {
    appName: '应用名称',
    appVersion: '应用版本'
  }
})

// 加载 Identity
import Identity from '@jk998/identity'
Vue.use(Identity, {
  auth: {
    // auth api
    baseApi: 'https://devauth.imedcloud.jk.com/'
  }
})

Auth

示例

以密码模式登录

const params = {
  grant_type: 'password',
  client_id: 'console',
  client_secret: 'console',
  username: '15600000000',
  password: '123456',
  encryption_key: 'sksksksksksksksk',
  mobile: '13888888888',
  code: '888888'
}

peace.identity.auth
  .login(params)
  .then(() => {
    // doSomething
  })
  .catch(() => {
    // doSomething
  })

获取登录状态

peace.identity.auth.isLogin()

配置请求头( 认证后 )

peace.identity.auth.setHeaderAfterAuth(token)

登出

peace.identity.auth.logout().then(() => {
  // doSomething
})

获取鉴权信息

peace.identity.auth.getAuth().then(() => {
  // doSomething
})

设置鉴权信息

const auth = {
  access_token: '111111'
}
peace.identity.auth.setAuth(auth)

清空鉴权信息

peace.identity.auth.removeAuth()

获取用户信息

peace.identity.auth.getAccountInfo().then((res) => {
  // doSomething
  console.log(res)
})

清空用户信息

peace.identity.auth.removeAccountInfo()

设置用户信息

const accountInfo = {
  id: '1',
  username: 'demo'
}
peace.identity.auth.setAccountInfo(accountInfo)

设置用户菜单

const accountMenu = []
peace.identity.auth.setAccountMenu(accountMenu)

获取用户菜单

const params = {
  // 产品编码
  productCode: 'kzt',
  // 菜单类型 left:左菜单, top:顶菜单, button:按钮   目前默认为left
  type: 'left',
  // 环境变量 process.env  用于处理iframe
  processEnv: process.env
}
peace.identity.auth.getAccountMenu(params).then((res) => {
  // doSomething
  console.log(res)
})

清空用户菜单

peace.identity.auth.removeAccountMenu()

清空 auth 相关所有信息

peace.identity.auth.removeAll()

登录工作流

const params = {
  grant_type: 'password',
  client_id: 'console',
  client_secret: 'console',
  username: '15600000000',
  password: '123456',
  encryption_key: 'sksksksksksksksk',
  mobile: '', // 手机号(grant_type=mobile 时必填)
  code: '', // 验证码(grant_type=mobile 时必填)
  productCode: 'kzt',
  type: 'left',
  processEnv: process.env // 环境变量 process.env
}

peace.identity.auth
  .workFlowAuth(params)
  .then(() => {
    // doSomething
  })
  .catch(() => {
    // doSomething
  })

属性

参数说明类型可选值默认值
authAuth 库配置对象Object
auth.enable是否启用 Auth 库Booleantrue / falsetrue
auth.baseApiapi 地址String

方法

方法名说明参数返回值
auth.login登录grant_typeclient_idclient_secretusernamepasswordencryption_keymobilecodePromise
auth.isLogin登陆状态Boolean
auth.setHeaderAfterAuth配置请求头(认证后)tokenVoid
auth.logout登出Promise
auth.getAuth获取鉴权信息Promise
auth.setAuth设置鉴权信息AuthInfoVoid
auth.removeAuth清空鉴权信息Void
auth.getAccountInfo获取用户信息Promise
auth.setAccountInfo设置用户信息AccountInfoVoid
auth.removeAccountInfo清空用户信息Void
auth.getAccountMenu获取用户菜单productCodetypeprocessEnvPromise
auth.setAccountMenu设置用户菜单AccountMenuVoid
auth.removeAccountMenu清空用户菜单Void
auth.removeAllVoid
auth.workFlowAuth工作流grant_typeclient_idclient_secretusernamepasswordencryption_keymobilecodeproductCodetypeprocessEnvPromise

Dictionary

示例

获取字典 - map 结构

const MAP_SOURCE = await Peace.identity.dictionary.getMap('TYPE')

获取字典 - list 结构

const LIST_SOURCE = await Peace.identity.dictionary.getList('TYPE')

map 结构 => list 结构

const LIST_SOURCE = Peace.identity.dictionary.mapToList(MAP_SOURCE)

list 结构 => map 结构

const MAP_SOURCE = Peace.identity.dictionary.listToMap(LIST_SOURCE)

方法

方法名说明参数返回值
dictionary.getMap获取 map 结构的数据字典typePromise
dictionary.getList获取 list 结构的数据字典typePromise
dictionary.mapToList将 map 结构转换成 list 结构的数据字典MAP_SOURCELIST_SOURCE
dictionary.listToMap将 list 结构转换成 map 结构的数据字典LIST_SOURCEMAP_SOURCE
dictionary.filterDictionary字典过滤,根据 value 获取 labelvalue, source, nullformatstring

Socket

示例

连接

Peace.identity.socket.connect()

关闭连接

Peace.identity.socket.close()

监听当连接成功时

Peace.identity.socket.onOpen = () => console.log('onOpen 连接成功')

监听当收到服务端消息时

Peace.identity.socket.onMessage = (message) => console.log('onMessage 收到消息自行处理', message)

监听当连接关闭时

  • 手动调用 Peace.identity.socket.close()

  • 配置项 reconnection.enable = false,连接异常关闭

Peace.identity.socket.onClose = () => console.log('onClose 连接关闭')

监听当连接发生错误时

Peace.identity.socket.onError = () => console.log('onError 连接失败')

监听当心跳检测时

  • 配置项 heartbeat.enable = true,在 heartbeat.heartbeatDelay 间隔
Peace.identity.socket.onHeartbeat = () => console.log('onHeartbeat 尝试心跳检测')

监听当心跳监测尝试次数上限,依然失败时

  • 配置项 heartbeat.enable = true 时,在 heartbeat.heartbeatResponseDelay 未收到服务端响应,并且重试 heartbeat.heartbeatAttempts 次数失败后,回调
Peace.identity.socket.onHeartbeatDeath = () => {
  // 提示用户,执行下一步
}

监听当重连时

  • 配置项 reconnection.enable = true 时,在 reconnection.reconnectionDelay 间隔
Peace.identity.socket.onReconnect = () => console.log('onReconnect 尝试重连')

监听当重连次数上限,依然失败时

  • 配置项 reconnection.enable = true 时,并且重试 heartbeat.reconnectionAttempts 次数失败后
Peace.identity.socket.onReconnectDeath = () => {
  // 提示用户,执行下一步
}

完整代码

const connectUrl = 'ws://'
const connectParams = { token: 'token' }

// 连接 ws
Peace.identity.socket.connect(connectUrl, connectParams)

// 连接成功
Peace.identity.socket.onOpen = () => console.log('onOpen 连接成功')

// 连接关闭
// 当用户手动调用 Peace.identity.socket.close() 会触发
// 当未启用重连,异常断开时,会触发
Peace.identity.socket.onClose = () => console.log('onClose 连接关闭')

// 连接失败
// 未知异常时,会触发
Peace.identity.socket.onError = () => console.log('onError 连接失败')

// 心跳监测
// 当配置 heartbeat.enable 时,在 heartbeat.heartbeatDelay 间隔,连续执行
Peace.identity.socket.onHeartbeat = () => console.log('onHeartbeat 尝试心跳检测')

// 心跳检测次数上限,检测已结束
// 当配置 heartbeat.enable 时,在 heartbeat.heartbeatResponseDelay 未收到服务端响应,并且重试 heartbeat.heartbeatAttempts 失败后
Peace.identity.socket.onHeartbeatDeath = () => {
  // 提示用户,执行下一步
}

// 重连
// 当配置 reconnection.enable 时,在 reconnection.reconnectionDelay 间隔,连续执行
Peace.identity.socket.onReconnect = () => console.log('onReconnect 尝试重连')

// 重连次数上限,重连已结束
// 当配置 reconnection.enable 时,并且重试 heartbeat.reconnectionAttempts 失败后
Peace.identity.socket.onReconnectDeath = () => {
  // 提示用户,执行下一步
}

// 收到服务端消息
Peace.identity.socket.onMessage = (message) => console.log('onMessage 收到消息自行处理', message)

属性

参数说明类型可选值默认值
socketSocket 库配置对象Object
socket.debug是否启用调试模式Booleanfalse
socket.pingRequest心跳请求码Stringping
socket.pingTimeout心跳请求间隔时间Number5000
socket.pongTimeout心跳请求间隔时间Number5000
socket.repeatLimit重连尝试次数上限NumberInfinity

方法

方法名说明参数返回值
socket.connect()连接 socketwebsocket urlwebsocket url queryVoid
Socket.close()关闭 socket 连接Void

事件

事件名说明参数
socket.onopen当连接成功
Socket.onmessage当收到服务端消息MessageEvent 参考字典
Socket.onclose当连接关闭
Socket.onerror当连接发生错误
Socket.onreconnect当重连

更新记录

0.2.5 - 2021-01-06

Changed

  • 🐛 修复 getAccountInfo 缓存问题

0.2.4 - 2020-11-19

Changed

  • 🐛 重构 peace.identity.socket
  • 🔥 新增 peace.identity.dictionary.filterDictionary

0.2.3 - 2020-11-19

Changed

  • 🐛 重构 peace.identity.auth

0.2.2 - 2020-11-03

Changed

  • 🐛 修复菜单名称赋值问题

0.2.1 - 2020-10-30

Changed

  • 🔥 修改 socket 默认初始配置

0.2.0 - 2020-10-30

Added

  • 🔥 新增 peace.identity.dictioanry 支持
  • 🔥 新增 peace.identity.socket 支持

0.1.17 - 2020-10-19

Changed

  • 🐛 修复一级菜单 id 赋值的问题(导致三级菜单不能正常显示)

0.1.16 - 2020-10-19

Changed

  • 🐛 修复 getAccountMenu 中 envKey 取不到值得问题

0.1.15 - 2020-10-19

Changed

  • 🐛 打包代码

0.1.14 - 2020-10-19

Changed

  • 🐛 修复正则?<=的浏览器兼容问题

0.1.13 - 2020-10-14

Changed

  • 🐛 修复 刷新页面同步 Authorization 的问题

0.1.12 - 2020-10-14

Changed

  • 🐛 更新 library

0.1.11 - 2020-10-14

Changed

  • 🐛 修复 login 登录后设置 Authorization 无效的问题

0.1.10 - 2020-10-14

Changed

  • 🔥 新增
    • peace.identity.auth.isLogin() 登录状态
    • peace.identity.auth.setHeaderAfterAuth(token) 配置请求头( 认证后 )
    • peace.identity.auth.removeAll() 清空 auth 相关所有信息
  • 🐛 移除
    • peace.identity.auth.isLogin

0.1.9 - 2020-10-13

Removed

  • 🐛 移除 getAccountMenu() 参数校验

0.1.8 - 2020-10-12

Changed

  • 🐛 更新 getAccountMenu() 路由匹配正则

0.1.7 - 2020-10-10

Changed

  • 🐛 初始化 http header

0.1.6 - 2020-10-10

Changed

  • 🐛 更新 workFlowAuth()

0.1.5 - 2020-10-10

Changed

  • 🐛 isLogin 初始化登录状态

0.1.4 - 2020-10-10

Added

  • 🔥 新增 peace.identity.auth.isLogin 登录状态

0.1.3 - 2020-10-10

Added & Removed

  • 🔥 新增
    • peace.identity.auth.removeAuth() 清空鉴权信息
    • peace.identify.auth.removeAccountInfo() 清空用户信息
    • peace.identity.auth.removeAccountMenu() 清空用户菜单
  • 🐛 移除
    • peace.identity.auth.setHeaderBeforeAuth() 配置请求头( 认证前 )
    • peace.identity.auth.setHeaderAfterAuth() 配置请求头( 认证后 )

0.1.2 - 2020-09-30

Added

  • 🔥 新增 auth
    • peace.identity.auth.setHeaderBeforeAuth() 配置请求头( 认证前 )
    • peace.identity.auth.setHeaderAfterAuth() 配置请求头( 认证后 )
    • peace.identity.auth.login() 登录
    • peace.identity.auth.logout() 登出
    • peace.identity.auth.getAuth() 获取鉴权信息
    • peace.identity.auth.setAuth() 设置鉴权信息
    • peace.identify.auth.getAccountInfo() 获取用户信息
    • peace.identify.auth.setAccountInfo() 设置用户信息
    • peace.identity.auth.getAccountMenu() 获取用户菜单
    • peace.identity.auth.setAccountMenu() 设置用户菜单
    • peace.identity.auth.workFlowAuth() 工作流

0.1.1 - 2020-09-27

Changed

  • 🔥 更新 Docs

0.1.0 - 2020-09-25

Added

  • 🔥 发布首个公开版本
0.2.5

3 years ago

0.2.3

3 years ago

0.2.4

3 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.13

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago