riku
2023-12-20 f58f07875461b7cc8205978cf4f0fac86564df72
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
<template>
  <FYForm
    :form-info="_formInfo"
    :rules="rules"
    :doClear="active"
    :useCancel="create"
    :useReset="!create"
    :clearAftSubmit="create"
    v-model:is-edit="_edit"
    @submit="submit"
    @cancel="cancel"
  >
    <template #form-item="{ formObj }">
      <!-- <el-form-item label="头像url" prop="HeadIconUrl">
        <el-input
          clearable
          v-model="formObj.HeadIconUrl"
          placeholder="头像url"
        />
      </el-form-item> -->
      <el-form-item label="账户名" prop="acountname">
        <el-input clearable v-model="formObj.acountname" placeholder="账户名" />
      </el-form-item>
      <el-form-item label="用户昵称" prop="realname">
        <el-input clearable v-model="formObj.realname" placeholder="用户昵称" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-col :span="18">
          <el-input
            :disabled="true"
            clearable
            type="password"
            v-model="formObj.password"
            placeholder="默认密码123456"
          />
        </el-col>
        <el-col :span="6" v-if="!create">
          <el-row justify="end">
            <el-button type="danger" @click="onResetPw" :loading="pwLoading">重置密码</el-button>
          </el-row>
        </el-col>
      </el-form-item>
      <FYOptionUserType
        prop="_usertype"
        :allOption="false"
        :initValue="false"
        v-model:value="formObj._usertype"
      ></FYOptionUserType>
      <!-- <el-form-item label="所属企业" prop="departmentname">
        <el-input
          clearable
          v-model="formObj.departmentname"
          placeholder="所属企业"
          disabled
        />
      </el-form-item> -->
      <el-form-item label="是否可用" prop="isenable">
        <el-switch v-model="formObj.isenable" />
        <span style="margin-left: 16px">{{ formObj.isenable ? '可用' : '不可用' }}</span>
      </el-form-item>
      <el-form-item label="工号" prop="workno">
        <el-input clearable v-model="formObj.workno" placeholder="工号" />
      </el-form-item>
      <el-form-item label="手机" prop="telephone">
        <el-input clearable type="tel" v-model="formObj.telephone" placeholder="手机" />
      </el-form-item>
      <FYOptionLocation
        v-if="create"
        prop="_locations"
        :allOption="false"
        :level="5"
        :checkStrictly="true"
        :initValue="false"
        v-model:value="formObj._locations"
      ></FYOptionLocation>
      <FYOptionScene
        prop="_scenetype"
        :allOption="false"
        :type="1"
        :initValue="false"
        v-model:value="formObj._scenetype"
      ></FYOptionScene>
    </template>
  </FYForm>
</template>
 
<script setup>
import { defineProps, defineEmits, reactive, ref, watch } from 'vue';
import { getSceneName } from '@/enum/scene';
import userApi from '@/api/fytz/userApi';
import { useMessageBoxTip } from '@/composables/messageBox';
 
const props = defineProps({
  //基本信息
  formInfo: Object,
  isEdit: Boolean,
  create: {
    type: Boolean,
    default: false
  },
  active: {
    type: Boolean,
    default: false
  }
});
 
const emit = defineEmits(['onSubmit', 'onCancel', 'update:isEdit']);
 
const _formInfo = ref({ isenable: true });
const _edit = ref(false);
const rules = reactive({
  acountname: [
    {
      required: true,
      message: '账户名不能为空',
      trigger: 'blur'
    }
  ],
  realname: [
    {
      required: true,
      message: '用户昵称不能为空',
      trigger: 'blur'
    }
  ],
  // password: [
  //   {
  //     required: props.create,
  //     message: '密码不能为空',
  //     trigger: 'blur',
  //   },
  // ],
});
 
// 用户基本信息格式化
function parseUserInfo(s) {
  s._usertype = {
    label: s.usertype,
    value: s.usertypeid + ''
  };
 
  s._scenetype = getSceneName(s.extension2, 1);
 
  s._locations = {};
 
  return s;
}
 
function parseUserInfoReverse(formObj) {
  // 行政区划信息填充
  const a = formObj.value._locations;
  formObj.value.extension1 = a.dName;
  // 用户类型信息填充
  const b = formObj.value._usertype;
  formObj.value.usertypeid = b.value;
  formObj.value.usertype = b.label;
 
  // 场景类型信息填充
  const c = formObj.value._scenetype;
  formObj.value.extension2 = c.value;
 
  return formObj;
}
 
function createUser(formObj, success, fail) {
  const l = formObj.value._locations;
  const params = {
    userInfo: formObj.value,
    baseInfo: {
      biProvinceCode: l.pCode,
      biProvinceName: l.pName,
      biCityCode: l.cCode,
      biCityName: l.cName,
      biDistrictCode: l.dCode,
      biDistrictName: l.dName,
      biTownCode: l.tCode,
      biTownName: l.tName,
      biAreaCode: l.aCode,
      biArea: l.aName,
      // biManagementCompanyId:
      // biManagementCompany:
      // biContact
      // biTelephone
      // biAddress
    }
  };
  return userApi
    .createUser(params)
    .then(() => {
      emit('onSubmit', params);
      success();
    })
    .catch((err) => {
      fail(err);
    });
}
 
function updateUser(formObj, success, fail) {
  return userApi
    .updateUserInfo(formObj.value)
    .then(() => {
      emit('onSubmit', formObj);
      if (success) success();
    })
    .catch((err) => {
      if (fail) fail(err);
    });
}
 
function submit(formObj, success, fail) {
  parseUserInfoReverse(formObj);
 
  return props.create ? createUser(formObj, success, fail) : updateUser(formObj, success, fail);
}
 
function cancel() {
  emit('onCancel');
}
 
watch(
  () => props.formInfo,
  (nValue) => {
    _formInfo.value = parseUserInfo(nValue);
  }
);
 
// watch(
//   () => props.create,
//   (nValue) => {
//     if (nValue) {
 
//     }
//   },
//   {immediate:true}
// );
 
watch(_edit, (nValue) => {
  emit('update:isEdit', nValue);
});
 
// 重置密码
 
const pwLoading = ref(false);
 
function onResetPw() {
  useMessageBoxTip({
    confirmMsg: '是否重置该场景密码?',
    confirmTitle: '重置密码',
    onConfirm: async () => {
      pwLoading.value = true;
      return userApi.resetPassword(props.formInfo.guid).finally(() => {
        pwLoading.value = false;
      });
    }
  });
}
</script>