listMixin.js 10 KB

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