addProjectModal.vue 9.0 KB

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