function.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { REFER } from "@/components/popover-select/api";
  2. import { ADD, EDIT, ITEM, CODE } from "@/api/business/purchase/contract";
  3. export default function useMethods() {
  4. const fetchCode = async ({ _this }) => {
  5. try {
  6. // try
  7. _this.loading = true;
  8. const code = await CODE();
  9. _this.params.code = code;
  10. } catch (err) {
  11. // catch
  12. console.error(err);
  13. } finally {
  14. // finally
  15. _this.loading = false;
  16. }
  17. };
  18. const fetchItem = async ({ _this, prop }) => {
  19. try {
  20. // try
  21. _this.loading = true;
  22. const { code, data } = await ITEM(prop);
  23. if (code === 200) {
  24. _this.params = data;
  25. }
  26. } catch (err) {
  27. // catch
  28. console.error(err);
  29. } finally {
  30. // finally
  31. _this.loading = false;
  32. }
  33. };
  34. const fetchRefer = async ({ _this, prop, type, source }) => {
  35. const { rateCode } = prop;
  36. if (type === "MATERIAL_PARAM") {
  37. try {
  38. // try
  39. _this.loading = true;
  40. const { code, rows } = await REFER({
  41. search: rateCode,
  42. type: "TAX_RATE_PARAM",
  43. });
  44. if (code === 200) {
  45. const [{ ntaxrate }] = rows;
  46. source.tax = ntaxrate === "0E-8" ? "0.00000000" : ntaxrate;
  47. }
  48. } catch (err) {
  49. // catch
  50. console.error(err);
  51. } finally {
  52. // finally
  53. _this.loading = false;
  54. }
  55. }
  56. };
  57. const open = ({ _this }) => {
  58. _this.visible = true;
  59. _this.tabName = _this.tabColumns[0].key;
  60. };
  61. const hide = ({ _this }) => {
  62. _this.visible = false;
  63. };
  64. const submit = ({ _this, prop, type }) => {
  65. _this.$refs[prop].validate(async (valid) => {
  66. if (valid) {
  67. try {
  68. // try
  69. _this.loading = true;
  70. const { buyer: createById, buyerName: createByName } = _this.params;
  71. const {
  72. user: { id: updateById, name: updateByName },
  73. } = _this.$store.state;
  74. const params = {
  75. createById,
  76. createByName,
  77. updateById,
  78. updateByName,
  79. ..._this.params,
  80. };
  81. const { msg, code } =
  82. type === "ADD" ? await ADD(params) : await EDIT(params);
  83. if (code === 200) {
  84. _this.hide();
  85. _this.$emit("success");
  86. _this.$notify.success(msg);
  87. }
  88. } catch (err) {
  89. // catch
  90. console.error(err);
  91. } finally {
  92. // finally
  93. _this.loading = false;
  94. }
  95. } else {
  96. return false;
  97. }
  98. });
  99. };
  100. return {
  101. open,
  102. hide,
  103. submit,
  104. fetchRefer,
  105. fetchCode,
  106. fetchItem,
  107. };
  108. }