address.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // 地址服务 - 对应 supermart-mini/service/address.js
  2. import { get, postL } from './http';
  3. const apis = {
  4. LIST: '/api/addressBook',
  5. ADD: '/api/addressBook/add',
  6. DELETE: '/api/addressBook/delete',
  7. DEFAULT: '/api/addressBook/getDefault',
  8. UPDATE: '/api/addressBook/update',
  9. UPDATE_ORDER_ADDRESS: '/api/mallOrder/updateAddress',
  10. GET_AREA: '/api/area',
  11. };
  12. export interface Address {
  13. id: string;
  14. contactName: string;
  15. contactNo: string;
  16. province: string;
  17. city: string;
  18. district: string;
  19. address: string;
  20. location?: string;
  21. defaultFlag?: number;
  22. }
  23. export interface AddAddressParams {
  24. contactName: string;
  25. contactNo: string;
  26. province: string;
  27. city: string;
  28. district?: string;
  29. address: string;
  30. location?: string;
  31. defaultFlag?: number;
  32. }
  33. export interface AreaItem {
  34. id: string;
  35. name: string;
  36. pid?: number;
  37. }
  38. // 获取默认地址
  39. export const getDefaultAddress = async (): Promise<Address | null> => {
  40. const res = await get<Address>(apis.DEFAULT);
  41. if (res.data && res.data.id) {
  42. return res.data;
  43. }
  44. return null;
  45. };
  46. // 获取地址列表
  47. export const getAddressList = async (size = 100): Promise<Address[]> => {
  48. const res = await get<{ records: Address[] }>(apis.LIST, { size });
  49. return res.data?.records || [];
  50. };
  51. // 添加地址
  52. export const addAddress = async (params: AddAddressParams): Promise<boolean> => {
  53. const res = await postL(apis.ADD, {
  54. ...params,
  55. district: params.district || '',
  56. });
  57. return res.success;
  58. };
  59. // 删除地址
  60. export const deleteAddress = async (id: string): Promise<boolean> => {
  61. const res = await postL(`${apis.DELETE}/${id}`);
  62. return res.success;
  63. };
  64. // 更新地址(设为默认)
  65. export const updateAddress = async (address: Address): Promise<boolean> => {
  66. const res = await postL(apis.UPDATE, address);
  67. return res.success;
  68. };
  69. // 更新订单地址
  70. export const updateOrderAddress = async (params: { tradeNo: string; addressBookId: string }) => {
  71. const res = await postL(apis.UPDATE_ORDER_ADDRESS, params);
  72. return res;
  73. };
  74. // 获取地区数据
  75. export const getArea = async (pid?: number): Promise<AreaItem[]> => {
  76. const res = await get<AreaItem[]>(apis.GET_AREA, pid ? { pid } : { pid: 1 });
  77. return res.data || [];
  78. };
  79. export default {
  80. getDefaultAddress,
  81. getAddressList,
  82. addAddress,
  83. deleteAddress,
  84. updateAddress,
  85. updateOrderAddress,
  86. getArea,
  87. };