JeecgListMixin.js 12 KB

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