addProjectModal.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="register"
  5. :title="isEditMode ? t('layout.map.projectInfo') : t('layout.map.addPro')"
  6. :minHeight="380"
  7. @ok="handleSubmit"
  8. @cancel="handleCancel"
  9. >
  10. <div class="p-20px">
  11. <BasicForm @register="registerForm">
  12. <template #location>
  13. <div class="map-select">
  14. <a-input v-model:value="location" allow-clear disabled />
  15. <a-button type="default" @click="handleOpenMap">{{
  16. t('layout.map.mapSearch')
  17. }}</a-button>
  18. </div>
  19. </template>
  20. </BasicForm>
  21. </div>
  22. <template #centerFooter> </template>
  23. </BasicModal>
  24. </template>
  25. <script lang="ts">
  26. import { computed, defineComponent, ref } from 'vue';
  27. import { BasicModal, useModalInner } from '/@/components/Modal';
  28. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  29. import { useI18n } from '/@/hooks/web/useI18n';
  30. import { AddOptOrUpdateApi, ListAllGpsApi } from '/@/api/mapOpt/list';
  31. import { uniqBy } from 'lodash-es';
  32. const { t } = useI18n();
  33. export default defineComponent({
  34. components: { BasicModal, BasicForm },
  35. props: {
  36. currentLatLng: {
  37. type: Object as PropType<{
  38. lat: number;
  39. lng: number;
  40. }>,
  41. },
  42. },
  43. emits: ['register', 'success', 'open-map', 'reload', 'cancel', 'update-gps'],
  44. setup(props, { emit }) {
  45. // const { createMessage } = useMessage();
  46. const isEditMode = ref(false);
  47. const locationEdit = ref('');
  48. const isDefaultSelect = ref(1);
  49. // const customEditSelect: FormSchema = {
  50. // field: 'sceneID',
  51. // label: '选择场景',
  52. // component: 'Input',
  53. // };
  54. const schemas: FormSchema[] = [
  55. {
  56. field: 'id',
  57. label: 'Id',
  58. component: 'Input',
  59. ifShow: false,
  60. },
  61. {
  62. field: 'projectName',
  63. label: t('layout.map.projectName'),
  64. component: 'Input',
  65. required: true,
  66. componentProps: {
  67. maxLength: 100,
  68. },
  69. colProps: {
  70. span: 24,
  71. },
  72. },
  73. {
  74. field: 'projectSn',
  75. label: t('layout.map.projectSn'),
  76. required: true,
  77. component: 'Input',
  78. componentProps: {
  79. maxLength: 100,
  80. },
  81. colProps: {
  82. span: 24,
  83. },
  84. },
  85. {
  86. field: 'tempSlect',
  87. label: t('layout.map.geoLocation'),
  88. component: 'Select',
  89. defaultValue: 1,
  90. componentProps: {
  91. onChange: function (value: number) {
  92. isDefaultSelect.value = value;
  93. },
  94. options: [
  95. {
  96. label: t('layout.map.geoLocation.opt1'),
  97. value: 1,
  98. },
  99. {
  100. label: t('layout.map.geoLocation.opt2'),
  101. value: 2,
  102. },
  103. ],
  104. },
  105. colProps: {
  106. span: 24,
  107. },
  108. },
  109. {
  110. field: 'location',
  111. label: t('layout.map.location'),
  112. component: 'Input',
  113. required: false,
  114. slot: 'location',
  115. colProps: {
  116. span: 24,
  117. },
  118. ifShow: () => {
  119. return isDefaultSelect.value === 1;
  120. },
  121. },
  122. {
  123. field: 'gpsNum',
  124. label: t('layout.map.selectScene'),
  125. component: 'ApiSelect',
  126. componentProps: {
  127. api: async (params) => {
  128. const data = await ListAllGpsApi(params);
  129. data.list = uniqBy(data.list, 'num');
  130. return data;
  131. },
  132. resultField: 'list',
  133. labelField: 'title',
  134. valueField: 'num',
  135. showSearch: true,
  136. optionFilterProp: 'label',
  137. immediate: true,
  138. listHeight: 160,
  139. allowClear: true,
  140. onSelect: (_, item) => {
  141. const { lat, lon } = item;
  142. console.log('选择场景', _, lat, lon);
  143. if (lat && lon) {
  144. emit('update-gps', {
  145. lat: lat,
  146. lng: lon,
  147. });
  148. }
  149. },
  150. params: {
  151. pageNum: 1,
  152. // type: 1,
  153. searchKey: '',
  154. pageSize: 5000,
  155. },
  156. },
  157. ifShow: () => {
  158. return isDefaultSelect.value === 2;
  159. },
  160. },
  161. {
  162. field: 'isShow',
  163. label: t('layout.map.homeShow'),
  164. component: 'Switch',
  165. defaultValue: true,
  166. colProps: {
  167. span: 24,
  168. },
  169. },
  170. ];
  171. const location = computed(() =>
  172. props.currentLatLng ? `${props.currentLatLng?.lat}, ${props.currentLatLng?.lng}` : '',
  173. );
  174. const googleKey = computed(() => import.meta.env.VITE_GOOGLE_KEY);
  175. const [registerForm, { setFieldsValue, resetFields, validate, getFieldsValue }] = useForm({
  176. schemas: schemas,
  177. labelWidth: 100,
  178. showActionButtonGroup: false,
  179. actionColOptions: {
  180. span: 24,
  181. },
  182. // submitFunc: handleSubmit,
  183. });
  184. const [register, { closeModal }] = useModalInner((data) => {
  185. resetFields();
  186. data && onDataReceive(data);
  187. });
  188. function onDataReceive(data) {
  189. if (data.gpsNum) {
  190. isDefaultSelect.value = 2;
  191. } else {
  192. isDefaultSelect.value = 1;
  193. }
  194. const allData = {
  195. ...data,
  196. tempSlect: isDefaultSelect.value,
  197. isShow: Boolean(data.isShow),
  198. };
  199. console.log('Data Received', allData);
  200. setFieldsValue(allData);
  201. isEditMode.value = true;
  202. }
  203. const handleSubmit = async () => {
  204. const data = await validate();
  205. const allValue = getFieldsValue();
  206. const pro = {
  207. ...allValue,
  208. id: allValue.id,
  209. isShow: Number(data.isShow),
  210. projectName: data.projectName,
  211. projectSn: data.projectSn,
  212. lat: props.currentLatLng?.lat,
  213. lon: props.currentLatLng?.lng,
  214. alt: 0,
  215. };
  216. if (!isEditMode.value) {
  217. delete pro.id;
  218. }
  219. console.log('save', pro);
  220. await AddOptOrUpdateApi(pro);
  221. closeModal();
  222. resetFields();
  223. isDefaultSelect.value = 1;
  224. emit('reload');
  225. };
  226. const handleCancel = async () => {
  227. resetFields();
  228. isDefaultSelect.value = 1;
  229. emit('cancel');
  230. };
  231. const handleOpenMap = () => {
  232. if (props.currentLatLng?.lat && props.currentLatLng?.lng) {
  233. emit('open-map', props.currentLatLng);
  234. } else {
  235. emit('open-map');
  236. }
  237. };
  238. return {
  239. t,
  240. register,
  241. schemas,
  242. handleSubmit,
  243. closeModal,
  244. registerForm,
  245. location,
  246. handleCancel,
  247. googleKey,
  248. locationEdit,
  249. isEditMode,
  250. handleOpenMap,
  251. isDefaultSelect,
  252. };
  253. },
  254. });
  255. </script>
  256. <style lang="less" scoped>
  257. .map-select {
  258. display: flex;
  259. flex-direction: row;
  260. gap: 0 20px;
  261. }
  262. </style>