watch.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { REFER } from "@/components/popover-select/api";
  2. const fetchOrg = async (prop) => {
  3. try {
  4. // try
  5. const { code, rows } = await REFER({
  6. search: prop,
  7. type: "ORG_PARAM",
  8. });
  9. if (code === 200) {
  10. return rows[0] || {};
  11. }
  12. } catch (err) {
  13. // catch
  14. console.error(err);
  15. } finally {
  16. // finally
  17. }
  18. };
  19. export default function useWatch() {
  20. const watchPuOrgName = () => ({
  21. handler: async function (newProp) {
  22. const index = this.params.priceApplyOrgs.findIndex(
  23. (item) => item.orgName === newProp
  24. );
  25. if (index === -1) {
  26. const {
  27. id: org,
  28. code: orgCode,
  29. name: orgName,
  30. } = await fetchOrg(this.params.puOrgName);
  31. await this.params.priceApplyOrgs.push({
  32. org,
  33. orgCode,
  34. orgName,
  35. createByName: undefined,
  36. updateByName: undefined,
  37. });
  38. }
  39. },
  40. deep: true,
  41. });
  42. const watchPriceApplyOrgs = () => ({
  43. handler: function (newValue) {
  44. this.newParams.priceApplyOrgs = newValue.filter(
  45. (item) => item.delFlag === "0"
  46. );
  47. },
  48. deep: true,
  49. });
  50. const watchPriceApplyItems = () => ({
  51. handler: function (newValue) {
  52. this.newParams.priceApplyItems = newValue.filter(
  53. (item) => item.delFlag === "0"
  54. );
  55. },
  56. deep: true,
  57. });
  58. return { watchPuOrgName, watchPriceApplyOrgs, watchPriceApplyItems };
  59. }