listMixin.js 11 KB

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