UNPKG

ttk-app-core

Version:
346 lines (292 loc) 11.5 kB
import React from 'react' import { action as MetaAction, AppLoader } from 'edf-meta-engine' import { fromJS } from 'immutable' import { Icon } from 'edf-component' import config from './config' import moment from 'moment' import utils from 'edf-utils' import extend from './extend' class action { constructor(option) { this.metaAction = option.metaAction this.extendAction = option.extendAction this.config = config.current this.webapi = this.config.webapi } onInit = ({ component, injections }) => { this.extendAction.gridAction.onInit({ component, injections }) this.component = component this.injections = injections injections.reduce('init') const pagination = this.metaAction.gf('data.pagination').toJS() //分页 const filter = this.metaAction.gf('data.filter').toJS() //过滤器 this.load(pagination, filter) //加载数据 } componentWillUnmount = () => { if (window.removeEventListener) { window.removeEventListener('resize', this.onResize, false) } else if (window.detachEvent) { window.detachEvent('onresize', this.onResize) } else { window.onresize = undefined } } componentDidMount = () => { if (window.addEventListener) { window.addEventListener('resize', this.onResize, false) } else if (window.attachEvent) { window.attachEvent('onresize', this.onResize) } else { window.onresize = this.onResize } } onResize = (e) => { let keyRandomTab = Math.floor(Math.random() * 10000) this.keyRandomTab = keyRandomTab setTimeout(()=>{ if( keyRandomTab == this.keyRandomTab ){ this.getTableScroll('ttk-edf-app-advancerpt-content', 'ant-table-thead', 2 , 'ant-table-body', 'data.tableOption', e) } },200) } getTableScroll = (contaienr, head, num, target, path, e) => { try{ const tableCon = document.getElementsByClassName(contaienr)[0] if( !tableCon ){ if( e ){ return } setTimeout(()=>{ this.getTableScroll(contaienr, head, num, target, path) }, 500) return } const header = tableCon.getElementsByClassName(head)[0] const body = tableCon.getElementsByClassName(target)[0].getElementsByTagName('table')[0] const pre = this.metaAction.gf(path).toJS() const y = tableCon.offsetHeight - header.offsetHeight - num const bodyHeight = body.offsetHeight if( bodyHeight > y && y != pre.y ){ this.metaAction.sf(path, fromJS({...pre, y})) }else if( bodyHeight < y && pre.y != null ){ this.metaAction.sf(path, fromJS({...pre, y: null})) }else { return false } }catch(err){ console.log(err) } } load = async (pagination, filter) => { const response = await this.webapi.deliverOrderList.init({ pagination, filter }) //调用接口 response.filter = filter//条件 this.injections.reduce('load', response) //调用reduce 把res给load方法实现更新state setTimeout(() => { this.onResize() }, 20) } //根据当前页及条件重新加载数据 reload = async () => { const pagination = this.metaAction.gf('data.pagination').toJS() //获取分页 const filter = this.metaAction.gf('data.filter').toJS() //获取过滤条件 this.load(pagination, filter) //加载数据 } //增加 add = async () => { if (!this.config.apps['mk-app-delivery-order']) { throw '依赖mk-app-delivery-order app,请使用mk clone mk-app-delivery-order命令添加' } this.component.props.setPortalContent && this.component.props.setPortalContent('销售出库单', 'mk-app-delivery-order') } //点击批量按钮 batchMenuClick = (e) => { switch (e.key) { case 'del': this.batchDel() break case 'audit': this.batchAudit() break } } //批量删除 batchDel = async () => { const lst = this.metaAction.gf('data.list') //列表 if (!lst || lst.size == 0) { this.metaAction.toast('error', '请选中要删除的记录') return } const selectRows = lst.filter(o => o.get('selected')) //选中的列表 if (!selectRows || selectRows.size == 0) { this.metaAction.toast('error', '请选中要删除的记录') return } //弹出modal const ret = await this.metaAction.modal('confirm', { title: '删除', content: '确认删除?' }) if (!ret) return const ids = selectRows.map(o => o.get('id')).toJS() //选中数据的id await this.webapi.deliverOrderList.del({ ids }) //通过接口删除id this.metaAction.toast('success', '删除成功') //提示删除成功 this.reload() //重新加载数据 } //批量审核 batchAudit = async () => { const lst = this.metaAction.gf('data.list') //全部列表元素 if (!lst || lst.size == 0) { this.metaAction.toast('error', '请选中要审核的记录') return } const selectRows = lst.filter(o => {console.log(o); o.get('selected')}) //选中的列表元素 // console.log(selectRows); if (!selectRows || selectRows.size == 0) { this.metaAction.toast('error', '请选中要审核的记录') return } const ids = selectRows.map(o => o.get('id')).toJS()//获取选中的id集合 await this.webapi.deliverOrderList.audit({ ids })//调用接口 this.metaAction.toast('success', '审核成功') //提示成功 this.reload() //重新加载页面 } //审核一个 audit = (id) => async () => { await this.webapi.deliverOrderList.audit({ ids: [id] }) this.metaAction.toast('success', '审核成功') this.reload() } //反审核一个 reject = (id) => async () => { await this.webapi.deliverOrderList.reject({ ids: [id] }) this.metaAction.toast('success', '反审核成功') this.reload() } //删除一个 del = (id) => async () => { //弹出confirm const ret = await this.metaAction.modal('confirm', { title: '删除', content: '确认删除?' }) if (!ret) return await this.webapi.deliverOrderList.del({ ids: [id] }) //调用接口删除 this.metaAction.toast('success', '删除成功') this.reload() //重新加载 } modify = (id) => async () => { if (!this.config.apps['ttk-edf-app-advancerpt']) { throw '依赖mk-app-delivery-order app,请使用mk clone mk-app-delivery-order命令添加' } this.component.props.setPortalContent && this.component.props.setPortalContent('存货卡片', 'ttk-edf-app-advancerpt', { deliveryOrderId: id }) } toggleShowAdvanceFilter = () => { this.metaAction.sf('data.other.isFold', !this.metaAction.gf('data.other.isFold')) } commonFilterChange = async (e) => { const key = e.target.value const pagination = this.metaAction.gf('data.pagination').toJS(), filter = this.metaAction.gf('data.filter').toJS() filter.common = key const response = await this.webapi.deliverOrderList.query({ pagination, filter }) response.filter = filter this.injections.reduce('load', response) //调用reduce 把res给load方法实现更新state setTimeout(() => { this.onResize() }, 20) } tabChange = async (key) => { const pagination = this.metaAction.gf('data.pagination').toJS(), filter = this.metaAction.gf('data.filter').toJS() filter.status = key const response = await this.webapi.deliverOrderList.query({ pagination, filter }) response.filter = filter this.injections.reduce('load', response) //调用reduce 把res给load方法实现更新state setTimeout(() => { this.onResize() }, 20) } customerChange = (v) => { const ds = this.metaAction.gf('data.other.customers') this.metaAction.sf(`data.filter.customer`, v) } search = () => { this.reload() } pageChanged = (current, pageSize) => { const filter = this.metaAction.gf('data.filter').toJS() this.load({ current, pageSize }, filter) } receipt = () => { throw '请实现收框功能' } print = () => { throw '请实现打印功能' } export = () => { throw '请实现导出功能' } setting = () => { throw '请实现设置功能' } numberFormat = utils.number.format renderColumns = () => { return [{ title: '序号', dataIndex: 'key', key: 'key', }, { title: '编号', dataIndex: 'id', key: 'id' }, { title: '日期', dataIndex: 'date', key: 'date', }, { title: '数量', dataIndex: 'number', key: 'number', }, { title: '产地', dataIndex: 'province', key: 'province' }, { title: '状态', dataIndex: 'status', key: 'status' },{ title: '来源', dataIndex: 'origin', key: 'origin', }, { title: '操作', dataIndex: 'action', key: 'action', fixed: 'right', width: 80, render: (text, record) => <Icon onClick={() => this.delClick(record)} style={{fontSize: '18px'}} fontFamily="edficon" type="shanchu" /> }] } delClick = async(record) => { console.log(record) await this.webapi.deliverOrderList.del({id: record.id}) this.metaAction.toast('success', '删除成功!') const pagination = this.metaAction.gf('data.pagination').toJS() //分页 const filter = this.metaAction.gf('data.filter').toJS() //过滤器 this.load(pagination, filter) //加载数据 } } export default function creator(option) { const metaAction = new MetaAction(option), extendAction = extend.actionCreator({ ...option, metaAction }), o = new action({ ...option, metaAction, extendAction }), ret = { ...metaAction, ...extendAction.gridAction, ...o } //装饰器的使用 写在...o的前面 metaAction.config({ metaHandlers: ret }) return ret }