he wei
2025-04-23 b9bd29a1a81f6f7de479e3cc3fdfe3d85fc660bf
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<script setup>
import { ref, reactive, computed, onMounted } from "vue";
// import { addKey, updateKey } from "@/api/keys";
import useElement from "@/hooks/useElement.js";
import { getAllUserName } from "@/api/user";
const { $loading, $message, $confirm } = useElement();
import {
    addCard,
    delCard,
} from '@/api/keys.js';
 
const props = defineProps({
  isUnbind: {
    type: Boolean,
    default: false,
  },
  lockId: {
    type: [String, Number],
    default: "",
  },
  list: {
    type: Array,
    default: () => [],
  },
});
const formRef = ref();
 
const form1 = reactive({
  cardIdInput: "",
  cardIdSelect: "",
});
 
const rules = {
  cardIdInput: [
    {
      required: true,
      message: "不能为空",
      trigger: ["blur", "change"],
    },
  ],
  cardIdSelect: [
    {
      required: true,
      message: "不能为空",
      trigger: ["change", "blur"],
    },
  ],
};
 
const $emit = defineEmits(["cancel", "success"]);
function close() {
  $emit("cancel");
}
 
 
 
 
 
async function unBindCard() {
  let valid = await formRef.value.validate(() => { });
  // console.log('valid', valid, '=============');
 
  if (!valid) {
    $message.error("表单验证失败");
    return false;
  }
 
  let loading = $loading();
 
  delCard(form1.cardIdSelect, props.lockId).then((res) => {
    let { code, data } = res;
    loading.close();
    if (code && data) {
      // console.log(data);
      $message.success("操作成功");
      $emit("success");
    } else {
      $message.error("操作失败");
    }
  })
  .catch((err) => {
    console.log(err);
    loading.close();
  });
}
async function bindCard() {
  let valid = await formRef.value.validate(() => { });
  // console.log('valid', valid, '=============');
 
  if (!valid) {
    $message.error("表单验证失败");
    return false;
  }
  form1.cardIdInput = form1.cardIdInput.trim();
 
  // 如果输入的卡号存在于list中,那么就提示并退出
  if (props.list.includes(form1.cardIdInput)) {
    $message.error("该卡号已存在");
    return false;
  }
  
 
  let loading = $loading();
 
  addCard(form1.cardIdInput, props.lockId).then((res) => {
    let { code, data } = res;
    loading.close();
    if (code && data) {
      console.log(data);
      $message.success("操作成功");
      $emit("success");
    } else {
      $message.error("操作失败");
    }
  })
  .catch((err) => {
    console.log(err);
    loading.close();
  });
}
 
 
onMounted(() => {
  let info = props.info;
  if (info) {
    form1.uname = info.uname;
    form1.uid = info.uid;
    form1.keyType = info.keyType;
    form1.keyName = info.keyName;
    form1.keyNumber = info.keyNumber;
  }
});
</script>
 
<template>
  <div class="">
    <el-form ref="formRef" :model="form1" label-width="80px" :rules="rules">
      <el-form-item v-if="!isUnbind" label="电子卡号" prop="cardIdInput">
        <el-input v-model="form1.cardIdInput"></el-input>
      </el-form-item>
      <el-form-item v-else label="电子卡号" prop="cardIdSelect">
        <el-select v-model="form1.cardIdSelect">
          <el-option v-for="(item, idx) in list" :key="'key_' + idx" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isUnbind" type="primary" @click="unBindCard">解绑</el-button>
        <el-button v-else type="primary" @click="bindCard">绑定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<style scoped lang="scss">
:deep(.select) {
  width: 100%;
}
</style>