zhouwx
2025-06-10 c8c99bf1f753e27c4d99a0ff6058ce16f973f9c4
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<template>
  <div class="notice">
    <el-dialog
        v-model="dialogVisible"
        :title="state.title"
        width="550px"
        :before-close="handleClose"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
    >
      <el-form :model="state.form" size="default" ref="superRef" :rules="state.formRules" label-width="180px" >
        <el-form-item label="企业:"  prop="companyName" v-if="state.title !== '修改密码'">
          <el-input v-model.trim="state.form.companyName" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名:"  prop="name" v-if="state.title !== '修改密码'">
          <el-input v-model.trim="state.form.name" :disabled="disabled" placeholder="请输入姓名" ></el-input>
        </el-form-item>
        <el-form-item label="性别:"  prop="sex" v-if="state.title !== '修改密码'">
          <el-radio-group v-model="state.form.sex"  :disabled="disabled">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证号:"  prop="idNo" v-if="state.title !== '修改密码'">
          <el-input v-model.trim="state.form.idNo" maxlength="18" :disabled="disabled" placeholder="请输入身份证号" ></el-input>
        </el-form-item>
        <el-form-item label="手机号(登录用户名):" prop="phone" v-if="state.title !== '修改密码'" >
          <el-input v-model.trim="state.form.phone" :maxlength="11" :disabled="disabled" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password" v-if="state.title == '新增' || state.title == '修改密码'">
          <el-input v-model.trim="state.form.password" type="password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="重复密码:" prop="confirmPassword" v-if="state.title == '新增' || state.title == '修改密码'">
          <el-input v-model.trim="state.form.confirmPassword" type="password" show-password placeholder="请输入确认密码"></el-input>
        </el-form-item>
        <el-form-item label="所属部门账号:"  prop="createBy" v-if="state.title !== '修改密码' && (currentUserType === 1 || currentUserType === 4)">
          <el-select
              clearable
              v-model="state.form.createBy"
              filterable
              remote
              @change="selectValue"
              reserve-keyword
              placeholder="请输入部门账号名称"
              remote-show-suffix
              :remote-method="getDeptUserList"
              :loading="loading"
              style="width: 100%"
          >
            <el-option
                v-for="item in state.deptUserList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号:"  prop="empno" v-if="state.title !== '修改密码'">
          <el-input v-model.trim="state.form.empno" :disabled="disabled" placeholder="请输入工号" ></el-input>
        </el-form-item>
        <el-form-item label="岗位:"  prop="post" v-if="state.title !== '修改密码'">
          <el-input v-model.trim="state.form.post" :disabled="disabled" placeholder="请输入岗位" ></el-input>
        </el-form-item>
        <el-form-item label="职务:"  prop="duty" v-if="state.title !== '修改密码'">
          <el-input v-model.trim="state.form.duty" :disabled="disabled" placeholder="请输入职务" ></el-input>
        </el-form-item>
      </el-form>
      <template #footer v-if="state.title !='查看'">
        <span class="dialog-footer">
            <el-button @click="handleClose" size="default">取 消</el-button>
            <el-button type="primary"  @click="onSubmit" size="default" v-preReClick>确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {reactive, ref, toRefs, defineEmits, nextTick, onMounted} from 'vue'
import { View } from "@element-plus/icons-vue";
import scorllSelect from '@/components/scrollSelect/index.vue'
import {ElMessage, ElMessageBox} from "element-plus";
import {verifyPhone, verifyPwd, verifyUsername} from "@/utils/validate";
import { checkUserName, checkPhone } from "@/api/login"
import {changeCompany, resetPwd} from "@/api/onlineEducation/student"
import {Base64} from "js-base64"
import Cookies from "js-cookie";
import {addStudent, checkStuIdNo, checkStuPhone, editStudent} from "@/api/onlineEducation/student";
import {getCompany} from "@/api/onlineEducation/company";
import {getUser} from "@/api/onlineEducation/user";
const emit = defineEmits(["getList"]);
const dialogVisible = ref(false)
const superRef = ref(null)
const scrollRef = ref(null)
 
 
 
