listMixin.js 11 KB

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