新增searchFacet API获取全新/古旧真实统计
This commit is contained in:
parent
4d557b4be0
commit
e062725f2d
@ -751,7 +751,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { getWarehouseList, getLocationList, searchBookByIsbn } from '@/utils/api.js'
|
import { getWarehouseList, getLocationList, searchBookByIsbn } from '@/utils/api.js'
|
||||||
import { login as kongfzLogin, searchProducts } from '@/utils/kongfz.js'
|
import { login as kongfzLogin, searchProducts, searchFacet } from '@/utils/kongfz.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
@ -993,11 +993,15 @@ export default {
|
|||||||
|
|
||||||
// 2. 搜索孔夫子 - 获取在售商品信息
|
// 2. 搜索孔夫子 - 获取在售商品信息
|
||||||
const phpsessid = this.kongfzToken || uni.getStorageSync('kongfz_phpsessid') || ''
|
const phpsessid = this.kongfzToken || uni.getStorageSync('kongfz_phpsessid') || ''
|
||||||
searchProducts(this.isbn, { phpsessid }).then(data => {
|
// 并行请求:商品列表 + 品相统计
|
||||||
|
Promise.all([
|
||||||
|
searchProducts(this.isbn, { phpsessid }),
|
||||||
|
searchFacet(this.isbn, { phpsessid })
|
||||||
|
]).then(([productsData, facetData]) => {
|
||||||
this.isLoading = false
|
this.isLoading = false
|
||||||
if (data && data.total > 0) {
|
if (productsData && productsData.total > 0) {
|
||||||
// 在售商品列表(最多12条)
|
// 在售商品列表(最多12条)
|
||||||
const list = (data.list || []).slice(0, 12)
|
const list = (productsData.list || []).slice(0, 12)
|
||||||
this.productList = list.map(item => ({
|
this.productList = list.map(item => ({
|
||||||
image: item.imgBigUrl || '',
|
image: item.imgBigUrl || '',
|
||||||
totalPrice: parseFloat((item.priceText || '0').replace(/[^\d.]/g, '')),
|
totalPrice: parseFloat((item.priceText || '0').replace(/[^\d.]/g, '')),
|
||||||
@ -1010,26 +1014,17 @@ export default {
|
|||||||
pubDate: item.pubDateText || '',
|
pubDate: item.pubDateText || '',
|
||||||
bookId: item.id || ''
|
bookId: item.id || ''
|
||||||
}))
|
}))
|
||||||
// 计算市场竞争数据(在售/旧/新/已售)
|
|
||||||
const oldCount = list.filter(item => {
|
|
||||||
const qt = (item.qualityText || '').toLowerCase()
|
|
||||||
return !qt.includes('全新') && !qt.includes('新')
|
|
||||||
}).length
|
|
||||||
const newCount = list.filter(item => {
|
|
||||||
const qt = (item.qualityText || '').toLowerCase()
|
|
||||||
return qt.includes('全新') || qt.includes('新')
|
|
||||||
}).length
|
|
||||||
this.marketData = {
|
|
||||||
onSale: data.total || 0,
|
|
||||||
old: oldCount,
|
|
||||||
new: newCount,
|
|
||||||
sold: 0
|
|
||||||
}
|
}
|
||||||
} else {
|
// 市场统计:使用facet接口的真实数据
|
||||||
this.marketData = { onSale: 0, old: 0, new: 0, sold: 0 }
|
this.marketData = {
|
||||||
|
onSale: productsData ? productsData.total : 0,
|
||||||
|
old: facetData ? facetData.oldCount : 0,
|
||||||
|
new: facetData ? facetData.newCount : 0,
|
||||||
|
sold: 0
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
this.isLoading = false
|
this.isLoading = false
|
||||||
|
this.marketData = { onSale: 0, old: 0, new: 0, sold: 0 }
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,6 @@ export function login(username, password) {
|
|||||||
|
|
||||||
// 提取PHPSESSID
|
// 提取PHPSESSID
|
||||||
let phpsessid = ''
|
let phpsessid = ''
|
||||||
// 尝试从 Set-Cookie 中提取
|
|
||||||
const setCookie = res.header['Set-Cookie'] || res.header['set-cookie']
|
const setCookie = res.header['Set-Cookie'] || res.header['set-cookie']
|
||||||
if (setCookie) {
|
if (setCookie) {
|
||||||
const match = setCookie.match(/PHPSESSID=([^;]+)/)
|
const match = setCookie.match(/PHPSESSID=([^;]+)/)
|
||||||
@ -39,7 +38,6 @@ export function login(username, password) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断登录是否成功
|
|
||||||
const responseText = typeof res.data === 'string' ? res.data : JSON.stringify(res.data)
|
const responseText = typeof res.data === 'string' ? res.data : JSON.stringify(res.data)
|
||||||
const isSuccess = responseText.includes('login.kongfz.cn/Pc/Session/rsync') ||
|
const isSuccess = responseText.includes('login.kongfz.cn/Pc/Session/rsync') ||
|
||||||
responseText.includes('window.location.href') ||
|
responseText.includes('window.location.href') ||
|
||||||
@ -53,7 +51,6 @@ export function login(username, password) {
|
|||||||
message: '登录成功'
|
message: '登录成功'
|
||||||
})
|
})
|
||||||
} else if (res.statusCode === 200 && responseText.includes('errCode')) {
|
} else if (res.statusCode === 200 && responseText.includes('errCode')) {
|
||||||
// 尝试解析错误信息
|
|
||||||
try {
|
try {
|
||||||
const json = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
const json = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||||
if (json.errCode === 1001) {
|
if (json.errCode === 1001) {
|
||||||
@ -80,12 +77,57 @@ export function login(username, password) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜索孔夫子商品品相统计(全新/古旧等)
|
||||||
|
* @param {string} keyword ISBN或书名
|
||||||
|
* @param {object} options {phpsessid, userArea}
|
||||||
|
* @returns {Promise<{newCount: number, oldCount: number, totalFound: number}>}
|
||||||
|
*/
|
||||||
|
export function searchFacet(keyword, options = {}) {
|
||||||
|
const { phpsessid = '', userArea = '1006000000' } = options
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
uni.request({
|
||||||
|
url: 'https://search.kongfz.com/pc-gw/search-web/client/pc/product/keyword/normal/facet',
|
||||||
|
method: 'GET',
|
||||||
|
data: {
|
||||||
|
dataType: 0,
|
||||||
|
keyword: keyword,
|
||||||
|
page: 1,
|
||||||
|
userArea: userArea
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
'Cookie': phpsessid ? `PHPSESSID=${phpsessid}` : ''
|
||||||
|
},
|
||||||
|
success: (res) => {
|
||||||
|
console.log('孔夫子统计响应:', res.statusCode, res.data)
|
||||||
|
if (res.statusCode === 200 && res.data && res.data.status === 1 && res.data.data) {
|
||||||
|
const qualities = res.data.data.qualities || []
|
||||||
|
let newCount = 0
|
||||||
|
let oldCount = 0
|
||||||
|
qualities.forEach(q => {
|
||||||
|
if (q.showName === '全新') {
|
||||||
|
newCount = parseInt(q.showValue || 0, 10)
|
||||||
|
}
|
||||||
|
if (q.showName === '古 | 旧 | 二手') {
|
||||||
|
oldCount = parseInt(q.showValue || 0, 10)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const totalFound = parseInt(res.data.data.totalFoundText || res.data.data.matchInfo?.totalFound || 0, 10)
|
||||||
|
resolve({ newCount, oldCount, totalFound })
|
||||||
|
} else {
|
||||||
|
resolve({ newCount: 0, oldCount: 0, totalFound: 0 })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: (err) => {
|
||||||
|
console.error('孔夫子统计失败:', err)
|
||||||
|
resolve({ newCount: 0, oldCount: 0, totalFound: 0 })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取商品列表(分页)
|
* 获取商品列表(分页)
|
||||||
* @param {string} token PHPSESSID
|
|
||||||
* @param {object} params 筛选参数
|
|
||||||
* @param {function} onProgress 进度回调
|
|
||||||
* @returns {Promise<Array>}
|
|
||||||
*/
|
*/
|
||||||
export function fetchItems(token, params = {}, onProgress) {
|
export function fetchItems(token, params = {}, onProgress) {
|
||||||
const items = []
|
const items = []
|
||||||
@ -140,11 +182,6 @@ export function fetchItems(token, params = {}, onProgress) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
|
||||||
login,
|
|
||||||
fetchItems
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 搜索孔夫子在售商品(需要登录Cookie)
|
* 搜索孔夫子在售商品(需要登录Cookie)
|
||||||
* @param {string} keyword ISBN或书名
|
* @param {string} keyword ISBN或书名
|
||||||
@ -190,3 +227,10 @@ export function searchProducts(keyword, options = {}) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
login,
|
||||||
|
searchFacet,
|
||||||
|
fetchItems,
|
||||||
|
searchProducts
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user