211 lines
5.2 KiB
JavaScript
211 lines
5.2 KiB
JavaScript
/**
|
|
* 书籍服务 - 处理图书上传、记录查询等
|
|
* @module services/book
|
|
*/
|
|
import request from '@/utils/request'
|
|
import config from '@/utils/config'
|
|
|
|
/**
|
|
* 书籍相关 API
|
|
*/
|
|
export const bookApi = {
|
|
/**
|
|
* 获取用户上书记录
|
|
* @param {String} phoneNumber - 用户手机号
|
|
* @param {Number} pageNum - 页码
|
|
* @param {Number} pageSize - 每页数量
|
|
* @param {String} date - 日期筛选(可选)
|
|
* @returns {Promise<Object>} 上书记录列表
|
|
*/
|
|
getBookRecords(phoneNumber, pageNum = 1, pageSize = 10, date = '') {
|
|
let url = `${config.book.records}/${phoneNumber}?pageNum=${pageNum}&pageSize=${pageSize}`
|
|
if (date) {
|
|
url += `&date=${date}`
|
|
}
|
|
return request({
|
|
url,
|
|
method: 'GET',
|
|
loading: true
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 上传书籍图片
|
|
* @param {Object} data - 上传参数
|
|
* @param {String} data.imageUrl - 图片 URL
|
|
* @param {String} data.isbn - ISBN 号(可选)
|
|
* @param {String} data.warehouseId - 仓库 ID
|
|
* @returns {Promise<Object>} 上传结果
|
|
*/
|
|
uploadBookImage(data) {
|
|
return request({
|
|
url: '/zhishu/shopGoods/uploadImage',
|
|
method: 'POST',
|
|
data
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 根据 ISBN 获取图书信息
|
|
* @param {String} isbn - ISBN 号
|
|
* @returns {Promise<Object>} 图书信息
|
|
*/
|
|
getBookByIsbn(isbn) {
|
|
return request({
|
|
url: `/zhishu/book/isbn/${isbn}`,
|
|
method: 'GET',
|
|
loading: true
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 根据书名搜索图书
|
|
* @param {String} title - 书名关键词
|
|
* @param {Number} pageNum - 页码
|
|
* @param {Number} pageSize - 每页数量
|
|
* @returns {Promise<Object>} 搜索结果
|
|
*/
|
|
searchByTitle(title, pageNum = 1, pageSize = 10) {
|
|
return request({
|
|
url: '/zhishu/book/search',
|
|
method: 'GET',
|
|
data: { title, pageNum, pageSize },
|
|
loading: true
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 提交图书信息
|
|
* @param {Object} data - 图书信息
|
|
* @returns {Promise<Object>} 提交结果
|
|
*/
|
|
submitBook(data) {
|
|
return request({
|
|
url: '/zhishu/shopGoods/add',
|
|
method: 'POST',
|
|
data,
|
|
loading: true
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 批量上传图书
|
|
* @param {Array} books - 图书列表
|
|
* @returns {Promise<Object>} 批量上传结果
|
|
*/
|
|
batchUpload(books) {
|
|
return request({
|
|
url: '/zhishu/shopGoods/batchAdd',
|
|
method: 'POST',
|
|
data: { books },
|
|
loading: true
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 条形码识别 API
|
|
*/
|
|
export const barcodeApi = {
|
|
/**
|
|
* 识别条形码
|
|
* @param {Object} data - 识别参数
|
|
* @param {String} data.imageUrl - 条形码图片 URL
|
|
* @returns {Promise<Object>} 识别结果,包含 ISBN
|
|
*/
|
|
recognizeBarcode(data) {
|
|
return request({
|
|
url: '/zhishu/barcode/recognize',
|
|
method: 'POST',
|
|
data,
|
|
loading: true
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 从本地图片识别条形码
|
|
* @param {String} filePath - 本地图片路径
|
|
* @returns {Promise<Object>} 识别结果
|
|
*/
|
|
async recognizeFromLocal(filePath) {
|
|
// 先上传图片
|
|
const uploadRes = await new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: '/zhishu/file/upload',
|
|
filePath,
|
|
name: 'file',
|
|
success: (res) => resolve(JSON.parse(res.data)),
|
|
fail: reject
|
|
})
|
|
})
|
|
|
|
// 再识别条形码
|
|
if (uploadRes.data && uploadRes.data.url) {
|
|
return this.recognizeBarcode({ imageUrl: uploadRes.data.url })
|
|
}
|
|
throw new Error('图片上传失败')
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据关键词获取作者和出版社数据
|
|
* @param {string} keyword - 搜索关键词
|
|
* @param {string} cookies - 用户cookies
|
|
* @returns {Promise<{authors: string[], publishers: string[]}>} 返回作者和出版社数据
|
|
*/
|
|
export const getAuthorAndPublisher = async (keyword, cookies) => {
|
|
try {
|
|
const response = await new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: 'https://api.buzhiyushu.cn/zhishu/shopGoods/getAuthorAndPublisher',
|
|
method: 'GET',
|
|
data: {
|
|
keyword: keyword,
|
|
cookies: cookies
|
|
},
|
|
success: (res) => {
|
|
console.log('API原始响应:', JSON.stringify(res, null, 2))
|
|
resolve(res)
|
|
},
|
|
fail: (err) => {
|
|
console.error('API请求失败:', err)
|
|
reject(new Error(err.errMsg || '网络请求失败'))
|
|
}
|
|
})
|
|
})
|
|
|
|
if (!response.data || response.data.code !== 200) {
|
|
console.log('API返回数据无效或请求失败')
|
|
return { authors: [], publishers: [] }
|
|
}
|
|
|
|
const data = response.data.data || []
|
|
|
|
const authors = new Set()
|
|
const publishers = new Set()
|
|
|
|
data.forEach(item => {
|
|
if (item.author) {
|
|
const author = item.author.trim()
|
|
if (author) authors.add(author)
|
|
}
|
|
if (item.press) {
|
|
const press = item.press.trim()
|
|
if (press) publishers.add(press)
|
|
}
|
|
})
|
|
|
|
return {
|
|
authors: Array.from(authors).filter(Boolean).slice(0, 10),
|
|
publishers: Array.from(publishers).filter(Boolean).slice(0, 10)
|
|
}
|
|
} catch (error) {
|
|
console.error('获取作者和出版社数据出错:', error)
|
|
return { authors: [], publishers: [] }
|
|
}
|
|
}
|
|
|
|
export default {
|
|
book: bookApi,
|
|
barcode: barcodeApi
|
|
} |