-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (171 loc) · 6.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _gm = require('gm');
var _gm2 = _interopRequireDefault(_gm);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _iconvLite = require('iconv-lite');
var _iconvLite2 = _interopRequireDefault(_iconvLite);
var _requestPromise = require('request-promise');
var _requestPromise2 = _interopRequireDefault(_requestPromise);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var URL = {
bind: 'http://open.memobird.cn/home/setuserbind',
print: 'http://open.memobird.cn/home/printpaper'
};
var Gugu = function () {
function Gugu(CONFIG) {
_classCallCheck(this, Gugu);
if (CONFIG === undefined) {
console.error('ERR: undefined CONFIG');
return;
}
var keys = ['useridentifying', 'memobirdID', 'ak'];
if (!this._checkExist(CONFIG, keys)) return;
this.CONFIG = CONFIG;
this.isReady = true;
this.userID = null;
}
_createClass(Gugu, [{
key: 'setup',
value: function setup() {
var _this = this;
if (this.CONFIG === undefined) return _bluebird2.default.reject('config error');
if (this.userID !== null) return _bluebird2.default.resolve();
return this._bindAccount().then(function () {
return _this;
}).catch(function () {
return _bluebird2.default.reject('can not binding your account');
});
}
}, {
key: 'print',
value: function print(str) {
var _this2 = this;
if (typeof str === 'undefined' || str.length === 0) {
return _bluebird2.default.reject('#print str can not be empty');
}
if (!this.isReady) return _bluebird2.default.reject('print fail, initialize fail');
return _bluebird2.default.resolve().then(function () {
return Array.isArray(str) ? _this2._flatStrArray(str) : _this2._encode(str);
}).then(function (printcontent) {
return _this2._request('print', { printcontent: printcontent, userID: _this2.userID });
}).then(function (d) {
if (d.showapi_res_code === 1) {
return _bluebird2.default.resolve(d);
} else if (d.showapi_res_code === 0 && d.result === -3) {
return _bluebird2.default.reject('can not connect to memobird');
}
return _bluebird2.default.reject(d);
});
}
}, {
key: 'catchErr',
value: function catchErr(e) {
if (typeof e === 'string') console.error('ERR: ' + e);else if (typeof e.message !== 'undefined') console.error('ERR: ' + e.message);else console.error(JSON.stringify(e));
}
// private
}, {
key: '_checkExist',
value: function _checkExist(obj, keys) {
return keys.every(function (v) {
var isExist = obj.hasOwnProperty(v);
if (!isExist) console.error('ERR: ' + v + ' is undefined, please check');
return isExist;
});
}
}, {
key: '_bindAccount',
value: function _bindAccount() {
var _this3 = this;
return this._request('bind').then(function (d) {
if (d.showapi_res_code === 1) {
_this3.isReady = true;
_this3.userID = d.showapi_userid;
} else if (d.showapi_res_code === 0) {
_bluebird2.default.reject(d.showapi_res_error);
} else {
_bluebird2.default.reject('bindAccount fail');
}
});
}
}, {
key: '_request',
value: function _request(type) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _CONFIG = this.CONFIG,
ak = _CONFIG.ak,
memobirdID = _CONFIG.memobirdID,
useridentifying = _CONFIG.useridentifying;
var defaultParams = {
ak: ak, memobirdID: memobirdID, useridentifying: useridentifying,
timestamp: new Date()
};
var body = Object.assign(params, defaultParams);
return (0, _requestPromise2.default)({
uri: URL[type],
method: 'post',
json: true,
body: body
});
}
}, {
key: '_flatStrArray',
value: function _flatStrArray(str) {
var _this4 = this;
var deal = {
text: function text(s) {
return _this4._encode(s);
},
pic: function pic(p) {
return _this4._encodePic(p);
},
pic_url: function pic_url(url) {
return (0, _requestPromise2.default)({ url: url, encoding: null }).then(function (d) {
return _this4._encodePic(d);
});
}
};
if (str.some(function (val) {
return deal[val.type] === undefined || val.value === undefined;
})) {
return _bluebird2.default.reject('array item shoule have prop :type && :value');
}
return _bluebird2.default.all(str.map(function (item) {
return deal[item.type](item.value);
})).then(function (arr) {
return arr.join('|');
});
}
}, {
key: '_encode',
value: function _encode(str) {
return 'T:' + _iconvLite2.default.encode(str + '\n', 'gbk').toString('base64');
}
}, {
key: '_encodePic',
value: function _encodePic(image) {
return new _bluebird2.default(function (res, rej) {
if (typeof image === 'string') {
try {
_fs2.default.readFileSync(image);
} catch (e) {
rej(e);
}
}
(0, _gm2.default)(image).resize(384).flip().type('Grayscale').colors(2).toBuffer('bmp', function (err, buffer) {
if (err) rej(err);
res('P:' + buffer.toString('base64'));
});
});
}
}]);
return Gugu;
}();
exports.default = Gugu;