crud.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. option: {
  54. handler() {
  55. this.init();
  56. },
  57. deep: true
  58. }
  59. },
  60. data() {
  61. return {
  62. DIC: {},
  63. dicCascaderList: []
  64. };
  65. },
  66. created() {
  67. this.init();
  68. },
  69. computed: {
  70. isMediumSize() {
  71. return this.controlSize === 'medium' ? 'small' : this.controlSize;
  72. },
  73. controlSize() {
  74. return this.tableOption.size || (this.$AVUE || {}).size || 'medium';
  75. }
  76. },
  77. methods: {
  78. init() {
  79. // 初始化工具
  80. this.initFun();
  81. this.tableOption = this.option;
  82. const dicFlag = this.vaildData(this.tableOption.dicFlag, true);
  83. // 规则初始化
  84. this.rulesInit();
  85. // 初始化字典
  86. if (dicFlag) this.dicInit();
  87. else this.DIC = this.tableOption.dicData;
  88. // 初始化表单formInitVal
  89. this.formInit();
  90. },
  91. dicInit() {
  92. let locaDic = this.tableOption.dicData || {};
  93. this.columnOption.forEach(ele => {
  94. if (this.vaildData(ele.dicFlag, true)) {
  95. if (!validatenull(ele.dicUrl)) {
  96. this.dicCascaderList.push({
  97. dicUrl: ele.dicUrl,
  98. dicData: ele.dicData
  99. });
  100. } else if (!validatenull(this.tableOption.dicUrl) && typeof ele.dicData === 'string') {
  101. this.dicCascaderList.push({
  102. dicUrl: this.tableOption.dicUrl,
  103. dicData: ele.dicData
  104. });
  105. }
  106. }
  107. });
  108. this.GetDic().then(data => {
  109. this.DIC = Object.assign({}, locaDic, data);
  110. });
  111. },
  112. vaildData(val, dafult) {
  113. if (typeof val === 'boolean') {
  114. return val;
  115. }
  116. return !validatenull(val) ? val : dafult;
  117. },
  118. GetDicByType(href) {
  119. return new Promise((resolve) => {
  120. this.$http.get(href).then(function(res) {
  121. // 降级处理
  122. const list = res.data;
  123. if (!validatenull(list.data)) {
  124. resolve(list.data instanceof Array ? list.data : []);
  125. } else if (!validatenull(list)) {
  126. resolve(list instanceof Array ? list : []);
  127. } else {
  128. resolve([]);
  129. }
  130. });
  131. });
  132. },
  133. GetDic() {
  134. return new Promise((resolve) => {
  135. let result = [];
  136. let dicData = {};
  137. let cascaderList = Object.assign([], this.dicCascaderList);
  138. if (validatenull(cascaderList)) resolve({});
  139. cascaderList.forEach(ele => {
  140. result.push(new Promise((resolve) => {
  141. this.GetDicByType(`${ele.dicUrl.replace('{{key}}', ele.dicData)}`).then(function(res) {
  142. resolve(res);
  143. });
  144. }));
  145. });
  146. Promise.all(result).then(data => {
  147. cascaderList.forEach((ele, index) => {
  148. dicData[ele.dicData] = data[index];
  149. });
  150. resolve(dicData);
  151. });
  152. });
  153. },
  154. initFun() {
  155. Object.keys(utils).forEach(key => {
  156. this[key] = utils[key];
  157. });
  158. }
  159. }
  160. };
  161. }