123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React from 'react';
  2. import Taro from '@tarojs/taro';
  3. import { View, ScrollView } from '@tarojs/components';
  4. import { Notify } from '@antmjs/vantui';
  5. import Page from '@/layouts/index';
  6. import {
  7. getTaCheckItemById,
  8. getTaCheckItemByCheck,
  9. preCheck,
  10. getTaCheckItemAnswer,
  11. putTaCheckItemAnswer,
  12. } from '@/services/tacheckitem';
  13. import { getTaCheckItemQu } from '@/services/tacheckitemqu';
  14. import VABC from '@/components/VABC';
  15. import { ROLE_INSPECTOR } from '@/utils/user';
  16. import { warn } from '@/utils/message';
  17. import LocForm from './components/LocForm';
  18. import Question from './components/Question';
  19. import Footer from './components/Footer/index';
  20. const isNotEmpty = str => str !== undefined && str !== null && `${str}`.replace(/\s/g, '') !== '';
  21. export default (props) => {
  22. const router = Taro.useRouter();
  23. const { id, checkId, typ } = router.params;
  24. const [loading, setLoading] = React.useState(false);
  25. const [readonly, setReadonly] = React.useState(false);
  26. const [checkItemInfo, setCheckItemInfo] = React.useState();
  27. const [quList, setQuList] = React.useState([]);
  28. const [index, setIndex] = React.useState(-1);
  29. const [answer, setAnswer] = React.useState();
  30. React.useMemo(() => {
  31. if (typ == 'loc') {
  32. Taro.setNavigationBarTitle({ title: '实地测评' });
  33. } else {
  34. Taro.setNavigationBarTitle({ title: '调查问卷' });
  35. }
  36. }, [typ]);
  37. const onIndexChange = (n) => {
  38. if (index == -1 && n != -1) {
  39. if (!readonly) {
  40. try {
  41. warn(typ == 'survey' && !answer?.communityName, '请填写社区名称');
  42. warn(!answer?.addr, typ == 'loc' ? '请填写地址' : '请填写小区名称');
  43. warn(!answer?.location, '未能获取定位信息, 请重试');
  44. console.log('!answer?.location', !answer?.location);
  45. warn(typ == 'survey' && !answer?.sex, '请选择性别');
  46. warn(typ == 'survey' && !answer?.age, '请选择年龄段');
  47. } catch (error) {
  48. return;
  49. }
  50. }
  51. }
  52. setIndex(n);
  53. }
  54. const onAnswerItemChange = (answerItem, quInfo) => {
  55. if (!answerItem) return;
  56. const an = { ...(answer || {}) }
  57. const answerItemList = an.answerItemList || []
  58. if ((isNotEmpty(answerItem.answerCode) || isNotEmpty(answerItem.answer)) &&
  59. (typ == 'loc' ? answerItem.attachList?.length : true)) {
  60. // 说明这一题已经回答完毕
  61. quInfo.finished = true
  62. } else {
  63. quInfo.finished = false
  64. }
  65. // 更新问题列表中是否作答的标志位
  66. setQuList(quList.map(x => x.quId == quInfo.quId ? quInfo : x));
  67. if (answerItemList.length) {
  68. let found = false;
  69. an.answerItemList = answerItemList.map(x => {
  70. if (x.quId == answerItem.quId) {
  71. found = true;
  72. return answerItem;
  73. } else {
  74. return x;
  75. }
  76. });
  77. if (!found) {
  78. answerItemList.push(answerItem);
  79. an.answerItemList = answerItemList;
  80. }
  81. } else {
  82. answerItemList.push(answerItem);
  83. an.answerItemList = answerItemList;
  84. }
  85. setAnswer(an);
  86. }
  87. // 作答
  88. const onSubmit = () => {
  89. if (readonly) return;
  90. const finished = quList.filter(x => x.finished).length == quList.length;
  91. if (!answer || !answer.answerItemList || !finished) {
  92. Notify.show({
  93. message: '请作答完成所有题目即照片',
  94. type: 'warning',
  95. })
  96. return;
  97. }
  98. setLoading(true);
  99. putTaCheckItemAnswer(checkItemInfo.itemId, answer).then(() => {
  100. setLoading(false);
  101. const t = setTimeout(() => {
  102. clearTimeout(t);
  103. Taro.navigateBack({ delta: 1 });
  104. }, 1000);
  105. }).catch(() => {
  106. setLoading(false);
  107. });
  108. }
  109. // 查询当前测评点位主记录
  110. React.useEffect(() => {
  111. if (id) {
  112. setLoading(true);
  113. getTaCheckItemById(id).then(res => {
  114. setLoading(false);
  115. setCheckItemInfo(res);
  116. }).catch(() => {
  117. setLoading(false);
  118. });
  119. }
  120. }, [id]);
  121. // 查询当前测评调查问卷主记录
  122. React.useEffect(() => {
  123. if (checkId && typ == 'survey') {
  124. setLoading(true);
  125. getTaCheckItemByCheck(checkId, typ).then(res => {
  126. setLoading(false);
  127. setCheckItemInfo(res);
  128. }).catch(() => {
  129. setLoading(false);
  130. });
  131. }
  132. }, [checkId, typ]);
  133. // 查询其他信息
  134. React.useEffect(() => {
  135. if (checkItemInfo?.itemId) {
  136. const { itemId } = checkItemInfo;
  137. setLoading(true);
  138. preCheck(itemId, { noSuccesTip: true }).then((checkRes) => {
  139. if (checkRes.errorCode < 0) {
  140. setLoading(false);
  141. Notify.show({
  142. message: checkRes.message,
  143. type: 'warning',
  144. })
  145. Taro.navigateBack({ delta: 1 })
  146. return;
  147. }
  148. if (checkRes.errorCode > 0) {
  149. setReadonly(true);
  150. // Notify.show({
  151. // message: checkRes.message,
  152. // type: 'warning',
  153. // })
  154. }
  155. // 查询我的答案
  156. // getTaCheckItemAnswer(itemId).then(setAnswer);
  157. // 查询当前测评主记录的所有问题
  158. getTaCheckItemQu({ pageSize: 200, itemId }).then(res => {
  159. setLoading(false);
  160. setQuList(res.records || []);
  161. }).catch(() => {
  162. setLoading(false);
  163. });
  164. }).catch(() => {
  165. setLoading(false);
  166. });
  167. }
  168. }, [checkItemInfo]);
  169. // console.log('--------------quList---------------->', quList);
  170. return (
  171. <Page loading={loading} roles={[ROLE_INSPECTOR]}>
  172. <VABC
  173. footer={(
  174. <Footer
  175. value={index}
  176. readonly={readonly}
  177. list={quList}
  178. onChange={onIndexChange}
  179. onSubmit={onSubmit}
  180. />
  181. )}
  182. >
  183. <ScrollView scrollY style={{ height: '100%' }}>
  184. {
  185. index == -1 && (
  186. <LocForm
  187. checkType={typ}
  188. readonly={readonly}
  189. checkItemInfo={checkItemInfo}
  190. answer={answer}
  191. onChange={setAnswer}
  192. onLoadingChange={setLoading}
  193. />
  194. )
  195. }
  196. {
  197. quList.map((x, inx) => {
  198. const answerItem = answer?.answerItemList?.filter(y => y.quId == x.quId)[0];
  199. return (
  200. index == inx && (
  201. <Question
  202. key={x.quId}
  203. checkType={typ}
  204. cursor={inx}
  205. readonly={readonly}
  206. quInfo={x}
  207. answerItem={answerItem}
  208. total={quList?.length}
  209. onChange={e => onAnswerItemChange(e, x)}
  210. onPrev={() => onIndexChange(null, -1)}
  211. onNext={() => onIndexChange(null, 1)}
  212. onLoadingChange={setLoading}
  213. />
  214. )
  215. )
  216. })
  217. }
  218. </ScrollView>
  219. </VABC>
  220. </Page>
  221. )
  222. }