index.jsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React from 'react';
  2. import Taro from '@tarojs/taro';
  3. import { View } from '@tarojs/components';
  4. import { Button, Notify, Field, Cell, CellGroup } from '@antmjs/vantui';
  5. import Page from '@/layouts/index';
  6. import LocType from '@/components/locType';
  7. import IssueType from '@/components/issueType';
  8. import Map from '@/components/map';
  9. import Uploader from '@/components/Uploader/index';
  10. import { getLocation } from '@/utils/authorize';
  11. import { ROLE_INSPECTOR } from '@/utils/user';
  12. import mapIcon from '@/assets/icons/marker.png';
  13. import { postTaIssue } from '@/services/taissue';
  14. import { warn } from '@/utils/message';
  15. export default (props) => {
  16. const [formData, setFormData] = React.useState({
  17. typeId: undefined,
  18. typeName: undefined,
  19. locId: undefined,
  20. locName: undefined,
  21. location: undefined,
  22. addr: undefined,
  23. content: undefined,
  24. attachList: [],
  25. });
  26. const [showLocType, setShowLocType] = React.useState(false);
  27. const [showIssueType, setShowIssueType] = React.useState(false);
  28. const [loading, setLoading] = React.useState(false);
  29. const onLocTypeChange = (_, it) => {
  30. setFormData({
  31. ...formData,
  32. locId: it.typeId,
  33. locName: it.name,
  34. });
  35. setShowLocType(false);
  36. }
  37. const onIssueTypeChange = (_, it) => {
  38. setFormData({
  39. ...formData,
  40. typeId: it.typeId,
  41. typeName: it.name,
  42. });
  43. setShowIssueType(false);
  44. }
  45. const onFieldChange = (field, value) => {
  46. setFormData({
  47. ...formData,
  48. [field]: value,
  49. })
  50. }
  51. const onSubmit = () => {
  52. try {
  53. warn(!formData.addr, '请填写地址')
  54. warn(!formData.locId, '请选择点位')
  55. warn(!formData.content, '请填写问题描述')
  56. warn(!formData.typeId, '请选择问题分类')
  57. warn(!formData.attachList || formData.attachList.length < 1, '请上传照片')
  58. } catch (e) {
  59. return;
  60. }
  61. setLoading(true)
  62. postTaIssue(formData).then(() => {
  63. setLoading(false);
  64. Taro.navigateBack({delta: 1});
  65. }).catch(() => {
  66. setLoading(false);
  67. })
  68. }
  69. React.useMemo(() => {
  70. getLocation().then((res) => {
  71. onFieldChange('location', `${res.longitude},${res.latitude}`);
  72. }).catch((err) => {
  73. Notify.show({
  74. message: '获取位置失败, 请退出重试',
  75. type: 'warning',
  76. })
  77. });
  78. }, []);
  79. return (
  80. <Page roles={[ROLE_INSPECTOR]}>
  81. <LocType
  82. show={showLocType}
  83. value={formData.addr}
  84. onCancel={() => setShowLocType(false)}
  85. onChange={onLocTypeChange}
  86. />
  87. <IssueType
  88. show={showIssueType}
  89. value={formData.typeName}
  90. onCancel={() => setShowIssueType(false)}
  91. onChange={onIssueTypeChange}
  92. />
  93. <Map location={formData.location} />
  94. <Field
  95. value={formData.addr}
  96. leftIcon={mapIcon}
  97. placeholder="请输入地址"
  98. onChange={e => onFieldChange('addr', e.detail)}
  99. />
  100. <CellGroup style={{marginTop: '20px'}}>
  101. <Cell
  102. isLink
  103. title="点位"
  104. value={formData.locName}
  105. onClick={() => setShowLocType(true)}
  106. />
  107. <Cell title="问题描述" border={false} />
  108. <Field
  109. type="textarea"
  110. placeholder="请输入问题描述"
  111. autosize={{ minHeight: '120px' }}
  112. value={formData.content}
  113. onChange={e => onFieldChange('content', e.detail)}
  114. />
  115. </CellGroup>
  116. <Cell
  117. isLink
  118. title="问题分类"
  119. style={{marginTop: '20px'}}
  120. value={formData.typeName}
  121. onClick={() => setShowIssueType(true)}
  122. />
  123. <CellGroup style={{marginTop: '20px'}}>
  124. <Cell title="拍照" border={false} />
  125. <Cell
  126. renderTitle={
  127. <Uploader value={formData.attachList} onChange={e => onFieldChange('attachList',e)} />
  128. }
  129. />
  130. </CellGroup>
  131. <View style={{margin: '20px auto', padding: '0 1em'}}>
  132. <Button block type="primary" loading={loading} onClick={onSubmit}>提交</Button>
  133. </View>
  134. </Page>
  135. )
  136. }