Files
2026-07-27 14:07:26 +08:00

328 lines
12 KiB
JavaScript

"use strict";
const common_vendor = require("../common/vendor.js");
const BASE_URL = "http://192.168.123.111:3000/api";
const mockPrices = [
{ category: "001", name: "纸类", unit: "kg", price: 1.2 },
{ category: "002", name: "金属", unit: "kg", price: 2.5 },
{ category: "003", name: "易拉罐", unit: "piece", price: 0.1 },
{ category: "004", name: "塑料", unit: "kg", price: 3 },
{ category: "005", name: "织物", unit: "kg", price: 0.8 },
{ category: "006", name: "塑料瓶", unit: "piece", price: 0.05 }
];
const mockUsers = [
{ id: "1", name: "管理员", phone: "13800138000", role: "admin", createTime: "2024-01-01" },
{ id: "2", name: "回收员小王", phone: "18698102228", role: "collector", createTime: "2024-01-15" }
];
const request = async (url, options = {}) => {
return new Promise((resolve, reject) => {
common_vendor.index.request({
url: BASE_URL + url,
method: options.method || "GET",
data: options.data,
header: {
"Content-Type": "application/json",
...options.header
},
success: (res) => {
if (res.statusCode === 200) {
resolve(res.data);
} else {
reject(new Error("请求失败"));
}
},
fail: (err) => {
console.error("API request failed, using mock data:", err);
reject(err);
}
});
});
};
const priceApi = {
async getPrices() {
try {
return await request("/prices");
} catch {
return {
date: (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
prices: mockPrices
};
}
},
async addPrice(item) {
try {
const result = await request("/prices", {
method: "POST",
data: item
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_prices") || { date: (/* @__PURE__ */ new Date()).toISOString().split("T")[0], prices: mockPrices };
cached.prices.push(item);
common_vendor.index.setStorageSync("api_prices", cached);
return true;
}
},
async updatePrice(item, originalCategory) {
try {
const category = originalCategory || item.category;
const result = await request(`/prices/${category}`, {
method: "PUT",
data: item
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_prices") || { date: (/* @__PURE__ */ new Date()).toISOString().split("T")[0], prices: mockPrices };
const searchCategory = originalCategory || item.category;
const index = cached.prices.findIndex((p) => p.category === searchCategory);
if (index > -1) {
cached.prices[index] = item;
common_vendor.index.setStorageSync("api_prices", cached);
}
return true;
}
},
async deletePrice(category) {
try {
const result = await request(`/prices/${category}`, {
method: "DELETE"
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_prices") || { date: (/* @__PURE__ */ new Date()).toISOString().split("T")[0], prices: mockPrices };
cached.prices = cached.prices.filter((p) => p.category !== category);
common_vendor.index.setStorageSync("api_prices", cached);
return true;
}
}
};
const orderApi = {
async getOrders() {
try {
const result = await request("/orders");
return result.data || [];
} catch {
const cached = common_vendor.index.getStorageSync("api_orders");
return cached || [];
}
},
async addOrder(order) {
try {
const result = await request("/orders", {
method: "POST",
data: order
});
return result.order || null;
} catch {
const cached = common_vendor.index.getStorageSync("api_orders") || [];
const newOrder = { ...order, id: Date.now().toString(), createTime: (/* @__PURE__ */ new Date()).toISOString() };
cached.unshift(newOrder);
common_vendor.index.setStorageSync("api_orders", cached);
return newOrder;
}
},
async updateOrder(order) {
try {
const result = await request(`/orders/${order.id}`, {
method: "PUT",
data: order
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_orders") || [];
const index = cached.findIndex((o) => o.id === order.id);
if (index > -1) {
cached[index] = order;
common_vendor.index.setStorageSync("api_orders", cached);
}
return true;
}
},
async deleteOrder(id) {
try {
const result = await request(`/orders/${id}`, {
method: "DELETE"
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_orders") || [];
const filtered = cached.filter((o) => o.id !== id);
common_vendor.index.setStorageSync("api_orders", filtered);
return true;
}
}
};
const userApi = {
async getUsers() {
try {
const result = await request("/users");
return result.data || [];
} catch {
const cached = common_vendor.index.getStorageSync("api_users");
return cached || mockUsers;
}
},
async addUser(user) {
try {
const result = await request("/users", {
method: "POST",
data: user
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_users") || mockUsers;
const newUser = {
...user,
id: Date.now().toString(),
createTime: (/* @__PURE__ */ new Date()).toISOString().split("T")[0]
};
cached.push(newUser);
common_vendor.index.setStorageSync("api_users", cached);
return true;
}
},
async updateUser(user) {
try {
const result = await request(`/users/${user.id}`, {
method: "PUT",
data: user
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_users") || mockUsers;
const index = cached.findIndex((u) => u.id === user.id);
if (index > -1) {
cached[index] = user;
common_vendor.index.setStorageSync("api_users", cached);
}
return true;
}
},
async deleteUser(id) {
try {
const result = await request(`/users/${id}`, {
method: "DELETE"
});
return result.success;
} catch {
const cached = common_vendor.index.getStorageSync("api_users") || mockUsers;
const filtered = cached.filter((u) => u.id !== id);
common_vendor.index.setStorageSync("api_users", filtered);
return true;
}
}
};
const regionApi = {
async getCities() {
var _a;
try {
const result = await request("/regions/cities");
return ((_a = result.data) == null ? void 0 : _a.map((c) => c.name)) || [];
} catch {
return ["天津市", "北京市", "上海市", "广州市"];
}
},
async getDistricts(city) {
var _a;
try {
const result = await request("/regions/districts");
return ((_a = result.data) == null ? void 0 : _a.filter((d) => d.cityName === city).map((d) => d.name)) || [];
} catch {
const districtMap = {
"天津市": ["高新区", "西青区", "保税区"],
"北京市": ["东城区", "西城区", "朝阳区", "海淀区", "丰台区"],
"上海市": ["黄浦区", "徐汇区", "浦东新区", "静安区", "长宁区"],
"广州市": ["天河区", "海珠区", "越秀区", "白云区", "荔湾区"]
};
return districtMap[city] || [];
}
},
async getStations(city, district) {
var _a, _b;
try {
const result = await request("/regions/stations");
return ((_a = result.data) == null ? void 0 : _a.filter((s) => s.cityName === city && s.districtName === district).map((s) => s.name)) || [];
} catch {
const stationMap = {
"天津市": {
"高新区": ["金融街融汇景苑", "金辉江山铭筑"],
"西青区": ["华兴里", "华兴南里"],
"保税区": ["意境兰庭"]
},
"北京市": {
"东城区": ["景山街道", "交道口街道", "东四街道", "朝阳门街道", "建国门街道"],
"西城区": ["西长安街街道", "新街口街道", "月坛街道", "展览路街道", "德胜街道"],
"朝阳区": ["朝外街道", "劲松街道", "呼家楼街道", "三里屯街道", "团结湖街道"],
"海淀区": ["海淀街道", "中关村街道", "学院路街道", "西三旗街道", "清河街道"]
},
"上海市": {
"黄浦区": ["南京东路街道", "外滩街道", "瑞金二路街道", "淮海中路街道", "豫园街道"],
"徐汇区": ["天平路街道", "湖南路街道", "斜土路街道", "枫林路街道", "长桥街道"],
"浦东新区": ["陆家嘴街道", "潍坊新村街道", "塘桥街道", "南码头路街道", "周家渡街道"]
},
"广州市": {
"天河区": ["五山街道", "员村街道", "车陂街道", "沙河街道", "石牌街道"],
"海珠区": ["赤岗街道", "新港街道", "昌岗街道", "江南中街道", "滨江街道"],
"越秀区": ["洪桥街道", "北京街道", "六榕街道", "流花街道", "光塔街道"]
}
};
return ((_b = stationMap[city]) == null ? void 0 : _b[district]) || [];
}
},
async getSiteUsers(stationName) {
var _a;
try {
const result = await request("/regions/site-users");
return ((_a = result.data) == null ? void 0 : _a.filter((u) => u.stationName === stationName).map((u) => u.name)) || [];
} catch {
return [];
}
},
async getCommunities(city, district) {
var _a;
try {
const result = await request(`/regions/communities?city=${encodeURIComponent(city)}&district=${encodeURIComponent(district)}`);
return result.data || [];
} catch {
const communityMap = {
"天津市": {
"高新区": ["金融街融汇景苑", "金辉江山铭筑"],
"西青区": ["华兴里", "华兴南里"],
"保税区": ["意境兰庭"]
},
"北京市": {
"东城区": ["景山街道", "交道口街道", "东四街道", "朝阳门街道", "建国门街道"],
"西城区": ["西长安街街道", "新街口街道", "月坛街道", "展览路街道", "德胜街道"],
"朝阳区": ["朝外街道", "劲松街道", "呼家楼街道", "三里屯街道", "团结湖街道"],
"海淀区": ["海淀街道", "中关村街道", "学院路街道", "西三旗街道", "清河街道"]
},
"上海市": {
"黄浦区": ["南京东路街道", "外滩街道", "瑞金二路街道", "淮海中路街道", "豫园街道"],
"徐汇区": ["天平路街道", "湖南路街道", "斜土路街道", "枫林路街道", "长桥街道"],
"浦东新区": ["陆家嘴街道", "潍坊新村街道", "塘桥街道", "南码头路街道", "周家渡街道"]
},
"广州市": {
"天河区": ["五山街道", "员村街道", "车陂街道", "沙河街道", "石牌街道"],
"海珠区": ["赤岗街道", "新港街道", "昌岗街道", "江南中街道", "滨江街道"],
"越秀区": ["洪桥街道", "北京街道", "六榕街道", "流花街道", "光塔街道"]
}
};
return ((_a = communityMap[city]) == null ? void 0 : _a[district]) || [];
}
},
async addRegion(city, district, community) {
try {
const result = await request("/regions", {
method: "POST",
data: { city, district, community }
});
return result.success;
} catch {
return true;
}
}
};
exports.orderApi = orderApi;
exports.priceApi = priceApi;
exports.regionApi = regionApi;
exports.userApi = userApi;