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