const equalToPassword = (rule, value, callback) => {
  if (state.form.password !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};
 
const validateUserPhone = (rule, value, callback)=>{
  if(value === ''){
    callback(new Error('请输入手机号'))
  }else if(state.title === '编辑' && value === startPhone.value){
    callback()
  } else if(!verifyPhone(value)){
    callback(new Error('手机号格式有误'))
  }else{
    let param = {}
    if(state.title === '新增') {
      param = {
        phone:value
      }
    }else if(state.title === '编辑'){
      param = {
        phone:value,
        id: state.form.id
      }
    }
    checkStuPhone(param).then((res)=>{
      if(res.data == false){
        callback(new Error('手机号已被占用,请更换其他手机号'))
      }else{
        callback()
      }
    })
  }
}
 
let validatePwd = (rule, value, callback)=>{
  if(value === ''){
    callback(new Error('请输入密码'))
  }else{
    if(!verifyPwd(value)){
      callback(new Error('密码须包含字母、数字、特殊字符,长度在6-16之间'))
    }else{
      callback()
    }
  }
}
let validateIdNo = (rule, value, callback)=>{
  if(value === ''){
    callback(new Error('请输入身份证号'))
  }else if(state.title === '编辑' && value === startIdNo.value){
    callback()
  }else{
    let param = {}
    if(state.title === '新增') {
      param = {
        idNo:value
      }
    }else if(state.title === '编辑'){
      param = {
        idNo:value,
        id: state.form.id
      }
    }
    checkStuIdNo(param).then((res)=>{
      if(res.data.status == 1){
        callback(new Error('身份证号在该企业已被占用,请更换其他身份证号'))
      }else if(res.data.status == 2){
        if(state.title == '新增'){
          ElMessageBox.confirm(
              `该人员 ${state.form.name} (身份证号: ${state.form.idNo} )与 ${res.data.companyName} 已经绑定,确定将该人员的责任归属变更到贵公企业?`,
              '提示',
              {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
                icon: ''
              }
          )
          .then(() => {
            const param = {
              companyId: state.form.companyId,
              studentId: res.data.studentId
            }
            changeCompany(param).then((res) => {
              if(res.code == 200){
                ElMessage({
                  type: 'success',
                  message: '变更成功',
                })
                emit('getList')
                handleClose()
 
                callback()
              }else {
                ElMessage.warning(res.message)
              }
            })
          })
        }else {
          callback(new Error('身份证号在其他企业已被占用,请更换身份证号'))
        }
      }else {
        callback()
      }
    })
  }
}
 
 
const state = reactive({
  title: '',
  form: {
    id: null,
    name: '',
    phone: '',
    password: '',
    confirmPassword: '',
    sex: 0,
    companyId: null,
    empno: '',
    post: '',
    duty: '',
    idNo: '',
    createId: null,
    createBy: '',
    userType: null
 
  },
  formRules:{
    name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
    password: [{ required: true, validator: validatePwd, trigger: 'blur' }],
    confirmPassword: [{ required: true, validator: equalToPassword, trigger: 'blur' }],
    phone: [{ required: true, validator: validateUserPhone, trigger: 'blur' }],
    idNo: [{ required: true, validator: validateIdNo, trigger: 'blur' }],
    createBy: [{ required: true, message: '请输入所属部门名称', trigger: 'blur' }],
 
  },
  isAdmin: false,
  deptUserList: []
 
})
const startPhone = ref('');
const startIdNo = ref('');
const UisMounted = ref(false);
onMounted(() => {
  UisMounted.value = true;
 
});
 
const currentUserType = ref(null)
const disabled = ref(false);
const openDialog = async (type, value) => {
  const userInfo = JSON.parse(Cookies.get('userInfo'))
  console.log("userInfo",userInfo)
  currentUserType.value = userInfo.userType
  if(userInfo.userType === 0){
    state.isAdmin = true;
    state.form.userType = 0;
  }else {
    state.isAdmin = false;
    state.form.companyId = userInfo.companyId;
    state.form.companyName = userInfo.companyName;
    state.form.userType = userInfo.userType;
  }
  if(userInfo.userType === 1 || userInfo.userType === 4) {
    await getDeptUserList("")
  }else if(userInfo.userType === 2){
    state.form.createId = userInfo.id
    state.form.createBy = userInfo.name
  }
  state.title = type === 'add' ? '新增' : type ==='edit' ? '编辑' : type ==='pwd' ? '修改密码' : '查看' ;
  if(type === 'edit' || type === 'view') {
    if( type === 'view'){
      disabled.value = true;
    }
    startPhone.value = value.phone
    startIdNo.value = value.idNo
    state.form =  JSON.parse(JSON.stringify(value))
    state.form.companyName = value.company.name;
    console.log("ba",state.form)
  }
  if(type == 'pwd'){
    state.form.id = value.id
  }
  dialogVisible.value = true
}
const onSubmit = async () => {
  const valid = await superRef.value.validate();
  if(valid){
    if(state.title == '新增'){
      const {confirmPassword,id,...data} = state.form
      data.password = Base64.encode(data.password)
      const res = await addStudent(data)
      if(res.code == 200){
        ElMessage.success(res.message)
        emit('getList')
        handleClose()
        dialogVisible.value = false;
      }else{
        ElMessage.warning(res.message)
      }
    }else if(state.title == '编辑'){
      const {id, name, phone, sex, companyId, empno, post, duty, idNo,createId,createBy} = state.form
      const data = {id, name, phone, sex, companyId, empno, post, duty, idNo,createId,createBy}
      const res = await editStudent(data)
      if(res.code == 200){
        ElMessage.success(res.message)
        emit('getList')
        handleClose()
      }else{
        ElMessage.warning(res.message)
      }
    }else{
      const {id,password} = state.form
      const data = {id,password}
      data.password = Base64.encode(data.password)
      const res = await resetPwd(data)
      if(res.code == 200){
        ElMessage.success(res.message)
        emit('getList')
        handleClose()
      }else{
        ElMessage.warning(res.message)
      }
    }
  }
}
const handleClose = () => {
  state.form = {
    id: null,
    name: '',
    phone: '',
    password: '',
    confirmPassword: '',
    sex: 0,
    companyId: null,
    empno: '',
    post: '',
    duty: '',
    idNo: '',
    createId: null,
    createBy: '',
    userType: null
  }
  superRef.value.clearValidate();
  superRef.value.resetFields()
  dialogVisible.value = false;
}
 
 
const selectValue = (val) => {
  // state.form.parentId = null;
  // state.form.parentName = null;
  state.deptUserList.forEach(item => {
    if(item.name === val){
      state.form.createId = item.id
    }
  })
}
 
const loading = ref(false)
const getDeptUserList = async (val)=>{
  let param = {}
  if(val != ""){
    param = {
      name: val,
      userType: 2,
      companyId: state.form.companyId
    }
  }else {
    param = {
      pageNum: 1,
      pageSize: 10,
      userType: 2,
      companyId: state.form.companyId
    }
  }
    loading.value = true;
    const res = await getUser(param)
    if (res.code == 200) {
      loading.value = false;
      state.deptUserList = res.data.list.map(item => {
        return {
          ...item,
          name: item.name + ' ('+ item.username +') '
        }
      })
 
    } else {
      ElMessage.warning(res.message)
    }
}
 
 
 
defineExpose({
  openDialog
});
 
</script>
 
<style scoped lang="scss">
.notice{
  :deep(.el-form .el-form-item__label) {
    font-size: 15px;
  }
  .file {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>