WMS_MAIN/app/extend/application.js
2026-04-09 01:36:49 +08:00

90 lines
2.9 KiB
JavaScript

const soap = require('soap');
const businessDict = require('../dict/businessDict');
module.exports = {
get businessDict() {
return businessDict;
},
getBusinessTypeName(code) {
const dict = this.businessDict.BUSINESS_TYPE || {};
for (const key in dict) {
if (dict[key].code === code) {
return dict[key].name;
}
}
return code;
},
getOperationCodeName(code) {
return this.businessDict.OPERATION_CODE?.[code] || code;
},
getPlatformName(code) {
return this.businessDict.PLATFORM_CODE?.[code] || code;
},
getExternalLogTypeName(type) {
return this.businessDict.EXTERNAL_LOG_TYPE?.[type] || type;
},
formatDate(date, formatStr = 'YYYY-MM-DD HH:mm:ss') {
const targetDate = date instanceof Date ? date : new Date(date);
const year = targetDate.getFullYear();
const month = String(targetDate.getMonth() + 1).padStart(2, '0');
const day = String(targetDate.getDate()).padStart(2, '0');
const hours = String(targetDate.getHours()).padStart(2, '0');
const minutes = String(targetDate.getMinutes()).padStart(2, '0');
const seconds = String(targetDate.getSeconds()).padStart(2, '0');
return formatStr
.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds);
},
async wsdl(methodName, args) {
try {
const wsdlUrl = 'http://ye4s.com/webservice/ws?wsdl';
const obj = {};
args.forEach((value, index) => {
obj['arg' + index] = value;
});
const client = await soap.createClientAsync(wsdlUrl);
const result = await client[methodName + 'Async'](obj);
return result[0];
} catch (error) {
console.error('Error calling SOAP service:', error);
throw error;
}
},
async setCacheAsync(cacheKey, data, expireSeconds = 300) {
const cacheRedis = this.redis && this.redis.get ? this.redis.get('cache') : null;
if (!cacheRedis) {
this.logger.warn('[RedisNotAvailable] Cannot set cache, Redis client is not available');
return;
}
try {
await cacheRedis.setex(cacheKey, expireSeconds, JSON.stringify(data));
this.logger.debug('[CacheSet] Successfully set cache', cacheKey);
} catch (error) {
this.logger.error('[CacheSetError] Failed to set cache', error.message, cacheKey);
}
},
async getCacheAsync(cacheKey) {
const cacheRedis = this.redis && this.redis.get ? this.redis.get('cache') : null;
if (!cacheRedis) {
this.logger.warn('[RedisNotAvailable] Cannot get cache, Redis client is not available');
return null;
}
try {
const data = await cacheRedis.get(cacheKey);
if (data) {
return JSON.parse(data);
}
return null;
} catch (error) {
this.logger.error('[CacheGetError] Failed to get cache', error.message, cacheKey);
return null;
}
},
};