228 lines
6.3 KiB
JavaScript
228 lines
6.3 KiB
JavaScript
const xe = require("xe-utils")
|
|
/**
|
|
* 状态机-编排复杂的流程状态业务
|
|
* @param {string} listName 列名.
|
|
*/
|
|
module.exports = class Status {
|
|
constructor(listName, status, data) {
|
|
this.data = data
|
|
this.status = status
|
|
this.listName = listName
|
|
this.list = []
|
|
}
|
|
|
|
/**
|
|
* 返回上个状态
|
|
*/
|
|
back() {
|
|
let index = this.getStatusIndex(this.listName, this.status)
|
|
const res = this.getList(this.listName)
|
|
if ((index - 1) < 0) {
|
|
return null
|
|
}
|
|
const status = res.status[index - 1]
|
|
if (res.listName && status.name) {
|
|
this.go(res.listName, status.name)
|
|
}
|
|
return this
|
|
}
|
|
|
|
/**
|
|
* 下一个状态
|
|
*/
|
|
next() {
|
|
let index = this.getStatusIndex(this.listName, this.status)
|
|
const res = this.getList(this.listName)
|
|
if (res.status.length <= (index + 1)) {
|
|
return null
|
|
}
|
|
const status = res.status[index + 1]
|
|
if (res.listName && status.name) {
|
|
this.go(res.listName, status.name)
|
|
}
|
|
return this
|
|
}
|
|
|
|
/**
|
|
* 触发成功事件
|
|
*/
|
|
success() {
|
|
const index = this.getStatusIndex(this.listName, this.status)
|
|
const res = this.getList(this.listName)
|
|
const status = res.status[index]
|
|
if (status.success.event) { this.emitEvent(status.success.event) }
|
|
if (status.success.listName && status.success.status) {
|
|
this.go(status.success.listName, status.success.status)
|
|
}
|
|
return this
|
|
}
|
|
|
|
emitEvent(eventName) {
|
|
console.log("emitEvent:", eventName);
|
|
}
|
|
|
|
fail() {
|
|
const index = this.getStatusIndex(this.listName, this.status)
|
|
const res = this.getList(this.listName)
|
|
const status = res.status[index]
|
|
if (status.fail.event) { this.emitEvent(status.fail.event) }
|
|
if (status.fail.listName && status.fail.status) {
|
|
this.go(status.fail.listName, status.fail.status)
|
|
}
|
|
return this
|
|
}
|
|
|
|
go(listName, status) {
|
|
this.status = status
|
|
this.listName = listName
|
|
return this
|
|
}
|
|
|
|
getStatusIndex(listName, status) {
|
|
const res = this.getList(listName)
|
|
let index = xe.findKey(res.status, item => item.name === status)
|
|
return index ? Number(index) : null
|
|
}
|
|
|
|
getList(listName) {
|
|
const res = this.list.filter(item => item.listName === listName)
|
|
if (res.length <= 0) { return false }
|
|
return res[0]
|
|
}
|
|
|
|
push({status, success, fail}) {
|
|
const res = this.getList(this.listName)
|
|
if (res) {
|
|
success = success || {}
|
|
success["listName"] = success["listName"] || ""
|
|
success["status"] = success["status"] || ""
|
|
success["event"] = success["event"] || ""
|
|
fail = fail || {}
|
|
fail["listName"] = fail["listName"] || ""
|
|
fail["status"] = fail["status"] || ""
|
|
fail["event"] = fail["event"] || ""
|
|
|
|
res.status.push({
|
|
name : status,
|
|
success: success,
|
|
fail : fail,
|
|
})
|
|
}
|
|
else {return false}
|
|
return this
|
|
}
|
|
|
|
create(listName) {
|
|
if (!listName) { return false }
|
|
const res = this.getList(listName)
|
|
if (res) { return false }
|
|
this.list.push({
|
|
listName: listName,
|
|
status : []
|
|
})
|
|
this.listName = listName
|
|
return this
|
|
}
|
|
}
|
|
//
|
|
// const s = new Status()
|
|
//
|
|
// s.create("ord")
|
|
// .push({
|
|
// status : "start",
|
|
// success: {
|
|
// listName: "ord", //成功后切换到的listName
|
|
// status : "pay", //成功后切换到的status
|
|
// event : "ord.start",
|
|
// },
|
|
// fail : {
|
|
// listName: "", //失败后切换到的listName
|
|
// status : "", //失败后切换到的status
|
|
// event : "",
|
|
// },
|
|
// })
|
|
// .push({
|
|
// status : "pay",
|
|
// success: {
|
|
// listName: "ord", //成功后切换到的listName
|
|
// status : "end", //成功后切换到的status
|
|
// event : "ord.pay",
|
|
// },
|
|
// fail : {
|
|
// listName: "refund", //失败后切换到的listName
|
|
// status : "start", //失败后切换到的status
|
|
// event : "",
|
|
// },
|
|
// })
|
|
// .push({
|
|
// status : "end",
|
|
// success: {
|
|
// listName: "", //成功后切换到的listName
|
|
// status : "", //成功后切换到的status
|
|
// event : "ord.end",
|
|
// },
|
|
// fail : {
|
|
// listName: "", //失败后切换到的listName
|
|
// status : "", //失败后切换到的status
|
|
// event : "",
|
|
// },
|
|
// })
|
|
// s.create("refund")
|
|
// .push({
|
|
// status : "start",
|
|
// success: {
|
|
// listName: "refund", //成功后切换到的listName
|
|
// status : "ret", //成功后切换到的status
|
|
// event : "refund.start",
|
|
// },
|
|
// fail : {
|
|
// listName: "", //失败后切换到的listName
|
|
// status : "", //失败后切换到的status
|
|
// event : "",
|
|
// },
|
|
// })
|
|
// .push({
|
|
// status : "ret",
|
|
// success: {
|
|
// listName: "refund", //成功后切换到的listName
|
|
// status : "end", //成功后切换到的status
|
|
// event : "refund.ret",
|
|
// },
|
|
// fail : {
|
|
// listName: "", //失败后切换到的listName
|
|
// status : "", //失败后切换到的status
|
|
// event : "",
|
|
// },
|
|
// })
|
|
// .push({
|
|
// status : "end",
|
|
// success: {
|
|
// listName: "", //成功后切换到的listName
|
|
// status : "", //成功后切换到的status
|
|
// event : "refund.end",
|
|
// },
|
|
// fail : {
|
|
// listName: "", //失败后切换到的listName
|
|
// status : "", //失败后切换到的status
|
|
// event : "",
|
|
// },
|
|
// })
|
|
// // console.log(s);
|
|
//
|
|
// s.go("ord", "start")
|
|
// console.log(s.listName, s.status);
|
|
// s.success()
|
|
// console.log(s.listName, s.status);
|
|
// s.fail()
|
|
// console.log(s.listName, s.status);
|
|
// s.success()
|
|
// console.log(s.listName, s.status);
|
|
// s.success()
|
|
// console.log(s.listName, s.status);
|
|
// s.success()
|
|
// console.log(s.listName, s.status);
|
|
// s.next()
|
|
// console.log(s.listName, s.status);
|
|
|
|
|