clew.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. export const clewMixins = {
  2. data() {
  3. return {};
  4. },
  5. computed: {},
  6. created() {},
  7. mounted() {},
  8. methods: {
  9. purchaseSubmit(callback) {
  10. this.requiredFlag = true;
  11. // 每一个层级都是一道题的题目,子级就是题,被选中和填写的题要带上题目一块上传(题的同级也要上传)
  12. // 第一级题目下的题默认都要上传
  13. let params = {
  14. customerClueItemList: [],
  15. };
  16. // 复制第一级题目
  17. params.customerClueItemList.push(...this.deepClone(this.taskGather, 0));
  18. this.filterOption(this.taskGather, params);
  19. console.log(JSON.stringify(params));
  20. // 必填验证
  21. if (this.requiredFlag) {
  22. callback && callback(params);
  23. } else {
  24. this.$toast(this.requiredMessage);
  25. }
  26. },
  27. filterOption(optionList, params) {
  28. for (let val = 0; val < optionList.length; val++) {
  29. if (
  30. optionList[val].isMust == '0' &&
  31. optionList[val].searchValue == null &&
  32. optionList[val].answerType == 'dx'
  33. ) {
  34. // 题目必填校验
  35. this.requiredFlag = false;
  36. this.requiredMessage = '请选择' + optionList[val].customerClueName;
  37. return;
  38. } else if (optionList[val].isMust == '0') {
  39. // 子级题校验
  40. let customerClueOptionList = optionList[val].customerClueOptionList;
  41. if (customerClueOptionList.length) {
  42. for (let i = 0; i < customerClueOptionList.length; i++) {
  43. // 选中的题目Y:选中,N:未选中
  44. if (customerClueOptionList[i].value == 'Y') {
  45. if (customerClueOptionList[i].customerClueItemList) {
  46. // 必填校验
  47. this.isRequiredFlag(customerClueOptionList[i].customerClueItemList);
  48. // 赋值选中题
  49. let customerClueItemList = params.customerClueItemList;
  50. customerClueItemList.push(
  51. ...this.deepClone(customerClueOptionList[i].customerClueItemList, 0)
  52. );
  53. if (customerClueOptionList[i].customerClueItemList.length) {
  54. this.filterOption(customerClueOptionList[i].customerClueItemList, params);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. },
  63. // 深拷贝指定拷贝层级
  64. deepClone(obj, num) {
  65. // 检查是否为对象或数组
  66. if (obj === null || typeof obj !== 'object') {
  67. return obj; // 基本类型直接返回
  68. }
  69. // 创建一个数组或对象
  70. const copy = Array.isArray(obj) ? [] : {};
  71. // 遍历对象的每个属性
  72. for (const key in obj) {
  73. // 每个题只复制两层子级
  74. if (obj.hasOwnProperty(key) && num < 2) {
  75. // 递归调用深拷贝
  76. if (key == 'customerClueOptionList' || key == 'customerClueItemList') {
  77. num = num + 1;
  78. }
  79. copy[key] = this.deepClone(obj[key], num);
  80. }
  81. }
  82. return copy;
  83. },
  84. isRequiredFlag(optionList) {
  85. // console.log(optionList);
  86. for (let i = 0; i < optionList.length; i++) {
  87. // 是否必填
  88. if (optionList[i].isMust == 0) {
  89. // 输入框
  90. if (optionList[i].answerType == 'wb' || optionList[i].answerType == 'sz') {
  91. if (!optionList[i].answerValue) {
  92. // 必填类型
  93. this.requiredFlag = false;
  94. this.requiredMessage = optionList[i].remark;
  95. return;
  96. } else {
  97. // 条件校验
  98. if (optionList[i].minTextLength) {
  99. // 输入内容长度校验
  100. if (optionList[i].answerValue.length < optionList[i].minTextLength) {
  101. this.requiredFlag = false;
  102. this.requiredMessage = optionList[i].remark;
  103. return;
  104. }
  105. }
  106. }
  107. } else if (optionList[i].answerType == 'zp') {
  108. // 照片
  109. if (!optionList[i].fileInfoList || !optionList[i].fileInfoList.length) {
  110. this.requiredFlag = false;
  111. this.requiredMessage = optionList[i].remark;
  112. return;
  113. }
  114. } else if (optionList[i].answerType == 'bg') {
  115. // 表格
  116. let tableData = optionList[i].tableData;
  117. let filterBGData = this.filterBGData(tableData);
  118. if (filterBGData.hasAllFilledRow && filterBGData.filledRow == '') {
  119. optionList[i].answerValue = JSON.stringify(tableData);
  120. } else {
  121. this.requiredFlag = false;
  122. this.requiredMessage =
  123. filterBGData.filledRow != ''
  124. ? filterBGData.filledRow
  125. : '请填写' + optionList[i].customerClueName;
  126. return;
  127. }
  128. }
  129. }
  130. }
  131. },
  132. filterBGData(tableData) {
  133. // 如果一行中有一个输入框输入了内容,其他输入框没有内容,就弹框提示当前行其它输入框也要填写,
  134. // 整个表格必须有一行所有输入框都输入了内容
  135. let hasAllFilledRow = false; // 是否有整行都填写
  136. let filledRow = ''; // 有内容行
  137. tableData.data.forEach((row, rowIndex) => {
  138. const hasContent = Object.values(row).some(
  139. (cell, index) => index !== 0 && cell.trim() !== ''
  140. );
  141. const allFilled = Object.values(row).every((cell, index) => {
  142. return cell.trim() !== '';
  143. });
  144. if (hasContent && !allFilled) {
  145. filledRow = `${row.typeName}请填写完整`;
  146. return;
  147. }
  148. if (allFilled) {
  149. hasAllFilledRow = true;
  150. }
  151. });
  152. return { hasAllFilledRow, filledRow };
  153. // let data = [];
  154. // const isValid = tableData.data.some((row) => {
  155. // if (Object.values(row).every((value) => value.trim() !== '')) {
  156. // data.push(row);
  157. // }
  158. // });
  159. // return data;
  160. },
  161. },
  162. };