闫增涛
4 天以前 6227519a1bd9007aedae11b77b0b3b1851837c38
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<template>
  <div class="user-manage">
    <div class="flex jc-sb ai-c">
      <div class="addBox" @click="addBtn">
        <el-icon><Plus /></el-icon>
        <span>新建</span>
      </div>
      <div class="search">
        <el-input
          v-model="searchValue"
          style="width: 400px"
          placeholder="请输入搜索关键字"
        >
          <template #suffix>
            <el-icon class="el-input__icon"><search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>
    <div class="list">
      <el-table
        :data="tableData"
        row-key="id"
        border
        style="width: 100%"
        class="roverTable"
      >
        <el-table-column prop="index" label="序号" width="60" />
        <el-table-column prop="name" label="用户姓名" />
        <el-table-column prop="loginName" label="用户名" />
        <el-table-column prop="state" label="状态">
          <template #default="scope">
            <span v-if="scope.row.isEnable" style="color: rgb(46, 236, 21)"
              >已启用</span
            >
            <span v-else style="color: rgb(255, 30, 0)">已禁用 </span>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="类型" />
        <el-table-column prop="createDate" label="创建时间" />
        <el-table-column label="操作" width="220">
          <template #default="scope">
            <el-button size="small" @click="update(scope.row)">
              编辑
            </el-button>
            <el-button size="small" @click="goDisable(scope.row)">
              禁用
            </el-button>
            <el-button size="small" @click="goDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="rover-pagination-block">
      <el-pagination
        v-model:current-page="currentPage"
        :page-size="pageSize"
        :background="background"
        layout="total, prev, pager, next"
        :total="100"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      v-model="dialogFormVisible"
      :title="dialogTitle"
      width="500"
      @close="closeDialog(formRef)"
    >
      <el-form
        :model="form"
        ref="formRef"
        :rules="formRules"
        label-width="140px"
      >
        <el-form-item label="用户名" prop="loginName">
          <el-input
            v-model="form.loginName"
            autocomplete="off"
            placeholder="请输入"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="请输入"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            autocomplete="off"
            placeholder="请输入"
            style="width: 240px"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            autocomplete="off"
            placeholder="请输入"
            style="width: 240px"
            show-password
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog(formRef)">取消</el-button>
          <el-button type="primary" @click="submitBtn(formRef)">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup lang="ts">
import { ref, onMounted, reactive, inject, watch } from "vue";
import { ElMessage, ElMessageBox, FormInstance, FormRules } from "element-plus";
const MG: any = inject("MG");
 
const searchValue = ref();
const tableData = ref([]);
// 分页
const currentPage = ref(4);
const pageSize = ref(100);
const background = ref(true);
const dialogFormVisible = ref(false);
const dialogTitle = ref();
const form = reactive({
  name: "",
  password: "",
  loginName: "",
  confirmPassword: "",
});
 
const formRef = ref<FormInstance>();
import { curStoreInfo } from "@/store/index";
const seleStore = curStoreInfo();
 
watch(
  () => seleStore.departmentInfo, // 监听 reactive 对象(默认深度监听)
  (newVal) => {
    if (newVal) {
      getUserData();
    }
  }
);
 
const closeDialog = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dialogFormVisible.value = false;
};
 
const submitBtn = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const body = {
        ...form,
        state: "Normal",
        type: "Normal",
        source: "",
        info: "",
        departmentId: seleStore.departmentInfo.id,
      };
      delete body.confirmPassword;
      const res = await MG.dps5.NewUser(body);
      console.log(res);
      if (res) {
        ElMessage({
          message: "新建用户成功",
          type: "success",
        });
        getUserData();
        dialogFormVisible.value = false;
      }
    }
  });
};
 
interface formInfo {
  name: string;
  refCode: string;
  password: string;
  confirmPassword: string;
  loginName: string;
}
 
// 自定义密码校验规则
const validatePassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else if (value.length < 6) {
    callback(new Error("密码长度不能少于6位"));
  } else {
    if (form.confirmPassword !== "") {
      // 如果确认密码已输入,触发确认密码的重新验证
      formRef.value.validateField("confirmPassword", () => null);
    }
    callback();
  }
};
 
// 自定义确认密码校验规则
const validateConfirmPassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== form.password) {
    callback(new Error("两次输入的密码不一致!"));
  } else {
    callback();
  }
};
const formRules = reactive<FormRules<formInfo>>({
  loginName: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
  name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
  password: [{ required: true, validator: validatePassword, trigger: "blur" }],
  confirmPassword: [
    { required: true, validator: validateConfirmPassword, trigger: "blur" },
  ],
});
 
const getUserData = async () => {
  const body = {
    departmentId: seleStore.departmentInfo.id,
    filterList: [],
    searchList: [],
    sort: {
      field: "CreateDate",
      subSorts: [],
      type: "Desc",
    },
    start: 0,
    size: 9999,
  };
  const res = await MG.dps5.GetDepartmentUserList(body);
  tableData.value = res.datas;
};
 
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
};
//新增用户
const addBtn = () => {
  dialogFormVisible.value = true;
  dialogTitle.value = "新增用户";
};
// 编辑
const update = (row) => {};
//禁用
const goDisable = (row) => {
  ElMessageBox.confirm("确定要禁用该用户?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      console.log(row);
    })
    .catch(() => {});
};
//删除
const goDelete = () => {
  ElMessageBox.confirm("确定要删除该用户?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      console.log(row);
    })
    .catch(() => {});
};
</script>
 
<style lang="less" scoped>
.user-manage {
  padding: 20px;
}
.addBox {
  display: flex;
  align-items: center;
  color: #409eff;
  cursor: pointer;
}
.list {
  margin-top: 20px;
}
.rover-pagination-block {
  height: 60px;
  display: flex;
  justify-content: flex-end;
  background-color: #fff;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>