JeecgListMixin.js 12 KB

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