useListPage.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { reactive, ref, Ref, unref } from 'vue';
  2. import { merge } from 'lodash-es';
  3. import { DynamicProps } from '/#/utils';
  4. import { BasicTableProps, TableActionType, useTable } from '/@/components/Table';
  5. import { ColEx } from '/@/components/Form/src/types';
  6. import { FormActionType } from '/@/components/Form';
  7. import { useMessage } from '/@/hooks/web/useMessage';
  8. import { useMethods } from '/@/hooks/system/useMethods';
  9. import { useDesign } from '/@/hooks/web/useDesign';
  10. import { filterObj } from '/@/utils/common/compUtils';
  11. const { handleExportXls, handleImportXls } = useMethods();
  12. // 定义 useListPage 方法所需参数
  13. interface ListPageOptions {
  14. // 样式作用域范围
  15. designScope?: string;
  16. // 【必填】表格参数配置
  17. tableProps: TableProps;
  18. // 分页
  19. pagination?: boolean;
  20. // 导出配置
  21. exportConfig?: {
  22. url: string | (() => string);
  23. // 导出文件名
  24. name?: string | (() => string);
  25. //导出参数
  26. params?: object;
  27. };
  28. // 导入配置
  29. importConfig?: {
  30. //update-begin-author:taoyan date:20220507 for: erp代码生成 子表 导入地址是动态的
  31. url: string | (() => string);
  32. //update-end-author:taoyan date:20220507 for: erp代码生成 子表 导入地址是动态的
  33. // 导出成功后的回调
  34. success?: (fileInfo?: any) => void;
  35. };
  36. }
  37. interface IDoRequestOptions {
  38. // 是否显示确认对话框,默认 true
  39. confirm?: boolean;
  40. // 是否自动刷新表格,默认 true
  41. reload?: boolean;
  42. // 是否自动清空选择,默认 true
  43. clearSelection?: boolean;
  44. }
  45. /**
  46. * listPage页面公共方法
  47. *
  48. * @param options
  49. */
  50. export function useListPage(options: ListPageOptions) {
  51. const $message = useMessage();
  52. let $design = {} as ReturnType<typeof useDesign>;
  53. if (options.designScope) {
  54. $design = useDesign(options.designScope);
  55. }
  56. const tableContext = useListTable(options.tableProps);
  57. const [, { getForm, reload, setLoading }, { selectedRowKeys }] = tableContext;
  58. // 导出 excel
  59. async function onExportXls(selectForm?) {
  60. debugger;
  61. //update-begin---author:wangshuai ---date:20220411 for:导出新增自定义参数------------
  62. const { url, name, params } = options?.exportConfig ?? {};
  63. const realUrl = typeof url === 'function' ? url() : url;
  64. if (realUrl) {
  65. const title = typeof name === 'function' ? name() : name;
  66. //update-begin-author:taoyan date:20220507 for: erp代码生成 子表 导出报错,原因未知-
  67. let paramsForm: any = {};
  68. try {
  69. if (selectForm) {
  70. paramsForm = selectForm;
  71. } else {
  72. paramsForm = await getForm().validate();
  73. }
  74. } catch (e) {
  75. console.error(e);
  76. }
  77. //update-end-author:taoyan date:20220507 for: erp代码生成 子表 导出报错,原因未知-
  78. //update-begin-author:liusq date:20230410 for:[/issues/409]导出功能没有按排序结果导出,设置导出默认排序,创建时间倒序
  79. if (!paramsForm?.column) {
  80. Object.assign(paramsForm, { column: 'createTime', order: 'desc' });
  81. }
  82. //update-begin-author:liusq date:20230410 for: [/issues/409]导出功能没有按排序结果导出,设置导出默认排序,创建时间倒序
  83. //如果参数不为空,则整合到一起
  84. //update-begin-author:taoyan date:20220507 for: erp代码生成 子表 导出动态设置mainId
  85. if (params) {
  86. Object.keys(params).map((k) => {
  87. const temp = (params as object)[k];
  88. if (temp) {
  89. paramsForm[k] = unref(temp);
  90. }
  91. });
  92. }
  93. //update-end-author:taoyan date:20220507 for: erp代码生成 子表 导出动态设置mainId
  94. if (selectedRowKeys.value && selectedRowKeys.value.length > 0) {
  95. paramsForm['selections'] = selectedRowKeys.value.join(',');
  96. }
  97. console.log();
  98. return handleExportXls(title as string, realUrl, filterObj(paramsForm));
  99. //update-end---author:wangshuai ---date:20220411 for:导出新增自定义参数--------------
  100. } else {
  101. $message.createMessage.warn('没有传递 exportConfig.url 参数');
  102. return Promise.reject();
  103. }
  104. }
  105. // 导入 excel
  106. function onImportXls(file) {
  107. const { url, success } = options?.importConfig ?? {};
  108. //update-begin-author:taoyan date:20220507 for: erp代码生成 子表 导入地址是动态的
  109. const realUrl = typeof url === 'function' ? url() : url;
  110. if (realUrl) {
  111. return handleImportXls(file, realUrl, success || reload);
  112. //update-end-author:taoyan date:20220507 for: erp代码生成 子表 导入地址是动态的
  113. } else {
  114. $message.createMessage.warn('没有传递 importConfig.url 参数');
  115. return Promise.reject();
  116. }
  117. }
  118. /**
  119. * 通用请求处理方法,可自动刷新表格,自动清空选择
  120. * @param api 请求api
  121. * @param options 是否显示确认框
  122. */
  123. function doRequest(api: () => Promise<any>, options?: IDoRequestOptions) {
  124. return new Promise((resolve, reject) => {
  125. const execute = async () => {
  126. try {
  127. setLoading(true);
  128. const res = await api();
  129. if (options?.reload ?? true) {
  130. reload();
  131. }
  132. if (options?.clearSelection ?? true) {
  133. selectedRowKeys.value = [];
  134. }
  135. resolve(res);
  136. } catch (e) {
  137. reject(e);
  138. } finally {
  139. setLoading(false);
  140. }
  141. };
  142. if (options?.confirm ?? true) {
  143. $message.createConfirm({
  144. iconType: 'warning',
  145. title: '删除',
  146. content: '确定要删除吗?',
  147. onOk: () => execute(),
  148. onCancel: () => reject(),
  149. });
  150. } else {
  151. execute();
  152. }
  153. });
  154. }
  155. /** 执行单个删除操作 */
  156. function doDeleteRecord(api: () => Promise<any>) {
  157. return doRequest(api, { confirm: false, clearSelection: false });
  158. }
  159. return {
  160. ...$design,
  161. ...$message,
  162. onExportXls,
  163. onImportXls,
  164. doRequest,
  165. doDeleteRecord,
  166. tableContext,
  167. };
  168. }
  169. // 定义表格所需参数
  170. type TableProps = Partial<DynamicProps<BasicTableProps>>;
  171. type UseTableMethod = TableActionType & {
  172. getForm: () => FormActionType;
  173. };
  174. /**
  175. * useListTable 列表页面标准表格参数
  176. *
  177. * @param tableProps 表格参数
  178. */
  179. export function useListTable(tableProps: TableProps): [
  180. (instance: TableActionType, formInstance: UseTableMethod) => void,
  181. TableActionType & {
  182. getForm: () => FormActionType;
  183. },
  184. {
  185. rowSelection: any;
  186. selectedRows: Ref<Recordable[]>;
  187. selectedRowKeys: Ref<any[]>;
  188. },
  189. ] {
  190. // 自适应列配置
  191. const adaptiveColProps: Partial<ColEx> = {
  192. xs: 24, // <576px
  193. sm: 12, // ≥576px
  194. md: 12, // ≥768px
  195. lg: 8, // ≥992px
  196. xl: 8, // ≥1200px
  197. xxl: 6, // ≥1600px
  198. };
  199. const defaultTableProps: TableProps = {
  200. rowKey: 'id',
  201. // 使用查询条件区域
  202. useSearchForm: true,
  203. // 查询条件区域配置
  204. formConfig: {
  205. // 紧凑模式
  206. compact: true,
  207. // label默认宽度
  208. // labelWidth: 120,
  209. // 按下回车后自动提交
  210. autoSubmitOnEnter: true,
  211. // 默认 row 配置
  212. rowProps: { gutter: 8 },
  213. // 默认 col 配置
  214. baseColProps: {
  215. ...adaptiveColProps,
  216. },
  217. labelCol: {
  218. xs: 24,
  219. sm: 8,
  220. md: 6,
  221. lg: 8,
  222. xl: 6,
  223. xxl: 6,
  224. },
  225. wrapperCol: {},
  226. // 是否显示 展开/收起 按钮
  227. showAdvancedButton: true,
  228. // 超过指定列数默认折叠
  229. autoAdvancedCol: 3,
  230. // 操作按钮配置
  231. actionColOptions: {
  232. ...adaptiveColProps,
  233. style: { textAlign: 'left' },
  234. },
  235. },
  236. // 斑马纹
  237. striped: false,
  238. // 是否可以自适应高度
  239. canResize: true,
  240. // 表格最小高度
  241. minHeight: 500,
  242. // 点击行选中
  243. clickToRowSelect: false,
  244. // 是否显示边框
  245. bordered: true,
  246. // 是否显示序号列
  247. showIndexColumn: false,
  248. // 显示表格设置
  249. showTableSetting: true,
  250. // 表格全屏设置
  251. tableSetting: {
  252. fullScreen: false,
  253. },
  254. // 是否显示操作列
  255. showActionColumn: true,
  256. // 操作列
  257. actionColumn: {
  258. width: 120,
  259. title: '操作',
  260. //是否锁定操作列取值 right ,left,false
  261. fixed: false,
  262. dataIndex: 'action',
  263. slots: { customRender: 'action' },
  264. },
  265. };
  266. // 合并用户个性化配置
  267. if (tableProps) {
  268. // merge 方法可深度合并对象
  269. merge(defaultTableProps, tableProps);
  270. }
  271. // 发送请求之前调用的方法
  272. function beforeFetch(params) {
  273. // 默认以 createTime 降序排序
  274. return Object.assign({ column: 'createTime' }, params);
  275. }
  276. // 合并方法
  277. Object.assign(defaultTableProps, { beforeFetch });
  278. if (typeof tableProps.beforeFetch === 'function') {
  279. defaultTableProps.beforeFetch = function (params) {
  280. params = beforeFetch(params);
  281. // @ts-ignore
  282. tableProps.beforeFetch(params);
  283. return params;
  284. };
  285. }
  286. // 当前选择的行
  287. const selectedRowKeys = ref<any[]>([]);
  288. // 选择的行记录
  289. const selectedRows = ref<Recordable[]>([]);
  290. // 表格选择列配置
  291. const rowSelection: any = tableProps?.rowSelection ?? {};
  292. const defaultRowSelection = reactive({
  293. ...rowSelection,
  294. type: rowSelection.type ?? 'checkbox',
  295. // 选择列宽度,默认 50
  296. columnWidth: rowSelection.columnWidth ?? 50,
  297. selectedRows: selectedRows,
  298. selectedRowKeys: selectedRowKeys,
  299. onChange(...args) {
  300. selectedRowKeys.value = args[0];
  301. selectedRows.value = args[1];
  302. if (typeof rowSelection.onChange === 'function') {
  303. rowSelection.onChange(...args);
  304. }
  305. },
  306. });
  307. delete defaultTableProps.rowSelection;
  308. return [
  309. ...useTable(defaultTableProps),
  310. {
  311. selectedRows,
  312. selectedRowKeys,
  313. rowSelection: defaultRowSelection,
  314. },
  315. ];
  316. }