addProjectModal.vue 9.2 KB

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