crud.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {mapGetters} from "vuex";
  2. export default (app, option = {}) => {
  3. const mixins = {
  4. data() {
  5. return {
  6. selectionList: [],
  7. data: [],
  8. form: {},
  9. params: {},
  10. loading: false,
  11. api: require(`@/api/${option.name}`),
  12. option: require(`@/option/${option.name}`).default,
  13. page: {
  14. pageSizes: [10, 30, 50, 100, 200],
  15. pageSize: 10
  16. },
  17. }
  18. },
  19. computed: {
  20. ...mapGetters(['userInfo', 'permission', 'roles']),
  21. ids() {
  22. const ids = [];
  23. this.selectionList.forEach(ele => {
  24. ids.push(ele[this.rowKey]);
  25. });
  26. return ids.join(",");
  27. },
  28. bindVal() {
  29. return {
  30. ref: 'crud',
  31. option: this.option,
  32. data: this.data,
  33. tableLoading: this.loading
  34. }
  35. },
  36. onEvent() {
  37. return {
  38. 'on-load': this.getList,
  39. 'row-save': this.rowSave,
  40. 'row-update': this.rowUpdate,
  41. 'row-del': this.rowDel,
  42. 'selection-change': this.selectionChange,
  43. 'refresh-change': this.refreshChange,
  44. 'date-change': this.dateChange,
  45. 'search-change': this.searchChange,
  46. 'search-reset': this.searchChange
  47. }
  48. },
  49. rowKey() {
  50. return this.option.rowKey || option.rowKey || 'id'
  51. }
  52. },
  53. methods: {
  54. getList() {
  55. const callback = () => {
  56. this.loading = true;
  57. this.api[option.list || 'getList'](this.page.currentPage, this.page.pageSize, this.params).then(res => {
  58. let data;
  59. if (option.res) {
  60. data = option.res(res.data);
  61. } else {
  62. data = res.data.data;
  63. }
  64. this.page.total = data[option.total || 'total'] || 0;
  65. this.data = data[option.records || 'records'];
  66. if (this.listAfter) {
  67. this.listAfter(data);
  68. }
  69. this.loading = false;
  70. })
  71. }
  72. if (this.listBefore) {
  73. this.listBefore();
  74. }
  75. callback();
  76. },
  77. rowSave(row, done, loading) {
  78. const callback = () => {
  79. delete this.form.params;
  80. this.api[option.add || 'add'](this.form).then((data) => {
  81. this.getList();
  82. if (this.addAfter) {
  83. this.addAfter(data);
  84. } else {
  85. this.$message.success('新增成功');
  86. }
  87. done();
  88. }).catch(() => {
  89. loading();
  90. })
  91. }
  92. if (this.addBefore) {
  93. this.addBefore();
  94. }
  95. callback();
  96. },
  97. rowUpdate(row, index, done, loading) {
  98. const callback = () => {
  99. delete this.form.params;
  100. this.api[option.update || 'update'](this.form).then((data) => {
  101. this.getList();
  102. if (this.updateAfter) {
  103. this.updateAfter(data);
  104. } else {
  105. this.$message.success('更新成功');
  106. }
  107. done();
  108. }).catch(() => {
  109. loading();
  110. })
  111. }
  112. if (this.updateBefore) {
  113. this.updateBefore();
  114. }
  115. callback();
  116. },
  117. rowDel(row, index) {
  118. const callback = () => {
  119. this.api[option.del || 'remove'](row[this.rowKey], row).then((data) => {
  120. this.getList();
  121. if (this.delAfter) {
  122. this.delAfter(data, row, index)
  123. } else {
  124. this.$message.success('删除成功');
  125. }
  126. })
  127. }
  128. if (this.delBefore) {
  129. this.delBefore();
  130. callback();
  131. } else {
  132. this.$confirm('确定将选择数据删除?', '提示', {
  133. confirmButtonText: '确定',
  134. cancelButtonText: '取消',
  135. type: 'warning'
  136. }).then(() => {
  137. callback();
  138. })
  139. }
  140. },
  141. handleDelete() {
  142. if (this.selectionList.length === 0) {
  143. this.$message.warning("请选择至少一条数据");
  144. return;
  145. }
  146. this.$confirm("确定将选择数据删除?", {
  147. confirmButtonText: "确定",
  148. cancelButtonText: "取消",
  149. type: "warning"
  150. })
  151. .then(() => {
  152. this.api[option.del || 'remove'](this.ids).then((data) => {
  153. this.getList();
  154. if (this.delMultiAfter) {
  155. this.delMultiAfter(data, this.ids);
  156. } else {
  157. this.$message.success('删除成功');
  158. }
  159. });
  160. });
  161. },
  162. searchChange(params, done) {
  163. if (done) done();
  164. if (this.validatenull(params)) {
  165. Object.keys(this.params).forEach(ele => {
  166. if (!['createTime_dategt', 'createTime_datelt'].includes(ele)) {
  167. delete this.params[ele];
  168. }
  169. })
  170. } else {
  171. Object.keys(params).forEach(ele => {
  172. if (this.validatenull(params[ele])) {
  173. delete this.params[ele];
  174. delete params[ele];
  175. }
  176. })
  177. }
  178. this.params = Object.assign(this.params, params);
  179. this.page.currentPage = 1;
  180. this.getList();
  181. },
  182. dateChange(date) {
  183. if (date) {
  184. this.params.createTime_dategt = date[0];
  185. this.params.createTime_datelt = date[1];
  186. } else {
  187. delete this.params.createTime_dategt;
  188. delete this.params.createTime_datelt;
  189. }
  190. this.page.currentPage = 1;
  191. this.getList();
  192. },
  193. selectionChange(list) {
  194. this.selectionList = list;
  195. },
  196. selectionClear() {
  197. this.selectionList = [];
  198. this.$refs.crud.toggleSelection();
  199. },
  200. refreshChange() {
  201. this.getList();
  202. }
  203. }
  204. }
  205. app.mixins = app.mixins || [];
  206. app.mixins.push(mixins);
  207. return app;
  208. }