liveDrawer.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <BasicDrawer
  3. v-bind="$attrs"
  4. @register="registerDrawer"
  5. showFooter
  6. :title="getTitle"
  7. width="60%"
  8. @ok="handleSubmit"
  9. @close="handleClose"
  10. >
  11. <div class="entry-x">
  12. <BasicForm @register="registerForm">
  13. <template #map>
  14. <!-- <Card style="width: 300px; height: 300px"> -->
  15. <div ref="wrapRef" style="width: 100%; height: 400px"></div>
  16. <!-- </Card> -->
  17. </template>
  18. <!-- ="{ model, field }" -->
  19. <template #detailAddr>
  20. <a-input
  21. class="live-drawer"
  22. v-bind="$attrs"
  23. v-model:value="detailAddr"
  24. :allowClear="true"
  25. placeholder=""
  26. >
  27. <template #addonAfter>
  28. <a-button type="primary" class="mr-10px" @click="handleMapSearch">
  29. {{ t('common.queryText') }}
  30. </a-button>
  31. <a-button @click="handleMapReset"> {{ t('common.resetText') }}</a-button>
  32. </template>
  33. </a-input>
  34. </template>
  35. </BasicForm>
  36. </div>
  37. </BasicDrawer>
  38. </template>
  39. <script lang="ts">
  40. import { defineComponent, ref, computed, unref, nextTick, reactive } from 'vue';
  41. import { BasicForm, useForm, FormSchema } from '/@/components/Form/index';
  42. // import { Card } from 'ant-design-vue';
  43. import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  44. // import { useMessage } from '/@/hooks/web/useMessage';
  45. import { useI18n } from '/@/hooks/web/useI18n';
  46. import {
  47. brandTypeListApi,
  48. uploadLiveApi,
  49. getAllSceneApi,
  50. uploadLiveVideoApi,
  51. // LiveSceneDeleteApi,
  52. } from '/@/api/scene/live';
  53. import { data as CascaderData } from '/@/utils/cascaderData';
  54. import { useScript } from '/@/hooks/web/useScript';
  55. const A_MAP_URL = 'https://webapi.amap.com/maps?v=2.0&key=e661b00bdf2c44cccf71ef6070ef41b8';
  56. // const A_MAP_URL = 'https://webapi.amap.com/maps?v=2.0&key=5a2d384532ae531bf99bd8487c4f03d2';
  57. // const A_MAP_URL = 'https://webapi.amap.com/maps?v=1.4.10&key=e661b00bdf2c44cccf71ef6070ef41b8';
  58. //webapi.amap.com/maps?v=1.4.10&key=e661b00bdf2c44cccf71ef6070ef41b8
  59. // Card
  60. export default defineComponent({
  61. name: 'MenuDrawer',
  62. components: { BasicDrawer, BasicForm },
  63. emits: ['success', 'register'],
  64. setup() {
  65. const isUpdate = ref(true);
  66. const detailAddr = ref('');
  67. const wrapRef = ref<HTMLDivElement | null>(null);
  68. interface AddressComponentType {
  69. city: string;
  70. district: string;
  71. province: string;
  72. }
  73. interface GeocodesType {
  74. adcode: string;
  75. addressComponent: AddressComponentType;
  76. formattedAddress: string;
  77. location: {
  78. lng: string;
  79. lat: string;
  80. };
  81. }
  82. let map;
  83. // Lat(120.262337, 30.178285),
  84. const defaultAddress = reactive({
  85. address: '山阴路688号恒隆广场B座1217',
  86. // address: '权晖花园21栋',
  87. lng: 120.262337,
  88. lat: 30.178285,
  89. location: ['33', '3301', '330109'],
  90. province: '浙江省',
  91. city: '杭州市',
  92. district: '萧山区',
  93. });
  94. console.log('defaultAddress', defaultAddress);
  95. const { toPromise } = useScript({ src: A_MAP_URL });
  96. const { t } = useI18n();
  97. // const { createMessage } = useMessage();
  98. const schemas: FormSchema[] = [
  99. {
  100. field: 'type',
  101. label: t('routes.scenes.liveType'),
  102. component: 'ApiSelect',
  103. // colProps: {
  104. // xl: 5,
  105. // xxl: 5,
  106. // },
  107. required: true,
  108. componentProps: {
  109. api: brandTypeListApi,
  110. resultField: 'list',
  111. labelField: 'name',
  112. valueField: 'id',
  113. params: {
  114. page: 1,
  115. limit: 1000,
  116. },
  117. },
  118. },
  119. {
  120. field: 'name',
  121. component: 'Input',
  122. label: t('routes.scenes.liveName'),
  123. required: true,
  124. },
  125. {
  126. field: 'appListPicUrl',
  127. label: t('routes.scenes.appListPicUrl'),
  128. component: 'Upload',
  129. helpMessage: '推荐大小:400 * 400 像素',
  130. required: true,
  131. colProps: {
  132. span: 10,
  133. },
  134. componentProps: {
  135. api: uploadLiveApi,
  136. onChange: async () => {
  137. await validateFields(['appListPicUrl']);
  138. },
  139. maxgoodsNumber: 1,
  140. afterFetch: function (data) {
  141. Reflect.set(data, 'url', data.message.url);
  142. return data;
  143. },
  144. },
  145. },
  146. {
  147. field: 'sceneUrl',
  148. label: t('routes.scenes.sceneUrl'),
  149. component: 'ApiSelect',
  150. // required: true,
  151. colProps: {
  152. span: 10,
  153. },
  154. componentProps: {
  155. api: getAllSceneApi,
  156. immediate: true,
  157. resultField: 'list',
  158. labelField: 'sceneName',
  159. valueField: 'webSite',
  160. params: {
  161. page: 1,
  162. limit: 100,
  163. },
  164. },
  165. },
  166. {
  167. field: 'location',
  168. label: '直播间位置',
  169. component: 'ApiCascader',
  170. componentProps: {
  171. api: () => {
  172. return CascaderData;
  173. },
  174. apiParamKey: 'provinceCode',
  175. dataField: 'children',
  176. labelField: 'name',
  177. valueField: 'code',
  178. // numberToString: true,
  179. isLeaf: (record) => {
  180. return !(record.levelType < 3);
  181. },
  182. onChange: (data) => {
  183. console.log('data', data);
  184. defaultAddress.location = data;
  185. },
  186. },
  187. colProps: {
  188. span: 20,
  189. },
  190. },
  191. {
  192. field: 'detailAddr',
  193. label: t('routes.scenes.detailAddr'),
  194. component: 'Input',
  195. slot: 'detailAddr',
  196. colProps: {
  197. span: 10,
  198. },
  199. },
  200. {
  201. field: 'map',
  202. label: '地图位置',
  203. component: 'Input',
  204. slot: 'map',
  205. colProps: {
  206. span: 20,
  207. },
  208. },
  209. {
  210. field: 'picList',
  211. label: '直播间图片',
  212. component: 'Upload',
  213. componentProps: {
  214. api: uploadLiveApi,
  215. maxSize: 5,
  216. emptyHidePreview: true,
  217. maxNumber: 15,
  218. accept: ['image/*'],
  219. afterFetch: function (data) {
  220. Reflect.set(data, 'url', data.message.url);
  221. return data;
  222. },
  223. },
  224. colProps: {
  225. span: 20,
  226. },
  227. },
  228. {
  229. field: 'introduceVideo',
  230. label: '视频',
  231. component: 'Upload',
  232. componentProps: {
  233. api: uploadLiveVideoApi,
  234. maxSize: 5,
  235. emptyHidePreview: true,
  236. maxNumber: 15,
  237. accept: ['video/*'],
  238. afterFetch: function (data) {
  239. Reflect.set(data, 'url', data.message.video);
  240. return data;
  241. },
  242. },
  243. colProps: {
  244. span: 20,
  245. },
  246. },
  247. {
  248. field: 'contractPhone',
  249. component: 'Input',
  250. label: t('routes.scenes.contractPhone'),
  251. required: true,
  252. helpMessage: ['支持填写400(400-xxx-xxxx)热线、手机号等联系方式'],
  253. },
  254. ];
  255. // updateSchema, validate
  256. const [
  257. registerForm,
  258. { resetFields, setFieldsValue, updateSchema, validate, validateFields },
  259. ] = useForm({
  260. labelWidth: 120,
  261. schemas: schemas,
  262. showActionButtonGroup: false,
  263. baseColProps: { lg: 24, md: 24 },
  264. });
  265. const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  266. resetFields();
  267. setDrawerProps({ confirmLoading: false });
  268. isUpdate.value = !!data?.isUpdate;
  269. if (unref(isUpdate)) {
  270. console.log('data.record', data);
  271. }
  272. initMap();
  273. });
  274. async function initMap() {
  275. await toPromise();
  276. await nextTick();
  277. const wrapEl = unref(wrapRef);
  278. console.log('wrapEl', wrapEl);
  279. if (!wrapEl) return;
  280. // center: [this.longitude || 120.262337, this.latitude || 30.178285],
  281. // const geocoder = new AMap.Geocoder({});
  282. const AMap = (window as any).AMap;
  283. map = new AMap.Map(wrapEl, {
  284. zoom: 18,
  285. center: [120.262337, 30.178285],
  286. viewMode: '3D',
  287. resizeEnable: true,
  288. floorControl: true,
  289. showIndoorMap: true,
  290. });
  291. AMap.plugin('AMap.Geocoder', function () {
  292. var geocoder = new AMap.Geocoder({
  293. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  294. // city: '010',
  295. });
  296. geocoder.getLocation(defaultAddress.address, async function (status, result) {
  297. if (status === 'complete' && result.info === 'OK') {
  298. // result中对应详细地理坐标信息
  299. console.log('result', result);
  300. const { geocodes } = result;
  301. if (geocodes?.length > 0) {
  302. const { adcode, addressComponent } = geocodes[0] as any as GeocodesType;
  303. defaultAddress.location = getCodeArray(adcode);
  304. await updateSchema({
  305. field: 'location',
  306. componentProps: {
  307. displayRenderArray: [
  308. addressComponent.province,
  309. addressComponent.city,
  310. addressComponent.district,
  311. ],
  312. },
  313. });
  314. await setFieldsValue({
  315. location: defaultAddress.location,
  316. // detailAddr: defaultAddress.address,
  317. });
  318. detailAddr.value = defaultAddress.address;
  319. }
  320. }
  321. });
  322. // const marker = new AMap.Marker({
  323. // position: new AMap.LngLat(defaultAddress.lng, defaultAddress.lat),
  324. // title: 'lala',
  325. // });
  326. // map && map.add(marker);
  327. });
  328. }
  329. function getCodeArray(code: string) {
  330. const pdCode = code.slice(0, 2);
  331. const cityCode = code.slice(2, 4);
  332. return [pdCode, cityCode, code];
  333. }
  334. const getTitle = computed(() => (!unref(isUpdate) ? '新增直播间' : '编辑直播间'));
  335. async function handleMapSearch() {
  336. if (detailAddr.value?.length > 0) {
  337. console.log('detailAddr.value', detailAddr.value);
  338. const AMap = (window as any).AMap;
  339. console.log('adcode', defaultAddress.location[2]);
  340. AMap.plugin('AMap.Geocoder', function () {
  341. const geocoder = new AMap.Geocoder({
  342. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  343. city:
  344. defaultAddress.location[2] ||
  345. defaultAddress.location[1] ||
  346. defaultAddress.location[0],
  347. // adcode: defaultAddress.location[2],
  348. // citycode: '0571',
  349. });
  350. console.log('geocoder', geocoder);
  351. geocoder.getLocation(detailAddr.value, async function (status, result) {
  352. if (status === 'complete' && result.info === 'OK') {
  353. // result中对应详细地理坐标信息
  354. console.log('result', result);
  355. const { geocodes } = result;
  356. if (geocodes?.length > 0) {
  357. // map.setCenter([lng, lat]);
  358. const { adcode, addressComponent, formattedAddress, location } =
  359. geocodes[0] as any as GeocodesType;
  360. console.log('location', location);
  361. map.setCenter(location);
  362. const marker = new AMap.Marker({
  363. position: new AMap.LngLat(location.lng, location.lat),
  364. title: formattedAddress,
  365. });
  366. map.add(marker);
  367. defaultAddress.location = getCodeArray(adcode);
  368. await updateSchema({
  369. field: 'location',
  370. componentProps: {
  371. displayRenderArray: [
  372. addressComponent.province,
  373. addressComponent.city,
  374. addressComponent.district,
  375. ],
  376. },
  377. });
  378. await setFieldsValue({
  379. location: defaultAddress.location,
  380. // detailAddr: defaultAddress.address,
  381. });
  382. // detailAddr.value = defaultAddress.address;
  383. }
  384. }
  385. });
  386. });
  387. }
  388. }
  389. async function handleMapReset() {
  390. defaultAddress.location = ['33', '3301', '330109'];
  391. detailAddr.value = '山阴路688号恒隆广场B座1217';
  392. handleMapSearch();
  393. }
  394. async function handleSubmit() {
  395. try {
  396. map && map.destroy();
  397. const values = await validate();
  398. console.log('values', values);
  399. resetFields();
  400. closeDrawer();
  401. } catch (error) {}
  402. }
  403. async function handleClose() {
  404. map && map.destroy();
  405. resetFields();
  406. closeDrawer();
  407. }
  408. return {
  409. detailAddr,
  410. registerDrawer,
  411. registerForm,
  412. getTitle,
  413. handleSubmit,
  414. wrapRef,
  415. handleMapSearch,
  416. handleMapReset,
  417. handleClose,
  418. t,
  419. };
  420. },
  421. });
  422. </script>
  423. <style lang="less">
  424. .live-drawer {
  425. .ant-input-group-addon {
  426. padding-right: 0;
  427. background-color: transparent;
  428. border: none;
  429. button {
  430. font-size: 14px;
  431. }
  432. }
  433. }
  434. </style>