197
openhis-ui-vue3/src/utils/apiRequestManager.js
Executable file
197
openhis-ui-vue3/src/utils/apiRequestManager.js
Executable file
@@ -0,0 +1,197 @@
|
||||
import globalRequestController from './globalRequestController.js';
|
||||
|
||||
/**
|
||||
* API请求管理器 - 用于合并相同参数的请求,避免重复请求
|
||||
* 在医院信息系统中特别重要,因为医疗数据的频繁请求可能影响系统性能
|
||||
*/
|
||||
|
||||
class ApiRequestManager {
|
||||
constructor() {
|
||||
// 存储正在进行的请求
|
||||
this.pendingRequests = new Map();
|
||||
// 缓存成功的响应结果
|
||||
this.responseCache = new Map();
|
||||
// 缓存过期时间(毫秒)
|
||||
this.cacheTimeout = 10000; // 10秒,医疗系统中数据更新可能较频繁
|
||||
|
||||
// 添加调试模式
|
||||
this.debugMode = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求的唯一键值
|
||||
* @param {string} url - 请求URL
|
||||
* @param {object} params - 请求参数
|
||||
* @returns {string} 唯一键值
|
||||
*/
|
||||
generateRequestKey(url, params = {}) {
|
||||
// 对参数进行排序以确保相同参数产生相同键值
|
||||
const sortedParams = Object.keys(params)
|
||||
.sort()
|
||||
.reduce((acc, key) => {
|
||||
acc[key] = params[key];
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const key = `${url}?${JSON.stringify(sortedParams)}`;
|
||||
|
||||
if (this.debugMode) {
|
||||
console.log(`Generated request key: ${key}`);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在相同的进行中请求
|
||||
* @param {string} requestKey - 请求键值
|
||||
* @returns {Promise|undefined} 如果存在则返回Promise,否则返回undefined
|
||||
*/
|
||||
getPendingRequest(requestKey) {
|
||||
return this.pendingRequests.get(requestKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加进行中的请求
|
||||
* @param {string} requestKey - 请求键值
|
||||
* @param {Promise} promise - 请求Promise
|
||||
*/
|
||||
addPendingRequest(requestKey, promise) {
|
||||
this.pendingRequests.set(requestKey, promise);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除进行中的请求
|
||||
* @param {string} requestKey - 请求键值
|
||||
*/
|
||||
removePendingRequest(requestKey) {
|
||||
this.pendingRequests.delete(requestKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在缓存的响应
|
||||
* @param {string} requestKey - 请求键值
|
||||
* @returns {object|undefined} 如果存在则返回缓存的响应,否则返回undefined
|
||||
*/
|
||||
getCachedResponse(requestKey) {
|
||||
const cached = this.responseCache.get(requestKey);
|
||||
if (cached && Date.now() < cached.expiry) {
|
||||
return cached.data;
|
||||
}
|
||||
// 如果缓存已过期,删除它
|
||||
if (cached) {
|
||||
this.responseCache.delete(requestKey);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加缓存的响应
|
||||
* @param {string} requestKey - 请求键值
|
||||
* @param {object} data - 响应数据
|
||||
*/
|
||||
addCachedResponse(requestKey, data) {
|
||||
this.responseCache.set(requestKey, {
|
||||
data,
|
||||
expiry: Date.now() + this.cacheTimeout
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除指定的缓存
|
||||
* @param {string} requestKey - 请求键值
|
||||
*/
|
||||
clearCache(requestKey) {
|
||||
this.responseCache.delete(requestKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有缓存
|
||||
*/
|
||||
clearAllCache() {
|
||||
this.responseCache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有进行中的请求
|
||||
*/
|
||||
clearAllPending() {
|
||||
this.pendingRequests.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行API请求(带去重和缓存)
|
||||
* @param {Function} apiFunction - API函数
|
||||
* @param {string} url - 请求URL
|
||||
* @param {object} params - 请求参数
|
||||
* @returns {Promise} API响应Promise
|
||||
*/
|
||||
async execute(apiFunction, url, params = {}) {
|
||||
const requestKey = this.generateRequestKey(url, params);
|
||||
|
||||
if (this.debugMode) {
|
||||
console.log(`Executing request with key: ${requestKey}`);
|
||||
console.log(`Pending requests count: ${this.pendingRequests.size}`);
|
||||
console.log(`Cached responses count: ${this.responseCache.size}`);
|
||||
}
|
||||
|
||||
// 检查是否有缓存的响应
|
||||
const cachedResponse = this.getCachedResponse(requestKey);
|
||||
if (cachedResponse) {
|
||||
if (this.debugMode) {
|
||||
console.log(`Returning cached response for: ${requestKey}`);
|
||||
}
|
||||
return Promise.resolve(cachedResponse);
|
||||
}
|
||||
|
||||
// 使用全局请求控制器来确保唯一性
|
||||
const requestPromise = globalRequestController.execute(apiFunction, url, params)
|
||||
.then(response => {
|
||||
if (this.debugMode) {
|
||||
console.log(`Request completed for: ${requestKey}`, response);
|
||||
}
|
||||
// 请求成功后,添加到缓存
|
||||
this.addCachedResponse(requestKey, response);
|
||||
return response;
|
||||
})
|
||||
.catch(error => {
|
||||
if (this.debugMode) {
|
||||
console.error(`Request failed for: ${requestKey}`, error);
|
||||
}
|
||||
throw error; // 不在这里处理错误,让调用方处理
|
||||
});
|
||||
|
||||
return requestPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行POST请求(带去重和缓存)
|
||||
* @param {Function} apiFunction - API函数
|
||||
* @param {string} url - 请求URL
|
||||
* @param {object} data - 请求数据
|
||||
* @returns {Promise} API响应Promise
|
||||
*/
|
||||
async executePost(apiFunction, url, data = {}) {
|
||||
const requestKey = this.generateRequestKey(url, data);
|
||||
|
||||
// POST请求通常不缓存,但仍然可以去重
|
||||
const pendingRequest = this.getPendingRequest(requestKey);
|
||||
if (pendingRequest) {
|
||||
return pendingRequest;
|
||||
}
|
||||
|
||||
const requestPromise = apiFunction(data)
|
||||
.finally(() => {
|
||||
this.removePendingRequest(requestKey);
|
||||
});
|
||||
|
||||
this.addPendingRequest(requestKey, requestPromise);
|
||||
|
||||
return requestPromise;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建全局实例
|
||||
const apiRequestManager = new ApiRequestManager();
|
||||
|
||||
export default apiRequestManager;
|
||||
15
openhis-ui-vue3/src/utils/auth.js
Executable file
15
openhis-ui-vue3/src/utils/auth.js
Executable file
@@ -0,0 +1,15 @@
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
const TokenKey = 'Admin-Token'
|
||||
|
||||
export function getToken() {
|
||||
return Cookies.get(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
return Cookies.set(TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return Cookies.remove(TokenKey)
|
||||
}
|
||||
24
openhis-ui-vue3/src/utils/dict.js
Executable file
24
openhis-ui-vue3/src/utils/dict.js
Executable file
@@ -0,0 +1,24 @@
|
||||
import useDictStore from '@/store/modules/dict'
|
||||
import {getDicts} from '@/api/system/dict/data'
|
||||
|
||||
/**
|
||||
* 获取字典数据
|
||||
*/
|
||||
export function useDict(...args) {
|
||||
const res = ref({});
|
||||
return (() => {
|
||||
args.forEach((dictType, index) => {
|
||||
res.value[dictType] = [];
|
||||
const dicts = useDictStore().getDict(dictType);
|
||||
if (dicts) {
|
||||
res.value[dictType] = dicts;
|
||||
} else {
|
||||
getDicts(dictType).then(resp => {
|
||||
res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass, remark: p.remark }))
|
||||
useDictStore().setDict(dictType, res.value[dictType]);
|
||||
})
|
||||
}
|
||||
})
|
||||
return toRefs(res.value);
|
||||
})()
|
||||
}
|
||||
14
openhis-ui-vue3/src/utils/dynamicTitle.js
Executable file
14
openhis-ui-vue3/src/utils/dynamicTitle.js
Executable file
@@ -0,0 +1,14 @@
|
||||
import defaultSettings from '@/settings'
|
||||
import useSettingsStore from '@/store/modules/settings'
|
||||
|
||||
/**
|
||||
* 动态修改标题
|
||||
*/
|
||||
export function useDynamicTitle() {
|
||||
const settingsStore = useSettingsStore();
|
||||
if (settingsStore.dynamicTitle) {
|
||||
document.title = settingsStore.title + ' - ' + defaultSettings.title;
|
||||
} else {
|
||||
document.title = defaultSettings.title;
|
||||
}
|
||||
}
|
||||
6
openhis-ui-vue3/src/utils/errorCode.js
Executable file
6
openhis-ui-vue3/src/utils/errorCode.js
Executable file
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
'401': '认证失败,无法访问系统资源',
|
||||
'403': '当前操作没有权限',
|
||||
'404': '访问资源不存在',
|
||||
'default': '系统未知错误,请反馈给管理员'
|
||||
}
|
||||
94
openhis-ui-vue3/src/utils/globalRequestController.js
Executable file
94
openhis-ui-vue3/src/utils/globalRequestController.js
Executable file
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* 全局请求控制器 - 用于在整个应用范围内控制重复请求
|
||||
* 特别针对医院信息系统中的病历数据访问进行优化
|
||||
*/
|
||||
class GlobalRequestController {
|
||||
constructor() {
|
||||
// 存储正在进行的请求
|
||||
this.activeRequests = new Map();
|
||||
// 请求计数器,用于调试
|
||||
this.requestCounter = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求的唯一标识符
|
||||
* @param {string} url - 请求URL
|
||||
* @param {object} params - 请求参数
|
||||
* @returns {string} 唯一标识符
|
||||
*/
|
||||
generateRequestId(url, params = {}) {
|
||||
// 标准化参数以确保一致性
|
||||
const normalizedParams = this.normalizeParams(params);
|
||||
const paramString = JSON.stringify(normalizedParams);
|
||||
return `${url}|${paramString}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准化参数对象
|
||||
* @param {object} params - 原始参数
|
||||
* @returns {object} 标准化后的参数
|
||||
*/
|
||||
normalizeParams(params) {
|
||||
const normalized = {};
|
||||
// 按字母顺序排序参数键
|
||||
Object.keys(params).sort().forEach(key => {
|
||||
normalized[key] = params[key];
|
||||
});
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已有相同请求在进行中
|
||||
* @param {string} requestId - 请求ID
|
||||
* @returns {Promise|undefined} 如果存在则返回Promise,否则返回undefined
|
||||
*/
|
||||
hasActiveRequest(requestId) {
|
||||
return this.activeRequests.get(requestId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个新请求
|
||||
* @param {string} requestId - 请求ID
|
||||
* @param {Promise} requestPromise - 请求Promise
|
||||
*/
|
||||
registerRequest(requestId, requestPromise) {
|
||||
this.requestCounter++;
|
||||
console.log(`[GlobalRequestController] Registering request #${this.requestCounter}: ${requestId}`);
|
||||
this.activeRequests.set(requestId, requestPromise);
|
||||
|
||||
// 当请求完成时,从活动请求中移除
|
||||
requestPromise.finally(() => {
|
||||
console.log(`[GlobalRequestController] Removing completed request: ${requestId}`);
|
||||
this.activeRequests.delete(requestId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行请求(确保相同参数的请求只执行一次)
|
||||
* @param {Function} apiFunction - API函数
|
||||
* @param {string} url - 请求URL
|
||||
* @param {object} params - 请求参数
|
||||
* @returns {Promise} 请求结果Promise
|
||||
*/
|
||||
async execute(apiFunction, url, params = {}) {
|
||||
const requestId = this.generateRequestId(url, params);
|
||||
|
||||
// 检查是否已有相同请求在进行中
|
||||
const existingRequest = this.hasActiveRequest(requestId);
|
||||
if (existingRequest) {
|
||||
console.log(`[GlobalRequestController] Returning existing request for: ${requestId}`);
|
||||
return existingRequest;
|
||||
}
|
||||
|
||||
// 创建新请求
|
||||
const requestPromise = apiFunction(params);
|
||||
this.registerRequest(requestId, requestPromise);
|
||||
|
||||
return requestPromise;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建全局实例
|
||||
const globalRequestController = new GlobalRequestController();
|
||||
|
||||
export default globalRequestController;
|
||||
518
openhis-ui-vue3/src/utils/his.js
Executable file
518
openhis-ui-vue3/src/utils/his.js
Executable file
@@ -0,0 +1,518 @@
|
||||
import useDictStore from '@/store/modules/dict';
|
||||
|
||||
// 日期格式化
|
||||
export function parseTime(time, pattern) {
|
||||
if (arguments.length === 0 || !time) {
|
||||
return null;
|
||||
}
|
||||
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
|
||||
let date;
|
||||
if (typeof time === 'object') {
|
||||
date = time;
|
||||
} else {
|
||||
if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
|
||||
time = parseInt(time);
|
||||
} else if (typeof time === 'string') {
|
||||
time = time
|
||||
.replace(new RegExp(/-/gm), '/')
|
||||
.replace('T', ' ')
|
||||
.replace(new RegExp(/\.[\d]{3}/gm), '');
|
||||
}
|
||||
if (typeof time === 'number' && time.toString().length === 10) {
|
||||
time = time * 1000;
|
||||
}
|
||||
date = new Date(time);
|
||||
}
|
||||
const formatObj = {
|
||||
y: date.getFullYear(),
|
||||
m: date.getMonth() + 1,
|
||||
d: date.getDate(),
|
||||
h: date.getHours(),
|
||||
i: date.getMinutes(),
|
||||
s: date.getSeconds(),
|
||||
a: date.getDay(),
|
||||
};
|
||||
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
||||
let value = formatObj[key];
|
||||
// Note: getDay() returns 0 on Sunday
|
||||
if (key === 'a') {
|
||||
return ['日', '一', '二', '三', '四', '五', '六'][value];
|
||||
}
|
||||
if (result.length > 0 && value < 10) {
|
||||
value = '0' + value;
|
||||
}
|
||||
return value || 0;
|
||||
});
|
||||
return time_str;
|
||||
}
|
||||
|
||||
// 表单重置
|
||||
export function resetForm(refName) {
|
||||
if (this.$refs[refName]) {
|
||||
this.$refs[refName].resetFields();
|
||||
}
|
||||
}
|
||||
|
||||
// 添加日期范围
|
||||
export function addDateRange(params, dateRange, propName) {
|
||||
let search = params;
|
||||
search.params =
|
||||
typeof search.params === 'object' && search.params !== null && !Array.isArray(search.params)
|
||||
? search.params
|
||||
: {};
|
||||
dateRange = Array.isArray(dateRange) ? dateRange : [];
|
||||
if (typeof propName === 'undefined') {
|
||||
search.params['beginTime'] = dateRange[0];
|
||||
search.params['endTime'] = dateRange[1];
|
||||
} else {
|
||||
search.params['begin' + propName] = dateRange[0];
|
||||
search.params['end' + propName] = dateRange[1];
|
||||
}
|
||||
return search;
|
||||
}
|
||||
export function addDateRanges(params, dateRange1, dateRange2, propName) {
|
||||
let search = params;
|
||||
search.params =
|
||||
typeof search.params === 'object' && search.params !== null && !Array.isArray(search.params)
|
||||
? search.params
|
||||
: {};
|
||||
dateRange1 = Array.isArray(dateRange1) ? dateRange1 : [];
|
||||
dateRange2 = Array.isArray(dateRange2) ? dateRange2 : [];
|
||||
if (typeof propName === 'undefined') {
|
||||
search.params['beginTime'] = dateRange1[0];
|
||||
search.params['endTime'] = dateRange1[1];
|
||||
search.params['timeFrom'] = dateRange2[0];
|
||||
search.params['timeTo'] = dateRange2[1];
|
||||
} else {
|
||||
search.params['begin' + propName] = dateRange1[0];
|
||||
search.params['end' + propName] = dateRange1[1];
|
||||
search.params['from' + propName] = dateRange2[0];
|
||||
search.params['to' + propName] = dateRange2[1];
|
||||
}
|
||||
return search;
|
||||
}
|
||||
|
||||
// 回显数据字典
|
||||
export function selectDictLabel(datas, value) {
|
||||
if (value === undefined) {
|
||||
return '';
|
||||
}
|
||||
var actions = [];
|
||||
Object.keys(datas).some((key) => {
|
||||
if (datas[key].value == '' + value) {
|
||||
actions.push(datas[key].label);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (actions.length === 0) {
|
||||
actions.push(value);
|
||||
}
|
||||
return actions.join('');
|
||||
}
|
||||
|
||||
// 回显数据字典(字符串数组)
|
||||
export function selectDictLabels(datas, value, separator) {
|
||||
if (value === undefined || value.length === 0) {
|
||||
return '';
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value = value.join(',');
|
||||
}
|
||||
var actions = [];
|
||||
var currentSeparator = undefined === separator ? ',' : separator;
|
||||
var temp = value.split(currentSeparator);
|
||||
Object.keys(value.split(currentSeparator)).some((val) => {
|
||||
var match = false;
|
||||
Object.keys(datas).some((key) => {
|
||||
if (datas[key].value == '' + temp[val]) {
|
||||
actions.push(datas[key].label + currentSeparator);
|
||||
match = true;
|
||||
}
|
||||
});
|
||||
if (!match) {
|
||||
actions.push(temp[val] + currentSeparator);
|
||||
}
|
||||
});
|
||||
return actions.join('').substring(0, actions.join('').length - 1);
|
||||
}
|
||||
|
||||
// 字符串格式化(%s )
|
||||
export function sprintf(str) {
|
||||
var args = arguments,
|
||||
flag = true,
|
||||
i = 1;
|
||||
str = str.replace(/%s/g, function () {
|
||||
var arg = args[i++];
|
||||
if (typeof arg === 'undefined') {
|
||||
flag = false;
|
||||
return '';
|
||||
}
|
||||
return arg;
|
||||
});
|
||||
return flag ? str : '';
|
||||
}
|
||||
|
||||
// 转换字符串,undefined,null等转化为""
|
||||
export function parseStrEmpty(str) {
|
||||
if (!str || str == 'undefined' || str == 'null') {
|
||||
return '';
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// 千位分隔
|
||||
export function thousandNumber(num) {
|
||||
return String(num).replace(/\B(?=(\d{3})+(?!\d))/g, ','); // 3是千分位,4是万分位
|
||||
}
|
||||
|
||||
// 数据合并
|
||||
export function mergeRecursive(source, target) {
|
||||
for (var p in target) {
|
||||
try {
|
||||
if (target[p].constructor == Object) {
|
||||
source[p] = mergeRecursive(source[p], target[p]);
|
||||
} else {
|
||||
source[p] = target[p];
|
||||
}
|
||||
} catch (e) {
|
||||
source[p] = target[p];
|
||||
}
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造树型结构数据
|
||||
* @param {*} data 数据源
|
||||
* @param {*} id id字段 默认 'id'
|
||||
* @param {*} parentId 父节点字段 默认 'parentId'
|
||||
* @param {*} children 孩子节点字段 默认 'children'
|
||||
*/
|
||||
export function handleTree(data, id, parentId, children) {
|
||||
let config = {
|
||||
id: id || 'id',
|
||||
parentId: parentId || 'parentId',
|
||||
childrenList: children || 'children',
|
||||
};
|
||||
|
||||
var childrenListMap = {};
|
||||
var nodeIds = {};
|
||||
var tree = [];
|
||||
|
||||
for (let d of data) {
|
||||
let parentId = d[config.parentId];
|
||||
if (childrenListMap[parentId] == null) {
|
||||
childrenListMap[parentId] = [];
|
||||
}
|
||||
nodeIds[d[config.id]] = d;
|
||||
childrenListMap[parentId].push(d);
|
||||
}
|
||||
|
||||
for (let d of data) {
|
||||
let parentId = d[config.parentId];
|
||||
if (nodeIds[parentId] == null) {
|
||||
tree.push(d);
|
||||
}
|
||||
}
|
||||
|
||||
for (let t of tree) {
|
||||
adaptToChildrenList(t);
|
||||
}
|
||||
|
||||
function adaptToChildrenList(o) {
|
||||
if (childrenListMap[o[config.id]] !== null) {
|
||||
o[config.childrenList] = childrenListMap[o[config.id]];
|
||||
}
|
||||
if (o[config.childrenList]) {
|
||||
for (let c of o[config.childrenList]) {
|
||||
adaptToChildrenList(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数处理
|
||||
* @param {*} params 参数
|
||||
*/
|
||||
export function tansParams(params) {
|
||||
let result = '';
|
||||
for (const propName of Object.keys(params)) {
|
||||
const value = params[propName];
|
||||
var part = encodeURIComponent(propName) + '=';
|
||||
if (value !== null && value !== '' && typeof value !== 'undefined') {
|
||||
if (typeof value === 'object') {
|
||||
for (const key of Object.keys(value)) {
|
||||
if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
|
||||
let params = propName + '[' + key + ']';
|
||||
var subPart = encodeURIComponent(params) + '=';
|
||||
result += subPart + encodeURIComponent(value[key]) + '&';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result += part + encodeURIComponent(value) + '&';
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 返回项目路径
|
||||
export function getNormalPath(p) {
|
||||
if (p.length === 0 || !p || p == 'undefined') {
|
||||
return p;
|
||||
}
|
||||
let res = p.replace('//', '/');
|
||||
if (res[res.length - 1] === '/') {
|
||||
return res.slice(0, res.length - 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// 验证是否为blob格式
|
||||
export function blobValidate(data) {
|
||||
return data.type !== 'application/json';
|
||||
}
|
||||
|
||||
// 按照频次天数计算总数量
|
||||
export function calculateQuantityByDays(frequency, days) {
|
||||
const dicts = useDictStore().getDict('rate_code');
|
||||
if (!dicts) return;
|
||||
const dict = dicts.find(item => item.value === frequency);
|
||||
if (!dict?.remark) return;
|
||||
const rate = Number(dict.remark);
|
||||
if (isNaN(rate) || !rate) return;
|
||||
const quantity = rate * days;
|
||||
return quantity < 1 ? 1 : Math.ceil(quantity);
|
||||
}
|
||||
|
||||
export function formatNumber(dose) {
|
||||
if (dose === undefined || dose === null) {
|
||||
return '';
|
||||
}
|
||||
const parsedDose = parseFloat(dose);
|
||||
return parsedDose.toFixed(Number.isInteger(parsedDose) ? 0 : 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理各种状态对应标签颜色,传值需要a,b两个数组状态值与颜色下标一致
|
||||
*
|
||||
* @param a 全部状态值数组
|
||||
* @param b 对应状态颜色,可重复
|
||||
* @param status 传入的当前状态值
|
||||
* @returns 当前状态值对应颜色
|
||||
*/
|
||||
export function handleColor(a, b, status) {
|
||||
if (a.length !== 0 && a.length === b.length && status) {
|
||||
const index = a.indexOf(status);
|
||||
if (index !== -1) {
|
||||
return b[index];
|
||||
}
|
||||
}
|
||||
return 'default';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统可用打印机列表
|
||||
* @returns {Array} 打印机列表
|
||||
*/
|
||||
export function getPrinterList() {
|
||||
try {
|
||||
if (window.hiprint && window.hiprint.hiwebSocket && window.hiprint.hiwebSocket.connected) {
|
||||
const printerList = window.hiprint.hiwebSocket.getPrinterList();
|
||||
return printerList || [];
|
||||
} else {
|
||||
console.warn('打印服务未连接,返回空打印机列表');
|
||||
return [];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取打印机列表失败:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存获取上次选择的打印机
|
||||
* @returns {string} 打印机名称
|
||||
*/
|
||||
export function getCachedPrinter() {
|
||||
return localStorage.getItem('selectedPrinter') || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存打印机选择到缓存
|
||||
* @param {string} printerName 打印机名称
|
||||
*/
|
||||
export function savePrinterToCache(printerName) {
|
||||
if (printerName) {
|
||||
localStorage.setItem('selectedPrinter', printerName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行打印操作
|
||||
* @param {Array} data 打印数据
|
||||
* @param {Object} template 打印模板
|
||||
* @param {string} printerName 打印机名称(可选)
|
||||
* @param {Object} options 打印选项(可选)
|
||||
* @returns {Promise} 打印结果Promise
|
||||
*/
|
||||
export function executePrint(data, template, printerName, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
if (!window.hiprint) {
|
||||
throw new Error('打印插件未加载');
|
||||
}
|
||||
|
||||
const hiprintTemplate = new window.hiprint.PrintTemplate({ template });
|
||||
const printOptions = {
|
||||
title: '打印标题',
|
||||
height: 210,
|
||||
width: 148,
|
||||
...options,
|
||||
};
|
||||
|
||||
// 如果指定了打印机,添加到打印选项中
|
||||
if (printerName) {
|
||||
printOptions.printer = printerName;
|
||||
// 保存到缓存
|
||||
savePrinterToCache(printerName);
|
||||
}
|
||||
|
||||
// 打印成功回调
|
||||
hiprintTemplate.on('printSuccess', function (e) {
|
||||
resolve({ success: true, event: e });
|
||||
});
|
||||
|
||||
// 打印失败回调
|
||||
hiprintTemplate.on('printError', function (e) {
|
||||
reject({ success: false, event: e, message: '打印失败' });
|
||||
});
|
||||
|
||||
// 执行打印
|
||||
hiprintTemplate.print2(data, printOptions);
|
||||
} catch (error) {
|
||||
reject({ success: false, error: error, message: error.message || '打印过程中发生错误' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择打印机并执行打印
|
||||
* @param {Array} data 打印数据
|
||||
* @param {Object} template 打印模板
|
||||
* @param {Function} showPrinterDialog 显示打印机选择对话框的函数
|
||||
* @param {Object} modal 消息提示对象
|
||||
* @param {Function} callback 打印完成后的回调函数
|
||||
*/
|
||||
export async function selectPrinterAndPrint(data, template, showPrinterDialog, modal, callback) {
|
||||
try {
|
||||
// 获取打印机列表
|
||||
const printerList = getPrinterList();
|
||||
|
||||
if (printerList.length === 0) {
|
||||
modal.msgWarning('未检测到可用打印机');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取缓存的打印机
|
||||
const cachedPrinter = getCachedPrinter();
|
||||
let selectedPrinter = '';
|
||||
|
||||
// 判断打印机选择逻辑
|
||||
if (printerList.length === 1) {
|
||||
selectedPrinter = printerList[0].name;
|
||||
await executePrint(data, template, selectedPrinter);
|
||||
if (callback) callback();
|
||||
} else if (cachedPrinter && printerList.some((printer) => printer.name === cachedPrinter)) {
|
||||
selectedPrinter = cachedPrinter;
|
||||
await executePrint(data, template, selectedPrinter);
|
||||
if (callback) callback();
|
||||
} else {
|
||||
// 调用显示打印机选择对话框的函数
|
||||
showPrinterDialog(printerList, async (chosenPrinter) => {
|
||||
try {
|
||||
await executePrint(data, template, chosenPrinter);
|
||||
if (callback) callback();
|
||||
} catch (error) {
|
||||
modal.msgError(error.message || '打印失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
modal.msgError(error.message || '获取打印机列表失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 分组标记处理
|
||||
export function getGroupMarkers(tableData) {
|
||||
// 初始化所有行的 groupIcon 为 null
|
||||
tableData.forEach((item) => {
|
||||
item.groupIcon = null;
|
||||
});
|
||||
|
||||
// 创建一个映射来存储每个 groupId 对应的行索引
|
||||
const groupMap = {};
|
||||
|
||||
// 遍历列表,按 groupId 分组(忽略无 groupId 的项)
|
||||
tableData.forEach((item, index) => {
|
||||
if (!item.groupId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!groupMap[item.groupId]) {
|
||||
groupMap[item.groupId] = [];
|
||||
}
|
||||
groupMap[item.groupId].push(index);
|
||||
});
|
||||
|
||||
// 为每个组设置 groupIcon
|
||||
Object.values(groupMap).forEach((indices) => {
|
||||
// 只有当组内元素大于1个时才需要显示分组标记
|
||||
if (indices.length > 1) {
|
||||
indices.forEach((index, i) => {
|
||||
if (i === 0) {
|
||||
// 第一行
|
||||
tableData[index].groupIcon = '┏';
|
||||
} else if (i === indices.length - 1) {
|
||||
// 最后一行
|
||||
tableData[index].groupIcon = '┗';
|
||||
} else {
|
||||
// 中间行
|
||||
tableData[index].groupIcon = '┃';
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return tableData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化库存数量显示(大单位情况) 示例 1盒5片
|
||||
* @param quantity 小单位库存数量
|
||||
* @param partPercent 拆零比
|
||||
* @param unitCode 大单位label
|
||||
* @param minUnitCode 小单位label
|
||||
*/
|
||||
export function formatInventory(quantity, partPercent, unitCode, minUnitCode) {
|
||||
// 处理负数情况
|
||||
const isNegative = quantity < 0;
|
||||
const absQuantity = Math.abs(quantity);
|
||||
|
||||
if (absQuantity % partPercent !== 0) {
|
||||
const integerPart = Math.floor(absQuantity / partPercent);
|
||||
const decimalPart = absQuantity % partPercent;
|
||||
|
||||
let result = integerPart.toString() + ' ' + unitCode;
|
||||
if (decimalPart > 0) {
|
||||
result += decimalPart.toString() + ' ' + minUnitCode;
|
||||
}
|
||||
|
||||
return isNegative ? '-' + result : result;
|
||||
}
|
||||
|
||||
// 整除情况
|
||||
const result = absQuantity / partPercent;
|
||||
return isNegative ? '-' + result : result + ' ' + unitCode;
|
||||
}
|
||||
431
openhis-ui-vue3/src/utils/index.js
Executable file
431
openhis-ui-vue3/src/utils/index.js
Executable file
@@ -0,0 +1,431 @@
|
||||
import { parseTime } from './openhis'
|
||||
|
||||
// 导出 parseTime 函数以供其他模块使用
|
||||
export { parseTime }
|
||||
|
||||
/**
|
||||
* 表格时间格式化
|
||||
*/
|
||||
export function formatDate(cellValue) {
|
||||
if (cellValue == null || cellValue == "") return "";
|
||||
var date = new Date(cellValue)
|
||||
var year = date.getFullYear()
|
||||
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
||||
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
|
||||
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
|
||||
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
|
||||
var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
|
||||
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
|
||||
}
|
||||
|
||||
export function formatDateStr(cellValue, format = 'YYYY-MM-DD HH:mm:ss') {
|
||||
if (cellValue == null || cellValue === "") return "";
|
||||
|
||||
const date = new Date(cellValue);
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const hours = String(date.getHours()).padStart(2, '0');
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0');
|
||||
|
||||
// 支持不带前导零的格式
|
||||
const monthNoPad = String(date.getMonth() + 1);
|
||||
const dayNoPad = String(date.getDate());
|
||||
|
||||
return format
|
||||
.replace('YYYY', year)
|
||||
.replace('MM', month)
|
||||
.replace('DD', day)
|
||||
.replace('M/', monthNoPad + '/') // 支持 M/ 格式
|
||||
.replace('D ', dayNoPad + ' ') // 支持 D 格式(后跟空格)
|
||||
.replace('/D', '/' + dayNoPad) // 支持 /D 格式
|
||||
.replace('HH', hours)
|
||||
.replace('mm', minutes)
|
||||
.replace('ss', seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 表格时间格式化
|
||||
*/
|
||||
export function formatDateymd(cellValue) {
|
||||
if (cellValue == null || cellValue == "") return "";
|
||||
var date = new Date(cellValue)
|
||||
var year = date.getFullYear()
|
||||
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
||||
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
|
||||
return year + '-' + month + '-' + day
|
||||
}
|
||||
/**
|
||||
* @param {number} time
|
||||
* @param {string} option
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatTime(time, option) {
|
||||
if (('' + time).length === 10) {
|
||||
time = parseInt(time) * 1000
|
||||
} else {
|
||||
time = +time
|
||||
}
|
||||
const d = new Date(time)
|
||||
const now = Date.now()
|
||||
|
||||
const diff = (now - d) / 1000
|
||||
|
||||
if (diff < 30) {
|
||||
return '刚刚'
|
||||
} else if (diff < 3600) {
|
||||
// less 1 hour
|
||||
return Math.ceil(diff / 60) + '分钟前'
|
||||
} else if (diff < 3600 * 24) {
|
||||
return Math.ceil(diff / 3600) + '小时前'
|
||||
} else if (diff < 3600 * 24 * 2) {
|
||||
return '1天前'
|
||||
}
|
||||
if (option) {
|
||||
return parseTime(time, option)
|
||||
} else {
|
||||
return (
|
||||
d.getMonth() +
|
||||
1 +
|
||||
'月' +
|
||||
d.getDate() +
|
||||
'日' +
|
||||
d.getHours() +
|
||||
'时' +
|
||||
d.getMinutes() +
|
||||
'分'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function getQueryObject(url) {
|
||||
url = url == null ? window.location.href : url
|
||||
const search = url.substring(url.lastIndexOf('?') + 1)
|
||||
const obj = {}
|
||||
const reg = /([^?&=]+)=([^?&=]*)/g
|
||||
search.replace(reg, (rs, $1, $2) => {
|
||||
const name = decodeURIComponent($1)
|
||||
let val = decodeURIComponent($2)
|
||||
val = String(val)
|
||||
obj[name] = val
|
||||
return rs
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input value
|
||||
* @returns {number} output value
|
||||
*/
|
||||
export function byteLength(str) {
|
||||
// returns the byte length of an utf8 string
|
||||
let s = str.length
|
||||
for (var i = str.length - 1; i >= 0; i--) {
|
||||
const code = str.charCodeAt(i)
|
||||
if (code > 0x7f && code <= 0x7ff) s++
|
||||
else if (code > 0x7ff && code <= 0xffff) s += 2
|
||||
if (code >= 0xDC00 && code <= 0xDFFF) i--
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array} actual
|
||||
* @returns {Array}
|
||||
*/
|
||||
export function cleanArray(actual) {
|
||||
const newArray = []
|
||||
for (let i = 0; i < actual.length; i++) {
|
||||
if (actual[i]) {
|
||||
newArray.push(actual[i])
|
||||
}
|
||||
}
|
||||
return newArray
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} json
|
||||
* @returns {Array}
|
||||
*/
|
||||
export function param(json) {
|
||||
if (!json) return ''
|
||||
return cleanArray(
|
||||
Object.keys(json).map(key => {
|
||||
if (json[key] === undefined) return ''
|
||||
return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
|
||||
})
|
||||
).join('&')
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function param2Obj(url) {
|
||||
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
const obj = {}
|
||||
const searchArr = search.split('&')
|
||||
searchArr.forEach(v => {
|
||||
const index = v.indexOf('=')
|
||||
if (index !== -1) {
|
||||
const name = v.substring(0, index)
|
||||
const val = v.substring(index + 1, v.length)
|
||||
obj[name] = val
|
||||
}
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} val
|
||||
* @returns {string}
|
||||
*/
|
||||
export function html2Text(val) {
|
||||
const div = document.createElement('div')
|
||||
div.innerHTML = val
|
||||
return div.textContent || div.innerText
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges two objects, giving the last one precedence
|
||||
* @param {Object} target
|
||||
* @param {(Object|Array)} source
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function objectMerge(target, source) {
|
||||
if (typeof target !== 'object') {
|
||||
target = {}
|
||||
}
|
||||
if (Array.isArray(source)) {
|
||||
return source.slice()
|
||||
}
|
||||
Object.keys(source).forEach(property => {
|
||||
const sourceProperty = source[property]
|
||||
if (typeof sourceProperty === 'object') {
|
||||
target[property] = objectMerge(target[property], sourceProperty)
|
||||
} else {
|
||||
target[property] = sourceProperty
|
||||
}
|
||||
})
|
||||
return target
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} element
|
||||
* @param {string} className
|
||||
*/
|
||||
export function toggleClass(element, className) {
|
||||
if (!element || !className) {
|
||||
return
|
||||
}
|
||||
let classString = element.className
|
||||
const nameIndex = classString.indexOf(className)
|
||||
if (nameIndex === -1) {
|
||||
classString += '' + className
|
||||
} else {
|
||||
classString =
|
||||
classString.substr(0, nameIndex) +
|
||||
classString.substr(nameIndex + className.length)
|
||||
}
|
||||
element.className = classString
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} type
|
||||
* @returns {Date}
|
||||
*/
|
||||
export function getTime(type) {
|
||||
if (type === 'start') {
|
||||
return new Date().getTime() - 3600 * 1000 * 24 * 90
|
||||
} else {
|
||||
return new Date(new Date().toDateString())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Function} func
|
||||
* @param {number} wait
|
||||
* @param {boolean} immediate
|
||||
* @return {*}
|
||||
*/
|
||||
export function debounce(func, wait, immediate) {
|
||||
let timeout, args, context, timestamp, result
|
||||
|
||||
const later = function() {
|
||||
// 据上一次触发时间间隔
|
||||
const last = +new Date() - timestamp
|
||||
|
||||
// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
|
||||
if (last < wait && last > 0) {
|
||||
timeout = setTimeout(later, wait - last)
|
||||
} else {
|
||||
timeout = null
|
||||
// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
|
||||
if (!immediate) {
|
||||
result = func.apply(context, args)
|
||||
if (!timeout) context = args = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return function(...args) {
|
||||
context = this
|
||||
timestamp = +new Date()
|
||||
const callNow = immediate && !timeout
|
||||
// 如果延时不存在,重新设定延时
|
||||
if (!timeout) timeout = setTimeout(later, wait)
|
||||
if (callNow) {
|
||||
result = func.apply(context, args)
|
||||
context = args = null
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is just a simple version of deep copy
|
||||
* Has a lot of edge cases bug
|
||||
* If you want to use a perfect deep copy, use lodash's _.cloneDeep
|
||||
* @param {Object} source
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function deepClone(source) {
|
||||
if (!source && typeof source !== 'object') {
|
||||
throw new Error('error arguments', 'deepClone')
|
||||
}
|
||||
const targetObj = source.constructor === Array ? [] : {}
|
||||
Object.keys(source).forEach(keys => {
|
||||
if (source[keys] && typeof source[keys] === 'object') {
|
||||
targetObj[keys] = deepClone(source[keys])
|
||||
} else {
|
||||
targetObj[keys] = source[keys]
|
||||
}
|
||||
})
|
||||
return targetObj
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array} arr
|
||||
* @returns {Array}
|
||||
*/
|
||||
export function uniqueArr(arr) {
|
||||
return Array.from(new Set(arr))
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string}
|
||||
*/
|
||||
export function createUniqueString() {
|
||||
const timestamp = +new Date() + ''
|
||||
const randomNum = parseInt((1 + Math.random()) * 65536) + ''
|
||||
return (+(randomNum + timestamp)).toString(32)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an element has a class
|
||||
* @param {HTMLElement} elm
|
||||
* @param {string} cls
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function hasClass(ele, cls) {
|
||||
return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Add class to element
|
||||
* @param {HTMLElement} elm
|
||||
* @param {string} cls
|
||||
*/
|
||||
export function addClass(ele, cls) {
|
||||
if (!hasClass(ele, cls)) ele.className += ' ' + cls
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove class from element
|
||||
* @param {HTMLElement} elm
|
||||
* @param {string} cls
|
||||
*/
|
||||
export function removeClass(ele, cls) {
|
||||
if (hasClass(ele, cls)) {
|
||||
const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
|
||||
ele.className = ele.className.replace(reg, ' ')
|
||||
}
|
||||
}
|
||||
|
||||
export function makeMap(str, expectsLowerCase) {
|
||||
const map = Object.create(null)
|
||||
const list = str.split(',')
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
map[list[i]] = true
|
||||
}
|
||||
return expectsLowerCase
|
||||
? val => map[val.toLowerCase()]
|
||||
: val => map[val]
|
||||
}
|
||||
|
||||
export const exportDefault = 'export default '
|
||||
|
||||
export const beautifierConf = {
|
||||
html: {
|
||||
indent_size: '2',
|
||||
indent_char: ' ',
|
||||
max_preserve_newlines: '-1',
|
||||
preserve_newlines: false,
|
||||
keep_array_indentation: false,
|
||||
break_chained_methods: false,
|
||||
indent_scripts: 'separate',
|
||||
brace_style: 'end-expand',
|
||||
space_before_conditional: true,
|
||||
unescape_strings: false,
|
||||
jslint_happy: false,
|
||||
end_with_newline: true,
|
||||
wrap_line_length: '110',
|
||||
indent_inner_html: true,
|
||||
comma_first: false,
|
||||
e4x: true,
|
||||
indent_empty_lines: true
|
||||
},
|
||||
js: {
|
||||
indent_size: '2',
|
||||
indent_char: ' ',
|
||||
max_preserve_newlines: '-1',
|
||||
preserve_newlines: false,
|
||||
keep_array_indentation: false,
|
||||
break_chained_methods: false,
|
||||
indent_scripts: 'normal',
|
||||
brace_style: 'end-expand',
|
||||
space_before_conditional: true,
|
||||
unescape_strings: false,
|
||||
jslint_happy: true,
|
||||
end_with_newline: true,
|
||||
wrap_line_length: '110',
|
||||
indent_inner_html: true,
|
||||
comma_first: false,
|
||||
e4x: true,
|
||||
indent_empty_lines: true
|
||||
}
|
||||
}
|
||||
|
||||
// 首字母大小
|
||||
export function titleCase(str) {
|
||||
return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
|
||||
}
|
||||
|
||||
// 下划转驼峰
|
||||
export function camelCase(str) {
|
||||
return str.replace(/_[a-z]/g, str1 => str1.substr(-1).toUpperCase())
|
||||
}
|
||||
|
||||
export function isNumberStr(str) {
|
||||
return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
|
||||
}
|
||||
|
||||
30
openhis-ui-vue3/src/utils/jsencrypt.js
Executable file
30
openhis-ui-vue3/src/utils/jsencrypt.js
Executable file
@@ -0,0 +1,30 @@
|
||||
import JSEncrypt from 'jsencrypt'
|
||||
|
||||
// 密钥对生成 http://web.chacuo.net/netrsakeypair
|
||||
|
||||
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
|
||||
'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
|
||||
|
||||
const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
|
||||
'7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
|
||||
'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
|
||||
'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
|
||||
'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
|
||||
'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
|
||||
'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
|
||||
'UP8iWi1Qw0Y='
|
||||
|
||||
// 加密
|
||||
export function encrypt(txt) {
|
||||
const encryptor = new JSEncrypt()
|
||||
encryptor.setPublicKey(publicKey) // 设置公钥
|
||||
return encryptor.encrypt(txt) // 对数据进行加密
|
||||
}
|
||||
|
||||
// 解密
|
||||
export function decrypt(txt) {
|
||||
const encryptor = new JSEncrypt()
|
||||
encryptor.setPrivateKey(privateKey) // 设置私钥
|
||||
return encryptor.decrypt(txt) // 对数据进行解密
|
||||
}
|
||||
|
||||
38
openhis-ui-vue3/src/utils/noticeHelper.js
Executable file
38
openhis-ui-vue3/src/utils/noticeHelper.js
Executable file
@@ -0,0 +1,38 @@
|
||||
// 公告帮助工具类
|
||||
// 用于登录后自动显示公告弹窗
|
||||
|
||||
export function initNoticePopupAfterLogin() {
|
||||
// 监听路由变化,在登录成功后显示公告
|
||||
const token = localStorage.getItem('Admin-Token') || sessionStorage.getItem('Admin-Token')
|
||||
if (!token) return
|
||||
|
||||
// 检查是否已经显示过公告
|
||||
if (sessionStorage.getItem('notice_popup_shown')) return
|
||||
|
||||
// 延迟执行,确保页面完全加载
|
||||
setTimeout(() => {
|
||||
try {
|
||||
// 查找公告弹窗组件并触发显示
|
||||
const noticeElements = document.querySelectorAll('[class*="notice-popup"]')
|
||||
if (noticeElements.length > 0) {
|
||||
// 触发点击事件或查找内部方法
|
||||
const event = new CustomEvent('trigger-notice-popup', { detail: { autoShow: true } })
|
||||
window.dispatchEvent(event)
|
||||
sessionStorage.setItem('notice_popup_shown', 'true')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('自动显示公告弹窗失败:', error)
|
||||
}
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
// 重置公告弹窗显示状态(用于测试)
|
||||
export function resetNoticePopupState() {
|
||||
sessionStorage.removeItem('notice_popup_shown')
|
||||
}
|
||||
|
||||
// 手动触发公告弹窗
|
||||
export function triggerNoticePopup() {
|
||||
const event = new CustomEvent('trigger-notice-popup', { detail: { autoShow: false } })
|
||||
window.dispatchEvent(event)
|
||||
}
|
||||
285
openhis-ui-vue3/src/utils/openhis.js
Executable file
285
openhis-ui-vue3/src/utils/openhis.js
Executable file
@@ -0,0 +1,285 @@
|
||||
|
||||
|
||||
/**
|
||||
* 通用js方法封装处理
|
||||
* Copyright (c) 2019 ruoyi
|
||||
*/
|
||||
|
||||
// 日期格式化
|
||||
export function parseTime(time, pattern) {
|
||||
if (arguments.length === 0 || !time) {
|
||||
return null
|
||||
}
|
||||
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
|
||||
let date
|
||||
if (typeof time === 'object') {
|
||||
date = time
|
||||
} else {
|
||||
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
|
||||
time = parseInt(time)
|
||||
} else if (typeof time === 'string') {
|
||||
// 处理 ISO 8601 格式的时间字符串,保留时区信息
|
||||
// 例如:"2026-01-19T01:59:27.239+08:00"
|
||||
if (time.includes('T')) {
|
||||
// 直接使用 ISO 8601 格式的字符串创建日期对象
|
||||
date = new Date(time)
|
||||
// 如果日期无效,尝试其他格式
|
||||
if (isNaN(date.getTime())) {
|
||||
// 移除毫秒部分但保留时区信息
|
||||
time = time.replace(/\.\d{3}/, '')
|
||||
date = new Date(time)
|
||||
}
|
||||
} else {
|
||||
// 处理其他格式的字符串
|
||||
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
|
||||
}
|
||||
}
|
||||
if (!date || isNaN(date.getTime())) {
|
||||
// 如果还没有创建有效的日期对象,则基于处理后的字符串创建
|
||||
if (typeof time === 'string') {
|
||||
date = new Date(time)
|
||||
}
|
||||
}
|
||||
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
||||
time = time * 1000
|
||||
}
|
||||
if (!date || isNaN(date.getTime())) {
|
||||
// 最终尝试使用原始时间值
|
||||
date = new Date(time)
|
||||
}
|
||||
}
|
||||
|
||||
// 检查日期是否有效
|
||||
if (isNaN(date.getTime())) {
|
||||
return null
|
||||
}
|
||||
|
||||
const formatObj = {
|
||||
y: date.getFullYear(),
|
||||
m: date.getMonth() + 1,
|
||||
d: date.getDate(),
|
||||
h: date.getHours(),
|
||||
i: date.getMinutes(),
|
||||
s: date.getSeconds(),
|
||||
a: date.getDay()
|
||||
}
|
||||
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
||||
let value = formatObj[key]
|
||||
// Note: getDay() returns 0 on Sunday
|
||||
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
|
||||
if (result.length > 0 && value < 10) {
|
||||
value = '0' + value
|
||||
}
|
||||
return value || 0
|
||||
})
|
||||
return time_str
|
||||
}
|
||||
|
||||
// 表单重置
|
||||
export function resetForm(refName) {
|
||||
if (this.$refs[refName]) {
|
||||
this.$refs[refName].resetFields();
|
||||
}
|
||||
}
|
||||
|
||||
// 添加日期范围
|
||||
export function addDateRange(params, dateRange, propName) {
|
||||
let search = params;
|
||||
search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
|
||||
dateRange = Array.isArray(dateRange) ? dateRange : [];
|
||||
if (typeof (propName) === 'undefined') {
|
||||
search.params['beginTime'] = dateRange[0];
|
||||
search.params['endTime'] = dateRange[1];
|
||||
} else {
|
||||
search.params['begin' + propName] = dateRange[0];
|
||||
search.params['end' + propName] = dateRange[1];
|
||||
}
|
||||
return search;
|
||||
}
|
||||
|
||||
// 回显数据字典
|
||||
export function selectDictLabel(datas, value) {
|
||||
if (value === undefined) {
|
||||
return "";
|
||||
}
|
||||
var actions = [];
|
||||
Object.keys(datas).some((key) => {
|
||||
if (datas[key].value == ('' + value)) {
|
||||
actions.push(datas[key].label);
|
||||
return true;
|
||||
}
|
||||
})
|
||||
if (actions.length === 0) {
|
||||
actions.push(value);
|
||||
}
|
||||
return actions.join('');
|
||||
}
|
||||
|
||||
// 回显数据字典(字符串数组)
|
||||
export function selectDictLabels(datas, value, separator) {
|
||||
if (value === undefined || value.length ===0) {
|
||||
return "";
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value = value.join(",");
|
||||
}
|
||||
var actions = [];
|
||||
var currentSeparator = undefined === separator ? "," : separator;
|
||||
var temp = value.split(currentSeparator);
|
||||
Object.keys(value.split(currentSeparator)).some((val) => {
|
||||
var match = false;
|
||||
Object.keys(datas).some((key) => {
|
||||
if (datas[key].value == ('' + temp[val])) {
|
||||
actions.push(datas[key].label + currentSeparator);
|
||||
match = true;
|
||||
}
|
||||
})
|
||||
if (!match) {
|
||||
actions.push(temp[val] + currentSeparator);
|
||||
}
|
||||
})
|
||||
return actions.join('').substring(0, actions.join('').length - 1);
|
||||
}
|
||||
|
||||
// 字符串格式化(%s )
|
||||
export function sprintf(str) {
|
||||
var args = arguments, flag = true, i = 1;
|
||||
str = str.replace(/%s/g, function () {
|
||||
var arg = args[i++];
|
||||
if (typeof arg === 'undefined') {
|
||||
flag = false;
|
||||
return '';
|
||||
}
|
||||
return arg;
|
||||
});
|
||||
return flag ? str : '';
|
||||
}
|
||||
|
||||
// 转换字符串,undefined,null等转化为""
|
||||
export function parseStrEmpty(str) {
|
||||
if (!str || str == "undefined" || str == "null") {
|
||||
return "";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// 数据合并
|
||||
export function mergeRecursive(source, target) {
|
||||
for (var p in target) {
|
||||
try {
|
||||
if (target[p].constructor == Object) {
|
||||
source[p] = mergeRecursive(source[p], target[p]);
|
||||
} else {
|
||||
source[p] = target[p];
|
||||
}
|
||||
} catch (e) {
|
||||
source[p] = target[p];
|
||||
}
|
||||
}
|
||||
return source;
|
||||
};
|
||||
|
||||
/**
|
||||
* 构造树型结构数据
|
||||
* @param {*} data 数据源
|
||||
* @param {*} id id字段 默认 'id'
|
||||
* @param {*} parentId 父节点字段 默认 'parentId'
|
||||
* @param {*} children 孩子节点字段 默认 'children'
|
||||
*/
|
||||
export function handleTree(data, id, parentId, children) {
|
||||
let config = {
|
||||
id: id || 'id',
|
||||
parentId: parentId || 'parentId',
|
||||
childrenList: children || 'children'
|
||||
};
|
||||
|
||||
var childrenListMap = {};
|
||||
var nodeIds = {};
|
||||
var tree = [];
|
||||
|
||||
for (let d of data) {
|
||||
let parentId = d[config.parentId];
|
||||
if (childrenListMap[parentId] == null) {
|
||||
childrenListMap[parentId] = [];
|
||||
}
|
||||
nodeIds[d[config.id]] = d;
|
||||
childrenListMap[parentId].push(d);
|
||||
}
|
||||
|
||||
for (let d of data) {
|
||||
let parentId = d[config.parentId];
|
||||
if (nodeIds[parentId] == null) {
|
||||
tree.push(d);
|
||||
}
|
||||
}
|
||||
|
||||
for (let t of tree) {
|
||||
adaptToChildrenList(t);
|
||||
}
|
||||
|
||||
function adaptToChildrenList(o) {
|
||||
if (childrenListMap[o[config.id]] !== null) {
|
||||
o[config.childrenList] = childrenListMap[o[config.id]];
|
||||
}
|
||||
if (o[config.childrenList]) {
|
||||
for (let c of o[config.childrenList]) {
|
||||
adaptToChildrenList(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数处理
|
||||
* @param {*} params 参数
|
||||
*/
|
||||
export function tansParams(params) {
|
||||
let result = ''
|
||||
for (const propName of Object.keys(params)) {
|
||||
const value = params[propName];
|
||||
var part = encodeURIComponent(propName) + "=";
|
||||
if (value !== null && value !== "" && typeof (value) !== "undefined") {
|
||||
if (typeof value === 'object') {
|
||||
// 处理数组类型
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
if (item !== null && item !== "" && typeof (item) !== 'undefined') {
|
||||
result += part + encodeURIComponent(item) + "&";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 处理对象类型
|
||||
for (const key of Object.keys(value)) {
|
||||
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
|
||||
let params = propName + '[' + key + ']';
|
||||
var subPart = encodeURIComponent(params) + "=";
|
||||
result += subPart + encodeURIComponent(value[key]) + "&";
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result += part + encodeURIComponent(value) + "&";
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// 返回项目路径
|
||||
export function getNormalPath(p) {
|
||||
if (p.length === 0 || !p || p == 'undefined') {
|
||||
return p
|
||||
};
|
||||
let res = p.replace('//', '/')
|
||||
if (res[res.length - 1] === '/') {
|
||||
return res.slice(0, res.length - 1)
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// 验证是否为blob格式
|
||||
export function blobValidate(data) {
|
||||
return data.type !== 'application/json'
|
||||
}
|
||||
51
openhis-ui-vue3/src/utils/permission.js
Executable file
51
openhis-ui-vue3/src/utils/permission.js
Executable file
@@ -0,0 +1,51 @@
|
||||
import useUserStore from '@/store/modules/user'
|
||||
|
||||
/**
|
||||
* 字符权限校验
|
||||
* @param {Array} value 校验值
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function checkPermi(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const permissions = useUserStore().permissions
|
||||
const permissionDatas = value
|
||||
const all_permission = "*:*:*";
|
||||
|
||||
const hasPermission = permissions.some(permission => {
|
||||
return all_permission === permission || permissionDatas.includes(permission)
|
||||
})
|
||||
|
||||
if (!hasPermission) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色权限校验
|
||||
* @param {Array} value 校验值
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function checkRole(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const roles = useUserStore().roles
|
||||
const permissionRoles = value
|
||||
const super_admin = "admin";
|
||||
|
||||
const hasRole = roles.some(role => {
|
||||
return super_admin === role || permissionRoles.includes(role)
|
||||
})
|
||||
|
||||
if (!hasRole) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
console.error(`need roles! Like checkRole="['admin','editor']"`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
702
openhis-ui-vue3/src/utils/printUtils.js
Executable file
702
openhis-ui-vue3/src/utils/printUtils.js
Executable file
@@ -0,0 +1,702 @@
|
||||
/**
|
||||
* 打印工具类
|
||||
* 集中管理所有打印相关功能
|
||||
*/
|
||||
|
||||
import {hiprint} from 'vue-plugin-hiprint';
|
||||
|
||||
// 打印模板映射表 .
|
||||
const TEMPLATE_MAP = {
|
||||
// CLINIC_CHARGE: () => import('@/views/charge/cliniccharge/components/template.json'),
|
||||
// DISPOSAL: () => import('@/views/clinicmanagement/disposal/components/disposalTemplate.json'),
|
||||
//处方签
|
||||
PRESCRIPTION: () => import('@/components/Print/Prescription.json'),
|
||||
//处置单
|
||||
DISPOSAL: () => import('@/components/Print/Disposal.json'),
|
||||
//门诊日结
|
||||
DAY_END: () => import('@/components/Print/DailyOutpatientSettlement.json'),
|
||||
WESTERN_MEDICINE: () =>
|
||||
import('@/views/pharmacymanagement/westernmedicine/components/templateJson.json'),
|
||||
IN_HOSPITAL_DISPENSING: () =>
|
||||
import('@/views/drug/inHospitalDispensing/components/templateJson.json'),
|
||||
// 门诊挂号
|
||||
OUTPATIENT_REGISTRATION: () => import('@/components/Print/OutpatientRegistration.json'),
|
||||
// 门诊手术计费(含流程图)
|
||||
OUTPATIENT_SURGERY_CHARGE: () => import('@/components/Print/OutpatientSurgeryCharge.json'),
|
||||
//门诊收费
|
||||
OUTPATIENT_CHARGE: () => import('@/components/Print/OutpatientBilling.json'),
|
||||
//门诊病历
|
||||
OUTPATIENT_MEDICAL_RECORD: () => import('@/components/Print/OutpatientMedicalRecord.json'),
|
||||
//门诊输液贴
|
||||
OUTPATIENT_INFUSION: () => import('@/components/Print/OutpatientInfusion.json'),
|
||||
//手术记录
|
||||
OPERATIVE_RECORD: () => import('@/components/Print/OperativeRecord.json'),
|
||||
|
||||
//红旗门诊病历
|
||||
HQOUTPATIENT_MEDICAL_RECORD: () => import('@/components/Print/HQOutpatientMedicalRecord.json'),
|
||||
//预交金
|
||||
ADVANCE_PAYMENT: () => import('@/components/Print/AdvancePayment.json'),
|
||||
//中药处方单
|
||||
CHINESE_MEDICINE_PRESCRIPTION: () =>
|
||||
import('@/components/Print/ChineseMedicinePrescription.json'),
|
||||
//药房处方单
|
||||
PHARMACY_PRESCRIPTION: () => import('@/components/Print/Pharmacy.json'),
|
||||
//中药医生处方单
|
||||
DOC_CHINESE_MEDICINE_PRESCRIPTION: () =>
|
||||
import('@/components/Print/DocChineseMedicinePrescription.json'),
|
||||
|
||||
// ========== 新增模板(原LODOP迁移)==========
|
||||
//腕带
|
||||
WRIST_BAND: () => import('@/components/Print/WristBand.json'),
|
||||
//分诊条
|
||||
TRIAGE_TICKET: () => import('@/components/Print/TriageTicket.json'),
|
||||
//输液标签
|
||||
INJECT_LABEL: () => import('@/components/Print/InjectLabel.json'),
|
||||
//床头卡
|
||||
BED_CARD: () => import('@/components/Print/BedCard.json'),
|
||||
//护理交接班
|
||||
CHANGE_SHIFT_BILL: () => import('@/components/Print/ChangeShiftBill.json'),
|
||||
//医嘱执行单
|
||||
EXE_ORDER_SHEET: () => import('@/components/Print/ExeOrderSheet.json'),
|
||||
//体温单
|
||||
TEMPERATURE_SHEET: () => import('@/components/Print/TemperatureSheet.json'),
|
||||
//会诊申请单
|
||||
CONSULTATION: () => import('@/components/Print/Consultation.json'),
|
||||
};
|
||||
|
||||
/**
|
||||
* 极简打印方法
|
||||
* @param {string} templateName 打印模板名称(常量或字符串)
|
||||
* @param {Array|Object} data 打印数据
|
||||
* @param {string} printerName 打印机名称(可选)
|
||||
* @param {Object} options 打印选项(可选)
|
||||
* @returns {Promise} 打印结果Promise
|
||||
*/
|
||||
export async function simplePrint(templateName, data, printerName, options = {}) {
|
||||
try {
|
||||
// 获取模板
|
||||
const template = await loadTemplate(templateName);
|
||||
|
||||
// 执行打印,业务名称默认为模板名称
|
||||
return await executePrint(data, template, printerName, options, templateName);
|
||||
} catch (error) {
|
||||
console.error('打印失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载打印模板
|
||||
* @param {string} templateName 模板名称
|
||||
* @returns {Promise<Object>} 模板对象
|
||||
*/
|
||||
async function loadTemplate(templateName) {
|
||||
// 如果是常量形式,获取实际的键名
|
||||
const templateKey = typeof templateName === 'symbol' ? templateName.description : templateName;
|
||||
|
||||
if (!TEMPLATE_MAP[templateKey]) {
|
||||
throw new Error(`未找到打印模板: ${templateKey}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const templateModule = await TEMPLATE_MAP[templateKey]();
|
||||
return templateModule.default || templateModule;
|
||||
} catch (error) {
|
||||
console.error(`加载模板 ${templateKey} 失败:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 带打印机选择的极简打印方法
|
||||
* @param {string} templateName 打印模板名称
|
||||
* @param {Array|Object} data 打印数据
|
||||
* @param {Function} showPrinterDialog 显示打印机选择对话框的函数
|
||||
* @param {Object} modal 消息提示对象
|
||||
* @param {Function} callback 打印完成后的回调函数(可选)
|
||||
* @returns {Promise} 打印结果Promise
|
||||
*/
|
||||
export async function simplePrintWithDialog(
|
||||
templateName,
|
||||
data,
|
||||
showPrinterDialog,
|
||||
modal,
|
||||
callback
|
||||
) {
|
||||
try {
|
||||
// 获取模板
|
||||
const template = await loadTemplate(templateName);
|
||||
|
||||
// 执行打印
|
||||
await selectPrinterAndPrint(data, template, showPrinterDialog, modal, callback, templateName);
|
||||
} catch (error) {
|
||||
modal.msgError(error.message || '打印失败');
|
||||
if (callback) callback(error);
|
||||
}
|
||||
}
|
||||
|
||||
// 导出模板名称常量
|
||||
export const PRINT_TEMPLATE = {
|
||||
// CLINIC_CHARGE: 'CLINIC_CHARGE',
|
||||
DAY_END: 'DAY_END',
|
||||
WESTERN_MEDICINE: 'WESTERN_MEDICINE',
|
||||
IN_HOSPITAL_DISPENSING: 'IN_HOSPITAL_DISPENSING',
|
||||
//门诊挂号
|
||||
OUTPATIENT_REGISTRATION: 'OUTPATIENT_REGISTRATION',
|
||||
// 门诊手术计费
|
||||
OUTPATIENT_SURGERY_CHARGE: 'OUTPATIENT_SURGERY_CHARGE',
|
||||
//门诊收费
|
||||
OUTPATIENT_CHARGE: 'OUTPATIENT_CHARGE',
|
||||
//处方签
|
||||
PRESCRIPTION: 'PRESCRIPTION',
|
||||
//处置单
|
||||
DISPOSAL: 'DISPOSAL',
|
||||
//门诊病历
|
||||
OUTPATIENT_MEDICAL_RECORD: 'OUTPATIENT_MEDICAL_RECORD',
|
||||
//门诊输液贴
|
||||
OUTPATIENT_INFUSION: 'OUTPATIENT_INFUSION',
|
||||
//手术记录
|
||||
OPERATIVE_RECORD: 'OPERATIVE_RECORD',
|
||||
//红旗门诊病历
|
||||
HQOUTPATIENT_MEDICAL_RECORD: 'HQOUTPATIENT_MEDICAL_RECORD',
|
||||
//预交金
|
||||
ADVANCE_PAYMENT: 'ADVANCE_PAYMENT',
|
||||
//中药处方单
|
||||
CHINESE_MEDICINE_PRESCRIPTION: 'CHINESE_MEDICINE_PRESCRIPTION',
|
||||
//药房处方单
|
||||
PHARMACY_PRESCRIPTION: 'PHARMACY_PRESCRIPTION',
|
||||
//中药医生处方单
|
||||
DOC_CHINESE_MEDICINE_PRESCRIPTION: 'DOC_CHINESE_MEDICINE_PRESCRIPTION',
|
||||
|
||||
// ========== 新增模板(原LODOP迁移)==========
|
||||
//腕带
|
||||
WRIST_BAND: 'WRIST_BAND',
|
||||
//分诊条
|
||||
TRIAGE_TICKET: 'TRIAGE_TICKET',
|
||||
//输液标签
|
||||
INJECT_LABEL: 'INJECT_LABEL',
|
||||
//床头卡
|
||||
BED_CARD: 'BED_CARD',
|
||||
//护理交接班
|
||||
CHANGE_SHIFT_BILL: 'CHANGE_SHIFT_BILL',
|
||||
//医嘱执行单
|
||||
EXE_ORDER_SHEET: 'EXE_ORDER_SHEET',
|
||||
//体温单
|
||||
TEMPERATURE_SHEET: 'TEMPERATURE_SHEET',
|
||||
//会诊申请单
|
||||
CONSULTATION: 'CONSULTATION',
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取打印机列表
|
||||
* @returns {Array} 打印机列表
|
||||
*/
|
||||
export function getPrinterList() {
|
||||
try {
|
||||
if (hiprint && hiprint.hiwebSocket && hiprint.hiwebSocket.connected) {
|
||||
const printerList = hiprint.hiwebSocket.getPrinterList();
|
||||
return printerList || [];
|
||||
} else {
|
||||
console.warn('打印服务未连接,返回空打印机列表');
|
||||
return [];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取打印机列表失败:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
import useUserStore from '@/store/modules/user';
|
||||
import {ElMessage} from 'element-plus';
|
||||
|
||||
/**
|
||||
* 获取当前登录用户ID
|
||||
* @returns {string} 用户ID
|
||||
*/
|
||||
function getCurrentUserId() {
|
||||
try {
|
||||
// 从Pinia store中获取当前用户ID
|
||||
const userStore = useUserStore();
|
||||
return userStore.id || '';
|
||||
} catch (e) {
|
||||
console.error('获取用户ID失败:', e);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成打印机缓存键
|
||||
* @param {string} businessName 打印业务名称
|
||||
* @returns {string} 缓存键
|
||||
*/
|
||||
function getPrinterCacheKey(businessName) {
|
||||
const userId = getCurrentUserId();
|
||||
return `selectedPrinter_${businessName || 'default'}_${userId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存获取上次选择的打印机
|
||||
* @param {string} businessName 打印业务名称
|
||||
* @returns {string} 打印机名称
|
||||
*/
|
||||
export function getCachedPrinter(businessName = 'default') {
|
||||
const cacheKey = getPrinterCacheKey(businessName);
|
||||
return localStorage.getItem(cacheKey) || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存打印机选择到缓存
|
||||
* @param {string} printerName 打印机名称
|
||||
* @param {string} businessName 打印业务名称
|
||||
*/
|
||||
export function savePrinterToCache(printerName, businessName = 'default') {
|
||||
if (printerName) {
|
||||
const cacheKey = getPrinterCacheKey(businessName);
|
||||
localStorage.setItem(cacheKey, printerName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行打印操作
|
||||
* @param {Array} data 打印数据
|
||||
* @param {Object} template 打印模板
|
||||
* @param {string} printerName 打印机名称(可选)
|
||||
* @param {Object} options 打印选项(可选)
|
||||
* @param {string} businessName 打印业务名称(可选)
|
||||
* @returns {Promise} 打印结果Promise
|
||||
*/
|
||||
export function executePrint(data, template, printerName, options = {}, businessName = 'default') {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 调试信息
|
||||
console.log('========== 打印诊断日志开始 ==========');
|
||||
console.log('[1] hiprint对象检查:');
|
||||
console.log(' - hiprint存在:', !!hiprint);
|
||||
console.log(' - hiprint.PrintTemplate存在:', !!(hiprint && hiprint.PrintTemplate));
|
||||
console.log(' - hiprint.hiwebSocket存在:', !!(hiprint && hiprint.hiwebSocket));
|
||||
console.log(' - hiprint.hiwebSocket.connected:', hiprint?.hiwebSocket?.connected);
|
||||
|
||||
console.log('[2] 模板数据检查:');
|
||||
console.log(' - 模板类型:', typeof template);
|
||||
console.log(' - 模板存在:', !!template);
|
||||
console.log(' - 模板panels存在:', !!(template && template.panels));
|
||||
console.log(' - panels数量:', template?.panels?.length);
|
||||
|
||||
if (template?.panels?.[0]) {
|
||||
const panel = template.panels[0];
|
||||
console.log(' - panel.name:', panel.name, '(类型:', typeof panel.name, ')');
|
||||
console.log(' - panel.index:', panel.index);
|
||||
console.log(' - panel.printElements数量:', panel.printElements?.length);
|
||||
|
||||
// 检查每个元素的类型
|
||||
if (panel.printElements) {
|
||||
panel.printElements.forEach((el, idx) => {
|
||||
console.log(` - 元素[${idx}]:`, el.printElementType?.type, '-', el.printElementType?.title);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const userStore = useUserStore();
|
||||
console.log('[3] 医院名称:', userStore.hospitalName);
|
||||
|
||||
let processedTemplate;
|
||||
try {
|
||||
processedTemplate = JSON.parse(
|
||||
JSON.stringify(template).replace(/{{HOSPITAL_NAME}}/g, userStore.hospitalName)
|
||||
);
|
||||
console.log('[4] 模板处理成功');
|
||||
} catch (parseError) {
|
||||
console.error('[4] 模板处理失败:', parseError);
|
||||
throw new Error('模板处理失败: ' + parseError.message);
|
||||
}
|
||||
|
||||
console.log('[5] 打印数据检查:');
|
||||
console.log(' - 数据类型:', typeof data);
|
||||
console.log(' - 数据存在:', !!data);
|
||||
if (data && typeof data === 'object') {
|
||||
console.log(' - 数据字段:', Object.keys(data));
|
||||
}
|
||||
|
||||
// 创建打印模板
|
||||
let hiprintTemplate;
|
||||
try {
|
||||
console.log('[6] 开始创建PrintTemplate...');
|
||||
hiprintTemplate = new hiprint.PrintTemplate({ template: processedTemplate });
|
||||
console.log('[6] PrintTemplate创建成功:', hiprintTemplate);
|
||||
} catch (templateError) {
|
||||
console.error('[6] 创建打印模板失败:', templateError);
|
||||
console.error('错误堆栈:', templateError.stack);
|
||||
console.error('模板内容:', JSON.stringify(processedTemplate, null, 2));
|
||||
throw new Error('打印模板创建失败: ' + templateError.message);
|
||||
}
|
||||
|
||||
const printOptions = {
|
||||
title: '打印标题',
|
||||
height: 210,
|
||||
width: 148,
|
||||
...options,
|
||||
};
|
||||
console.log('[7] 打印选项:', printOptions);
|
||||
|
||||
// 检查客户端是否连接
|
||||
const isClientConnected = hiprint.hiwebSocket && hiprint.hiwebSocket.connected;
|
||||
console.log('[8] 客户端连接状态:', isClientConnected);
|
||||
|
||||
// 如果指定了打印机且客户端已连接,添加到打印选项中
|
||||
if (printerName && isClientConnected) {
|
||||
printOptions.printer = printerName;
|
||||
// 保存到缓存
|
||||
savePrinterToCache(printerName, businessName);
|
||||
console.log('[8] 使用指定打印机:', printerName);
|
||||
}
|
||||
|
||||
// 打印成功回调
|
||||
hiprintTemplate.on('printSuccess', function (e) {
|
||||
console.log('[9] 打印成功:', e);
|
||||
console.log('========== 打印诊断日志结束 ==========');
|
||||
resolve({ success: true, event: e });
|
||||
});
|
||||
|
||||
// 打印失败回调
|
||||
hiprintTemplate.on('printError', function (e) {
|
||||
console.error('[9] 打印失败:', e);
|
||||
console.log('========== 打印诊断日志结束 ==========');
|
||||
reject({ success: false, event: e, message: '打印失败' });
|
||||
});
|
||||
|
||||
// 根据客户端连接状态选择打印方式
|
||||
console.log('[10] 开始执行打印...');
|
||||
if (isClientConnected && printerName) {
|
||||
// 客户端已连接且指定了打印机,使用静默打印
|
||||
console.log('[10] 使用print2静默打印');
|
||||
try {
|
||||
hiprintTemplate.print2(data, printOptions);
|
||||
console.log('[10] print2调用完成');
|
||||
} catch (print2Error) {
|
||||
console.error('[10] print2调用失败:', print2Error);
|
||||
console.error('[10] print2错误堆栈:', print2Error.stack);
|
||||
throw new Error('print2打印失败: ' + print2Error.message);
|
||||
}
|
||||
} else {
|
||||
// 客户端未连接或未指定打印机,使用浏览器打印预览(不需要客户端连接)
|
||||
console.log('[10] 使用print浏览器打印预览');
|
||||
console.log('[10] hiprintTemplate.print方法存在:', typeof hiprintTemplate.print === 'function');
|
||||
try {
|
||||
hiprintTemplate.print(data, printOptions, {
|
||||
styleHandler: () => {
|
||||
console.log('[10] styleHandler被调用');
|
||||
return '<style>@media print { @page { margin: 0; } }</style>';
|
||||
},
|
||||
callback: (e) => {
|
||||
console.log('[10] 打印回调被调用:', e);
|
||||
}
|
||||
});
|
||||
console.log('[10] print调用完成');
|
||||
} catch (printError) {
|
||||
console.error('[10] print调用失败:', printError);
|
||||
console.error('[10] print错误堆栈:', printError.stack);
|
||||
throw new Error('print打印失败: ' + printError.message);
|
||||
}
|
||||
// 浏览器打印模式下,直接resolve(因为打印窗口已打开)
|
||||
console.log('[10] 浏览器打印模式,直接返回成功');
|
||||
console.log('========== 打印诊断日志结束 ==========');
|
||||
resolve({ success: true, message: '打印窗口已打开' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[ERROR] 打印过程中发生错误:', error);
|
||||
console.error('[ERROR] 错误类型:', error?.constructor?.name);
|
||||
console.error('[ERROR] 错误消息:', error?.message);
|
||||
console.error('[ERROR] 错误堆栈:', error?.stack);
|
||||
console.log('========== 打印诊断日志结束 ==========');
|
||||
reject({ success: false, error: error, message: error?.message || '打印过程中发生错误' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择打印机并执行打印
|
||||
* @param {Array} data 打印数据
|
||||
* @param {Object} template 打印模板
|
||||
* @param {Function} showPrinterDialog 显示打印机选择对话框的函数
|
||||
* @param {Object} modal 消息提示对象
|
||||
* @param {Function} callback 打印完成后的回调函数
|
||||
* @param {string} businessName 打印业务名称(可选)
|
||||
*/
|
||||
export async function selectPrinterAndPrint(
|
||||
data,
|
||||
template,
|
||||
showPrinterDialog,
|
||||
modal,
|
||||
callback,
|
||||
businessName = 'default'
|
||||
) {
|
||||
try {
|
||||
// 获取打印机列表
|
||||
const printerList = getPrinterList();
|
||||
|
||||
if (printerList.length === 0) {
|
||||
modal.msgWarning('未检测到可用打印机');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取缓存的打印机
|
||||
const cachedPrinter = getCachedPrinter(businessName);
|
||||
let selectedPrinter = '';
|
||||
|
||||
// 判断打印机选择逻辑
|
||||
if (printerList.length === 1) {
|
||||
selectedPrinter = printerList[0].name;
|
||||
await executePrint(data, template, selectedPrinter, {}, businessName);
|
||||
if (callback) callback();
|
||||
} else if (cachedPrinter && printerList.some((printer) => printer.name === cachedPrinter)) {
|
||||
selectedPrinter = cachedPrinter;
|
||||
await executePrint(data, template, selectedPrinter, {}, businessName);
|
||||
if (callback) callback();
|
||||
} else {
|
||||
// 调用显示打印机选择对话框的函数
|
||||
showPrinterDialog(printerList, async (chosenPrinter) => {
|
||||
try {
|
||||
await executePrint(data, template, chosenPrinter, {}, businessName);
|
||||
if (callback) callback();
|
||||
} catch (error) {
|
||||
modal.msgError(error.message || '打印失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
modal.msgError(error.message || '获取打印机列表失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 预览打印
|
||||
export function previewPrint(elementDom) {
|
||||
if (elementDom) {
|
||||
// 初始化已在 main.js 中完成,无需重复调用
|
||||
// hiprint.init();
|
||||
const hiprintTemplate = new hiprint.PrintTemplate();
|
||||
// printByHtml为预览打印
|
||||
hiprintTemplate.printByHtml(elementDom, {});
|
||||
} else {
|
||||
ElMessage({
|
||||
type: 'error',
|
||||
message: '加载模版失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 打印门诊挂号收据(使用浏览器打印,模板与补打挂号一致)
|
||||
* @param {Object} data 打印数据
|
||||
* @param {Object} options 打印选项
|
||||
* @returns {Promise} 打印结果 Promise
|
||||
*/
|
||||
export function printRegistrationReceipt(data, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 构建打印内容的 HTML
|
||||
const printContent = `
|
||||
<div class="print-header">
|
||||
<div class="header-content">
|
||||
<div class="document-title">门诊预约挂号凭条</div>
|
||||
<div class="print-time">打印时间:${data.printTime || new Date().toLocaleString()}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="print-section">
|
||||
<div class="section-title">患者基本信息</div>
|
||||
<div class="info-row">
|
||||
<span class="label">患者姓名:</span>
|
||||
<span class="value">${data.patientName || '-'}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">就诊卡号:</span>
|
||||
<span class="value">${data.cardNo || data.busNo || '-'}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">身份证号:</span>
|
||||
<span class="value">${data.idCard ? maskIdCard(data.idCard) : '-'}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">联系电话:</span>
|
||||
<span class="value">${data.phone || '-'}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="print-section">
|
||||
<div class="section-title">挂号信息</div>
|
||||
<div class="info-row">
|
||||
<span class="label">就诊科室:</span>
|
||||
<span class="value">${data.organizationName || '-'}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">医生姓名:</span>
|
||||
<span class="value">${data.practitionerName || '-'}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">挂号类型:</span>
|
||||
<span class="value">${data.healthcareName || '-'}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">挂号时间:</span>
|
||||
<span class="value">${data.visitTime || data.chargeTime || '-'}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="print-section">
|
||||
<div class="section-title">费用信息</div>
|
||||
<table class="fee-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>项目</th>
|
||||
<th>数量</th>
|
||||
<th>单价</th>
|
||||
<th>金额</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>挂号费</td>
|
||||
<td>1</td>
|
||||
<td>¥${parseFloat(data.price || 0).toFixed(2)}</td>
|
||||
<td>¥${parseFloat(data.price || 0).toFixed(2)}</td>
|
||||
</tr>
|
||||
${parseFloat(data.activityPrice || 0) > 0 ? `
|
||||
<tr>
|
||||
<td>诊疗费</td>
|
||||
<td>1</td>
|
||||
<td>¥${parseFloat(data.activityPrice || 0).toFixed(2)}</td>
|
||||
<td>¥${parseFloat(data.activityPrice || 0).toFixed(2)}</td>
|
||||
</tr>` : ''}
|
||||
${parseFloat(data.medicalRecordFee || 0) > 0 ? `
|
||||
<tr>
|
||||
<td>病历费</td>
|
||||
<td>1</td>
|
||||
<td>¥${parseFloat(data.medicalRecordFee || 0).toFixed(2)}</td>
|
||||
<td>¥${parseFloat(data.medicalRecordFee || 0).toFixed(2)}</td>
|
||||
</tr>` : ''}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="3" class="total-label">合计:</td>
|
||||
<td class="total-value">¥${parseFloat(data.totalPrice || data.amount || 0).toFixed(2)}</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 流水号显示在左下角 -->
|
||||
<div class="serial-number-bottom-left">
|
||||
<span class="serial-label">流水号:</span>
|
||||
<span class="serial-value">${data.serialNo || data.encounterId || '-'}</span>
|
||||
</div>
|
||||
|
||||
<div class="print-footer">
|
||||
<div class="reminder">温馨提示:请妥善保管此凭条,就诊时请携带。</div>
|
||||
</div>
|
||||
|
||||
<!-- 二维码区域 -->
|
||||
<div class="qr-code-section">
|
||||
<div class="qr-code-container">
|
||||
<div id="qrcode-print" class="qrcode-print"></div>
|
||||
<div class="qr-code-label">扫码查看挂号信息</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// 创建新窗口用于打印
|
||||
const printWindow = window.open('', '_blank');
|
||||
if (!printWindow) {
|
||||
reject(new Error('无法打开打印窗口,请检查浏览器弹窗设置'));
|
||||
return;
|
||||
}
|
||||
|
||||
// 写入打印内容
|
||||
printWindow.document.write(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>门诊预约挂号凭条</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; padding: 20px; margin: 0; }
|
||||
.print-header { margin-bottom: 20px; position: relative; }
|
||||
.header-content { text-align: center; }
|
||||
.document-title { font-size: 16px; font-weight: bold; margin-bottom: 10px; }
|
||||
.print-time { font-size: 12px; color: #666; text-align: right; }
|
||||
.print-section { margin-bottom: 20px; }
|
||||
.section-title { font-size: 14px; font-weight: bold; margin-bottom: 10px; border-bottom: 1px solid #ddd; padding-bottom: 5px; }
|
||||
.info-row { margin-bottom: 8px; font-size: 13px; }
|
||||
.label { display: inline-block; width: 100px; font-weight: bold; }
|
||||
.value { display: inline-block; }
|
||||
.fee-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
|
||||
.fee-table th, .fee-table td { border: 1px solid #ddd; padding: 8px; text-align: left; }
|
||||
.fee-table th { background-color: #f5f5f5; font-weight: bold; }
|
||||
.total-label { font-weight: bold; text-align: right; }
|
||||
.total-value { font-weight: bold; color: red; }
|
||||
.serial-number-bottom-left { position: absolute; bottom: 20px; left: 20px; font-size: 14px; font-weight: bold; }
|
||||
.serial-number-bottom-left .serial-label { font-weight: bold; margin-right: 5px; }
|
||||
.serial-number-bottom-left .serial-value { font-weight: bold; color: #333; }
|
||||
.print-content { position: relative; min-height: 500px; padding-bottom: 60px; }
|
||||
.print-footer { margin-top: 20px; font-size: 12px; color: #666; }
|
||||
.reminder { text-align: center; padding: 10px; background-color: #f9f9f9; border-radius: 4px; }
|
||||
.qr-code-section { margin-top: 20px; display: flex; justify-content: center; align-items: center; padding: 15px; }
|
||||
.qr-code-container { display: flex; flex-direction: column; align-items: center; gap: 10px; }
|
||||
.qrcode-print { width: 120px; height: 120px; }
|
||||
.qr-code-label { font-size: 12px; color: #666; text-align: center; }
|
||||
@media print { body { padding: 0; } .qr-code-section { page-break-inside: avoid; } }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="print-content">
|
||||
${printContent}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
|
||||
printWindow.document.close();
|
||||
printWindow.onload = function() {
|
||||
setTimeout(() => {
|
||||
printWindow.print();
|
||||
resolve({ success: true, message: '打印窗口已打开' });
|
||||
}, 250);
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('打印门诊挂号收据失败:', error);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 脱敏身份证号
|
||||
* @param {string} idCard 身份证号
|
||||
* @returns {string} 脱敏后的身份证号
|
||||
*/
|
||||
function maskIdCard(idCard) {
|
||||
if (!idCard) return '';
|
||||
if (idCard.length >= 10) {
|
||||
const prefix = idCard.substring(0, 6);
|
||||
const suffix = idCard.substring(idCard.length - 4);
|
||||
const stars = '*'.repeat(Math.max(0, idCard.length - 10));
|
||||
return prefix + stars + suffix;
|
||||
} else if (idCard.length >= 6) {
|
||||
const prefix = idCard.substring(0, 3);
|
||||
const suffix = idCard.substring(idCard.length - 1);
|
||||
return prefix + '*'.repeat(idCard.length - 4) + suffix;
|
||||
}
|
||||
return idCard;
|
||||
}
|
||||
|
||||
// 默认导出简化的打印方法
|
||||
export default {
|
||||
print: simplePrint,
|
||||
printWithDialog: simplePrintWithDialog,
|
||||
TEMPLATE: PRINT_TEMPLATE,
|
||||
executePrint,
|
||||
selectPrinterAndPrint,
|
||||
getPrinterList,
|
||||
getCachedPrinter,
|
||||
savePrinterToCache,
|
||||
};
|
||||
275
openhis-ui-vue3/src/utils/request.js
Executable file
275
openhis-ui-vue3/src/utils/request.js
Executable file
@@ -0,0 +1,275 @@
|
||||
import axios from 'axios'
|
||||
import {ElLoading, ElMessage, ElMessageBox, ElNotification} from 'element-plus'
|
||||
import {getToken} from '@/utils/auth'
|
||||
import errorCode from '@/utils/errorCode'
|
||||
import {blobValidate, tansParams} from '@/utils/openhis'
|
||||
import cache from '@/plugins/cache'
|
||||
import {saveAs} from 'file-saver'
|
||||
import useUserStore from '@/store/modules/user'
|
||||
import JSONBig from 'json-bigint'
|
||||
|
||||
// 初始化json-bigint,配置大数字转字符串(关键:storeAsString: true)
|
||||
const jsonBig = JSONBig({ storeAsString: true })
|
||||
|
||||
// 🔧 Bug Fix #281: 转换所有ID字段为字符串,防止BigInt精度丢失
|
||||
const convertIdsToString = (obj) => {
|
||||
if (obj === null || obj === undefined) return obj
|
||||
|
||||
if (typeof obj === 'number' && obj > 9007199254740991) {
|
||||
// 如果是超过安全范围的数字,转为字符串
|
||||
return String(obj)
|
||||
}
|
||||
|
||||
if (typeof obj === 'object') {
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(item => convertIdsToString(item))
|
||||
} else {
|
||||
const newObj = {}
|
||||
for (const key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
const value = obj[key]
|
||||
// 如果key以Id结尾或者是id,且值是数字,转为字符串
|
||||
if ((key === 'id' || key.endsWith('Id') || key.endsWith('ID')) && typeof value === 'number') {
|
||||
newObj[key] = String(value)
|
||||
} else {
|
||||
newObj[key] = convertIdsToString(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return newObj
|
||||
}
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
let downloadLoadingInstance;
|
||||
// 是否显示重新登录
|
||||
export let isRelogin = { show: false };
|
||||
|
||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||
// 从环境变量读取租户ID,如果没有则使用默认值'1'
|
||||
axios.defaults.headers['X-Tenant-ID'] = import.meta.env.VITE_APP_TENANT_ID || '1'
|
||||
axios.defaults.headers['Request-Method-Name'] = 'login'
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
baseURL: import.meta.env.VITE_APP_BASE_API,
|
||||
timeout: 120000, // 增加到120秒,配合后端超时设置
|
||||
// 新增:重写响应解析逻辑,大数字自动转字符串(移到这里!)
|
||||
transformResponse: [
|
||||
function (data) {
|
||||
if (!data) return {}
|
||||
// 如果是 Blob 或 ArrayBuffer,直接返回,不进行 JSON 解析
|
||||
if (data instanceof Blob || data instanceof ArrayBuffer) {
|
||||
return data
|
||||
}
|
||||
try {
|
||||
return jsonBig.parse(data)
|
||||
} catch (err) {
|
||||
return JSON.parse(data)
|
||||
}
|
||||
}
|
||||
],
|
||||
// 可选:请求体序列化,使用json-bigint处理大数字
|
||||
transformRequest: [
|
||||
function (data) {
|
||||
if (!data) return data
|
||||
// 🔧 Bug Fix #281: 使用json-bigint序列化,保留大数字精度
|
||||
return jsonBig.stringify(data)
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// request拦截器
|
||||
service.interceptors.request.use(config => {
|
||||
// 🔧 Bug Fix #281: 转换请求数据中的ID字段为字符串
|
||||
if (config.data && typeof config.data === 'object') {
|
||||
config.data = convertIdsToString(config.data)
|
||||
}
|
||||
|
||||
// 是否需要设置 token
|
||||
const isToken = (config.headers || {}).isToken === false
|
||||
// 是否需要防止数据重复提交
|
||||
const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
|
||||
if (getToken() && !isToken) {
|
||||
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||||
}
|
||||
// get请求映射params参数
|
||||
if (config.method === 'get' && config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params);
|
||||
url = url.slice(0, -1);
|
||||
config.params = {};
|
||||
config.url = url;
|
||||
}
|
||||
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
|
||||
const requestObj = {
|
||||
url: config.url,
|
||||
data: typeof config.data === 'object' ? jsonBig.stringify(config.data) : config.data,
|
||||
time: new Date().getTime()
|
||||
}
|
||||
// 🔧 Bug Fix #281: 使用json-bigint计算大小
|
||||
const requestSize = Object.keys(jsonBig.stringify(requestObj)).length; // 请求数据大小
|
||||
const limitSize = 5 * 1024 * 1024; // 限制存放数据5M
|
||||
if (requestSize >= limitSize) {
|
||||
console.warn(`[${config.url}]: ` + '请求数据大小超出允许的5M限制,无法进行防重复提交验证。')
|
||||
return config;
|
||||
}
|
||||
const sessionObj = cache.session.getJSON('sessionObj')
|
||||
if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
|
||||
cache.session.setJSON('sessionObj', requestObj)
|
||||
} else {
|
||||
const s_url = sessionObj.url; // 请求地址
|
||||
const s_data = sessionObj.data; // 请求数据
|
||||
const s_time = sessionObj.time; // 请求时间
|
||||
const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交
|
||||
if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
|
||||
const message = '数据正在处理,请勿重复提交';
|
||||
console.warn(`[${s_url}]: ` + message)
|
||||
return Promise.reject(new Error(message))
|
||||
} else {
|
||||
cache.session.setJSON('sessionObj', requestObj)
|
||||
}
|
||||
}
|
||||
}
|
||||
return config
|
||||
}, error => {
|
||||
console.log(error)
|
||||
Promise.reject(error)
|
||||
})
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(res => {
|
||||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || 200;
|
||||
// 获取错误信息
|
||||
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
||||
// 二进制数据则直接返回
|
||||
if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
|
||||
return res.data
|
||||
}
|
||||
if (code === 401) {
|
||||
if (!isRelogin.show) {
|
||||
isRelogin.show = true;
|
||||
// 判断是否在登录页面
|
||||
const isLoginPage = window.location.pathname === '/login' || window.location.pathname === '/';
|
||||
|
||||
if (isLoginPage) {
|
||||
// 登录页面直接清理token,不弹窗
|
||||
useUserStore().logOut().then(() => {
|
||||
isRelogin.show = false;
|
||||
}).catch(() => {
|
||||
isRelogin.show = false;
|
||||
});
|
||||
return Promise.reject('登录已过期,请重新登录。')
|
||||
}
|
||||
|
||||
// 其他页面:显示提示后自动跳转
|
||||
ElMessage.warning('登录已过期,正在跳转到登录页面...');
|
||||
useUserStore().logOut().then(() => {
|
||||
isRelogin.show = false;
|
||||
// 跳转到登录页,保留当前路径作为redirect参数
|
||||
const currentPath = window.location.pathname;
|
||||
const redirectUrl = currentPath !== '/login' ? `/login?redirect=${encodeURIComponent(currentPath)}` : '/login';
|
||||
location.href = redirectUrl;
|
||||
}).catch(() => {
|
||||
isRelogin.show = false;
|
||||
location.href = '/login';
|
||||
});
|
||||
}
|
||||
return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
|
||||
} else if (code === 500) {
|
||||
// 检查是否需要跳过错误提示(静默请求:返回响应让.then()处理)
|
||||
if (res.config?.skipErrorMsg) {
|
||||
return Promise.resolve(res.data)
|
||||
}
|
||||
ElMessage({ message: msg, type: 'error' })
|
||||
return Promise.reject(new Error(msg))
|
||||
} else if (code === 601) {
|
||||
// 检查是否需要跳过错误提示(静默请求:返回响应让.then()处理)
|
||||
if (res.config?.skipErrorMsg) {
|
||||
return Promise.resolve(res.data)
|
||||
}
|
||||
ElMessage({ message: msg, type: 'warning' })
|
||||
return Promise.reject(new Error(msg))
|
||||
} else if (code !== 200) {
|
||||
// 检查是否需要跳过错误提示(静默请求:返回响应让.then()处理)
|
||||
if (res.config?.skipErrorMsg) {
|
||||
return Promise.resolve(res.data)
|
||||
}
|
||||
ElNotification.error({ title: msg })
|
||||
return Promise.reject('error')
|
||||
} else {
|
||||
return Promise.resolve(res.data)
|
||||
}
|
||||
},
|
||||
error => {
|
||||
console.log('err' + error)
|
||||
let { message } = error;
|
||||
if (message == "Network Error") {
|
||||
message = "后端接口连接异常";
|
||||
} else if (message.includes("timeout")) {
|
||||
message = "系统接口请求超时";
|
||||
} else if (message.includes("Request failed with status code")) {
|
||||
message = "系统接口" + message.substr(message.length - 3) + "异常";
|
||||
}
|
||||
// 检查是否需要跳过错误提示
|
||||
if (!error.config?.skipErrorMsg) {
|
||||
ElMessage({ message: message, type: 'error', duration: 5 * 1000 })
|
||||
}
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
// 通用下载方法
|
||||
export function download(url, params, filename, config) {
|
||||
downloadLoadingInstance = ElLoading.service({ text: "正在下载数据,请稍候", background: "rgba(0, 0, 0, 0.7)", })
|
||||
return service.post(url, params, {
|
||||
transformRequest: [(params) => { return tansParams(params) }],
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
responseType: 'blob',
|
||||
...config
|
||||
}).then(async (data) => {
|
||||
const isBlob = blobValidate(data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([data])
|
||||
saveAs(blob, filename)
|
||||
} else {
|
||||
const resText = await data.text();
|
||||
const rspObj = JSON.parse(resText);
|
||||
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
|
||||
ElMessage.error(errMsg);
|
||||
}
|
||||
downloadLoadingInstance.close();
|
||||
}).catch((r) => {
|
||||
console.error(r)
|
||||
ElMessage.error('下载文件出现错误,请联系管理员!')
|
||||
downloadLoadingInstance.close();
|
||||
})
|
||||
}
|
||||
|
||||
// 添加GET方式下载方法
|
||||
export function downloadGet(url, params, filename, config) {
|
||||
downloadLoadingInstance = ElLoading.service({ text: "正在下载数据,请稍候", background: "rgba(0, 0, 0, 0.7)", })
|
||||
return service.get(url, {
|
||||
params: params,
|
||||
responseType: 'blob',
|
||||
...config
|
||||
}).then(async (data) => {
|
||||
const isBlob = blobValidate(data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([data])
|
||||
saveAs(blob, filename)
|
||||
} else {
|
||||
const resText = await data.text();
|
||||
const rspObj = JSON.parse(resText);
|
||||
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
|
||||
ElMessage.error(errMsg);
|
||||
}
|
||||
downloadLoadingInstance.close();
|
||||
}).catch((r) => {
|
||||
console.error(r)
|
||||
ElMessage.error('下载文件出现错误,请联系管理员!')
|
||||
downloadLoadingInstance.close();
|
||||
})
|
||||
}
|
||||
|
||||
export default service
|
||||
58
openhis-ui-vue3/src/utils/scroll-to.js
Executable file
58
openhis-ui-vue3/src/utils/scroll-to.js
Executable file
@@ -0,0 +1,58 @@
|
||||
Math.easeInOutQuad = function(t, b, c, d) {
|
||||
t /= d / 2
|
||||
if (t < 1) {
|
||||
return c / 2 * t * t + b
|
||||
}
|
||||
t--
|
||||
return -c / 2 * (t * (t - 2) - 1) + b
|
||||
}
|
||||
|
||||
// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
|
||||
var requestAnimFrame = (function() {
|
||||
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) }
|
||||
})()
|
||||
|
||||
/**
|
||||
* Because it's so fucking difficult to detect the scrolling element, just move them all
|
||||
* @param {number} amount
|
||||
*/
|
||||
function move(amount) {
|
||||
document.documentElement.scrollTop = amount
|
||||
document.body.parentNode.scrollTop = amount
|
||||
document.body.scrollTop = amount
|
||||
}
|
||||
|
||||
function position() {
|
||||
return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} to
|
||||
* @param {number} duration
|
||||
* @param {Function} callback
|
||||
*/
|
||||
export function scrollTo(to, duration, callback) {
|
||||
const start = position()
|
||||
const change = to - start
|
||||
const increment = 20
|
||||
let currentTime = 0
|
||||
duration = (typeof (duration) === 'undefined') ? 500 : duration
|
||||
var animateScroll = function() {
|
||||
// increment the time
|
||||
currentTime += increment
|
||||
// find the value with the quadratic in-out easing function
|
||||
var val = Math.easeInOutQuad(currentTime, start, change, duration)
|
||||
// move the document.body
|
||||
move(val)
|
||||
// do the animation unless its over
|
||||
if (currentTime < duration) {
|
||||
requestAnimFrame(animateScroll)
|
||||
} else {
|
||||
if (callback && typeof (callback) === 'function') {
|
||||
// the animation is done so lets callback
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
animateScroll()
|
||||
}
|
||||
49
openhis-ui-vue3/src/utils/theme.js
Executable file
49
openhis-ui-vue3/src/utils/theme.js
Executable file
@@ -0,0 +1,49 @@
|
||||
// 处理主题样式
|
||||
export function handleThemeStyle(theme) {
|
||||
document.documentElement.style.setProperty('--el-color-primary', theme)
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(theme, i / 10)}`)
|
||||
}
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, `${getDarkColor(theme, i / 10)}`)
|
||||
}
|
||||
}
|
||||
|
||||
// hex颜色转rgb颜色
|
||||
export function hexToRgb(str) {
|
||||
str = str.replace('#', '')
|
||||
let hexs = str.match(/../g)
|
||||
for (let i = 0; i < 3; i++) {
|
||||
hexs[i] = parseInt(hexs[i], 16)
|
||||
}
|
||||
return hexs
|
||||
}
|
||||
|
||||
// rgb颜色转Hex颜色
|
||||
export function rgbToHex(r, g, b) {
|
||||
let hexs = [r.toString(16), g.toString(16), b.toString(16)]
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (hexs[i].length == 1) {
|
||||
hexs[i] = `0${hexs[i]}`
|
||||
}
|
||||
}
|
||||
return `#${hexs.join('')}`
|
||||
}
|
||||
|
||||
// 变浅颜色值
|
||||
export function getLightColor(color, level) {
|
||||
let rgb = hexToRgb(color)
|
||||
for (let i = 0; i < 3; i++) {
|
||||
rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i])
|
||||
}
|
||||
return rgbToHex(rgb[0], rgb[1], rgb[2])
|
||||
}
|
||||
|
||||
// 变深颜色值
|
||||
export function getDarkColor(color, level) {
|
||||
let rgb = hexToRgb(color)
|
||||
for (let i = 0; i < 3; i++) {
|
||||
rgb[i] = Math.floor(rgb[i] * (1 - level))
|
||||
}
|
||||
return rgbToHex(rgb[0], rgb[1], rgb[2])
|
||||
}
|
||||
127
openhis-ui-vue3/src/utils/validate.js
Executable file
127
openhis-ui-vue3/src/utils/validate.js
Executable file
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* 判断url是否是http或https
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isHttp(url) {
|
||||
return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断path是否为外链
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isExternal(path) {
|
||||
return /^(https?:|mailto:|tel:)/.test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validUsername(str) {
|
||||
const valid_map = ['admin', 'editor'];
|
||||
return valid_map.indexOf(str.trim()) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validURL(url) {
|
||||
const reg =
|
||||
/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
|
||||
return reg.test(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validLowerCase(str) {
|
||||
const reg = /^[a-z]+$/;
|
||||
return reg.test(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validUpperCase(str) {
|
||||
const reg = /^[A-Z]+$/;
|
||||
return reg.test(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validAlphabets(str) {
|
||||
const reg = /^[A-Za-z]+$/;
|
||||
return reg.test(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} email
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function validEmail(email) {
|
||||
const reg =
|
||||
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
return reg.test(email);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isString(str) {
|
||||
if (typeof str === 'string' || str instanceof String) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array} arg
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isArray(arg) {
|
||||
if (typeof Array.isArray === 'undefined') {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]';
|
||||
}
|
||||
return Array.isArray(arg);
|
||||
}
|
||||
// 手机号正则
|
||||
export function isValidCNPhoneNumber(phone) {
|
||||
const regex = /^1[3-9]\d{9}$/;
|
||||
return regex.test(phone);
|
||||
}
|
||||
// 身份证号正则
|
||||
export function isValidCNidCardNumber(idCard) {
|
||||
const regex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
|
||||
return regex.test(idCard);
|
||||
}
|
||||
// 根据身份证号获取性别和年龄
|
||||
export function getGenderAndAge(idCard) {
|
||||
// 确保身份证号码是18位
|
||||
if (idCard.length !== 18) {
|
||||
return { error: '身份证号码必须是18位' };
|
||||
}
|
||||
// 提取出生年月日
|
||||
const birthDate = idCard.substr(6, 8); // YYYYMMDD
|
||||
const year = birthDate.substr(0, 4);
|
||||
const month = birthDate.substr(4, 2);
|
||||
const day = birthDate.substr(6, 2);
|
||||
const dateOfBirth = new Date(`${year}-${month}-${day}`);
|
||||
// 计算年龄
|
||||
let age = new Date().getFullYear() - dateOfBirth.getFullYear();
|
||||
const m = new Date().getMonth() - dateOfBirth.getMonth();
|
||||
if (m < 0 || (m === 0 && new Date().getDate() < dateOfBirth.getDate())) {
|
||||
age--;
|
||||
}
|
||||
// 提取性别(身份证第17位:奇数=男, 偶数=女)对应数据库字典 1=男 2=女
|
||||
const gender = idCard.charAt(16) % 2 === 0 ? 2 : 1;
|
||||
return { age, gender };
|
||||
}
|
||||
Reference in New Issue
Block a user