location-api.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /**
  2. * 省市区管理API接口系统
  3. * 支持省市区数据的增删改查、层级管理
  4. */
  5. class LocationApiService {
  6. constructor() {
  7. this.listeners = []; // 数据变化监听器
  8. this.initLocationData();
  9. }
  10. // 初始化省市区数据
  11. initLocationData() {
  12. const locations = this.getAllLocations();
  13. if (locations.length === 0) {
  14. const sampleLocations = [
  15. // 省份
  16. {
  17. id: 'province_001',
  18. name: '北京市',
  19. code: '110000',
  20. level: 'province',
  21. parentId: null,
  22. sort: 1,
  23. status: 'active',
  24. createTime: '2024-01-15 10:00:00',
  25. updateTime: '2024-01-15 10:00:00'
  26. },
  27. {
  28. id: 'province_002',
  29. name: '上海市',
  30. code: '310000',
  31. level: 'province',
  32. parentId: null,
  33. sort: 2,
  34. status: 'active',
  35. createTime: '2024-01-15 10:00:00',
  36. updateTime: '2024-01-15 10:00:00'
  37. },
  38. {
  39. id: 'province_003',
  40. name: '广东省',
  41. code: '440000',
  42. level: 'province',
  43. parentId: null,
  44. sort: 3,
  45. status: 'active',
  46. createTime: '2024-01-15 10:00:00',
  47. updateTime: '2024-01-15 10:00:00'
  48. },
  49. // 城市
  50. {
  51. id: 'city_001',
  52. name: '北京市',
  53. code: '110100',
  54. level: 'city',
  55. parentId: 'province_001',
  56. sort: 1,
  57. status: 'active',
  58. createTime: '2024-01-15 10:00:00',
  59. updateTime: '2024-01-15 10:00:00'
  60. },
  61. {
  62. id: 'city_002',
  63. name: '上海市',
  64. code: '310100',
  65. level: 'city',
  66. parentId: 'province_002',
  67. sort: 1,
  68. status: 'active',
  69. createTime: '2024-01-15 10:00:00',
  70. updateTime: '2024-01-15 10:00:00'
  71. },
  72. {
  73. id: 'city_003',
  74. name: '广州市',
  75. code: '440100',
  76. level: 'city',
  77. parentId: 'province_003',
  78. sort: 1,
  79. status: 'active',
  80. createTime: '2024-01-15 10:00:00',
  81. updateTime: '2024-01-15 10:00:00'
  82. },
  83. {
  84. id: 'city_004',
  85. name: '深圳市',
  86. code: '440300',
  87. level: 'city',
  88. parentId: 'province_003',
  89. sort: 2,
  90. status: 'active',
  91. createTime: '2024-01-15 10:00:00',
  92. updateTime: '2024-01-15 10:00:00'
  93. },
  94. // 区县
  95. {
  96. id: 'district_001',
  97. name: '朝阳区',
  98. code: '110105',
  99. level: 'district',
  100. parentId: 'city_001',
  101. sort: 1,
  102. status: 'active',
  103. createTime: '2024-01-15 10:00:00',
  104. updateTime: '2024-01-15 10:00:00'
  105. },
  106. {
  107. id: 'district_002',
  108. name: '海淀区',
  109. code: '110108',
  110. level: 'district',
  111. parentId: 'city_001',
  112. sort: 2,
  113. status: 'active',
  114. createTime: '2024-01-15 10:00:00',
  115. updateTime: '2024-01-15 10:00:00'
  116. },
  117. {
  118. id: 'district_003',
  119. name: '东城区',
  120. code: '110101',
  121. level: 'district',
  122. parentId: 'city_001',
  123. sort: 3,
  124. status: 'active',
  125. createTime: '2024-01-15 10:00:00',
  126. updateTime: '2024-01-15 10:00:00'
  127. },
  128. {
  129. id: 'district_004',
  130. name: '黄浦区',
  131. code: '310101',
  132. level: 'district',
  133. parentId: 'city_002',
  134. sort: 1,
  135. status: 'active',
  136. createTime: '2024-01-15 10:00:00',
  137. updateTime: '2024-01-15 10:00:00'
  138. },
  139. {
  140. id: 'district_005',
  141. name: '浦东新区',
  142. code: '310115',
  143. level: 'district',
  144. parentId: 'city_002',
  145. sort: 2,
  146. status: 'active',
  147. createTime: '2024-01-15 10:00:00',
  148. updateTime: '2024-01-15 10:00:00'
  149. }
  150. ];
  151. this.saveLocations(sampleLocations);
  152. }
  153. }
  154. // 获取所有地区
  155. getAllLocations() {
  156. try {
  157. return wx.getStorageSync('locations') || [];
  158. } catch (error) {
  159. console.error('获取地区数据失败:', error);
  160. return [];
  161. }
  162. }
  163. // 保存地区数据
  164. saveLocations(locations) {
  165. try {
  166. wx.setStorageSync('locations', locations);
  167. this.notifyListeners(locations);
  168. return true;
  169. } catch (error) {
  170. console.error('保存地区数据失败:', error);
  171. return false;
  172. }
  173. }
  174. // 根据ID获取地区
  175. getLocationById(locationId) {
  176. const locations = this.getAllLocations();
  177. return locations.find(location => location.id === locationId);
  178. }
  179. // 根据层级获取地区
  180. getLocationsByLevel(level) {
  181. const locations = this.getAllLocations();
  182. return locations.filter(location => location.level === level);
  183. }
  184. // 根据父级ID获取子地区
  185. getLocationsByParentId(parentId) {
  186. const locations = this.getAllLocations();
  187. return locations.filter(location => location.parentId === parentId);
  188. }
  189. // 获取省份列表
  190. getProvinces() {
  191. return this.getLocationsByLevel('province');
  192. }
  193. // 获取城市列表
  194. getCities(provinceId = null) {
  195. if (provinceId) {
  196. return this.getLocationsByParentId(provinceId);
  197. }
  198. return this.getLocationsByLevel('city');
  199. }
  200. // 获取区县列表
  201. getDistricts(cityId = null) {
  202. if (cityId) {
  203. return this.getLocationsByParentId(cityId);
  204. }
  205. return this.getLocationsByLevel('district');
  206. }
  207. // 创建地区
  208. createLocation(locationData) {
  209. const locations = this.getAllLocations();
  210. const newLocation = {
  211. ...locationData,
  212. id: this.generateLocationId(locationData.level),
  213. createTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
  214. updateTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
  215. status: locationData.status || 'active'
  216. };
  217. locations.push(newLocation);
  218. this.saveLocations(locations);
  219. return newLocation;
  220. }
  221. // 更新地区
  222. updateLocation(locationId, updateData) {
  223. const locations = this.getAllLocations();
  224. const locationIndex = locations.findIndex(location => location.id === locationId);
  225. if (locationIndex === -1) {
  226. throw new Error('地区不存在');
  227. }
  228. locations[locationIndex] = {
  229. ...locations[locationIndex],
  230. ...updateData,
  231. updateTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
  232. };
  233. this.saveLocations(locations);
  234. return locations[locationIndex];
  235. }
  236. // 删除地区
  237. deleteLocation(locationId) {
  238. const locations = this.getAllLocations();
  239. // 检查是否有子地区
  240. const children = this.getLocationsByParentId(locationId);
  241. if (children.length > 0) {
  242. throw new Error('该地区下还有子地区,无法删除');
  243. }
  244. const filteredLocations = locations.filter(location => location.id !== locationId);
  245. if (filteredLocations.length === locations.length) {
  246. throw new Error('地区不存在');
  247. }
  248. this.saveLocations(filteredLocations);
  249. return true;
  250. }
  251. // 搜索地区
  252. searchLocations(searchParams) {
  253. const locations = this.getAllLocations();
  254. let filteredLocations = [...locations];
  255. // 按名称搜索
  256. if (searchParams.name) {
  257. filteredLocations = filteredLocations.filter(location =>
  258. location.name.includes(searchParams.name)
  259. );
  260. }
  261. // 按层级搜索
  262. if (searchParams.level) {
  263. filteredLocations = filteredLocations.filter(location =>
  264. location.level === searchParams.level
  265. );
  266. }
  267. // 按父级ID搜索
  268. if (searchParams.parentId) {
  269. filteredLocations = filteredLocations.filter(location =>
  270. location.parentId === searchParams.parentId
  271. );
  272. }
  273. // 按状态搜索
  274. if (searchParams.status) {
  275. filteredLocations = filteredLocations.filter(location =>
  276. location.status === searchParams.status
  277. );
  278. }
  279. // 排序
  280. const sortBy = searchParams.sortBy || 'sort';
  281. const sortOrder = searchParams.sortOrder || 'asc';
  282. filteredLocations.sort((a, b) => {
  283. let aValue = a[sortBy];
  284. let bValue = b[sortBy];
  285. if (sortOrder === 'asc') {
  286. return aValue > bValue ? 1 : -1;
  287. } else {
  288. return aValue < bValue ? 1 : -1;
  289. }
  290. });
  291. return {
  292. locations: filteredLocations,
  293. total: filteredLocations.length,
  294. page: searchParams.page || 1,
  295. pageSize: searchParams.pageSize || 20
  296. };
  297. }
  298. // 获取地区统计信息
  299. getLocationStatistics() {
  300. const locations = this.getAllLocations();
  301. const stats = {
  302. total: locations.length,
  303. byLevel: {},
  304. byStatus: {},
  305. byParent: {}
  306. };
  307. locations.forEach(location => {
  308. // 按层级统计
  309. stats.byLevel[location.level] = (stats.byLevel[location.level] || 0) + 1;
  310. // 按状态统计
  311. stats.byStatus[location.status] = (stats.byStatus[location.status] || 0) + 1;
  312. // 按父级统计
  313. if (location.parentId) {
  314. stats.byParent[location.parentId] = (stats.byParent[location.parentId] || 0) + 1;
  315. }
  316. });
  317. return stats;
  318. }
  319. // 批量更新地区状态
  320. batchUpdateLocationStatus(locationIds, status) {
  321. const locations = this.getAllLocations();
  322. const updatedLocations = locations.map(location => {
  323. if (locationIds.includes(location.id)) {
  324. return {
  325. ...location,
  326. status,
  327. updateTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
  328. };
  329. }
  330. return location;
  331. });
  332. this.saveLocations(updatedLocations);
  333. return true;
  334. }
  335. // 获取地区树形结构
  336. getLocationTree() {
  337. const locations = this.getAllLocations();
  338. const tree = [];
  339. const locationMap = {};
  340. // 创建地区映射
  341. locations.forEach(location => {
  342. locationMap[location.id] = { ...location, children: [] };
  343. });
  344. // 构建树形结构
  345. locations.forEach(location => {
  346. if (location.parentId) {
  347. if (locationMap[location.parentId]) {
  348. locationMap[location.parentId].children.push(locationMap[location.id]);
  349. }
  350. } else {
  351. tree.push(locationMap[location.id]);
  352. }
  353. });
  354. return tree;
  355. }
  356. // 导出地区数据
  357. exportLocations(searchParams = {}) {
  358. const result = this.searchLocations(searchParams);
  359. return {
  360. locations: result.locations,
  361. exportTime: new Date().toISOString(),
  362. total: result.total,
  363. searchParams
  364. };
  365. }
  366. // 生成地区ID
  367. generateLocationId(level) {
  368. const timestamp = Date.now();
  369. const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
  370. const levelPrefix = {
  371. 'province': 'province',
  372. 'city': 'city',
  373. 'district': 'district'
  374. };
  375. return `${levelPrefix[level]}_${timestamp}_${random}`;
  376. }
  377. // 监听器管理
  378. addListener(callback) {
  379. this.listeners.push(callback);
  380. }
  381. removeListener(callback) {
  382. const index = this.listeners.indexOf(callback);
  383. if (index > -1) {
  384. this.listeners.splice(index, 1);
  385. }
  386. }
  387. notifyListeners(locations) {
  388. this.listeners.forEach(callback => {
  389. try {
  390. callback(locations);
  391. } catch (error) {
  392. console.error('地区数据监听器回调执行失败:', error);
  393. }
  394. });
  395. }
  396. // 同步地区数据
  397. syncLocationData() {
  398. const locations = this.getAllLocations();
  399. this.notifyListeners(locations);
  400. return locations;
  401. }
  402. }
  403. // 创建单例实例
  404. const locationApiService = new LocationApiService();
  405. export default locationApiService;