NbcioListMixin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 NbcioListMixin = {
  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. if(this.selectionRows[a].hasOwnProperty('status') && this.selectionRows[a].status === 2) {
  173. this.$message.warning(`第${a+1}行单据已经审核,不能删除!`)
  174. return
  175. }
  176. }
  177. var that = this;
  178. this.$confirm({
  179. title: "确认删除",
  180. content: "是否删除选中数据?",
  181. onOk: function () {
  182. that.loading = true;
  183. deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
  184. if (res.success) {
  185. //重新计算分页问题
  186. that.reCalculatePage(that.selectedRowKeys.length)
  187. that.$message.success(res.message);
  188. that.loadData();
  189. that.onClearSelected();
  190. } else {
  191. that.$message.warning(res.message);
  192. }
  193. }).finally(() => {
  194. that.loading = false;
  195. });
  196. }
  197. });
  198. }
  199. },
  200. handleDelete: function (record) {
  201. if(!this.url.delete){
  202. this.$message.error("请设置url.delete属性!")
  203. return
  204. }
  205. if(record.status === 2) {
  206. this.$message.warning("单据已经审核,不能删除!")
  207. return
  208. }
  209. var that = this;
  210. deleteAction(that.url.delete, {id: record.id}).then((res) => {
  211. if (res.success) {
  212. //重新计算分页问题
  213. that.reCalculatePage(1)
  214. that.$message.success(res.message);
  215. that.loadData();
  216. } else {
  217. that.$message.warning(res.message);
  218. }
  219. });
  220. },
  221. reCalculatePage(count){
  222. //总数量-count
  223. let total=this.ipagination.total-count;
  224. //获取删除后的分页数
  225. let currentIndex=Math.ceil(total/this.ipagination.pageSize);
  226. //删除后的分页数<所在当前页
  227. if(currentIndex<this.ipagination.current){
  228. this.ipagination.current=currentIndex;
  229. }
  230. console.log('currentIndex',currentIndex)
  231. },
  232. handleEdit: function (record) {
  233. if(record.status === 2) {
  234. this.$message.warning("单据已经审核,不能编辑!")
  235. return
  236. }
  237. this.$refs.modalForm.edit(record);
  238. this.$refs.modalForm.title = "编辑";
  239. this.$refs.modalForm.approve=false;
  240. this.$refs.modalForm.disableSubmit = false;
  241. },
  242. handleAdd: function () {
  243. this.$refs.modalForm.add();
  244. this.$refs.modalForm.approve=false;
  245. this.$refs.modalForm.title = "新增";
  246. this.$refs.modalForm.disableSubmit = false;
  247. },
  248. handleAddSQL: function () {//大屏用
  249. this.$refs.modalForm.add("sql");
  250. this.$refs.modalForm.approve=false;
  251. this.$refs.modalForm.title = "新增";
  252. this.$refs.modalForm.disableSubmit = false;
  253. },
  254. handleAddHTTP: function () {//大屏用
  255. this.$refs.modalForm.add("http");
  256. this.$refs.modalForm.approve=false;
  257. this.$refs.modalForm.title = "新增";
  258. this.$refs.modalForm.disableSubmit = false;
  259. },
  260. handleTableChange(pagination, filters, sorter) {
  261. //分页、排序、筛选变化时触发
  262. //TODO 筛选
  263. console.log(pagination)
  264. if (Object.keys(sorter).length > 0) {
  265. this.isorter.column = sorter.field;
  266. this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
  267. }
  268. this.ipagination = pagination;
  269. this.loadData();
  270. },
  271. handleToggleSearch(){
  272. this.toggleSearchStatus = !this.toggleSearchStatus;
  273. },
  274. // 给popup查询使用(查询区域不支持回填多个字段,限制只返回一个字段)
  275. getPopupField(fields){
  276. return fields.split(',')[0]
  277. },
  278. modalFormOk() {
  279. // 新增/修改 成功时,重载列表
  280. this.loadData();
  281. //清空列表选中
  282. this.onClearSelected()
  283. },
  284. handleDetail:function(record){
  285. this.$refs.modalForm.edit(record);
  286. this.$refs.modalForm.title="详情";
  287. this.$refs.modalForm.disableSubmit = true;
  288. },
  289. handleApprove:function(record){
  290. if(record.status === 2) {
  291. this.$message.warning("单据已经审核,不能再次审核!")
  292. return
  293. }
  294. this.$refs.modalForm.approve=true;
  295. this.$refs.modalForm.edit(record);
  296. this.$refs.modalForm.title="审核详情";
  297. this.$refs.modalForm.okText="审核通过"
  298. this.$refs.modalForm.disableSubmit = true;
  299. },
  300. /* 导出 */
  301. handleExportXls2(){
  302. let paramsStr = encodeURI(JSON.stringify(this.getQueryParams()));
  303. let url = `${window._CONFIG['domianURL']}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`;
  304. window.location.href = url;
  305. },
  306. handleExportXls(fileName){
  307. if(!fileName || typeof fileName != "string"){
  308. fileName = "导出文件"
  309. }
  310. let param = this.getQueryParams();
  311. if(this.selectedRowKeys && this.selectedRowKeys.length>0){
  312. param['selections'] = this.selectedRowKeys.join(",")
  313. }
  314. console.log("导出参数",param)
  315. downFile(this.url.exportXlsUrl,param).then((data)=>{
  316. if (!data) {
  317. this.$message.warning("文件下载失败")
  318. return
  319. }
  320. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  321. window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
  322. }else{
  323. let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
  324. let link = document.createElement('a')
  325. link.style.display = 'none'
  326. link.href = url
  327. link.setAttribute('download', fileName+'.xls')
  328. document.body.appendChild(link)
  329. link.click()
  330. document.body.removeChild(link); //下载完成移除元素
  331. window.URL.revokeObjectURL(url); //释放掉blob对象
  332. }
  333. })
  334. },
  335. /* 导入 */
  336. handleImportExcel(info){
  337. this.loading = true;
  338. if (info.file.status !== 'uploading') {
  339. console.log(info.file, info.fileList);
  340. }
  341. if (info.file.status === 'done') {
  342. this.loading = false;
  343. if (info.file.response.success) {
  344. // this.$message.success(`${info.file.name} 文件上传成功`);
  345. if (info.file.response.code === 201) {
  346. let { message, result: { msg, fileUrl, fileName } } = info.file.response
  347. let href = window._CONFIG['domianURL'] + fileUrl
  348. this.$warning({
  349. title: message,
  350. content: (<div>
  351. <span>{msg}</span><br/>
  352. <span>具体详情请 <a href={href} target="_blank" download={fileName}>点击下载</a> </span>
  353. </div>
  354. )
  355. })
  356. } else {
  357. this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
  358. }
  359. this.loadData()
  360. } else {
  361. this.$message.error(`${info.file.name} ${info.file.response.message}.`);
  362. }
  363. } else if (info.file.status === 'error') {
  364. this.loading = false;
  365. if (info.file.response.status === 500) {
  366. let data = info.file.response
  367. const token = Vue.ls.get(ACCESS_TOKEN)
  368. if (token && data.message.includes("Token失效")) {
  369. this.$error({
  370. title: '登录已过期',
  371. content: '很抱歉,登录已过期,请重新登录',
  372. okText: '重新登录',
  373. mask: false,
  374. onOk: () => {
  375. store.dispatch('Logout').then(() => {
  376. Vue.ls.remove(ACCESS_TOKEN)
  377. window.location.reload();
  378. })
  379. }
  380. })
  381. }
  382. } else {
  383. this.$message.error(`文件上传失败: ${info.file.msg} `);
  384. }
  385. }
  386. },
  387. /* 图片预览 */
  388. getImgView(text){
  389. if(text && text.indexOf(",")>0){
  390. text = text.substring(0,text.indexOf(","))
  391. }
  392. return getFileAccessHttpUrl(text)
  393. },
  394. /* 文件下载 */
  395. // update--autor:lvdandan-----date:20200630------for:修改下载文件方法名uploadFile改为downloadFile------
  396. downloadFile(text){
  397. if(!text){
  398. this.$message.warning("未知的文件")
  399. return;
  400. }
  401. if(text.indexOf(",")>0){
  402. text = text.substring(0,text.indexOf(","))
  403. }
  404. let url = getFileAccessHttpUrl(text)
  405. window.open(url);
  406. },
  407. /** 表格增加合计行 */
  408. tableAddTotalRow(columns, dataSource) {
  409. if(dataSource.length>0 && this.ipagination.pageSize%10===1) {
  410. //分页条数为11、21、31等的时候增加合计行
  411. let numKey = 'rowIndex'
  412. let totalRow = { [numKey]: '合计' }
  413. //需要合计的列
  414. let parseCols = 'initialStock,currentStock,currentStockPrice,initialAmount,thisMonthAmount,currentAmount,inSum,inSumPrice,inOutSumPrice,' +
  415. 'outSum,outSumPrice,outInSumPrice,operNumber,allPrice,numSum,priceSum,prevSum,thisSum,thisAllPrice,billMoney,changeAmount,' +
  416. 'allPrice,currentNumber,lowSafeStock,highSafeStock,lowCritical,highCritical'
  417. columns.forEach(column => {
  418. let { key, dataIndex } = column
  419. if (![key, dataIndex].includes(numKey)) {
  420. let total = 0
  421. dataSource.forEach(data => {
  422. if(parseCols.indexOf(dataIndex)>-1) {
  423. if(data[dataIndex]) {
  424. total += Number.parseFloat(data[dataIndex])
  425. } else {
  426. total += 0
  427. }
  428. } else {
  429. total = '-'
  430. }
  431. })
  432. if (total !== '-') {
  433. total = total.toFixed(2)
  434. }
  435. totalRow[dataIndex] = total
  436. }
  437. })
  438. dataSource.push(totalRow)
  439. //总数要增加合计的行数,每页都有一行合计,所以总数要加上
  440. let size = Math.ceil(this.ipagination.total/(this.ipagination.pageSize-1))
  441. this.ipagination.total = this.ipagination.total + size
  442. }
  443. },
  444. paginationChange(page, pageSize) {
  445. this.ipagination.current = page
  446. this.ipagination.pageSize = pageSize
  447. this.loadData(this.ipagination.current);
  448. },
  449. paginationShowSizeChange(current, size) {
  450. this.ipagination.current = current
  451. this.ipagination.pageSize = size
  452. this.loadData(this.ipagination.current);
  453. }
  454. }
  455. }