|
|
@@ -1,3 +1,84 @@
|
|
|
+/**
|
|
|
+ * [顺丰] 顺丰面单打印实例
|
|
|
+ * @typedef {Object} sf_express_instance
|
|
|
+ * @property {string} version - 实例的版本号。
|
|
|
+ * @property {sf_express_instance_print_function} print - 打印信息的方法。
|
|
|
+ * @property {sf_express_instance_devices_function} devices - 获取打印机设备的方法。
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * [顺丰] 打印信息的方法的类型定义。
|
|
|
+ * @callback sf_express_instance_print_function
|
|
|
+ * @param {object} data - 要打印的数据。
|
|
|
+ * @param {Object} [options] - 打印选项(可选)。
|
|
|
+ * @param {'PRINT' | 'PREVIEW'} [options.lodopFn="PRINT"] - 打印类型。
|
|
|
+ * @param {boolean} [options.allPreview=false] - 预览全部面单
|
|
|
+ * @returns {Promise<void>}
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * [顺丰] 打印设备的方法的类型定义。
|
|
|
+ * @callback sf_express_instance_devices_function
|
|
|
+ * @param {string} [name] - 打印机名称前缀。
|
|
|
+ * @returns {Promise<{name: string; index: number}[]>}
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+import {withResolvers} from '@/tools/object';
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取顺丰打印实例
|
|
|
+ * @param options
|
|
|
+ * @param {string?} options.appid 合作伙伴编码(即顾客编码)
|
|
|
+ * @param {string?} options.appname 打印机设备名称(即设备名称)
|
|
|
+ * @param {'pro' | 'sbox'?} options.env 环境
|
|
|
+ * @returns {Promise<sf_express_instance>}
|
|
|
+ */
|
|
|
+let sf_express = async function (options = {}) {
|
|
|
+ const module = await import('@/libs/print/SCPPrint-2.7.js');
|
|
|
+
|
|
|
+ const {promise: load, resolve, reject} = withResolvers();
|
|
|
+
|
|
|
+ const instance = new module.default({
|
|
|
+ partnerID: options.appid || process.env.VUE_APP_SF_EXPRESS_APPID,
|
|
|
+ env: options.env || process.env.VUE_APP_SF_EXPRESS_ENV || (process.env.NODE_ENV === 'development' ? 'sbox' : 'pro'),
|
|
|
+ notips: false,
|
|
|
+ callback(result) {
|
|
|
+ if (result.code === 1) resolve(instance);
|
|
|
+ else reject({code: result.code, message: result.msg});
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ try {
|
|
|
+ const instance = await load;
|
|
|
+ sf_express = () => Promise.resolve(instance);
|
|
|
+ } catch (e) {}
|
|
|
+ return instance;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @type {sf_express_instance_print_function}
|
|
|
+ */
|
|
|
+export async function sf_express_print(data, options = {}) {
|
|
|
+ const instance = await sf_express(options);
|
|
|
+ const {promise: loadDevices, resolve, reject} = withResolvers();
|
|
|
+
|
|
|
+ instance['getPrinters']((result) => {
|
|
|
+ if (result.code === 1) {
|
|
|
+ const devices = Array.isArray(result.printers) ? result.printers : [];
|
|
|
+ resolve(devices);
|
|
|
+ } else reject({message: result.msg, code: result.code});
|
|
|
+ });
|
|
|
+
|
|
|
+ const devices = await loadDevices;
|
|
|
+
|
|
|
+ const device = devices.find(device => device.name.startsWith(process.env.VUE_APP_SF_EXPRESS_APPNAME));
|
|
|
+
|
|
|
+ if (device) instance['setPrinter'](device.index);
|
|
|
+ await instance.print(data);
|
|
|
+}
|
|
|
+
|
|
|
let c_lodop = async function (options = {}) {
|
|
|
const module = await import('@/libs/print/CLodop.js');
|
|
|
|
|
|
@@ -45,3 +126,4 @@ export async function getDevice(...priority) {
|
|
|
export default function (options = {}) {
|
|
|
return c_lodop(options);
|
|
|
}
|
|
|
+
|