You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

417 lines
12 KiB

import {getShipAll} from "@/api/gj/base";
import {endLoading, startLoading} from "@/utils/index";
import {upload} from "@/api/gj/jhgl";
import {getByCode} from "@/api/sy/xtgl";
import {getDepartByCon} from "@/api/sy/depart";
import XEUtils from 'xe-utils'
import request from '@/utils/request'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export const PageUtil = function () {
let _that = null;
const option = {
height: 0,
initMethod: [],//初始化数据调用方法定义,['getProjects','initDict','initDept']
validate: [],//校验定义[{methodName:'initList',validateParamName:'queryParam',validateName:'project','message':'工程编号不能为空'}],暂时只支持一级属性校验
requestOption: {},//通用接口调用方法参数
columnOption:[],//表格列配置数据[{type:'',field:'',title:'',width:100,formatter:{dictCode:'',with:''},sortable:true,filterRender:'',props:{}]
};
//下拉框数据,需要的时候自己扩展
const select = {
//工程编号
shipList: [],
//计划分类
jhfl: [],
//图纸类型
tzlx: [],
//责任部门
sscj: [],
//业务流程状态,编制,提交,审核...
status: [],
//配送状态
psStatus: [],
comDept:[],
outPaintCode:[],
inPaintCode:[],
scDept:[]
},
//组件中定义的table的refs
tableName = '',
//表格高度属性
tableHeight = {
height: 0,
defaultHeight: ''
},
tableColumn=[],
//查询参数
queryParam = {
status:'',
project:'',
scDept:'',
departCode:''
},
//批量输入参数
batch = {
jsclJhsj:'',
blwgJhsj:'',
jjwgJhsj:'',
zzwgJhsj:'',
clwgJhsj:'',
tzwgJhsj:'',
jprkJhsj:''
},
//表格数据
tableData = [],
//api
method = {};
const init = function () {
if (!this._that) {
alert('vue对象未初始化')
return;
}
if (!this.tableName){
console.log('表格名称未配置,某些功能无法使用');
return;
}
this.setTableHeight();
//运行所有数据初始化方法
for (const key of option.initMethod) {
try {
eval(option.initMethod[key]);
}catch(e){
alert("找不到方法");
return;
}
this[key]();
}
window.addEventListener('resize', this.setTableHeight)
excelUpload.parent=this;
};
const validate = function (methodName) {
for (const validate of this.option.validate) {
if (validate.methodName === methodName) {
if (this[validate.validateParamName] && !this[validate.validateParamName][validate.validateName]) {
this._that.$message.warning(validate.message);
return false;
}
}
}
return true;
};
const initList = function () {
if (this.validate('initList')) {
for (const key in this.queryParam) {
if (this.queryParam[key]===''){
this.queryParam[key]=null;
}
}
this.method.initList(this.queryParam).then(res => {
this.tableData = res.data;
})
}
};
//偷懒,通用request
//建议查询使用,否则可能会造成修改为''的数据不被update
const commonRequestNonTip=function (methodName,data={},beforeRequest,callback){
beforeRequest=beforeRequest||new Function('return true;');
callback=callback||new Function();
for (const key in data) {
if (data[key]===''){
data[key]=null;
}
}
if (this.validate(methodName)&&beforeRequest(data)){
let url=this.option.requestOption[methodName].url
if (this.option.requestOption[methodName].method==='get'){
url+="?"
for (const key in data) {
url+=key+"="+data[key]+"&";
}
}
request({
url:url,
method:this.option.requestOption[methodName].method,
data
}).then(res=>{
if (res.success){
callback(res.data);
}else {
this._that.$message.warning(res.message);
}
})
}
};
const commonRequest=function (methodName,data={},beforeRequest,callback){
beforeRequest=beforeRequest||new Function('return true;');
callback=callback||new Function();
if (this.validate(methodName)&&beforeRequest(data)){
let tips=this.option.requestOption[methodName].tips;
if (!tips){
tips='是否确认保存选中的数据?'
}
this._that.$confirm(tips, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
let url=this.option.requestOption[methodName].url
if (this.option.requestOption[methodName].method==='get'){
url+="?"
for (const key in data) {
url+=key+"="+data[key]+"&";
}
}
request({
url:url,
method:this.option.requestOption[methodName].method,
data
}).then(res=>{
if (res.success){
callback(res.data);
}else {
this._that.$message.warning(res.message);
}
})
}).catch((e) => {
return false;
});
}
};
//扩展,直接传请求地址和请求方法类型,通用request
const commonRequestEx=function (requestParams,data,beforeRequest,callback){
beforeRequest=beforeRequest||new Function('return true;');
callback=callback||new Function();
if (beforeRequest()){
const tips=requestParams.tips||'是否保存';
this._that.$confirm(tips, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
request({
url:requestParams.url,
method:requestParams.method,
data
}).then(res=>{
if (res.success){
callback(res.data);
}else {
this._that.$message.warning(res.message);
}
})
}).catch((e) => {
return false;
});
}
};
//批量输入
const plsr=function (){
if (!this.tableName){
return;
}
const table=this._that.$refs[this.tableName]
const selectedList=table.getCheckboxRecords();
if (selectedList.length<=0){
this._that.$message.warning('请先选择批量处理的数据');
return;
}
if (this.validate('plsr')){
selectedList.forEach(l=>{
for (const key in this.batch) {
if (this.batch[key]){
if (Object.prototype.toString.call(this.batch[key])==='[object Date]'){
l[key]=XEUtils.toDateString(this.batch[key], 'yyyy/MM/dd');
continue;
}
l[key]=this.batch[key];
}
}
})
}
table.updateData(selectedList);
};
//直接导出表格
const exportExcel=function (param) {
// 表名
const xlsxParam = { raw: true }
var wb = XLSX.utils.table_to_book(document.querySelector('.'+param.tableName), xlsxParam)
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
try {
FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), param.fileName+'.xlsx')
} catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
return wbout
}
//表格列配置数据[{type:'',field:'',title:'',width:100,formatter:{dictCode:'',with:''},sortable:true,filterRender:'',props:{}]
const setTableColumn=function (){
this.option.columnOption.forEach(c=>{
const column={};
if (c.type){
column.type=c.type;
}
if (c.field){
column.field=c.field;
}
if (c.title){
column.title=c.title;
}
c.width=100;
if (c.width){
column.width=c.width;
}
if (c.formatter){
column.formatter.push(c.formatter.dictCode)
if (c.formatter.with){
column.formatter.push(c.formatter.with)
}
}
if (c.sortable){
column.sortable=true
}
if (c.filterRender){
column.filters=[{ data: { checks: [], sVal: '', sMenu: '', fType1: '', fVal1: '', fMode: 'and', fType2: '', fVal2: '' } }]
column.filterRender={name:c.filterRender}
}
this.tableColumn.push(column);
})
}
//设置表格高度
const setTableHeight=function () {
if (!option.height){
console.log('未配置form高度,table高度无法初始化')
//this._that.$message.error('未配置form高度,table高度无法初始化');
return;
}
tableHeight.height = window.innerHeight - option.height;
tableHeight.defaultHeight = tableHeight.height + 'px';
};
//加载工程编号下拉框数据
const getProjects=function (fieldName='project'){
getShipAll().then(res => {
const _that=this._that;
if (res.success) {
this.select.shipList = res.data;
this.initEditRenderOption(this.select.shipList,fieldName,'project','project')
} else {
_that.$message.error(res.message);
}
})
};
// //加载字典下拉框,转码
const initDict=function (){
for (const dict of this.option.dict) {
if (dict.default){
this.queryParam[dict.name]=dict.default;
}
getByCode({dictCode:dict.code}).then(res=>{
let dictData=res.data;
if (dict.startWith){
dictData=dictData.filter(dictItem=>{
return dictItem.itemCode.startsWith(dict.startWith)
})
}
this.select[dict.name]=dictData;
this.initEditRenderOption(dictData,dict.render,'itemValue','itemCode')
})
}
};
const initDept=function (){
for (const dept of this.option.dept) {
getDepartByCon({parentCode:dept.code}).then(res=>{
let deptData=res.data;
if (dept.name){
this.select[dept.name]=deptData;
}
if (dept.render){
this.initEditRenderOption(deptData,dept.render,'departName','departCode')
}
if (dept.default&&!this.queryParam[dept.name]){
this.queryParam[dept.name]=dept.default;
}
})
}
};
const initEditRenderOption=function (data,fieldName,label,value){
if (!this.tableName){
return;
}
const table=this._that.$refs[this.tableName]
const edit=table.getColumnByField(fieldName)
if (edit&&edit.editRender&&edit.editRender.options){
const optionList = []
for (const item of data) {
const temp = { label: item[label], value: item[value] }
optionList.push(temp)
}
edit.editRender.options=optionList;
}
};
const beforexx=function (){
console.log(this)
}
const selectChange=function (row) {
this._that.$refs[tableName].clearActived() // 清除单元格激活状态
this._that.$refs[tableName].setActiveCell(row.row, row.column.property) // 设置单元格为激活状态
};
const excelUpload= {
parent:'',
option: {
uploadAction: '',
params: {}
},
beforeUpload(file,callBack) {
if (!this.option.uploadAction){
console.log('excelUpload.option.uploadAction,上传地址未定义')
return false;
}
let fd = new FormData()
fd.append('file', file);
for (const key in this.option.params) {
fd.append(key, this.option.params[key]);
}
callBack=callBack||new Function()
upload(this.option.uploadAction, fd).then(res => {
if (res.data.successData) {
this.parent.tableData = res.data.successData;
} else {
this.tableData = res.data
}
});
return false;
},
handleAvatarSuccess(res, file) {
endLoading();
}
};
return {
_that,
option,
select,
tableName,
tableHeight,
queryParam,
batch,
tableData,
method,
validate,
init,
initList,
commonRequest,
commonRequestNonTip,
commonRequestEx,
getProjects,
initDict,
initDept,
initEditRenderOption,
plsr,
beforexx,
excelUpload,
exportExcel,
selectChange,
setTableHeight
}
}