model.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import request from '../../Utils/RequestUtils';
  2. import Toast from '../../flooks/Toast';
  3. import User from '../../flooks/User';
  4. import submitPhone from '../../Utils/FormUtils';
  5. import { alert } from '../../Utils/TotastUtils';
  6. const AddressModel = (now) => ({
  7. addressList: [],
  8. goEdit: false,
  9. chooseAddressId: 0,
  10. getAddressList() {
  11. const { id } = now(User);
  12. return request
  13. .get(`/address/all`, {
  14. params: {
  15. query: {
  16. userId: id,
  17. enabled: true,
  18. },
  19. },
  20. })
  21. .then((res) => {
  22. res.content = res.content.filter((item) => {
  23. return item.latitude && item.longitude;
  24. });
  25. now({
  26. addressList: res.content,
  27. });
  28. const addressInfo = res.content.find((item) => {
  29. return item.isDefault;
  30. });
  31. if (addressInfo) {
  32. now({
  33. chooseAddressId: addressInfo.id,
  34. });
  35. }
  36. });
  37. },
  38. saveAddress(
  39. addressId,
  40. name,
  41. sex,
  42. phone,
  43. addressName,
  44. number,
  45. addressTag,
  46. isDefault,
  47. latitude,
  48. longitude
  49. ) {
  50. const { id } = now(User);
  51. const { success } = now(Toast);
  52. const { getAddressList } = now();
  53. return request
  54. .post(`/address/save`, {
  55. data: {
  56. userId: id,
  57. id: addressId || '',
  58. name,
  59. sex,
  60. phone: submitPhone(phone),
  61. addressName,
  62. number,
  63. addressTag: addressTag || null,
  64. isDefault,
  65. latitude,
  66. longitude,
  67. },
  68. })
  69. .then(() => {
  70. return getAddressList();
  71. })
  72. .then(() => {
  73. success('保存成功');
  74. });
  75. },
  76. delAddress(id) {
  77. alert('', '确定要删除该地址吗?删除后不可恢复!', () => {
  78. const { success } = now(Toast);
  79. const { getAddressList } = now();
  80. return request
  81. .post(`/address/del/${id}`)
  82. .then(() => {
  83. return getAddressList();
  84. })
  85. .then(() => {
  86. success('删除成功');
  87. });
  88. });
  89. },
  90. setShow(bool) {
  91. now({
  92. goEdit: bool,
  93. });
  94. },
  95. setChoose(id) {
  96. now({ chooseAddressId: id });
  97. },
  98. });
  99. export default AddressModel;