390 lines
10 KiB
JavaScript
390 lines
10 KiB
JavaScript
|
function check(v, re, msg) {
|
||
|
if (!re.test(v)) {
|
||
|
alert(msg)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function padPkcs7(data) {
|
||
|
|
||
|
var d = data.length % 16;
|
||
|
d = 16 - d;
|
||
|
var arr = Array.from(data)
|
||
|
for (var i = 0; i < d; i++) {
|
||
|
arr.push(d)
|
||
|
}
|
||
|
return arr;
|
||
|
}
|
||
|
|
||
|
function stripPcks7(data) {
|
||
|
var arr = Array.from(data)
|
||
|
var padder = arr[arr.length - 1];
|
||
|
return arr.slice(0, arr.length - padder);
|
||
|
}
|
||
|
|
||
|
function ecb(bytes, cb) {
|
||
|
var ret = []
|
||
|
for (var i = 0; i < bytes.length; i += 16) {
|
||
|
var d = bytes.slice(i, i + 16);
|
||
|
var en = cb(d)
|
||
|
ret = ret.concat(Array.from(en))
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|
||
|
class ActiveEpc {
|
||
|
constructor() {
|
||
|
this.server = "ws://127.0.0.1:12321/active";
|
||
|
this.waitMap = {}
|
||
|
|
||
|
}
|
||
|
|
||
|
__debug(...str) {
|
||
|
if (window.active_debug) {
|
||
|
console.log(...str)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
connect() {
|
||
|
this.__debug("connect")
|
||
|
var _this = this;
|
||
|
try {
|
||
|
|
||
|
if (this.sock) {
|
||
|
this.sock.onclose = function () {
|
||
|
}
|
||
|
this.sock.close()
|
||
|
}
|
||
|
|
||
|
this.sock = new WebSocket(this.server);
|
||
|
this.__debug("sock")
|
||
|
this.sock.onopen = function () {
|
||
|
_this.__debug("open");
|
||
|
_this.sock.isopened = true;
|
||
|
_this.sock.onmessage = function (e) {
|
||
|
_this.__debug(e.data)
|
||
|
var ret = JSON.parse(e.data);
|
||
|
|
||
|
var cb = _this.waitMap[ret.id]
|
||
|
cb && cb(ret)
|
||
|
}
|
||
|
};
|
||
|
|
||
|
this.sock.onclose = function () {
|
||
|
_this.sock.isopened = false;
|
||
|
_this.__debug("close");
|
||
|
_this.reconnect();
|
||
|
};
|
||
|
|
||
|
// this.sock.onerror = function () {
|
||
|
// _this.sock.isopened = false;
|
||
|
// this.__debug("onerror");
|
||
|
// _this.reconnect();
|
||
|
// };
|
||
|
|
||
|
} catch (e) {
|
||
|
_this.__debug("onerror");
|
||
|
_this.reconnect();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
reconnect() {
|
||
|
this.__debug("reconnect")
|
||
|
var _this = this;
|
||
|
|
||
|
if (_this.sock && _this.sock.isopened) {
|
||
|
this.__debug("miss")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
setTimeout(function () {
|
||
|
_this.connect();
|
||
|
}, 3000);
|
||
|
}
|
||
|
|
||
|
__send(cmd, param, cb) {
|
||
|
var id = (Math.random() + "").substr(2);
|
||
|
this.sock.send(
|
||
|
JSON.stringify({
|
||
|
id: id,
|
||
|
cmd: cmd,
|
||
|
param: param
|
||
|
})
|
||
|
);
|
||
|
if (cb) {
|
||
|
this.waitMap[id] = cb;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
__check(v, re, msg, reject) {
|
||
|
if (!re.test(v)) {
|
||
|
return {
|
||
|
msg,
|
||
|
code: "C0"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
login(uid, utype, pwd) {
|
||
|
var _this = this;
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
var e = _this.__check(uid, /\d{1,2}/g, "用户编号填写错误!")
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
|
||
|
e = _this.__check(utype, /\d{1}/g, "用户类型填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
|
||
|
e = _this.__check(pwd, /\d{6}/g, "密码填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
|
||
|
_this.__send("login", {
|
||
|
uid, utype, pwd
|
||
|
}, function (ret) {
|
||
|
if (ret.code === "00" || ret.code === "33") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
logout() {
|
||
|
var _this = this;
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
_this.__send("logout", {}, function (ret) {
|
||
|
if (ret.code === "00" || ret.code === "2E") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
getSn() {
|
||
|
var _this = this;
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
_this.__send("getSn", {}, function (ret) {
|
||
|
if (ret.code === "00") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
setAutoSleepTime(minutes) {
|
||
|
var _this = this;
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
|
||
|
var e = _this.__check(minutes, /\d{1,2}/g, "时间填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var v = parseInt(minutes, 10);
|
||
|
if( v > 20 || v < 2 ) {
|
||
|
reject({
|
||
|
code : "C0",
|
||
|
msg: "时间超出范围"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
_this.__send("setAutoSleepTime", { minutes }, function (ret) {
|
||
|
if (ret.code === "00") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
|
||
|
modifyPassword(uid, utype, pwd) {
|
||
|
var _this = this;
|
||
|
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
var e = _this.__check(uid, /\d{1,2}/g, "用户编号填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
e = _this.__check(utype, /\d{1}/g, "用户类型填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
e = _this.__check(pwd, /\d{6}/g, "密码填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
|
||
|
_this.__send("modifyPassword", {
|
||
|
uid, utype, pwd
|
||
|
}, function (ret) {
|
||
|
if (ret.code === "00") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
calcPaycode(accr, accu, ticketType, ticketNo, date, banlance) {
|
||
|
var _this = this;
|
||
|
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
var e = _this.__check(accu, /\d{1,32}/g, "收方账户填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
e = _this.__check(ticketType, /\d{1}/g, "票据类型填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
e = _this.__check(ticketNo, /\d{1,8}/g, "票据编号填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
e = _this.__check(date, /^(?:(?:1[6-9]|[2-9][0-9])[0-9]{2}([-/.]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:(?:1[6-9]|[2-9][0-9])(?:0[48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)([-/.]?)0?2\2(?:29))$/g, "时间填写错误!", reject);
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
e = _this.__check(banlance, /\d{1,16}/g, "金额填写错误!");
|
||
|
if ( e ) {
|
||
|
reject && reject(e);
|
||
|
return
|
||
|
}
|
||
|
if ( '4' == ticketType || '5' == ticketType ) {
|
||
|
if ( undefined === accr || "" === accr ) {
|
||
|
reject({
|
||
|
code: "C0",
|
||
|
msg: "票据类型为汇兑和其他时,收方账号必填。"
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( undefined === accr ) {
|
||
|
accr = ""
|
||
|
}
|
||
|
|
||
|
_this.__send("calcPaycode", {
|
||
|
accr, accu, ticketType, ticketNo, date, banlance
|
||
|
}, function (ret) {
|
||
|
if (ret.code === "00") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
getAccountList() {
|
||
|
var _this = this;
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
if (!_this.sock.isopened) {
|
||
|
reject({
|
||
|
code: "CB",
|
||
|
msg: "Active 通信失败"
|
||
|
})
|
||
|
}
|
||
|
_this.__send("getAccountList", {}, function (ret) {
|
||
|
if (ret.code === "00") {
|
||
|
resolve(ret)
|
||
|
} else {
|
||
|
reject(ret)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 检测通过返回 true
|
||
|
* 检测不通过返回 fase
|
||
|
*/
|
||
|
ActiveEpc.checkSupport = function () {
|
||
|
try {
|
||
|
if (!!window.WebSocket && window.WebSocket.prototype.send) {
|
||
|
return true;
|
||
|
} else {
|
||
|
console.error("您的浏览器不支持!")
|
||
|
return false;
|
||
|
}
|
||
|
} catch (e) {
|
||
|
console.error("当前环境不支持!", e)
|
||
|
return false;
|
||
|
}
|
||
|
}
|