settings.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // 引入fast-crud
  2. import {FastCrud, useTypes} from '@fast-crud/fast-crud';
  3. const {getType} = useTypes();
  4. import '@fast-crud/fast-crud/dist/style.css';
  5. import {setLogger} from '@fast-crud/fast-crud';
  6. import {getBaseURL} from '/@/utils/baseUrl';
  7. // element
  8. import ui from '@fast-crud/ui-element';
  9. import {request} from '/@/utils/service';
  10. //扩展包
  11. import {FsExtendsEditor, FsExtendsUploader } from '@fast-crud/fast-extends';
  12. import '@fast-crud/fast-extends/dist/style.css';
  13. import {successNotification} from '/@/utils/message';
  14. import XEUtils from "xe-utils";
  15. export default {
  16. async install(app: any, options: any) {
  17. // 先安装ui
  18. app.use(ui);
  19. // 然后安装FastCrud
  20. app.use(FastCrud, {
  21. //i18n, //i18n配置,可选,默认使用中文,具体用法请看demo里的 src/i18n/index.js 文件
  22. // 此处配置公共的dictRequest(字典请求)
  23. async dictRequest({dict,url}: any) {
  24. const {isTree} = dict
  25. //根据dict的url,异步返回一个字典数组
  26. return await request({url: url, params: dict.params || {}}).then((res: any) => {
  27. if (isTree) {
  28. return XEUtils.toArrayTree(res.data, {parentKey: 'parent'})
  29. }
  30. return res.data
  31. });
  32. },
  33. //公共crud配置
  34. commonOptions() {
  35. return {
  36. request: {
  37. //接口请求配置
  38. //你项目后台接口大概率与fast-crud所需要的返回结构不一致,所以需要配置此项
  39. //请参考文档http://fast-crud.docmirror.cn/api/crud-options/request.html
  40. transformQuery: ({page, form, sort}: any) => {
  41. if (sort.asc !== undefined) {
  42. form['ordering'] = `${sort.asc ? '' : '-'}${sort.prop}`;
  43. }
  44. //转换为你pageRequest所需要的请求参数结构
  45. return {page: page.currentPage, limit: page.pageSize, ...form};
  46. },
  47. transformRes: ({res}: any) => {
  48. //将pageRequest的返回数据,转换为fast-crud所需要的格式
  49. //return {records,currentPage,pageSize,total};
  50. return {records: res.data, currentPage: res.page, pageSize: res.limit, total: res.total};
  51. },
  52. },
  53. form: {
  54. afterSubmit(ctx: any) {
  55. // 增加crud提示
  56. if (ctx.res.code == 2000) {
  57. successNotification(ctx.res.msg);
  58. }
  59. },
  60. },
  61. /* search: {
  62. layout: 'multi-line',
  63. collapse: true,
  64. col: {
  65. span: 4,
  66. },
  67. options: {
  68. labelCol: {
  69. style: {
  70. width: '100px',
  71. },
  72. },
  73. },
  74. }, */
  75. };
  76. },
  77. logger: { off: { tableColumns: false } }
  78. });
  79. //富文本
  80. app.use(FsExtendsEditor, {
  81. wangEditor: {
  82. width: 300,
  83. },
  84. });
  85. // 文件上传
  86. app.use(FsExtendsUploader, {
  87. defaultType: "form",
  88. form: {
  89. action: `/api/system/file/`,
  90. name: "file",
  91. withCredentials: false,
  92. uploadRequest: async ({ action, file, onProgress }: { action: string; file: any; onProgress: Function }) => {
  93. // @ts-ignore
  94. const data = new FormData();
  95. data.append("file", file);
  96. return await request({
  97. url: action,
  98. method: "post",
  99. timeout: 60000,
  100. headers: {
  101. "Content-Type": "multipart/form-data"
  102. },
  103. data,
  104. onUploadProgress: (p: any) => {
  105. onProgress({percent: Math.round((p.loaded / p.total) * 100)});
  106. }
  107. });
  108. },
  109. successHandle(ret: any) {
  110. // 上传完成后的结果处理, 此处应返回格式为{url:xxx,key:xxx}
  111. return {
  112. url: getBaseURL(ret.data.url),
  113. key: ret.data.id,
  114. ...ret.data
  115. };
  116. }
  117. },
  118. valueBuilder(context: any){
  119. const { row, key } = context
  120. return getBaseURL(row[key])
  121. }
  122. })
  123. setLogger({level: 'error'});
  124. // 设置自动染色
  125. const dictComponentList = ['dict-cascader', 'dict-checkbox', 'dict-radio', 'dict-select', 'dict-switch', 'dict-tree'];
  126. dictComponentList.forEach((val) => {
  127. getType(val).column.component.color = 'auto';
  128. getType(val).column.align = 'center';
  129. });
  130. // 设置placeholder 的默认值
  131. const placeholderComponentList = [
  132. {key: 'text', placeholder: "请输入"},
  133. {key: 'textarea', placeholder: "请输入"},
  134. {key: 'input', placeholder: "请输入"},
  135. {key: 'password', placeholder: "请输入"}
  136. ]
  137. placeholderComponentList.forEach((val) => {
  138. if (getType(val.key)?.search?.component) {
  139. getType(val.key).search.component.placeholder = val.placeholder;
  140. } else if (getType(val.key)?.search) {
  141. getType(val.key).search.component = {placeholder: val.placeholder};
  142. }
  143. if (getType(val.key)?.form?.component) {
  144. getType(val.key).form.component.placeholder = val.placeholder;
  145. } else if (getType(val.key)?.form) {
  146. getType(val.key).form.component = {placeholder: val.placeholder};
  147. }
  148. if (getType(val.key)?.column?.align) {
  149. getType(val.key).column.align = 'center'
  150. } else if (getType(val.key)?.column) {
  151. getType(val.key).column = {align: 'center'};
  152. } else if (getType(val.key)) {
  153. getType(val.key).column = {align: 'center'};
  154. }
  155. });
  156. },
  157. };