JeecgListMixin.js 11 KB

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