modify:新增小程序

This commit is contained in:
ECRZ
2026-01-06 18:00:43 +08:00
parent 498fa0e915
commit da4a055c1c
47 changed files with 7321 additions and 61 deletions

236
Sale/utils/request.js Normal file
View File

@@ -0,0 +1,236 @@
/**
* API 请求封装工具
* 统一处理 wx.request支持 baseURL、错误处理、加载状态
*/
// API 基础地址配置
const API_BASE_URL = 'http://makepower.top:9333'
/**
* 发起 HTTP 请求
* @param {string} url - 请求路径
* @param {object} options - 请求配置
* @param {string} options.method - 请求方法 (GET/POST/PUT/DELETE)
* @param {object} options.data - 请求数据
* @param {boolean} options.showLoading - 是否显示加载提示
* @param {string} options.loadingText - 加载提示文字
* @returns {Promise} 返回 Promise 对象
*/
function request(url, options = {}) {
const {
method = 'GET',
data = {},
showLoading = true,
loadingText = '加载中...'
} = options
// 显示加载提示
if (showLoading) {
wx.showLoading({
title: loadingText,
mask: true
})
}
return new Promise((resolve, reject) => {
wx.request({
url: `${API_BASE_URL}${url}`,
method: method.toUpperCase(),
data: method.toUpperCase() === 'GET' ? data : JSON.stringify(data),
header: {
'content-type': 'application/json'
},
success: (res) => {
// 隐藏加载提示
if (showLoading) {
wx.hideLoading()
}
// 检查业务状态码
if (res.statusCode === 200) {
if (res.data.success !== false) {
resolve(res.data)
} else {
// 业务错误
showError(res.data.message || '请求失败')
reject(res.data)
}
} else {
// HTTP 错误
showError(`网络错误: ${res.statusCode}`)
reject({
success: false,
message: `网络错误: ${res.statusCode}`,
statusCode: res.statusCode
})
}
},
fail: (err) => {
// 隐藏加载提示
if (showLoading) {
wx.hideLoading()
}
// 网络请求失败
showError('网络连接失败,请检查网络设置')
reject({
success: false,
message: '网络连接失败',
error: err
})
}
})
})
}
/**
* 显示错误提示
* @param {string} message - 错误信息
*/
function showError(message) {
wx.showToast({
title: message,
icon: 'none',
duration: 2000
})
}
/**
* 显示成功提示
* @param {string} message - 成功信息
*/
function showSuccess(message) {
wx.showToast({
title: message,
icon: 'success',
duration: 2000
})
}
/**
* GET 请求
*/
function get(url, data = {}, options = {}) {
return request(url, {
...options,
method: 'GET',
data
})
}
/**
* POST 请求
*/
function post(url, data = {}, options = {}) {
return request(url, {
...options,
method: 'POST',
data
})
}
/**
* PUT 请求
*/
function put(url, data = {}, options = {}) {
return request(url, {
...options,
method: 'PUT',
data
})
}
/**
* DELETE 请求
*/
function del(url, data = {}, options = {}) {
return request(url, {
...options,
method: 'DELETE',
data
})
}
// ========== API 接口定义 ==========
/**
* 健康检查
*/
function checkHealth() {
return get('/api/health', {}, { showLoading: false })
}
/**
* 按地区查询价格
* @param {string} region - 地区名称
* @param {string} date - 价格日期 (可选)
*/
function getPricesByRegion(region, date = '') {
const params = { region }
if (date) params.date = date
return get('/api/prices/region', params)
}
/**
* 搜索价格数据
* @param {object} params - 搜索参数
* @param {string} params.material - 材质
* @param {string} params.specification - 规格型号
* @param {string} params.startDate - 开始日期
* @param {string} params.endDate - 结束日期
* @param {string} params.region - 地区
* @param {number} params.page - 页码
* @param {number} params.pageSize - 每页数量
*/
function searchPrices(params = {}) {
return get('/api/prices/search', params)
}
/**
* 获取价格统计
* @param {object} params - 统计参数
* @param {string} params.region - 地区
* @param {string} params.material - 材质
* @param {number} params.days - 统计天数
* @param {string} params.startDate - 开始日期
* @param {string} params.endDate - 结束日期
*/
function getPriceStats(params = {}) {
return get('/api/prices/stats', params)
}
/**
* 获取价格趋势
* @param {object} params - 查询参数
* @param {string} params.region - 地区
* @param {string} params.material - 材质
* @param {number} params.days - 统计天数 (默认30)
*/
function getPriceTrend(params = {}) {
return get('/api/prices/trend', params)
}
/**
* 导入价格数据
* @param {array} prices - 价格数据数组
*/
function importPrices(prices) {
return post('/api/prices/import', { prices })
}
module.exports = {
request,
get,
post,
put,
del,
showSuccess,
showError,
// API 接口
checkHealth,
getPricesByRegion,
searchPrices,
getPriceStats,
getPriceTrend,
importPrices
}