edit-device-dialog.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <my-dialog
  3. :visible="visible"
  4. :header="title"
  5. :width="600"
  6. attach="body"
  7. :closeOnOverlayClick="false"
  8. @close="emit('update:visible', false)"
  9. >
  10. <t-form ref="formRef" :data="formData" :rules="rules" labelAlign="top">
  11. <t-row :gutter="[20, 20]">
  12. <t-col :span="6">
  13. <t-form-item label="设备编号" name="deviceCode">
  14. <t-input v-model="formData.deviceCode" clearable />
  15. </t-form-item>
  16. </t-col>
  17. <t-col :span="6">
  18. <t-form-item label="序列号" name="serialNo">
  19. <t-input v-model="formData.serialNo" clearable />
  20. </t-form-item>
  21. </t-col>
  22. <t-col :span="6">
  23. <t-form-item label="品牌" name="brand">
  24. <t-select
  25. v-model="formData.brand"
  26. :options="brandList"
  27. clearable
  28. @change="brandChange"
  29. />
  30. </t-form-item>
  31. </t-col>
  32. <t-col :span="6">
  33. <t-form-item label="型号" name="model">
  34. <t-select
  35. v-model="formData.model"
  36. :options="brandModelList"
  37. clearable
  38. />
  39. </t-form-item>
  40. </t-col>
  41. <t-col :span="6">
  42. <t-form-item label="购买时间">
  43. <t-date-picker
  44. v-model="formData.buyTime"
  45. mode="date"
  46. value-type="time-stamp"
  47. enable-time-picker
  48. format="YYYY/MM/DD HH:mm"
  49. :time-picker-props="{ format: 'HH:mm' }"
  50. clearable
  51. />
  52. </t-form-item>
  53. </t-col>
  54. <t-col :span="6">
  55. <t-form-item label="供应商" name="supplierId">
  56. <select-supplier
  57. v-model="formData.supplierId"
  58. type="DEVICE"
  59. @getOptions="getSupplierOptions"
  60. >
  61. </select-supplier>
  62. </t-form-item>
  63. </t-col>
  64. <t-col :span="6">
  65. <t-form-item label="运行状态" name="status">
  66. <t-select v-model="formData.status">
  67. <t-option
  68. v-for="(val, key) in RUNNING_STATUS"
  69. :key="key"
  70. :label="val"
  71. :value="key"
  72. />
  73. </t-select>
  74. </t-form-item>
  75. </t-col>
  76. <t-col :span="6">
  77. <t-form-item label="总扫描量">
  78. <t-input-number
  79. v-model="formData.scanCount"
  80. :decimalPlaces="0"
  81. :min="1"
  82. theme="column"
  83. style="width: 100%"
  84. />
  85. </t-form-item>
  86. </t-col>
  87. <t-col :span="6">
  88. <t-form-item label="当前所在地" name="location">
  89. <!-- <select-area v-model="formData.location" :level="2"></select-area> -->
  90. <t-select v-model="formData.location">
  91. <t-option
  92. v-for="item in supplierOptions"
  93. :key="item.id"
  94. :value="item.name"
  95. :label="item.name"
  96. ></t-option>
  97. </t-select>
  98. </t-form-item>
  99. </t-col>
  100. </t-row>
  101. </t-form>
  102. <template #foot>
  103. <t-button theme="default" @click="emit('update:visible', false)"
  104. >取消</t-button
  105. >
  106. <t-button theme="primary" @click="save">保存</t-button>
  107. </template>
  108. </my-dialog>
  109. </template>
  110. <script setup name="EditDeviceDialog">
  111. import { ref, computed } from 'vue';
  112. import { MessagePlugin } from 'tdesign-vue-next';
  113. import useClearDialog from '@/hooks/useClearDialog';
  114. import { RUNNING_STATUS } from '@/config/constants';
  115. import { deviceEditApi } from '@/api/system';
  116. const emit = defineEmits(['update:visible', 'success']);
  117. const props = defineProps({
  118. visible: Boolean,
  119. curRow: Object,
  120. brandList: {
  121. type: Array,
  122. default() {
  123. return [];
  124. },
  125. },
  126. });
  127. const supplierOptions = ref([]);
  128. const getSupplierOptions = (options) => {
  129. supplierOptions.value = options;
  130. };
  131. const title = computed(() => {
  132. return (isEdit.value ? '编辑' : '新增') + '设备';
  133. });
  134. const formRef = ref(null);
  135. const brandModelList = ref([]);
  136. const { formData, isEdit } = useClearDialog(
  137. {
  138. id: null,
  139. deviceCode: '',
  140. serialNo: '',
  141. brand: '',
  142. model: '',
  143. buyTime: '',
  144. supplierId: '',
  145. status: '',
  146. location: '',
  147. scanCount: null,
  148. sync: true,
  149. enable: true,
  150. },
  151. props,
  152. formRef,
  153. () => {
  154. for (let key in formData) {
  155. formData[key] = props.curRow[key];
  156. }
  157. getBrandModelList(formData.brand);
  158. }
  159. );
  160. const rules = {
  161. deviceCode: [
  162. {
  163. required: true,
  164. message: '设备编号必填',
  165. type: 'error',
  166. trigger: 'change',
  167. },
  168. {
  169. max: 50,
  170. message: '至多需要50个字',
  171. type: 'error',
  172. trigger: 'change',
  173. },
  174. ],
  175. serialNo: [
  176. {
  177. required: true,
  178. message: '序列号必填',
  179. type: 'error',
  180. trigger: 'change',
  181. },
  182. {
  183. max: 100,
  184. message: '至多需要100个字',
  185. type: 'error',
  186. trigger: 'change',
  187. },
  188. ],
  189. brand: [
  190. { required: true, message: '品牌必选', type: 'error', trigger: 'change' },
  191. ],
  192. model: [
  193. { required: true, message: '型号必选', type: 'error', trigger: 'change' },
  194. ],
  195. location: [
  196. {
  197. required: true,
  198. message: '所在地必填',
  199. type: 'error',
  200. trigger: 'change',
  201. },
  202. ],
  203. supplierId: [
  204. { required: true, message: '供应商必选', type: 'error', trigger: 'change' },
  205. ],
  206. status: [
  207. {
  208. required: true,
  209. message: '运行状态必选',
  210. type: 'error',
  211. trigger: 'change',
  212. },
  213. ],
  214. location: [
  215. {
  216. required: true,
  217. message: '当前所在地必填',
  218. type: 'error',
  219. trigger: 'change',
  220. },
  221. ],
  222. };
  223. const brandChange = () => {
  224. formData.model = '';
  225. brandModelList.value = [];
  226. getBrandModelList(formData.brand);
  227. };
  228. const getBrandModelList = (brand) => {
  229. const curBrand = props.brandList.find((item) => item.value === brand);
  230. if (!curBrand) {
  231. brandModelList.value = [];
  232. return;
  233. }
  234. brandModelList.value = curBrand.list;
  235. };
  236. const save = async () => {
  237. const valid = await formRef.value.validate();
  238. if (valid !== true) return;
  239. const res = await deviceEditApi(formData).catch(() => {});
  240. if (!res) return;
  241. MessagePlugin.success('保存成功');
  242. emit('update:visible', false);
  243. emit('success');
  244. };
  245. </script>