index.jsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React from 'react';
  2. import Taro from '@tarojs/taro';
  3. import { View } from '@tarojs/components';
  4. import Page from '@/layouts/index';
  5. import { Button, Notify, Field, Cell, CellGroup } from '@antmjs/vantui';
  6. import Map from '@/components/map';
  7. import Uploader from '@/components/Uploader/index';
  8. import mapIcon from '@/assets/icons/marker.png';
  9. import { warn } from '@/utils/message';
  10. import { postTaFeedback } from '@/services/tafeedback';
  11. import { geocoder } from '@/utils/map';
  12. export default (props) => {
  13. const readOnly = false;
  14. const [loading, setLoading] = React.useState(false);
  15. const fmRef = React.useRef({
  16. typeId: undefined,
  17. typeName: undefined,
  18. locId: undefined,
  19. locName: undefined,
  20. location: undefined,
  21. addr: undefined,
  22. content: undefined,
  23. attachList: [],
  24. });
  25. const [formData, setFormData] = React.useState(fmRef.current);
  26. const setFieldChange = (field, value) => {
  27. const data = {
  28. ...fmRef.current,
  29. [field]: value,
  30. };
  31. fmRef.current = data;
  32. setFormData(data);
  33. }
  34. const onSubmit = () => {
  35. console.log(formData)
  36. try {
  37. warn(!formData.addr, '请填写地址')
  38. warn(!formData.content, '请填写问题描述')
  39. warn(!formData.attachList || formData.attachList.length < 1, '请上传照片')
  40. } catch (e) {
  41. return;
  42. }
  43. setLoading(true)
  44. postTaFeedback(formData).then(() => {
  45. setLoading(false);
  46. Taro.navigateBack({
  47. delta: 1,
  48. fail: () => {
  49. Taro.reLaunch({
  50. url: '/pages/home/index'
  51. })
  52. }
  53. });
  54. }).catch(() => {
  55. setLoading(false);
  56. })
  57. }
  58. const onLocationChange = (loc) => {
  59. setFieldChange('location', loc);
  60. if (loc) {
  61. // 交换经纬度位置
  62. const [x, y] = loc.split(',');
  63. const location = [y, x].join(',');
  64. geocoder(location).then(e => setFieldChange('addr', e?.address_component?.street_number || e?.address)).catch(console.error)
  65. }
  66. }
  67. return (
  68. <Page tabBar="feedback">
  69. <Map
  70. readOnly={readOnly}
  71. location={formData.location}
  72. onChange={onLocationChange}
  73. />
  74. <CellGroup>
  75. <Field
  76. placeholder="请填写当前位置"
  77. value={formData.addr}
  78. leftIcon={mapIcon}
  79. readonly={readOnly}
  80. onChange={e => setFieldChange('addr', e.detail)}
  81. />
  82. </CellGroup>
  83. <CellGroup style={{ marginTop: '20px' }}>
  84. <Cell title="问题描述" size="large" border={false} />
  85. <Field
  86. type="textarea"
  87. placeholder="请输入问题描述"
  88. readonly={readOnly}
  89. autosize={{ minHeight: '120px' }}
  90. value={formData.content}
  91. onChange={e => setFieldChange('content', e.detail)}
  92. />
  93. </CellGroup>
  94. <CellGroup style={{ marginTop: '20px' }}>
  95. <Cell title="拍照" size="large" border={false} />
  96. <Cell
  97. renderTitle={
  98. <Uploader
  99. value={formData.attachList}
  100. disabled={readOnly}
  101. onChange={e => setFieldChange('attachList', e)}
  102. />
  103. }
  104. />
  105. </CellGroup>
  106. <View style={{ padding: 'var(--main-space)', background: '#fff' }}>
  107. <Button block type="primary" onClick={onSubmit}>提交</Button>
  108. </View>
  109. </Page>
  110. )
  111. }