ttk-app-core
Version:
enterprise develop framework
313 lines (299 loc) • 12 kB
JavaScript
import React from 'react'
import { action as MetaAction, AppLoader } from 'edf-meta-engine'
import { List, fromJS } from 'immutable'
import moment from 'moment'
import config from './config'
import md5 from 'md5'
import { userInfo } from 'os';
import { FormDecorator } from 'edf-component'
import { ifError } from 'assert';
import { runInThisContext } from 'vm';
import Item from 'antd/lib/list/Item';
class action {
constructor(option) {
this.metaAction = option.metaAction
this.voucherAction = option.voucherAction
this.config = config.current
this.webapi = this.config.webapi
}
//初始化数据
onInit = ({ component, injections }) => {
this.voucherAction.onInit({ component, injections })
this.component = component
this.injections = injections
let addEventListener = this.component.props.addEventListener
this.changeSipmleDate = false
if (addEventListener) {
addEventListener('onTabFocus', :: this.onTabFocus)
}
injections.reduce('init');
this.load()
}
onTabFocus = () => {
this.load()
}
getCurrentOrg = () => this.metaAction.context.get('currentOrg') || {}
getOrgId = () => {
const org = this.getCurrentOrg()
if (org) {
return org.id;
}
return ""
}
load = async () => {
// const hello=await this.webapi.tplus.test();
const res = await this.webapi.tplus.configExist();//是否已绑定
const config = await this.webapi.tplus.configQuery();//查询配置信息
if (res && config) {
let foreseeClientHost = config.foreseeClientHost;
let params = foreseeClientHost.split(':');
foreseeClientHost = params[0];
let foreseeClientPort = params[1];
//已经绑定过=》解绑
this.metaAction.sf('data.other.step', 3);
this.metaAction.sf('data.other.hasbind', true);
this.metaAction.sf('data.form.foreseeClientHost', foreseeClientHost);
this.metaAction.sf('data.form.foreseeClientPort', foreseeClientPort);
this.metaAction.sf('data.form.appUserName', config.appUserName);
this.metaAction.sf('data.form.appAccountNumber', config.appAccountNumber);
}
}
dbTest = async () => {
const other = this.metaAction.gf('data.other').toJS();
const form = this.metaAction.gf('data.form').toJS();
const checkArr = [
{
path: 'data.form.app', value: form.app
},
{
path: 'data.form.foreseeClientHost', value: form.foreseeClientHost
},
{
path: 'data.form.foreseeClientPort', value: form.foreseeClientPort
},
{
path: 'data.form.dbUsername', value: form.dbUsername
},
{
path: 'data.form.dbPassword', value: form.dbPassword
}]
const ok = await this.voucherAction.check(checkArr, this.check)
if (!ok) {
this.metaAction.toast('warning', '请按页面提示修改信息后才可提交')
return false
}
let { dbHost, dbPort, dbUsername, dbPassword, foreseeClientHost, foreseeClientPort,app } = form;
const baseUrl = `http://${foreseeClientHost}:${foreseeClientPort}`
const url = `${baseUrl}/common/config/DbTest`;
if (!dbHost || !dbPort) {
dbHost = foreseeClientHost;
dbPort = foreseeClientPort
}
let appselect = other.app.filter(item => item.value == app)[0];
const params = {
dbHost,
dbPort,
dbUsername,
dbPassword,
dbVersion: appselect.dbVersion,
appName: appselect.appName
}
const options = {
headers: {
token: this.getOrgId()
}
}
let res
try {
debugger
res = this.webapi.tplus.Dbtest(url, params, options);
} catch (error) {
debugger
}
if (res) {
this.metaAction.toast('success', '连接成功');
return {
baseUrl,
params,
options
};
} else {
this.metaAction.toast('error', '连接失败');
return false;
}
}
nextStep = async () => {
const other = this.metaAction.gf('data.other').toJS();
const form = this.metaAction.gf('data.form').toJS();
if (other.step === 1) {
const testResult = await this.dbTest();
if (testResult) {
const {
baseUrl,
params,
options
} = testResult;
const {
appUserName,
appPassword,
} = form;
this.webapi.tplus.accountBooks(`${baseUrl}/common/accountBooks/Query`, {
...params,
appUserName,
appPassword,
}, options)
}
this.metaAction.sf('data.other.step', 2);
} else if (other.step === 2) {
const checkArr = [
{
path: 'data.form.appUserName', value: form.appUserName
},
{
path: 'data.form.appPassword', value: form.appPassword
},
{
path: 'data.form.appAccountNumber', value: form.appAccountNumber
}
]
const ok = await this.voucherAction.check(checkArr, this.check)
if (!ok) {
this.metaAction.toast('warning', '请按页面提示修改信息后才可提交')
return false
}
let {
app,
dbHost,
dbPort,
dbUsername,
dbPassword,
appUserName,
appPassword,
appAccountNumber,
ttkUserName,
ttkPassword,
foreseeClientHost,
foreseeClientPort,
bsAppHost,
bsAppPort
} = form;
// let proxy = `http://${dbHost}:${dbPort}`;
let date = moment().format('YYYY/MM/DD');
let orgId = this.getOrgId();
let appselect = other.app.filter(item => item.value == app)[0];
let parms = {
app,
dbHost,
dbPort,
dbUsername,
dbPassword,
appName: appselect.appName,
dbVersion: appselect.dbVersion,
bsAppHost,
bsAppPort,
foreseeClientHost: `${foreseeClientHost}:${foreseeClientPort}`,
appUserName,
appPassword,
appAccountNumber,
appLoginDate: date,
ttkUserName,
ttkPassword,
ttkOrgId: orgId,
ttkLoginDate: date
}
let url = `http://${foreseeClientHost}:${foreseeClientPort}/common/config/Save`;
let options = {
headers: {
token: this.getOrgId()
}
}
const res1 = this.webapi.tplus.save(url, parms, options);
const res2 = this.webapi.tplus.configSave(parms);
if (res1 && res2) {
this.metaAction.toast('绑定成功');
this.metaAction.sf('data.other.step', 3)
} else {
this.metaAction.toast('绑定失败');
}
} else if (other.step === 3) {
this.component.props.tabEdit('对接财务账套', 'remove')
}
}
//返回上一步
backLastStep = async () => {
let step = this.metaAction.gf('data.other.step')
if (step == 2) {
this.metaAction.sf('data.other.step', step - 1)
} else if (step == 3) {
//解除绑定
const res = await this.webapi.tplus.configDelete();
if (res) {
this.metaAction.sf('data.other.hasbind', false);
this.metaAction.sf('data.form', fromJS({}));
this.metaAction.sf('data.other.step', 1)
}
}
}
//检查是否要置灰下一步按钮
checkNext = () => {
return false
}
fieldotherChange = (path, value) => {
let values = value.trim();
this.fieldChange(path, values)
}
fieldChange = (path, value) => {
this.voucherAction.fieldChange(path, value, this.check)
}
check = async (option) => {
if (!option || !option.path)
return
if (option.path == 'data.form.app') {
return { errorPath: 'data.other.error.app', message: !option.value ? '请选择软件' : "" }
} else if (option.path == 'data.form.dbHost') {
return { errorPath: 'data.other.error.dbHost', message: !option.value ? '请录入数据库IP地址' : "" }
}
else if (option.path == 'data.form.dbPort') {
return { errorPath: 'data.other.error.dbPort', message: !option.value ? '必填' : "" }
}
else if (option.path == 'data.form.dbUsername') {
return { errorPath: 'data.other.error.dbUsername', message: option.value ? '' : '请录入数据库账号' }
}
else if (option.path == 'data.form.dbPassword') {
return { errorPath: 'data.other.error.dbPassword', message: option.value ? '' : '请录入数据库密码' }
}
if (option.path == 'data.form.bsAppHost') {
return { errorPath: 'data.other.error.bsAppHost', message: !option.value ? '请录入T+服务IP地址' : "" }
}
else if (option.path == 'data.form.bsAppPort') {
return { errorPath: 'data.other.error.bsAppPort', message: !option.value ? '必填' : "" }
}
if (option.path == 'data.form.foreseeClientHost') {
return { errorPath: 'data.other.error.foreseeClientHost', message: !option.value ? '请录入客户端IP地址' : "" }
}
else if (option.path == 'data.form.foreseeClientPort') {
return { errorPath: 'data.other.error.foreseeClientPort', message: !option.value ? '必填' : "" }
}
else if (option.path == 'data.form.appUserName') {
return { errorPath: 'data.other.error.appUserName', message: option.value ? '' : '请录入T+登录账号' }
}
else if (option.path == 'data.form.appPassword') {
return { errorPath: 'data.other.error.appPassword', message: option.value ? '' : '请录入T+登录密码' }
}
else if (option.path == 'data.form.appAccountNumber') {
return { errorPath: 'data.other.error.appAccountNumber', message: option.value ? '' : '请录入T+登录账套号' }
}
}
moreClick = () => {
const moreInfo = this.metaAction.gf('data.other.moreInfo');
this.metaAction.sf('data.other.moreInfo', !moreInfo);
}
}
export default function creator(option) {
const metaAction = new MetaAction(option),
voucherAction = FormDecorator.actionCreator({ ...option, metaAction }),
o = new action({ ...option, metaAction, voucherAction }),
ret = { ...metaAction, ...voucherAction, ...o }
metaAction.config({ metaHandlers: ret })
return ret
}