JeecgListMixin.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * 新增修改完成调用 modalFormOk方法 编辑弹框组件ref定义为modalForm
  3. * 高级查询按钮调用 superQuery方法 高级查询组件ref定义为superQueryModal
  4. * data中url定义 list为查询列表 delete为删除单条记录 deleteBatch为批量删除
  5. */
  6. import { filterObj } from '@/utils/util';
  7. import { deleteAction, getAction,downFile } from '@/api/manage'
  8. import Vue from 'vue'
  9. import { ACCESS_TOKEN } from "@/store/mutation-types"
  10. export const JeecgListMixin = {
  11. data(){
  12. return {
  13. //token header
  14. tokenHeader: {'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)},
  15. /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
  16. queryParam: {},
  17. /* 数据源 */
  18. dataSource:[],
  19. /* 分页参数 */
  20. ipagination:{
  21. current: 1,
  22. pageSize: 10,
  23. pageSizeOptions: ['10', '20', '30'],
  24. showTotal: (total, range) => {
  25. return range[0] + "-" + range[1] + " 共" + total + "条"
  26. },
  27. showQuickJumper: true,
  28. showSizeChanger: true,
  29. total: 0
  30. },
  31. /* 排序参数 */
  32. isorter:{
  33. column: 'createTime',
  34. order: 'desc',
  35. },
  36. /* 筛选参数 */
  37. filters: {},
  38. /* table加载状态 */
  39. loading:false,
  40. /* table选中keys*/
  41. selectedRowKeys: [],
  42. /* table选中records*/
  43. selectionRows: [],
  44. /* 查询折叠 */
  45. toggleSearchStatus:false,
  46. /* 高级查询条件生效状态 */
  47. superQueryFlag:false,
  48. /* 高级查询条件 */
  49. superQueryParams:""
  50. }
  51. },
  52. computed:{
  53. scroll:function(){
  54. var width = window.innerWidth;
  55. //ant-table
  56. let $antTable = window.document.getElementsByClassName("ant-table");
  57. if ($antTable[0]){
  58. width = $antTable[0].clientWidth;
  59. }
  60. console.log("$antTable",$antTable)
  61. return {
  62. // x:'max-content',
  63. x:width,
  64. y:window.innerHeight/2,
  65. }
  66. },
  67. innerHeight:function(){
  68. var innerHeight = window.innerHeight;
  69. return innerHeight;
  70. },
  71. },
  72. created() {
  73. if(!this.disableMixinCreated){
  74. console.log(' -- mixin created -- ')
  75. this.loadData();
  76. //初始化字典配置 在自己页面定义
  77. this.initDictConfig();
  78. }
  79. },
  80. methods:{
  81. loadData(arg) {
  82. if(!this.url.list){
  83. this.$message.error("请设置url.list属性!")
  84. return
  85. }
  86. //加载数据 若传入参数1则加载第一页的内容
  87. if (arg === 1) {
  88. this.ipagination.current = 1;
  89. }
  90. var params = this.getQueryParams();//查询条件
  91. this.loading = true;
  92. getAction(this.url.list, params).then((res) => {
  93. if (res.success) {
  94. this.dataSource = res.result.records;
  95. this.ipagination.total = res.result.total;
  96. }
  97. if(res.code===510){
  98. this.$message.warning(res.message)
  99. }
  100. this.loading = false;
  101. })
  102. },
  103. initDictConfig(){
  104. console.log("--这是一个假的方法!")
  105. },
  106. handleSuperQuery(arg) {
  107. //高级查询方法
  108. if(!arg){
  109. this.superQueryParams=''
  110. this.superQueryFlag = false
  111. }else{
  112. this.superQueryFlag = true
  113. this.superQueryParams=JSON.stringify(arg)
  114. }
  115. this.loadData()
  116. },
  117. getQueryParams() {
  118. //获取查询条件
  119. let sqp = {}
  120. if(this.superQueryParams){
  121. sqp['superQueryParams']=encodeURI(this.superQueryParams)
  122. }
  123. var param = Object.assign(sqp, this.queryParam, this.isorter ,this.filters);
  124. param.field = this.getQueryField();
  125. param.pageNo = this.ipagination.current;
  126. param.pageSize = this.ipagination.pageSize;
  127. return filterObj(param);
  128. },
  129. getQueryField() {
  130. //TODO 字段权限控制
  131. var str = "id,";
  132. this.columns.forEach(function (value) {
  133. str += "," + value.dataIndex;
  134. });
  135. return str;
  136. },
  137. onSelectChange(selectedRowKeys, selectionRows) {
  138. this.selectedRowKeys = selectedRowKeys;
  139. this.selectionRows = selectionRows;
  140. },
  141. onClearSelected() {
  142. this.selectedRowKeys = [];
  143. this.selectionRows = [];
  144. },
  145. searchQuery() {
  146. this.loadData(1);
  147. },
  148. superQuery() {
  149. this.$refs.superQueryModal.show();
  150. },
  151. searchReset() {
  152. this.queryParam = {}
  153. this.loadData(1);
  154. },
  155. batchDel: function () {
  156. if(!this.url.deleteBatch){
  157. this.$message.error("请设置url.deleteBatch属性!")
  158. return
  159. }
  160. if (this.selectedRowKeys.length <= 0) {
  161. this.$message.warning('请选择一条记录!');
  162. return;
  163. } else {
  164. var ids = "";
  165. for (var a = 0; a < this.selectedRowKeys.length; a++) {
  166. ids += this.selectedRowKeys[a] + ",";
  167. }
  168. var that = this;
  169. this.$confirm({
  170. title: "确认删除",
  171. content: "是否删除选中数据?",
  172. onOk: function () {
  173. that.loading = true;
  174. deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
  175. if (res.success) {
  176. that.$message.success(res.message);
  177. that.loadData();
  178. that.onClearSelected();
  179. } else {
  180. that.$message.warning(res.message);
  181. }
  182. }).finally(() => {
  183. that.loading = false;
  184. });
  185. }
  186. });
  187. }
  188. },
  189. handleDelete: function (id) {
  190. if(!this.url.delete){
  191. this.$message.error("请设置url.delete属性!")
  192. return
  193. }
  194. var that = this;
  195. deleteAction(that.url.delete, {id: id}).then((res) => {
  196. if (res.success) {
  197. that.$message.success(res.message);
  198. that.loadData();
  199. } else {
  200. that.$message.warning(res.message);
  201. }
  202. });
  203. },
  204. handleEdit: function (record) {
  205. this.$refs.modalForm.edit(record);
  206. this.$refs.modalForm.title = "编辑";
  207. this.$refs.modalForm.disableSubmit = false;
  208. },
  209. handleAdd: function () {
  210. this.$refs.modalForm.add();
  211. this.$refs.modalForm.title = "新增";
  212. this.$refs.modalForm.disableSubmit = false;
  213. },
  214. handleTableChange(pagination, filters, sorter) {
  215. //分页、排序、筛选变化时触发
  216. //TODO 筛选
  217. if (Object.keys(sorter).length > 0) {
  218. this.isorter.column = sorter.field;
  219. this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
  220. }
  221. this.ipagination = pagination;
  222. this.loadData();
  223. },
  224. handleToggleSearch(){
  225. this.toggleSearchStatus = !this.toggleSearchStatus;
  226. },
  227. modalFormOk() {
  228. // 新增/修改 成功时,重载列表
  229. this.loadData();
  230. },
  231. handleDetail:function(record,state){
  232. this.$refs.modalForm.edit(record,state);
  233. this.$refs.modalForm.title="详情";
  234. this.$refs.modalForm.disableSubmit = true;
  235. },
  236. /* 导出 */
  237. handleExportXls2(){
  238. let paramsStr = encodeURI(JSON.stringify(this.getQueryParams()));
  239. let url = `${window._CONFIG['domianURL']}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`;
  240. window.location.href = url;
  241. },
  242. handleExportXls(fileName){
  243. if(!fileName || typeof fileName != "string"){
  244. fileName = "导出文件"
  245. }
  246. let param = {...this.queryParam};
  247. if(this.selectedRowKeys && this.selectedRowKeys.length>0){
  248. param['selections'] = this.selectedRowKeys.join(",")
  249. }
  250. console.log("导出参数",param)
  251. downFile(this.url.exportXlsUrl,param).then((data)=>{
  252. if (!data) {
  253. this.$message.warning("文件下载失败")
  254. return
  255. }
  256. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  257. window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
  258. }else{
  259. let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
  260. let link = document.createElement('a')
  261. link.style.display = 'none'
  262. link.href = url
  263. link.setAttribute('download', fileName+'.xls')
  264. document.body.appendChild(link)
  265. link.click()
  266. document.body.removeChild(link); //下载完成移除元素
  267. window.URL.revokeObjectURL(url); //释放掉blob对象
  268. }
  269. })
  270. },
  271. /* 导入 */
  272. handleImportExcel(info){
  273. if (info.file.status !== 'uploading') {
  274. console.log(info.file, info.fileList);
  275. }
  276. if (info.file.status === 'done') {
  277. if (info.file.response.success) {
  278. // this.$message.success(`${info.file.name} 文件上传成功`);
  279. if (info.file.response.code === 201) {
  280. let { message, result: { msg, fileUrl, fileName } } = info.file.response
  281. let href = window._CONFIG['domianURL'] + fileUrl
  282. this.$warning({
  283. title: message,
  284. content: (
  285. <div>
  286. <span>{msg}</span><br/>
  287. <span>具体详情请 <a href={href} target="_blank" download={fileName}>点击下载</a> </span>
  288. </div>
  289. )
  290. })
  291. } else {
  292. this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
  293. }
  294. this.loadData()
  295. } else {
  296. this.$message.error(`${info.file.name} ${info.file.response.message}.`);
  297. }
  298. } else if (info.file.status === 'error') {
  299. this.$message.error(`文件上传失败: ${info.file.msg} `);
  300. }
  301. },
  302. /* 图片预览 */
  303. getImgView(text){
  304. if(text && text.indexOf(",")>0){
  305. text = text.substring(0,text.indexOf(","))
  306. }
  307. return window._CONFIG['staticDomainURL']+"/"+text
  308. },
  309. /* 文件下载 */
  310. uploadFile(text){
  311. if(!text){
  312. this.$message.warning("未知的文件")
  313. return;
  314. }
  315. if(text.indexOf(",")>0){
  316. text = text.substring(0,text.indexOf(","))
  317. }
  318. window.open(window._CONFIG['staticDomainURL']+ "/"+text);
  319. },
  320. }
  321. }