// app.js const path = require('path'); // 直接导出函数,而不是类 module.exports = app => { // 设置全局应用根目录 global.APP_ROOT = path.resolve(__dirname, './'); app.appRoot = APP_ROOT; app.logger.info('✅ Application 扩展能力已加载'); // 初始化工厂 app.beforeStart(async () => { // skip factory init when model layer is absent (e.g. during tests) if (!app.model) { app.logger.info('⚠️ skipping factory init: app.model not available'); return; } try { const OrderDeliveryFactory = require('./app/model/order_delivery_factory'); const OrderItemsFactory = require('./app/model/order_items_factory'); app.OrderDeliveryFactory = OrderDeliveryFactory(app); app.model.OrderDeliveryFactory = app.OrderDeliveryFactory; app.OrderItemsFactory = OrderItemsFactory(app); app.model.OrderItemsFactory = app.OrderItemsFactory; app.logger.info('✅ 工厂类初始化完成'); } catch (error) { app.logger.error('❌ 工厂类初始化失败:', error); } // 验证缓存方法 if (typeof app.setCacheAsync === 'function') { app.setCacheAsync('system', 'eggjs'); const system = await app.getCacheAsync('system'); app.logger.info('✅ 缓存方法验证通过' + system); } app.logger.info('✅ l类型' + typeof app.Message); }); // 扩展 Date 原型 Date.prototype.format = function(formatStr = 'YYYY-MM-DD HH:mm:ss') { const year = this.getFullYear(); const month = String(this.getMonth() + 1).padStart(2, '0'); const day = String(this.getDate()).padStart(2, '0'); const hours = String(this.getHours()).padStart(2, '0'); const minutes = String(this.getMinutes()).padStart(2, '0'); const seconds = String(this.getSeconds()).padStart(2, '0'); return formatStr .replace('YYYY', year) .replace('MM', month) .replace('DD', day) .replace('HH', hours) .replace('mm', minutes) .replace('ss', seconds); }; };