answerList.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <div>
  3. <div
  4. class="questions"
  5. v-if="questionList.length > 0"
  6. v-show="!resultStatus"
  7. >
  8. <div class="top-tips">{{ form.title }}</div>
  9. <el-card class="box-card">
  10. <div slot="header" class="clearfix">
  11. <span class="questionName">{{
  12. questionList[currentIndex - 1]["content"]
  13. }}</span>
  14. </div>
  15. <el-radio-group
  16. v-model="check"
  17. v-if="questionList[currentIndex - 1]['type'] == 'radio'"
  18. >
  19. <div
  20. class=""
  21. v-for="item in questionList[currentIndex - 1][
  22. 'questionnaireQuestionItemList'
  23. ]"
  24. :key="item.id"
  25. >
  26. <el-radio :label="item.id">{{ item.answerValue }}</el-radio>
  27. </div>
  28. </el-radio-group>
  29. <el-checkbox-group
  30. v-model="checkBox"
  31. v-if="questionList[currentIndex - 1]['type'] == 'checkbox'"
  32. >
  33. <el-checkbox
  34. v-for="item in questionList[currentIndex - 1][
  35. 'questionnaireQuestionItemList'
  36. ]"
  37. :key="item.id"
  38. :label="item.id"
  39. >{{ item.answerValue }}</el-checkbox
  40. >
  41. </el-checkbox-group>
  42. <el-input
  43. v-if="questionList[currentIndex - 1]['type'] == 'textarea'"
  44. type="textarea"
  45. style="border: 0"
  46. :autosize="{ minRows: 3, maxRows: 6 }"
  47. placeholder="请输入其它意见"
  48. v-model="textarea"
  49. >
  50. </el-input>
  51. <!-- 图片或者文件 -->
  52. <el-upload
  53. v-if="
  54. questionList[currentIndex - 1]['type'] == 'image' ||
  55. questionList[currentIndex - 1]['type'] == 'file'
  56. "
  57. class="ivu-upload"
  58. :file-list="fileList"
  59. :show-file-list="true"
  60. :headers="headers"
  61. :on-remove="handleRemove"
  62. :accept="
  63. questionList[currentIndex - 1]['type'] == 'image'
  64. ? '.jpg, .jpeg, .png'
  65. : '.XLSX,.XLS,.DOCX,.PDF,.TXT,.PSD,.EPS'
  66. "
  67. :before-upload="
  68. (flie) => {
  69. return beforeUpload(flie, questionList[currentIndex - 1]['type']);
  70. }
  71. "
  72. :on-success="
  73. (response, file, list) => {
  74. handleUploadSuccess(
  75. response,
  76. file,
  77. list,
  78. questionList[currentIndex - 1]
  79. );
  80. }
  81. "
  82. :on-error="handleUploadError"
  83. :max-size="2048"
  84. multiple
  85. action="/api-web/uploadFile"
  86. >
  87. <div slot="trigger" class="el-upload el-upload--picture-card">
  88. <i class="el-icon-plus"></i>
  89. </div>
  90. <div
  91. slot="tip"
  92. v-if="questionList[currentIndex - 1]['type'] == 'image'"
  93. class="el-upload__tip"
  94. >
  95. 只能上传JPG、PNG、JPEG、GIF文件,且不超过5MB
  96. </div>
  97. <div slot="tip" v-else class="el-upload__tip">
  98. 只能上传XLSX、XLS、DOCX、PDF、TXT、PSD、EPS文件,且不超过5MB
  99. </div>
  100. </el-upload>
  101. </el-card>
  102. <el-button
  103. size="large"
  104. type="primary"
  105. v-if="questionList[currentIndex - 1]['type'] == 'radio'"
  106. block
  107. round
  108. class="btn-submit"
  109. color="#01C1B5"
  110. :disabled="!check && !!isRequire"
  111. @click="onSubmit"
  112. >提交</el-button
  113. >
  114. <el-button
  115. size="large"
  116. type="primary"
  117. v-if="questionList[currentIndex - 1]['type'] == 'checkbox'"
  118. block
  119. round
  120. class="btn-submit"
  121. color="#01C1B5"
  122. :disabled="checkBox.length <= 0 && !!isRequire"
  123. @click="onSubmit"
  124. >提交</el-button
  125. >
  126. <el-button
  127. size="large"
  128. type="primary"
  129. v-if="questionList[currentIndex - 1]['type'] == 'textarea'"
  130. block
  131. round
  132. class="btn-submit"
  133. color="#01C1B5"
  134. :disabled="!textarea && !!isRequire"
  135. @click="onSubmit"
  136. >提交</el-button
  137. >
  138. <el-button
  139. size="large"
  140. type="primary"
  141. v-if="questionList[currentIndex - 1]['type'] == 'image'"
  142. block
  143. round
  144. class="btn-submit"
  145. color="#01C1B5"
  146. :disabled="fileList.length <= 0 && !!isRequire"
  147. @click="onSubmit"
  148. >提交</el-button
  149. >
  150. <el-button
  151. size="large"
  152. type="primary"
  153. v-if="questionList[currentIndex - 1]['type'] == 'file'"
  154. block
  155. round
  156. class="btn-submit"
  157. color="#01C1B5"
  158. :disabled="fileList.length <= 0 && !!isRequire"
  159. @click="onSubmit"
  160. >提交</el-button
  161. >
  162. </div>
  163. <div class="resultModel" v-show="resultStatus">
  164. <img src="@/assets/images/pay_success.png" class="img" alt="" />
  165. <p class="content">感谢您的参与!</p>
  166. <el-button
  167. size="large"
  168. type="primary"
  169. round
  170. class="btn-submit"
  171. @click="onSubmitClose"
  172. >确认</el-button
  173. >
  174. </div>
  175. </div>
  176. </template>
  177. <script>
  178. import { getToken } from "@/utils/auth";
  179. export default {
  180. name: "question",
  181. props: ["form", "close"],
  182. data() {
  183. return {
  184. currentIndex: 1,
  185. questionList: [],
  186. check: null,
  187. isRequire: 1,
  188. checkBox: [],
  189. textarea: null,
  190. checkList: [],
  191. fileList: [],
  192. resultStatus: false,
  193. headers: {
  194. Authorization: getToken(),
  195. },
  196. };
  197. },
  198. async mounted() {
  199. try {
  200. this.questionList = this.form.questionnaireQuestionList || [];
  201. console.log(this.questionList);
  202. this.isRequire = this.questionList[this.currentIndex - 1].isRequire;
  203. } catch {
  204. //
  205. }
  206. },
  207. methods: {
  208. toggle(index) {
  209. let selectList = this.questionList[this.currentIndex - 1];
  210. if (selectList.type == "radio") {
  211. this.check = index;
  212. } else if (selectList.type == "checkbox") {
  213. this.$refs.checkboxes[index].toggle();
  214. }
  215. },
  216. onSubmit() {
  217. let selectList = this.questionList[this.currentIndex - 1];
  218. if (selectList.type == "radio" && selectList.isRequire && !this.check) {
  219. this.$message.error("请回答当前问题");
  220. return;
  221. } else if (
  222. selectList.type == "checkbox" &&
  223. selectList.isRequire &&
  224. this.checkBox.length <= 0
  225. ) {
  226. this.$message.error("请回答当前问题");
  227. return;
  228. } else if (
  229. selectList.type == "textarea" &&
  230. selectList.isRequire &&
  231. !this.textarea
  232. ) {
  233. this.$message.error("请回答当前问题");
  234. return;
  235. } else if (
  236. selectList.type == "image" &&
  237. selectList.isRequire &&
  238. this.fileList.length <= 0
  239. ) {
  240. this.$message.error("请回答当前问题");
  241. return;
  242. } else if (
  243. selectList.type == "file" &&
  244. selectList.isRequire &&
  245. this.fileList.length <= 0
  246. ) {
  247. this.$message.error("请回答当前问题");
  248. return;
  249. }
  250. let answerList = selectList.questionnaireQuestionItemList || []; //当前题的选项
  251. let currentIsOver = 0; // 是否终止答题
  252. answerList.forEach((item) => {
  253. if (selectList.type == "radio" && this.check == item.id) {
  254. currentIsOver = item.isOver;
  255. } else if (
  256. selectList.type == "checkbox" &&
  257. this.checkBox.includes(item.id) &&
  258. !currentIsOver
  259. ) {
  260. currentIsOver = item.isOver;
  261. }
  262. });
  263. // 判断是否在题目列表里面
  264. if (this.currentIndex <= this.questionList.length) {
  265. if (selectList.type == "radio") {
  266. this.checkList[this.currentIndex - 1] = {
  267. questionnaireQuestionId: selectList.id,
  268. questionnaireQuestionItemIdList: this.check,
  269. };
  270. this.check = null;
  271. } else if (selectList.type == "checkbox") {
  272. this.checkList[this.currentIndex - 1] = {
  273. questionnaireQuestionId: selectList.id,
  274. questionnaireQuestionItemIdList: this.checkBox.join(","),
  275. };
  276. this.checkBox = [];
  277. } else if (selectList.type == "textarea") {
  278. this.checkList[this.currentIndex - 1] = {
  279. questionnaireQuestionId: selectList.id,
  280. additionalValue: this.textarea,
  281. };
  282. this.textarea = null;
  283. } else if (selectList.type == "image") {
  284. this.checkList[this.currentIndex - 1] = {
  285. questionnaireQuestionId: selectList.id,
  286. additionalValue: JSON.stringify(
  287. this.fileList.map((item) => {
  288. return {
  289. name: item.name,
  290. url: item.response.data.url,
  291. };
  292. })
  293. ),
  294. };
  295. this.fileList = [];
  296. } else if (selectList.type == "file") {
  297. this.checkList[this.currentIndex - 1] = {
  298. questionnaireQuestionId: selectList.id,
  299. additionalValue: JSON.stringify(
  300. this.fileList.map((item) => {
  301. return {
  302. name: item.name,
  303. url: item.response.data.url,
  304. };
  305. })
  306. ),
  307. };
  308. this.fileList = [];
  309. }
  310. // 1 为终止答题
  311. if (currentIsOver) {
  312. this.onConfirm();
  313. return;
  314. }
  315. this.check = null;
  316. if (this.currentIndex == this.questionList.length) {
  317. this.saveKey();
  318. return;
  319. }
  320. this.currentIndex++;
  321. }
  322. },
  323. saveKey() {
  324. // this.$message.info('谢谢参与')
  325. this.resultStatus = true;
  326. },
  327. async onConfirm() {
  328. // this.$message.info('谢谢参与')
  329. this.resultStatus = true;
  330. },
  331. onSubmitClose() {
  332. this.close();
  333. },
  334. beforeUpload(file, type) {
  335. // const isJPG = file.type === 'image/jpeg';
  336. const isLt2M = file.size / 1024 / 1024 < 5;
  337. const imgType = ["JPG", "PNG", "JPEG", "GIF"];
  338. const testType = ["XLSX", "XLS", "DOCX", "PDF", "TXT","PSD","EPS"];
  339. const fileExtension = file.name.split(".").pop().toUpperCase();
  340. console.log(
  341. type,
  342. fileExtension,
  343. testType,
  344. testType.indexOf(fileExtension) != -1
  345. );
  346. let fileType = false;
  347. if (type == "file") {
  348. if (testType.indexOf(fileExtension) != -1) {
  349. fileType = true;
  350. } else {
  351. this.$message.error("请上传正确的附件!");
  352. return false;
  353. }
  354. } else if ((type = "image")) {
  355. if (imgType.indexOf(fileExtension) != -1) {
  356. fileType = true;
  357. } else {
  358. this.$message.error("请上传正确的附件!");
  359. return false;
  360. }
  361. }
  362. // if (!isJPG) {
  363. // this.$message.error('上传头像图片只能是 JPG 格式!');
  364. // }
  365. if (!isLt2M) {
  366. this.$message.error("上传附件大小不能超过 5MB!");
  367. }
  368. this.uploadLoading = true;
  369. return isLt2M && fileType;
  370. },
  371. handleUploadError(file) {
  372. this.uploadLoading = false;
  373. this.$message.error("上传附件失败");
  374. },
  375. handleUploadSuccess(response, file, list, question) {
  376. this.uploadLoading = false;
  377. this.$message.success("上传附件成功");
  378. this.fileList = [...list];
  379. console.log(this.fileList);
  380. // this.fileList.push(file)
  381. },
  382. handleRemove(file, list) {
  383. this.fileList = [...list];
  384. console.log(this.fileList);
  385. },
  386. },
  387. };
  388. </script>
  389. <style lang="less" scoped>
  390. .top-tips {
  391. color: #808080;
  392. font-size: 14px;
  393. padding: 0 0 10px;
  394. }
  395. .btn-submit {
  396. width: 90%;
  397. margin: 20px 5%;
  398. }
  399. .form-info {
  400. margin: 10px;
  401. width: auto;
  402. border-radius: 5px;
  403. }
  404. .questionName {
  405. padding-top: 5px;
  406. font-size: 16px;
  407. color: #444;
  408. font-weight: 500;
  409. line-height: 1.5;
  410. }
  411. /deep/.el-radio-group,
  412. /deep/.el-checkbox-group {
  413. width: 100%;
  414. }
  415. /deep/.el-radio__input.is-checked + .el-radio__label {
  416. color: #606266;
  417. }
  418. /deep/.el-radio,
  419. /deep/.el-checkbox {
  420. width: 100%;
  421. display: flex;
  422. justify-content: space-between;
  423. flex-direction: row-reverse;
  424. padding: 10px 0;
  425. .el-radio__label,
  426. .el-checkbox__label {
  427. padding-left: 0;
  428. }
  429. }
  430. /deep/.el-button--primary {
  431. background: #01c1b5 !important;
  432. border-color: #01c1b5 !important;
  433. }
  434. /deep/.el-upload {
  435. display: block;
  436. }
  437. /deep/.el-upload--picture-card {
  438. width: 100%;
  439. height: 100px;
  440. align-items: center;
  441. display: flex;
  442. flex-direction: row;
  443. justify-content: center;
  444. }
  445. .resultModel {
  446. .img {
  447. width: 145px;
  448. margin: 0 auto;
  449. display: block;
  450. // padding-top: 50px;
  451. }
  452. .content {
  453. font-size: 20px;
  454. color: #1a1a1a;
  455. text-align: center;
  456. font-weight: 500;
  457. padding-bottom: 30px;
  458. }
  459. }
  460. </style>