addProjectModal.vue 8.1 KB

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