addProjectModal.vue 9.6 KB

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