crud.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import * as utils from '../utils/util.js';
  2. import { validatenull } from '../utils/validate.js';
  3. import crudInput from '../crud/src/crud-input';
  4. import crudSelect from '../crud/src/crud-select';
  5. import crudRadio from '../crud/src/crud-radio';
  6. import crudCheckbox from '../crud/src/crud-checkbox';
  7. import crudCascader from '../crud/src/crud-cascader';
  8. import crudDate from '../crud/src/crud-date';
  9. import crudTime from '../crud/src/crud-time';
  10. import crudInputNumber from '../crud/src/crud-input-number';
  11. import crudSwitch from '../crud/src/crud-switch';
  12. import crudRate from '../crud/src/crud-rate';
  13. import crudUpload from '../crud/src/crud-upload';
  14. import crudSilder from '../crud/src/crud-silder';
  15. export default function() {
  16. return {
  17. props: {
  18. option: {
  19. type: Object,
  20. required: true,
  21. default: () => {
  22. return {};
  23. }
  24. }
  25. },
  26. components: {
  27. crudInput,
  28. crudSelect,
  29. crudRadio,
  30. crudCheckbox,
  31. crudDate,
  32. crudTime,
  33. crudCascader,
  34. crudInputNumber,
  35. crudSwitch,
  36. crudRate,
  37. crudUpload,
  38. crudSilder
  39. },
  40. watch: {
  41. tableForm: {
  42. handler() {
  43. this.$emit('input', this.tableForm);
  44. },
  45. deep: true
  46. },
  47. form: {
  48. handler() {
  49. this.$emit('input', this.form);
  50. },
  51. deep: true
  52. },
  53. value: {
  54. handler() {
  55. this.formVal();
  56. },
  57. deep: true
  58. },
  59. option: {
  60. handler() {
  61. this.init();
  62. },
  63. deep: true
  64. }
  65. },
  66. data() {
  67. return {
  68. DIC: {},
  69. dicCascaderList: []
  70. };
  71. },
  72. created() {
  73. this.init();
  74. },
  75. methods: {
  76. init() {
  77. // 初始化工具
  78. this.initFun();
  79. this.tableOption = this.deepClone(this.option);
  80. const dicFlag = this.vaildData(this.tableOption.dicFlag, true);
  81. // 规则初始化
  82. this.rulesInit();
  83. // 初始化字典
  84. if (dicFlag) this.dicInit();
  85. else this.DIC = this.tableOption.dicData;
  86. // 初始化表单formInitVal
  87. this.formInit();
  88. },
  89. dicInit() {
  90. let locaDic = this.tableOption.dicData || {};
  91. this.columnOption.forEach(ele => {
  92. if (this.vaildData(ele.dicFlag, true)) {
  93. if (!validatenull(ele.dicUrl)) {
  94. this.dicCascaderList.push({
  95. dicUrl: ele.dicUrl,
  96. dicData: ele.dicData
  97. });
  98. } else if (!validatenull(this.tableOption.dicUrl) && typeof ele.dicData === 'string') {
  99. this.dicCascaderList.push({
  100. dicUrl: this.tableOption.dicUrl,
  101. dicData: ele.dicData
  102. });
  103. }
  104. }
  105. });
  106. this.GetDic().then(data => {
  107. this.DIC = Object.assign({}, locaDic, data);
  108. });
  109. },
  110. vaildData(val, dafult) {
  111. if (typeof val === 'boolean') {
  112. return val;
  113. }
  114. return !validatenull(val) ? val : dafult;
  115. },
  116. GetDicByType(href) {
  117. return new Promise((resolve) => {
  118. this.$http.get(href).then(function(res) {
  119. // 降级处理
  120. const list = res.data;
  121. if (!validatenull(list.data)) {
  122. resolve(list.data instanceof Array ? list.data : []);
  123. } else if (!validatenull(list)) {
  124. resolve(list instanceof Array ? list : []);
  125. } else {
  126. resolve([]);
  127. }
  128. });
  129. });
  130. },
  131. GetDic() {
  132. return new Promise((resolve) => {
  133. let result = [];
  134. let dicData = {};
  135. let cascaderList = Object.assign([], this.dicCascaderList);
  136. if (validatenull(cascaderList)) resolve({});
  137. cascaderList.forEach(ele => {
  138. result.push(new Promise((resolve) => {
  139. this.GetDicByType(`${ele.dicUrl.replace('{{key}}', ele.dicData)}`).then(function(res) {
  140. resolve(res);
  141. });
  142. }));
  143. });
  144. Promise.all(result).then(data => {
  145. cascaderList.forEach((ele, index) => {
  146. dicData[ele.dicData] = data[index];
  147. });
  148. resolve(dicData);
  149. });
  150. });
  151. },
  152. initFun() {
  153. Object.keys(utils).forEach(key => {
  154. this[key] = utils[key];
  155. });
  156. }
  157. }
  158. };
  159. }