clew.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // 填写服务商代码 answerValue 添加 FSQ
  80. if (key == 'customerClueName') {
  81. if (obj[key].indexOf('填写服务商代码') != -1) {
  82. obj['answerValue'] = 'FSQ' + obj['answerValue'];
  83. }
  84. }
  85. copy[key] = this.deepClone(obj[key], num);
  86. }
  87. }
  88. return copy;
  89. },
  90. isRequiredFlag(optionList) {
  91. // console.log(optionList);
  92. for (let i = 0; i < optionList.length; i++) {
  93. // 是否必填
  94. if (optionList[i].isMust == 0) {
  95. // 输入框
  96. if (optionList[i].answerType == 'wb' || optionList[i].answerType == 'sz') {
  97. if (!optionList[i].answerValue) {
  98. // 必填类型
  99. this.requiredFlag = false;
  100. this.requiredMessage = optionList[i].remark;
  101. return;
  102. } else {
  103. // 条件校验
  104. if (optionList[i].minTextLength) {
  105. // 输入内容长度校验
  106. if (optionList[i].answerValue.length < optionList[i].minTextLength) {
  107. this.requiredFlag = false;
  108. this.requiredMessage =
  109. optionList[i].customerClueName + '长度必须等于' + optionList[i].minTextLength;
  110. return;
  111. }
  112. }
  113. }
  114. } else if (optionList[i].answerType == 'zp') {
  115. // 照片
  116. if (!optionList[i].fileInfoList || !optionList[i].fileInfoList.length) {
  117. this.requiredFlag = false;
  118. this.requiredMessage = optionList[i].remark;
  119. return;
  120. }
  121. } else if (optionList[i].answerType == 'bg') {
  122. // 表格
  123. let tableData = optionList[i].tableData;
  124. let filterBGData = this.filterBGData(tableData);
  125. if (filterBGData.hasAllFilledRow && filterBGData.filledRow == '') {
  126. optionList[i].answerValue = JSON.stringify(tableData);
  127. } else {
  128. this.requiredFlag = false;
  129. this.requiredMessage =
  130. filterBGData.filledRow != ''
  131. ? filterBGData.filledRow
  132. : '请填写' + optionList[i].customerClueName;
  133. return;
  134. }
  135. }
  136. }
  137. }
  138. },
  139. filterBGData(tableData) {
  140. // 如果一行中有一个输入框输入了内容,其他输入框没有内容,就弹框提示当前行其它输入框也要填写,
  141. // 整个表格必须有一行所有输入框都输入了内容
  142. let hasAllFilledRow = false; // 是否有整行都填写
  143. let filledRow = ''; // 有内容行
  144. tableData.data.forEach((row, rowIndex) => {
  145. const hasContent = Object.values(row).some(
  146. (cell, index) => index !== 0 && cell.trim() !== ''
  147. );
  148. const allFilled = Object.values(row).every((cell, index) => {
  149. return cell.trim() !== '';
  150. });
  151. if (hasContent && !allFilled) {
  152. filledRow = `${row.typeName}请填写完整`;
  153. return;
  154. }
  155. if (allFilled) {
  156. hasAllFilledRow = true;
  157. }
  158. });
  159. return { hasAllFilledRow, filledRow };
  160. // let data = [];
  161. // const isValid = tableData.data.some((row) => {
  162. // if (Object.values(row).every((value) => value.trim() !== '')) {
  163. // data.push(row);
  164. // }
  165. // });
  166. // return data;
  167. },
  168. },
  169. };