0.2.5 • Published 3 years ago
@jk998/identity v0.2.5
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
})
属性
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
auth | Auth 库配置对象 | Object | ||
auth.enable | 是否启用 Auth 库 | Boolean | true / false | true |
auth.baseApi | api 地址 | String |
方法
方法名 | 说明 | 参数 | 返回值 |
---|---|---|---|
auth.login | 登录 | grant_typeclient_idclient_secretusernamepasswordencryption_keymobilecode | Promise |
auth.isLogin | 登陆状态 | Boolean | |
auth.setHeaderAfterAuth | 配置请求头(认证后) | token | Void |
auth.logout | 登出 | Promise | |
auth.getAuth | 获取鉴权信息 | Promise | |
auth.setAuth | 设置鉴权信息 | AuthInfo | Void |
auth.removeAuth | 清空鉴权信息 | Void | |
auth.getAccountInfo | 获取用户信息 | Promise | |
auth.setAccountInfo | 设置用户信息 | AccountInfo | Void |
auth.removeAccountInfo | 清空用户信息 | Void | |
auth.getAccountMenu | 获取用户菜单 | productCodetypeprocessEnv | Promise |
auth.setAccountMenu | 设置用户菜单 | AccountMenu | Void |
auth.removeAccountMenu | 清空用户菜单 | Void | |
auth.removeAll | Void | ||
auth.workFlowAuth | 工作流 | grant_typeclient_idclient_secretusernamepasswordencryption_keymobilecodeproductCodetypeprocessEnv | Promise |
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 结构的数据字典 | type | Promise |
dictionary.getList | 获取 list 结构的数据字典 | type | Promise |
dictionary.mapToList | 将 map 结构转换成 list 结构的数据字典 | MAP_SOURCE | LIST_SOURCE |
dictionary.listToMap | 将 list 结构转换成 map 结构的数据字典 | LIST_SOURCE | MAP_SOURCE |
dictionary.filterDictionary | 字典过滤,根据 value 获取 label | value, source, nullformat | string |
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)
属性
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
socket | Socket 库配置对象 | Object | ||
socket.debug | 是否启用调试模式 | Boolean | false | |
socket.pingRequest | 心跳请求码 | String | ping | |
socket.pingTimeout | 心跳请求间隔时间 | Number | 5000 | |
socket.pongTimeout | 心跳请求间隔时间 | Number | 5000 | |
socket.repeatLimit | 重连尝试次数上限 | Number | Infinity |
方法
方法名 | 说明 | 参数 | 返回值 |
---|---|---|---|
socket.connect() | 连接 socket | websocket urlwebsocket url query | Void |
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