JeecgListMixin.js 13 KB

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