JeecgListMixin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /* 导出 */
  264. handleExportXls2(){
  265. let paramsStr = encodeURI(JSON.stringify(this.getQueryParams()));
  266. let url = `${window._CONFIG['domianURL']}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`;
  267. window.location.href = url;
  268. },
  269. handleExportXls(fileName){
  270. if(!fileName || typeof fileName != "string"){
  271. fileName = "导出文件"
  272. }
  273. let param = this.getQueryParams();
  274. if(this.selectedRowKeys && this.selectedRowKeys.length>0){
  275. param['selections'] = this.selectedRowKeys.join(",")
  276. }
  277. console.log("导出参数",param)
  278. downFile(this.url.exportXlsUrl,param).then((data)=>{
  279. if (!data) {
  280. this.$message.warning("文件下载失败")
  281. return
  282. }
  283. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  284. window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
  285. }else{
  286. let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
  287. let link = document.createElement('a')
  288. link.style.display = 'none'
  289. link.href = url
  290. link.setAttribute('download', fileName+'.xls')
  291. document.body.appendChild(link)
  292. link.click()
  293. document.body.removeChild(link); //下载完成移除元素
  294. window.URL.revokeObjectURL(url); //释放掉blob对象
  295. }
  296. })
  297. },
  298. /* 导入 */
  299. handleImportExcel(info){
  300. this.loading = true;
  301. if (info.file.status !== 'uploading') {
  302. console.log(info.file, info.fileList);
  303. }
  304. if (info.file.status === 'done') {
  305. this.loading = false;
  306. if (info.file.response.success) {
  307. // this.$message.success(`${info.file.name} 文件上传成功`);
  308. if (info.file.response.code === 201) {
  309. let { message, result: { msg, fileUrl, fileName } } = info.file.response
  310. let href = window._CONFIG['domianURL'] + fileUrl
  311. this.$warning({
  312. title: message,
  313. content: (<div>
  314. <span>{msg}</span><br/>
  315. <span>具体详情请 <a href={href} target="_blank" download={fileName}>点击下载</a> </span>
  316. </div>
  317. )
  318. })
  319. } else {
  320. this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
  321. }
  322. this.loadData()
  323. } else {
  324. this.$message.error(`${info.file.name} ${info.file.response.message}.`);
  325. }
  326. } else if (info.file.status === 'error') {
  327. this.loading = false;
  328. if (info.file.response.status === 500) {
  329. let data = info.file.response
  330. const token = Vue.ls.get(ACCESS_TOKEN)
  331. if (token && data.message.includes("Token失效")) {
  332. this.$error({
  333. title: '登录已过期',
  334. content: '很抱歉,登录已过期,请重新登录',
  335. okText: '重新登录',
  336. mask: false,
  337. onOk: () => {
  338. store.dispatch('Logout').then(() => {
  339. Vue.ls.remove(ACCESS_TOKEN)
  340. window.location.reload();
  341. })
  342. }
  343. })
  344. }
  345. } else {
  346. this.$message.error(`文件上传失败: ${info.file.msg} `);
  347. }
  348. }
  349. },
  350. /* 图片预览 */
  351. getImgView(text){
  352. if(text && text.indexOf(",")>0){
  353. text = text.substring(0,text.indexOf(","))
  354. }
  355. return getFileAccessHttpUrl(text)
  356. },
  357. /* 文件下载 */
  358. // update--autor:lvdandan-----date:20200630------for:修改下载文件方法名uploadFile改为downloadFile------
  359. downloadFile(text){
  360. if(!text){
  361. this.$message.warning("未知的文件")
  362. return;
  363. }
  364. if(text.indexOf(",")>0){
  365. text = text.substring(0,text.indexOf(","))
  366. }
  367. let url = getFileAccessHttpUrl(text)
  368. window.open(url);
  369. },
  370. }
  371. }