JeecgListMixin.js 12 KB

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