import axios from 'axios' /** * 测试连接核价器 * @param {string} ip - IP 地址 * @param {string} port - 端口 * @returns {Promise} */ export const testConnection = (ip, port) => { const params = new URLSearchParams() params.append('isbn', '0') params.append('out_id', '0') params.append('quality', '0') params.append('query_index', '1') params.append('user_id', '0') return axios.post(`http://${ip}:${port}/api/goods/query`, params.toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, timeout: 10000 }).then(res => res.data) } /** * 保存新价格到核价器 * @param {string} ip - IP 地址 * @param {string} port - 端口 * @returns {Promise} */ export const saveNewPrice = (ip, port, newPrice, placeholderDownPrice, minShippingFee, minPrice, verifyIndex) => { const formData = new URLSearchParams() formData.append('new_price', newPrice) formData.append('placeholder_down_price', placeholderDownPrice) formData.append('min_shipping_fee', minShippingFee) formData.append('min_price', minPrice) formData.append('query_index', verifyIndex) console.log(formData.toString()) return axios.post(`http://${ip}:${port}/api/config/price/set`, formData.toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, timeout: 10000 }).then(res => res.data) } /** * 孔夫子旧书网登录(直接请求核价器) * @param {string} username - 孔网用户名 * @param {string} password - 孔网密码 * @param {string} ip - 核价器 IP * @param {string} port - 核价器端口 * @returns {Promise<{code: number, data: {token: string, nickname: string}, message: string}>} */ export const kongfzLogin = (username, password, ip, port) => { const formData = new FormData() formData.append('username', username) formData.append('password', password) return axios.post(`http://${ip}:${port}/api/kfz/login`, formData, { timeout: 15000 }).then(res => res.data) } /** * 批量提交 Token 到核价器 * @param {Array<{username: string, token: string}>} tokens - 账号 Token 列表 * @param {string} ip - 核价器 IP * @param {string} port - 核价器端口 * @returns {Promise} */ export const batchAddTokens = (tokens, ip, port) => { return axios.post(`http://${ip}:${port}/api/token/add`, tokens, { headers: { 'Content-Type': 'application/json' }, timeout: 10000 }).then(res => res.data) } /** * 从核价器获取已保存的 Token 列表 * @param {string} ip - 核价器 IP * @param {string} port - 核价器端口 * @returns {Promise<{code: number, data: Array<{Username: string, Token: string, ID: number, IsEnable: boolean}>, message: string}>} */ export const fetchTokenList = (ip, port) => { return axios.post(`http://${ip}:${port}/api/token/list`, {}, { headers: { 'Content-Type': 'application/json' }, timeout: 10000 }).then(res => res.data) } /** * 从核价器获取config配置 * @param {string} ip - 核价器 IP * @param {string} port - 核价器端口 */ export const fetchConfig = (ip, port) => { return axios.post(`http://${ip}:${port}/api/config/price/get`, {}, { headers: { 'Content-Type': 'application/json' }, timeout: 10000 }).then(res => res.data) }