Files
autoAiWorkSys/app/api/config.js
张成 e17d5610f5 1
2025-12-22 16:26:59 +08:00

103 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 系统配置 API 服务
* 封装配置相关的API调用
*/
import apiClient from '../utils/api.js';
// API 基础 URL用于拼接图片路径去掉 /api 后缀)
const getBaseURL = () => {
const apiUrl = 'http://localhost:9097/api';
// 如果包含 /api去掉它
return apiUrl.replace(/\/api$/, '');
};
class ConfigAPI {
/**
* 获取配置
* @param {String|Array} configKeys - 配置键(单个字符串或数组)
* @returns {Promise}
*/
async getConfig(configKeys) {
try {
let params = {};
// 支持单个字符串或数组
if (Array.isArray(configKeys)) {
params.configKeys = configKeys.join(',');
} else if (typeof configKeys === 'string') {
params.configKey = configKeys;
} else {
throw new Error('配置键格式错误');
}
const result = await apiClient.get('/config/get', params);
return result;
} catch (error) {
console.error('获取配置失败:', error);
throw error;
}
}
/**
* 获取微信相关配置
* @returns {Promise<Object>} { wechatNumber, wechatQRCode }
*/
async getWechatConfig() {
try {
const result = await this.getConfig(['wx_num', 'wx_img']);
if (result && result.code === 0) {
let qrcodeUrl = result.data.wx_img || '';
// 如果二维码是相对路径转换为完整URL
if (qrcodeUrl && !qrcodeUrl.startsWith('http://') && !qrcodeUrl.startsWith('https://') && !qrcodeUrl.startsWith('data:')) {
const baseURL = getBaseURL();
// 如果以 / 开头拼接服务器基础URL
if (qrcodeUrl.startsWith('/')) {
qrcodeUrl = baseURL + qrcodeUrl;
} else {
// 如果不是以 / 开头,可能是文件路径,需要拼接
qrcodeUrl = baseURL + '/' + qrcodeUrl;
}
}
return {
wechatNumber: result.data.wx_num || '',
wechatQRCode: qrcodeUrl
};
}
return {
wechatNumber: '',
wechatQRCode: ''
};
} catch (error) {
console.error('获取微信配置失败:', error);
return {
wechatNumber: '',
wechatQRCode: ''
};
}
}
/**
* 获取价格套餐列表
* @returns {Promise<Array>} 价格套餐列表
*/
async getPricingPlans() {
try {
const result = await apiClient.get('/config/pricing-plans');
if (result && result.code === 0) {
return result.data || [];
}
return [];
} catch (error) {
console.error('获取价格套餐失败:', error);
return [];
}
}
}
export default new ConfigAPI();