model.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. const AddressModel = (now) => ({
  6. addressList: [],
  7. goEdit: false,
  8. chooseAddressId: 0,
  9. getAddressList() {
  10. const { id } = now(User);
  11. return request
  12. .get(`/address/all`, {
  13. params: {
  14. query: {
  15. userId: id,
  16. },
  17. },
  18. })
  19. .then((res) => {
  20. now({
  21. addressList: res.content,
  22. });
  23. const addressInfo = res.content.find((item) => {
  24. return item.isDefault;
  25. });
  26. if (addressInfo) {
  27. now({
  28. chooseAddressId: addressInfo.id,
  29. });
  30. }
  31. });
  32. },
  33. saveAddress(
  34. addressId,
  35. name,
  36. sex,
  37. phone,
  38. addressName,
  39. number,
  40. addressTag,
  41. isDefault
  42. ) {
  43. const { id } = now(User);
  44. const { success } = now(Toast);
  45. const { getAddressList } = now();
  46. return request
  47. .post(`/address/save`, {
  48. data: {
  49. userId: id,
  50. id: addressId || '',
  51. name,
  52. sex,
  53. phone: submitPhone(phone),
  54. addressName,
  55. number,
  56. addressTag,
  57. isDefault,
  58. },
  59. })
  60. .then(() => {
  61. return getAddressList();
  62. })
  63. .then(() => {
  64. success('保存成功');
  65. });
  66. },
  67. setShow(bool) {
  68. now({
  69. goEdit: bool,
  70. });
  71. },
  72. setChoose(id) {
  73. now({ chooseAddressId: id });
  74. },
  75. });
  76. export default AddressModel;