deviceModal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="register"
  5. :title="t('routes.devices.addDevice')"
  6. @ok="submitModal"
  7. @cancel="handleCloseModal"
  8. @visible-change="handleVisibleChange"
  9. >
  10. <div class="pt-3px pr-3px">
  11. <BasicForm @register="registerForm">
  12. <template #userName="{ model, field }">
  13. {{ model[field] }}
  14. </template>
  15. <template #name="{ model, field }">
  16. {{ model[field] }}
  17. </template>
  18. <template #subNum="{ model, field }">
  19. <!-- {{ model[field] }} -->
  20. <input-number
  21. v-model:value="model[field]"
  22. style="width: 260px; text-align: center"
  23. class="justify-center suNum"
  24. :min="0"
  25. :max="20"
  26. @blur="deviceMapping"
  27. @press-enter="deviceMapping"
  28. >
  29. <template #addonBefore>
  30. <a-button size="small" type="link" @click="handleMinusDevice">
  31. <Icon icon="ic:baseline-minus" :size="20" />
  32. </a-button>
  33. </template>
  34. <template #addonAfter>
  35. <a-button size="small" type="link" @click="handlePlusDevice">
  36. <Icon icon="ic:round-plus" :size="20" />
  37. </a-button>
  38. </template>
  39. </input-number>
  40. </template>
  41. </BasicForm>
  42. </div>
  43. </BasicModal>
  44. </template>
  45. <script lang="ts">
  46. import { defineComponent, ref, nextTick, unref } from 'vue';
  47. import { BasicModal, useModalInner } from '/@/components/Modal';
  48. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  49. import { useMessage } from '/@/hooks/web/useMessage';
  50. import { InputNumber } from 'ant-design-vue';
  51. // AddDevice
  52. import { checkDevice, AddDevice } from '/@/api/corporation/modal';
  53. import { useI18n } from '/@/hooks/web/useI18n';
  54. import { Icon } from '/@/components/Icon/index';
  55. import { clamp, range } from 'lodash-es';
  56. export default defineComponent({
  57. components: { BasicModal, BasicForm, InputNumber, Icon },
  58. props: {
  59. userData: { type: Object },
  60. },
  61. emits: ['register', 'reload'],
  62. setup(props, { emit }) {
  63. const modelRef = ref({
  64. mappingLength: 0,
  65. });
  66. // const num = ref(0);
  67. const { t } = useI18n();
  68. const { createMessage } = useMessage();
  69. const { success, error } = createMessage;
  70. const [register, { closeModal }] = useModalInner((data) => {
  71. data && onDataReceive(data);
  72. });
  73. const handlevalidator = async (_, value) => {
  74. console.log('handlevalidator', value);
  75. if (!value) {
  76. return Promise.resolve();
  77. }
  78. try {
  79. let res = await checkDevice({
  80. childName: value,
  81. });
  82. if (res.message) {
  83. return Promise.reject(res.message);
  84. } else {
  85. return Promise.resolve();
  86. }
  87. } catch (err) {
  88. return Promise.reject(err);
  89. }
  90. };
  91. const schemas: FormSchema[] = [
  92. {
  93. field: 'id',
  94. label: 'id',
  95. show: false,
  96. component: 'Input',
  97. },
  98. {
  99. field: 'subNum',
  100. label: 'subNum',
  101. show: false,
  102. component: 'Input',
  103. },
  104. {
  105. field: 'name',
  106. label: t('routes.corporation.enterpriseName'),
  107. slot: 'userName',
  108. component: 'Input',
  109. },
  110. {
  111. field: 'userName',
  112. label: t('routes.corporation.enterpriseId'),
  113. slot: 'name',
  114. component: 'Input',
  115. },
  116. {
  117. field: 'deviceNumber',
  118. component: 'InputNumber',
  119. label: t('routes.corporation.cameraNum'),
  120. defaultValue: 0,
  121. slot: 'subNum',
  122. colProps: {
  123. span: 8,
  124. },
  125. helpMessage: '最多批量增加20个!',
  126. // componentProps: () => {
  127. // return {
  128. // // xxxx props schema, tableAction, formModel checkDevice
  129. // min: 0,
  130. // max: 10,
  131. // // onChange: numOnChange,
  132. // onblur: deviceMapping,
  133. // };
  134. // },
  135. },
  136. ];
  137. // let schemasList = []
  138. const [
  139. registerForm,
  140. {
  141. setFieldsValue,
  142. resetFields,
  143. getFieldsValue,
  144. validate,
  145. appendSchemaByField,
  146. removeSchemaByFiled,
  147. },
  148. ] = useForm({
  149. labelWidth: 120,
  150. schemas,
  151. showActionButtonGroup: false,
  152. actionColOptions: {
  153. span: 24,
  154. },
  155. });
  156. // async function submitModal() {
  157. // let formData = {
  158. // ...getFieldsValue(),
  159. // };
  160. // let validate = false;
  161. // try {
  162. // const res = await validateFields();
  163. // validate = true;
  164. // console.log('passing', res, formData);
  165. // } catch (error: unknown) {
  166. // console.log('not passing', error);
  167. // }
  168. // if (validate) {
  169. // const { subNum, id, userName } = modelRef.value;
  170. // let childNameList = [];
  171. // Object.keys(formData).map((ele) => {
  172. // if (ele.includes('ID')) {
  173. // childNameList.push(formData[ele]);
  174. // }
  175. // });
  176. // console.log('modelRef.value', Object.keys(formData), childNameList);
  177. // try {
  178. // const res = await AddDevice({
  179. // childNames: childNameList as any as string[],
  180. // companyId: id,
  181. // subNum: String(subNum),
  182. // userName,
  183. // });
  184. // success(res);
  185. // closeModal();
  186. // } catch (errors) {
  187. // // error('errors');
  188. // // console.log('not passing', error);
  189. // }
  190. // }
  191. // }
  192. async function submitModal() {
  193. try {
  194. const values = await validate();
  195. console.log('values,', values);
  196. let childNameList: string[] = [];
  197. Object.keys(values).map((ele) => {
  198. if (ele.includes('ID')) {
  199. childNameList.push(values[ele].replace(/\s*/g, ''));
  200. }
  201. });
  202. await AddDevice({
  203. childNames: childNameList,
  204. id: values.id,
  205. subNum: String(values.subNum),
  206. userName: values.userName,
  207. });
  208. success(t('common.optSuccess'));
  209. closeModal();
  210. emit('reload');
  211. } catch (error) {}
  212. }
  213. function onDataReceive(data) {
  214. // 方式1;
  215. console.log('userName', data.record);
  216. resetFields();
  217. setFieldsValue({
  218. ...data.record,
  219. subNum: 0,
  220. });
  221. deviceMapping();
  222. }
  223. // function numOnChange(data) {
  224. // const value = Number(data);
  225. // if (num.value > value) {
  226. // //减
  227. // let delList = Array.from(new Array(num.value)).map((_, index) => {
  228. // console.log(index, value, num.value);
  229. // if (index >= value) {
  230. // return `ID${index}`;
  231. // }
  232. // });
  233. // removeSchemaByFiled(delList as any as string[]);
  234. // console.log('schemasList减', value, num.value, delList);
  235. // // value,num.value,schemasList.filter((_,index) => {return !(index<num.value)}).map((_,index) => `ID${index}`))
  236. // } else {
  237. // //增
  238. // let device = t('routes.corporation.device');
  239. // let schemasList: FormSchema[] = Array.from(new Array(value)).map((_, index) => {
  240. // return {
  241. // field: `ID${index}`,
  242. // component: 'Input',
  243. // label: device + 'ID' + index,
  244. // helpMessage: [t('common.checkTips'), `${device} ${t('common.unusual')}`],
  245. // itemProps: {
  246. // validateTrigger: 'blur',
  247. // },
  248. // colProps: {
  249. // span: 24,
  250. // },
  251. // rules: [
  252. // {
  253. // required: true,
  254. // message: `${t('common.inputText')} ${device} ID`,
  255. // },
  256. // {
  257. // validator: handlevalidator,
  258. // },
  259. // ],
  260. // };
  261. // });
  262. // console.log(
  263. // 'schemasList增',
  264. // num.value,
  265. // schemasList.filter((_, index) => {
  266. // return index >= num.value;
  267. // }),
  268. // );
  269. // schemasList
  270. // .filter((_, index) => {
  271. // return index >= num.value;
  272. // })
  273. // .forEach((item) => appendSchemaByField(item, ''));
  274. // }
  275. // num.value = value;
  276. // }
  277. function handleVisibleChange(v) {
  278. v && props.userData && nextTick(() => onDataReceive(props.userData));
  279. }
  280. async function handleCloseModal() {
  281. // numOnChange(0);
  282. await setFieldsValue({
  283. subNum: 0,
  284. deviceNumber: 0,
  285. });
  286. }
  287. function setMappingDevice(devices: number, start = 0): FormSchema[] {
  288. return Array.from(new Array(devices)).map((_, index) => {
  289. const startIndex = start + index + 1;
  290. return {
  291. field: `ID${startIndex}`,
  292. component: 'Input',
  293. label: `${t('routes.corporation.device')} ID ${startIndex}`,
  294. helpMessage: [
  295. t('common.checkTips'),
  296. `${t('routes.corporation.device')} ${t('common.unusual')}`,
  297. ],
  298. itemProps: {
  299. validateTrigger: 'blur',
  300. },
  301. colProps: {
  302. span: 24,
  303. },
  304. rules: [
  305. {
  306. required: true,
  307. message: `${t('common.inputText')} ${t('routes.corporation.device')} ID`,
  308. },
  309. {
  310. validator: handlevalidator,
  311. },
  312. ],
  313. };
  314. });
  315. }
  316. async function deviceMapping() {
  317. const values = getFieldsValue();
  318. const devices: number = clamp(values.deviceNumber, 0, 20);
  319. console.log('deviceNumber', devices);
  320. const currentLength = unref(modelRef).mappingLength;
  321. const rest = devices - currentLength;
  322. if (rest > 0) {
  323. //plus
  324. const schemas = setMappingDevice(rest, currentLength);
  325. console.log('schemas', schemas);
  326. const patchs = schemas.map((i: FormSchema) => {
  327. return appendSchemaByField(i, '');
  328. });
  329. await Promise.all(patchs);
  330. unref(modelRef).mappingLength = devices;
  331. console.log('patch in');
  332. } else {
  333. // minus
  334. const startIndex = currentLength + rest;
  335. console.log('rest', rest);
  336. console.log('currentLength', currentLength);
  337. console.log('startIndex', startIndex);
  338. const ranges = range(startIndex + 1, currentLength + 1, 1).map((i) => {
  339. return `ID${i}`;
  340. });
  341. console.log('ranges', ranges);
  342. removeSchemaByFiled(ranges);
  343. unref(modelRef).mappingLength = devices;
  344. }
  345. }
  346. async function handlePlusDevice() {
  347. const values = getFieldsValue();
  348. const currentsubNum = values.deviceNumber;
  349. const currentDevice = clamp(currentsubNum + 1, 0, 20);
  350. await setFieldsValue({
  351. deviceNumber: currentDevice,
  352. });
  353. deviceMapping();
  354. }
  355. async function handleMinusDevice() {
  356. const values = getFieldsValue();
  357. const currentsubNum = values.deviceNumber;
  358. const currentDevice = clamp(currentsubNum - 1, 0, 20);
  359. await setFieldsValue({
  360. deviceNumber: currentDevice,
  361. });
  362. deviceMapping();
  363. }
  364. return {
  365. register,
  366. submitModal,
  367. schemas,
  368. registerForm,
  369. // numOnChange,
  370. modelRef,
  371. handleVisibleChange,
  372. // num,
  373. errorMsg: error,
  374. handleCloseModal,
  375. deviceMapping,
  376. t,
  377. handlePlusDevice,
  378. handleMinusDevice,
  379. };
  380. },
  381. });
  382. </script>
  383. <style lang="less">
  384. .suNum .ant-input-number-input {
  385. text-align: center;
  386. }
  387. </style>