JeecgListMixin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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, getFileAccessHttpUrl } from '@/api/manage'
  8. import Vue from 'vue'
  9. // , TENANT_ID
  10. import { ACCESS_TOKEN } from '@/store/mutation-types'
  11. // import store from '@/store' //new
  12. export const JeecgListMixin = {
  13. data() {
  14. return {
  15. // token header BY-----LY 2022-04-28
  16. tokenHeader: { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) },
  17. /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
  18. queryParam: {},
  19. /* 数据源 */
  20. dataSource: [],
  21. /* 分页参数 */
  22. ipagination: {
  23. current: 1,
  24. pageSize: 10,
  25. pageSizeOptions: ['10', '20', '30'],
  26. showTotal: (total, range) => {
  27. return range[0] + '-' + range[1] + ' 共' + total + '条'
  28. },
  29. showQuickJumper: true,
  30. showSizeChanger: true,
  31. total: 0
  32. },
  33. /* 排序参数 */
  34. isorter: {
  35. column: 'createTime',
  36. order: 'desc'
  37. },
  38. /* 筛选参数 */
  39. filters: {},
  40. /* table加载状态 */
  41. loading: false,
  42. /* table选中keys */
  43. selectedRowKeys: [],
  44. /* table选中records */
  45. selectionRows: [],
  46. /* 查询折叠 */
  47. toggleSearchStatus: false,
  48. /* 高级查询条件生效状态 */
  49. superQueryFlag: false,
  50. /* 高级查询条件 */
  51. superQueryParams: '',
  52. /** 高级查询拼接方式 */
  53. superQueryMatchType: 'and'
  54. }
  55. },
  56. computed: {
  57. // token header
  58. // tokenHeader() {
  59. // let head = { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) }
  60. // let tenantid = Vue.ls.get(TENANT_ID)
  61. // if (tenantid) {
  62. // head['tenant-id'] = tenantid
  63. // }
  64. // return head
  65. // }
  66. scroll: function() {
  67. var width = window.innerWidth
  68. // ant-table
  69. let $antTable = window.document.getElementsByClassName('ant-table')
  70. if ($antTable[0]) {
  71. width = $antTable[0].clientWidth
  72. }
  73. console.log('$antTable', $antTable)
  74. return {
  75. // x:'max-content',
  76. x: width,
  77. y: window.innerHeight / 2
  78. }
  79. }
  80. },
  81. created() {
  82. if (!this.disableMixinCreated) {
  83. // console.log('this.disableMixinCreated:', this.disableMixinCreated)
  84. // console.log(' -- mixin created -- ')
  85. this.loadData()
  86. // 初始化字典配置 在自己页面定义
  87. this.initDictConfig()
  88. }
  89. },
  90. methods: {
  91. loadData(arg) {
  92. // console.log('List前面的arg', arg)
  93. if (this.url == undefined)
  94. return;
  95. if (!this.url.list) {
  96. console.log('111:', this.url)
  97. // console.log('this.url.list!!!!!', this.url.list)
  98. this.$message.error('请设置url.list属性!')
  99. return
  100. }
  101. // 加载数据 若传入参数1则加载第一页的内容
  102. if (arg === 1) {
  103. this.ipagination.current = 1
  104. }
  105. var params = this.getQueryParams() // 查询条件
  106. this.loading = true
  107. getAction(this.url.list, params)
  108. .then(res => {
  109. if (res.success) {
  110. // update-begin---author:zhangyafei Date:20201118 for:适配不分页的数据列表------------
  111. this.dataSource = res.result.records || res.result
  112. if (res.result.total) {
  113. this.ipagination.total = res.result.total
  114. } else {
  115. this.ipagination.total = 0
  116. }
  117. // update-end---author:zhangyafei Date:20201118 for:适配不分页的数据列表------------
  118. } else {
  119. this.$message.warning(res.message)
  120. }
  121. })
  122. .finally(() => {
  123. this.loading = false
  124. })
  125. },
  126. initDictConfig() {
  127. console.log('--这是一个假的方法!')
  128. },
  129. handleSuperQuery(params, matchType) {
  130. // 高级查询方法
  131. if (!params) {
  132. this.superQueryParams = ''
  133. this.superQueryFlag = false
  134. } else {
  135. this.superQueryFlag = true
  136. this.superQueryParams = JSON.stringify(params)
  137. this.superQueryMatchType = matchType
  138. }
  139. this.loadData(1)
  140. },
  141. getQueryParams() {
  142. // 获取查询条件
  143. let sqp = {}
  144. if (this.superQueryParams) {
  145. sqp['superQueryParams'] = encodeURI(this.superQueryParams)
  146. sqp['superQueryMatchType'] = this.superQueryMatchType
  147. }
  148. var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters)
  149. param.field = this.getQueryField()
  150. param.pageNo = this.ipagination.current
  151. param.pageSize = this.ipagination.pageSize
  152. return filterObj(param)
  153. },
  154. getQueryField() {
  155. // todo: 字段权限控制
  156. var str = 'id,'
  157. this.columns.forEach(function(value) {
  158. str += ',' + value.dataIndex
  159. })
  160. return str
  161. },
  162. onSelectChange(selectedRowKeys, selectionRows) {
  163. this.selectedRowKeys = selectedRowKeys
  164. this.selectionRows = selectionRows
  165. },
  166. onClearSelected() {
  167. this.selectedRowKeys = []
  168. this.selectionRows = []
  169. },
  170. searchQuery() {
  171. this.loadData(1)
  172. // 点击查询清空列表选中行
  173. // https://gitee.com/jeecg/jeecg-boot/issues/I4KTU1
  174. this.selectedRowKeys = []
  175. this.selectionRows = []
  176. },
  177. superQuery() {
  178. this.$refs.superQueryModal.show()
  179. },
  180. searchReset() {
  181. this.queryParam = {}
  182. this.loadData(1)
  183. },
  184. batchDel: function() {
  185. if (!this.url.deleteBatch) {
  186. this.$message.error('请设置url.deleteBatch属性!')
  187. return
  188. }
  189. if (this.selectedRowKeys.length <= 0) {
  190. this.$message.warning('请选择一条记录!')
  191. } else {
  192. var ids = ''
  193. for (var a = 0; a < this.selectedRowKeys.length; a++) {
  194. ids += this.selectedRowKeys[a] + ','
  195. }
  196. var that = this
  197. this.$confirm({
  198. title: '确认删除',
  199. content: '是否删除选中数据?',
  200. onOk: function() {
  201. that.loading = true
  202. deleteAction(that.url.deleteBatch, { ids: ids })
  203. .then(res => {
  204. if (res.success) {
  205. // 重新计算分页问题
  206. that.reCalculatePage(that.selectedRowKeys.length)
  207. that.$message.success(res.message)
  208. that.loadData()
  209. that.onClearSelected()
  210. } else {
  211. that.$message.warning(res.message)
  212. }
  213. })
  214. .finally(() => {
  215. that.loading = false
  216. })
  217. }
  218. })
  219. }
  220. },
  221. handleDelete: function(id) {
  222. if (!this.url.delete) {
  223. this.$message.error('请设置url.delete属性!')
  224. return
  225. }
  226. var that = this
  227. deleteAction(that.url.delete, { id: id }).then(res => {
  228. if (res.success) {
  229. // 重新计算分页问题
  230. that.reCalculatePage(1)
  231. that.$message.success(res.message)
  232. that.loadData()
  233. } else {
  234. that.$message.warning(res.message)
  235. }
  236. })
  237. },
  238. reCalculatePage(count) {
  239. // 总数量-count
  240. let total = this.ipagination.total - count
  241. // 获取删除后的分页数
  242. let currentIndex = Math.ceil(total / this.ipagination.pageSize)
  243. // 删除后的分页数<所在当前页
  244. if (currentIndex < this.ipagination.current) {
  245. this.ipagination.current = currentIndex
  246. }
  247. console.log('currentIndex', currentIndex)
  248. },
  249. handleEdit: function(record) {
  250. this.$refs.modalForm.edit(record)
  251. this.$refs.modalForm.title = '编辑'
  252. this.$refs.modalForm.disableSubmit = false
  253. },
  254. handleAdd: function() {
  255. this.$refs.modalForm.add()
  256. this.$refs.modalForm.title = '新增'
  257. this.$refs.modalForm.disableSubmit = false
  258. },
  259. handleTableChange(pagination, filters, sorter) {
  260. // 分页、排序、筛选变化时触发
  261. // todo: 筛选
  262. console.log(pagination)
  263. if (Object.keys(sorter).length > 0) {
  264. this.isorter.column = sorter.field
  265. this.isorter.order = sorter.order == 'ascend' ? 'asc' : 'desc'
  266. }
  267. this.ipagination = pagination
  268. this.loadData()
  269. },
  270. handleToggleSearch() {
  271. this.toggleSearchStatus = !this.toggleSearchStatus
  272. },
  273. // 给popup查询使用(查询区域不支持回填多个字段,限制只返回一个字段)
  274. getPopupField(fields) {
  275. return fields.split(',')[0]
  276. },
  277. // 对话框 @ok点击确定回调
  278. modalFormOk() {
  279. // 新增/修改 成功时,重载列表
  280. this.loadData()
  281. // 清空列表选中
  282. this.onClearSelected()
  283. },
  284. handleDetail: function(record) {
  285. this.$refs.modalForm.edit(record)
  286. this.$refs.modalForm.title = '详情'
  287. this.$refs.modalForm.disableSubmit = true
  288. },
  289. /* 导出 */
  290. handleExportXls2() {
  291. let paramsStr = encodeURI(JSON.stringify(this.getQueryParams()))
  292. let url = `${window._CONFIG['domianURL']}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`
  293. window.location.href = url
  294. },
  295. // 导出文件
  296. handleExportXls(fileName) {
  297. // console.log('染损');
  298. if (!fileName || typeof fileName !== 'string') {
  299. fileName = '导出文件'
  300. }
  301. let param = this.getQueryParams()
  302. if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
  303. param['selections'] = this.selectedRowKeys.join(',')
  304. }
  305. console.log('导出参数', param)
  306. downFile(this.url.exportXlsUrl, param).then(data => {
  307. if (!data) {
  308. this.$message.warning('文件下载失败')
  309. return
  310. }
  311. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  312. window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
  313. } else {
  314. let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
  315. let link = document.createElement('a')
  316. link.style.display = 'none'
  317. link.href = url
  318. link.setAttribute('download', fileName + '.xls')
  319. document.body.appendChild(link)
  320. link.click()
  321. document.body.removeChild(link) // 下载完成移除元素
  322. window.URL.revokeObjectURL(url) // 释放掉blob对象
  323. }
  324. })
  325. },
  326. /* 导入 */
  327. handleImportExcel(info) {
  328. this.loading = true
  329. if (info.file.status !== 'uploading') {
  330. console.log(info.file, info.fileList)
  331. }
  332. if (info.file.status === 'done') {
  333. this.loading = false
  334. if (info.file.response.success) {
  335. // this.$message.success(`${info.file.name} 文件上传成功`);
  336. if (info.file.response.code === 201) {
  337. let {
  338. message,
  339. result: { msg, fileUrl, fileName }
  340. } = info.file.response
  341. let href = window._CONFIG['domianURL'] + fileUrl
  342. this.$warning({
  343. title: message,
  344. content: (
  345. <div>
  346. <span>{msg}</span>
  347. <br />
  348. <span>
  349. 具体详情请{' '}
  350. <a href={href} target="_blank" download={fileName}>
  351. 点击下载
  352. </a>{' '}
  353. </span>
  354. </div>
  355. )
  356. })
  357. } else {
  358. this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
  359. }
  360. this.loadData()
  361. } else {
  362. this.$message.error(`${info.file.name} ${info.file.response.message}.`)
  363. }
  364. } else if (info.file.status === 'error') {
  365. this.loading = false
  366. if (info.file.response.status === 500) {
  367. let data = info.file.response
  368. const token = Vue.ls.get(ACCESS_TOKEN)
  369. if (token && data.message.includes('Token失效')) {
  370. this.$error({
  371. title: '登录已过期',
  372. content: '很抱歉,登录已过期,请重新登录',
  373. okText: '重新登录',
  374. mask: false,
  375. onOk: () => {
  376. store.dispatch('Logout').then(() => {
  377. Vue.ls.remove(ACCESS_TOKEN)
  378. window.location.reload()
  379. })
  380. }
  381. })
  382. }
  383. } else {
  384. this.$message.error(`文件上传失败: ${info.file.msg} `)
  385. }
  386. }
  387. },
  388. /* 图片预览 */
  389. getImgView(text) {
  390. if (text && text.indexOf(',') > 0) {
  391. text = text.substring(0, text.indexOf(','))
  392. }
  393. return getFileAccessHttpUrl(text)
  394. },
  395. /* 文件下载 */
  396. // update--autor:lvdandan-----date:20200630------for:修改下载文件方法名uploadFile改为downloadFile------
  397. downloadFile(text) {
  398. if (!text) {
  399. this.$message.warning('未知的文件')
  400. return
  401. }
  402. if (text.indexOf(',') > 0) {
  403. text = text.substring(0, text.indexOf(','))
  404. }
  405. let url = getFileAccessHttpUrl(text)
  406. window.open(url)
  407. }
  408. }
  409. }