experimentationProcess.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template>
  2. <div class="ele-body">
  3. <el-button type="primary" @click="addHtml('customText')" v-if="edit"
  4. >插入自定义文本</el-button
  5. >
  6. <el-button type="primary" @click="addHtml('customTable')" v-if="edit"
  7. >插入表格</el-button
  8. >
  9. <!-- <el-button type="primary" @click="save()">保存</el-button> -->
  10. <div
  11. style="
  12. display: flex;
  13. width: 100%;
  14. padding: 10px;
  15. border: solid 1px #f1f1f1;
  16. margin-top: 10px;
  17. "
  18. >
  19. <vue-draggable
  20. v-model="list"
  21. group="project1"
  22. :animation="300"
  23. handle=".sort-handle"
  24. style="flex: 1;max-height:calc(100vh - 500px);overflow: auto;"
  25. >
  26. <div
  27. class="demo-drag-list-item ele-cell"
  28. v-for="item in list"
  29. :key="item.id"
  30. >
  31. <div class="listItem">
  32. <i
  33. class="sort-handle el-icon-_nav move"
  34. style="display: none"
  35. v-if="edit"
  36. ></i>
  37. <i
  38. class="sort-handle el-icon-delete delete"
  39. v-if="edit"
  40. style="display: none"
  41. @click="del(item.id)"
  42. ></i>
  43. <customText
  44. :ref="'customTextRef' + item.id"
  45. style="flex: 1"
  46. v-if="item.type == 'customText'"
  47. :id="item.id"
  48. :form="item.value"
  49. :valueObj="item.valueObj"
  50. @editShow="editShowFn"
  51. @calculation="calculation"
  52. :edit="edit"
  53. :readonly="readonly"
  54. ></customText>
  55. <customTable
  56. :ref="'customTextRef' + item.id"
  57. style="flex: 1"
  58. v-if="item.type == 'customTable'"
  59. :id="item.id"
  60. :form="item.value"
  61. :valueObj="item.valueObj"
  62. @calculation="calculation"
  63. @editShow="editShowFn"
  64. :readonly="readonly"
  65. @copy="copy"
  66. :edit="edit"
  67. ></customTable>
  68. </div>
  69. </div>
  70. </vue-draggable>
  71. <el-card class="box-card" v-show="editShow" style="width: 320px">
  72. <div slot="header" class="clearfix">
  73. <span>配置</span>
  74. <el-button
  75. style="float: right; padding: 3px 0"
  76. type="text"
  77. @click="editShow = false"
  78. >关闭</el-button
  79. >
  80. </div>
  81. <el-form label-width="80px">
  82. <el-form-item label="字段标识:" prop="id">
  83. <el-input
  84. v-model="domObj.id"
  85. placeholder=""
  86. @change="editInputChange"
  87. ></el-input>
  88. </el-form-item>
  89. <el-form-item label="宽度:" prop="width" v-if="!domObj.isNoWidth">
  90. <el-input
  91. v-model="domObj.width"
  92. type="number"
  93. placeholder=""
  94. @change="editInputChange"
  95. ></el-input>
  96. </el-form-item>
  97. <el-form-item label="是否只读:" prop="readonly">
  98. <el-select
  99. v-model="domObj.readonly"
  100. placeholder="请选择"
  101. @change="editInputChange"
  102. >
  103. <el-option :key="1" label="否" :value="1"> </el-option>
  104. <el-option :key="2" label="是" :value="2"> </el-option>
  105. </el-select>
  106. </el-form-item>
  107. <el-form-item label="计算公式:" prop="readonly">
  108. <el-input
  109. :value="domObj.equation?.map((item) => item.value).join('')"
  110. type="textarea"
  111. placeholder=""
  112. disabled
  113. ></el-input>
  114. <el-button type="primary" @click="setEquation">配置公式</el-button>
  115. <el-button type="primary" @click="delEquation">重置</el-button>
  116. </el-form-item>
  117. </el-form>
  118. </el-card>
  119. </div>
  120. <ele-modal
  121. title="配置公式"
  122. :visible.sync="visible"
  123. :close-on-click-modal="false"
  124. append-to-body
  125. width="800px"
  126. resizable
  127. maxable
  128. >
  129. <div class="formula-builder__selects">
  130. <!-- 选择参数:从已填的非计算参数内容里取 -->
  131. <el-select
  132. v-model="equationUnit.paramSelect"
  133. placeholder="选择参数"
  134. size="mini"
  135. style="width: 100px; margin-right: 8px; flex-shrink: 0"
  136. @change="paramSelectChange($event, 'id')"
  137. filterable
  138. >
  139. <el-option
  140. v-for="item in idList"
  141. :key="item"
  142. :label="item"
  143. :value="item"
  144. />
  145. </el-select>
  146. <!-- 选择运算符 -->
  147. <el-select
  148. v-model="equationUnit.opSelect"
  149. placeholder="选择符号"
  150. size="mini"
  151. style="width: 100px; flex-shrink: 0; margin-right: 8px"
  152. @change="paramSelectChange($event, 'symbol')"
  153. >
  154. <el-option
  155. v-for="op in opSelectOptions"
  156. :key="op"
  157. :label="op"
  158. :value="op"
  159. />
  160. </el-select>
  161. <!-- 选择值 -->
  162. <el-input
  163. v-model="equationUnit.currentValue"
  164. placeholder="输入值"
  165. size="mini"
  166. style="width: 150px; flex-shrink: 0"
  167. >
  168. <template slot="append">
  169. <span
  170. style="cursor: pointer"
  171. @click="paramSelectChange(equationUnit.currentValue, 'value')"
  172. >确认</span
  173. >
  174. </template>
  175. </el-input>
  176. <!-- 替换或者追加 -->
  177. <el-select
  178. v-if="equationUnit.activeIndex != undefined"
  179. v-model="equationUnit.replaceOrAppend"
  180. placeholder="选择"
  181. size="mini"
  182. style="width: 80px; margin-left: 8px; flex-shrink: 0"
  183. >
  184. <el-option key="append" label="追加" value="append" />
  185. <el-option key="replace" label="替换" value="replace" />
  186. </el-select>
  187. <el-input
  188. v-model.number="domObj.units.decimalPlace"
  189. placeholder="小数位"
  190. size="mini"
  191. style="width: 120px; margin-left: 8px; flex-shrink: 0"
  192. >
  193. </el-input>
  194. <el-select
  195. v-model="domObj.units.takeValueMethod"
  196. placeholder="取值方法"
  197. size="mini"
  198. style="width: 100px; margin-left: 8px; flex-shrink: 0"
  199. >
  200. <el-option key="1" label="四舍五入" value="1" />
  201. <el-option key="2" label="去尾" value="2" />
  202. </el-select>
  203. </div>
  204. <!-- 已组装公式标签展示 -->
  205. <div
  206. v-if="equationUnit.equation.length"
  207. style="
  208. display: inline-flex;
  209. flex-wrap: wrap;
  210. max-width: 100%;
  211. margin-top: 5px;
  212. "
  213. >
  214. <el-tag
  215. v-for="(p, index) in equationUnit.equation"
  216. :key="index"
  217. size="mini"
  218. closable
  219. :type="equationUnit.activeIndex === index ? 'primary' : 'info'"
  220. @click="formulaPartsTagClick(index)"
  221. @close="tagItemDelete(index)"
  222. >
  223. {{ p.value }}
  224. </el-tag>
  225. </div>
  226. <el-input
  227. style="margin-top: 5px"
  228. :value="equationUnit.equation?.map((item) => item.value).join('')"
  229. type="textarea"
  230. placeholder=""
  231. disabled
  232. ></el-input>
  233. <div slot="footer" class="footer">
  234. <el-button type="primary" @click="editInputChange('equation')"
  235. >确认</el-button
  236. >
  237. <el-button @click="visible = false">返回</el-button>
  238. </div>
  239. </ele-modal>
  240. </div>
  241. </template>
  242. <script>
  243. import customText from '../components/templateDiv/customText.vue';
  244. import customTable from '../components/templateDiv/customTable.vue';
  245. import VueDraggable from 'vuedraggable';
  246. import { generateRandomString } from '@/utils/util';
  247. export default {
  248. components: {
  249. customText,
  250. VueDraggable,
  251. customTable
  252. },
  253. props: {
  254. edit: {
  255. default: true,
  256. type: Boolean
  257. },
  258. readonly: {
  259. default: false,
  260. type: Boolean
  261. }
  262. },
  263. computed: {},
  264. data() {
  265. return {
  266. list: [],
  267. editShow: false,
  268. visible: false,
  269. templateDivRef: '',
  270. domObj: { units: {} },
  271. idList: [],
  272. opSelectOptions: ['+', '-', '*', '/', '%', '(', ')'],
  273. equationUnit: {
  274. equation: [],
  275. activeIndex: '',
  276. paramSelect: '',
  277. opSelect: '',
  278. currentValue: '',
  279. replaceOrAppend: 'append'
  280. }
  281. };
  282. },
  283. mounted() {},
  284. created() {},
  285. methods: {
  286. // 计算
  287. calculation() {
  288. this.getValue();
  289. let equation = [];
  290. this.list.forEach((item) => {
  291. equation.push({
  292. id: item.id,
  293. equation: item.equation
  294. });
  295. });
  296. equation.forEach((item) => {
  297. for (const key in item.equation) {
  298. let { data, units } = this.getObjValue(); //每次计算都获取最新的值
  299. let value = '';
  300. if (item.equation[key].length) {
  301. item.equation[key].forEach((equationItem) => {
  302. if (
  303. equationItem.type == 'symbol' ||
  304. equationItem.type == 'value'
  305. ) {
  306. value += equationItem.value;
  307. } else if (equationItem.type == 'id') {
  308. value += Number(data[equationItem.value]) || 0;
  309. }
  310. });
  311. if (units[key]?.decimalPlace) {
  312. if (units[key]?.takeValueMethod) {
  313. value =
  314. units[key]?.takeValueMethod == 1
  315. ? parseFloat(eval(value).toFixed(units[key].decimalPlace))
  316. : this.truncateToFixedManual(
  317. eval(value),
  318. units[key].decimalPlace
  319. );
  320. } else {
  321. value = parseFloat(
  322. eval(value).toFixed(units[key].decimalPlace)
  323. );
  324. }
  325. } else {
  326. value = parseFloat(eval(value).toFixed(2));
  327. }
  328. if (this.$refs['customTextRef' + item.id][0]) {
  329. this.$refs['customTextRef' + item.id][0].equationValue({
  330. domId: key,
  331. value
  332. });
  333. }
  334. }
  335. }
  336. });
  337. },
  338. truncateToFixedManual(num, decimalPlaces) {
  339. let factor = Math.pow(10, decimalPlaces);
  340. return Math.floor(num * factor) / factor;
  341. },
  342. getObjValue() {
  343. this.getValue();
  344. let data = {};
  345. let units = {};
  346. this.list.forEach((item) => {
  347. units = { ...item.units, ...units };
  348. if (item.type == 'customText') {
  349. data = { ...item.valueObj, ...data };
  350. } else {
  351. item.valueObj.columns.forEach((row) => {
  352. row.forEach((cell) => {
  353. data[cell.id] = cell.value;
  354. });
  355. });
  356. }
  357. });
  358. return { data: data || {}, units: units || {} };
  359. },
  360. setEquation() {
  361. this.getValue();
  362. this.idList = [];
  363. if (this.domObj.equation) {
  364. this.equationUnit.equation = JSON.parse(
  365. JSON.stringify(this.domObj.equation)
  366. );
  367. this.equationUnit.activeIndex = this.domObj.equation.length;
  368. } else {
  369. this.equationUnit.equation = [];
  370. }
  371. this.list.forEach((item) => {
  372. if (item.type == 'customText') {
  373. for (let key in item.valueObj) {
  374. this.idList.push(key);
  375. }
  376. } else {
  377. item.valueObj.columns.forEach((row) => {
  378. row.forEach((cell) => {
  379. this.idList.push(cell.id);
  380. });
  381. });
  382. }
  383. });
  384. this.visible = true;
  385. },
  386. delEquation() {
  387. this.equationUnit.equation = [];
  388. this.editInputChange('equation');
  389. },
  390. tagItemDelete(index) {
  391. this.equationUnit.equation.splice(index, 1);
  392. },
  393. formulaPartsTagClick(index, row) {
  394. if (!this.equationUnit.replaceOrAppend) {
  395. // 默认追加
  396. this.equationUnit_replaceOrAppend = 'append';
  397. }
  398. if (
  399. this.equationUnit.activeIndex &&
  400. this.equationUnit.activeIndex === index
  401. ) {
  402. this.$set(this.equationUnit, 'activeIndex', undefined);
  403. } else {
  404. this.$set(this.equationUnit, 'activeIndex', index);
  405. }
  406. },
  407. paramSelectChange(val, type) {
  408. if (!val) {
  409. return;
  410. }
  411. if (type == 'id') {
  412. this.setValue({ type: 'id', value: val });
  413. this.equationUnit.paramSelect = null;
  414. } else if (type == 'symbol') {
  415. this.setValue({ type: 'symbol', value: val });
  416. this.equationUnit.opSelect = null;
  417. } else if (type == 'value') {
  418. this.setValue({ type: 'value', value: val });
  419. }
  420. },
  421. setValue(val) {
  422. if (this.equationUnit.activeIndex != undefined) {
  423. if (
  424. !this.equationUnit.replaceOrAppend ||
  425. this.equationUnit.replaceOrAppend === 'replace'
  426. ) {
  427. this.equationUnit.equation.splice(
  428. this.equationUnit.activeIndex,
  429. 1,
  430. val
  431. );
  432. } else if (this.equationUnit.replaceOrAppend === 'append') {
  433. this.equationUnit.equation.splice(
  434. this.equationUnit.activeIndex + 1,
  435. 0,
  436. val
  437. );
  438. // 追加后activeIndex后移一位
  439. this.$set(
  440. this.equationUnit,
  441. 'activeIndex',
  442. this.equationUnit.activeIndex + 1
  443. );
  444. }
  445. } else {
  446. this.equationUnit.equation.push(val);
  447. }
  448. },
  449. init(list) {
  450. this.list = JSON.parse(list);
  451. this.editShow = false;
  452. if (this.list.length) {
  453. this.$nextTick(() => {
  454. this.list.forEach((item) => {
  455. this.$refs['customTextRef' + item.id][0].init({
  456. form: item.value,
  457. valueObj: item.valueObj,
  458. equation: item.equation,
  459. units: item.units
  460. });
  461. });
  462. });
  463. }
  464. },
  465. editShowFn({ templateDivRef, domObj }) {
  466. if (!this.edit) {
  467. return;
  468. }
  469. this.templateDivRef = templateDivRef;
  470. this.$set(this, 'domObj', domObj);
  471. this.editShow = true;
  472. },
  473. editInputChange(type) {
  474. if (type == 'equation') {
  475. this.visible = false;
  476. this.domObj.equation = JSON.parse(
  477. JSON.stringify(this.equationUnit.equation)
  478. );
  479. }
  480. this.$nextTick(() => {
  481. this.$refs[this.templateDivRef][0].editInputChange(this.domObj);
  482. });
  483. },
  484. del(id) {
  485. this.list = this.list.filter((item) => item.id != id);
  486. },
  487. addHtml(type) {
  488. this.list.push({
  489. id: generateRandomString(6),
  490. type: type,
  491. value: '',
  492. valueObj: {}
  493. });
  494. },
  495. copy(data) {
  496. console.log(data, 'asdasd');
  497. let id = generateRandomString(6);
  498. this.list.push({
  499. id,
  500. type: 'customTable',
  501. ...data
  502. });
  503. this.$nextTick(() => {
  504. this.$refs['customTextRef' + id][0].init({
  505. form: null,
  506. ...data
  507. });
  508. });
  509. },
  510. getValue() {
  511. this.list.forEach((item, index) => {
  512. let { form, valueObj, equation, units } =
  513. this.$refs['customTextRef' + item.id][0].getValue();
  514. this.$set(this.list[index], 'value', form);
  515. this.$set(this.list[index], 'valueObj', valueObj);
  516. this.$set(this.list[index], 'equation', equation);
  517. this.$set(this.list[index], 'units', units);
  518. });
  519. return this.list;
  520. }
  521. }
  522. };
  523. </script>
  524. <style scoped lang="scss">
  525. .listItem {
  526. padding: 5px;
  527. padding-top: 15px;
  528. border: solid 1px #f1f1f1;
  529. display: flex;
  530. width: 100%;
  531. align-items: center;
  532. margin-top: 3px;
  533. position: relative;
  534. }
  535. .sort-handle {
  536. font-size: 16px;
  537. position: absolute;
  538. left: 0;
  539. top: 0;
  540. }
  541. .move {
  542. cursor: move;
  543. }
  544. .delete {
  545. cursor: pointer;
  546. left: 20px;
  547. color: #f56c6c;
  548. }
  549. .listItem:hover {
  550. .sort-handle {
  551. display: block !important;
  552. }
  553. }
  554. </style>