AdaKing88
2023-08-23 9cad48db6c56c3e2796a9d6da881817ef13b6eca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<template>
  <el-switch
    v-model="init_data.value"
    :loading="init_data.loading"
    @change="handlerSwitch($event)"
  >
  </el-switch>
</template>
 
<script>
import { reactive, getCurrentInstance } from "vue";
// requestUrl
// import ApiUrl from "@/api/requestUrl";
//API
// import { SwitchStatus } from "@/api/common";
export default {
  name: "SwitchComponent",
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
    config: {
      type: Object,
      default: () => ({}),
    },
  },
  setup(props) {
    // 配置信息
    const config = reactive(props.config);
    // 数据
    const data = reactive(props.data);
    // 初始值
    const init_data = reactive({
      value: data[config.prop],
      loading: false,
    });
    // 获取实例上下文
    const { proxy } = getCurrentInstance();
    // 事件
    const handlerSwitch = (value) => {
      // 请求参数
      const key_id = config.key_id;
      if (!key_id) {
        return false;
      }
      init_data.loading = true;
      // 参数
      const request_data = {
        url: config.api_url || ApiUrl[config.api_module][config.api_module_key],
        method: config.method || "post",
        data: {
          [key_id]: data[key_id], // 等价于["id"]: data["id"]
          [config.prop]: value, // 等价于["status"]: value
        },
      };
      // 接口请求
      //   SwitchStatus(request_data)
      //     .then((response) => {
      //       proxy.$message.success(response.message);
      //       init_data.value = value;
      //       init_data.loading = false;
      //     })
      //     .catch((error) => {
      //       init_data.loading = false;
      //     });
    };
    return { config, init_data, handlerSwitch };
  },
};
</script>