init commit

This commit is contained in:
iqudoo
2026-04-30 10:16:43 +08:00
commit 2c8eb4f587
44 changed files with 17574 additions and 0 deletions

17
.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
.DS_Store
/node_modules
/release
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw*

8
README.md Normal file
View File

@@ -0,0 +1,8 @@
# scan-code-jssdk
桥接扫一扫/微信扫一扫/扫码枪 JSSDK
## Development
* make sure node and npm installed;
* clone the repo to local;
* run `npm install` to install node modules;
* run `npm run build-${env}` to get an *unminified* build file at `dist` folder;

125
analyzer/index.html Normal file
View File

@@ -0,0 +1,125 @@
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,user-scalable=no"/><title>Demo</title><script src="https://cdn.jsdelivr.net/npm/vconsole@3.12.0/dist/vconsole.min.js"></script><script>var vconsole = new VConsole();</script><style>.btn {
width: auto;
height: 40px;
line-height: 40px;
margin-bottom: 5px;
margin-left: 5px;
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
text-align: center;
}
.output {
border: 1px solid #eeeeee;
background: #f1f1f1;
padding: 10px;
min-height: 40px;
max-height: 400px;
white-space: pre-wrap;
word-wrap: break-word;
}
.result {
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}
.error {
border: 1px solid #6e2020;
background: #804646;
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}</style></head><body><h1 style="text-align: center;">IScan</h1><div style="text-align: center;"><button onclick="ready()" class="btn">ready</button> <button onclick="stopScan()" class="btn">stopScan</button> <button onclick="startScan()" class="btn">startScan</button></div><pre id="output" class="output"></pre><pre id="result" class="result"></pre><pre id="error" class="error"></pre><br><script>(function (params) {
output(window.navigator.userAgent);
window.onerror = function (params) {
error(params);
}
})();
function hide() {
document.getElementById("result").style.display = "none"
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "none"
document.getElementById("error").innerHTML = "";
}
function error(data) {
document.getElementById("result").style.display = "none"
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "block"
document.getElementById("error").innerHTML = JSON.stringify(data);
// console.log("error:", data);
}
function result(data) {
document.getElementById("error").style.display = "none"
document.getElementById("error").innerHTML = "";
document.getElementById("result").style.display = "block"
document.getElementById("result").innerHTML = JSON.stringify(data);
// console.log("result:", data);
}
function output(key, data) {
document.getElementById("output").innerHTML = key;
if (data) {
document.getElementById("output").innerHTML += "\n\n";
document.getElementById("output").innerHTML += JSON.stringify(data);
document.getElementById("output").innerHTML += "\n\n";
// console.log("output:", data);
}
}
function initSDK(options, callback) {
if (window.IScan) {
IScan.config(options).then(function () {
callback && callback();
}).catch(function (err) {
error(err);
})
} else {
window.addEventListener("IScanReady", function () {
initSDK(callback);
})
}
}
function ready() {
output("call ready");
hide();
var url = "https://vet.iqudoo.com/api?action=api.biz.wechat.JSSDKConfig";
initSDK({ initWechatJssdk: { apiUrl: url } }, function () {
IScan.setStatusListener(function (res) {
output("status", IScan.getStatus());
});
IScan.onScanListener(function (res) {
result(res);
}, "scan", null, 100);
output(window.navigator.userAgent);
result("初始化成功");
});
}
function stopScan() {
output("call stopScan");
hide();
IScan.stopScan();
}
function startScan() {
output("call startScan");
hide();
IScan.startScan();
}</script><script src="index.js"></script></body></html>

8
analyzer/index.js Normal file

File diff suppressed because one or more lines are too long

159
demo.html Normal file
View File

@@ -0,0 +1,159 @@
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<title>Demo</title>
<!-- 添加vconsole-->
<script src="https://cdn.jsdelivr.net/npm/vconsole@3.12.0/dist/vconsole.min.js"></script>
<script>
var vconsole = new VConsole();
</script>
<style>
.btn {
width: auto;
height: 40px;
line-height: 40px;
margin-bottom: 5px;
margin-left: 5px;
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
text-align: center;
}
.output {
border: 1px solid #eeeeee;
background: #f1f1f1;
padding: 10px;
min-height: 40px;
max-height: 400px;
white-space: pre-wrap;
word-wrap: break-word;
}
.result {
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}
.error {
border: 1px solid #6e2020;
background: #804646;
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}
</style>
</head>
<body>
<h1 style="text-align: center;">IScan</h1>
<div style="text-align: center;">
<button onclick="ready()" class="btn">ready</button>
<button onclick="stopScan()" class="btn">stopScan</button>
<button onclick="startScan()" class="btn">startScan</button>
</div>
<pre id="output" class="output"></pre>
<pre id="result" class="result"></pre>
<pre id="error" class="error"></pre>
</br>
<script>
(function (params) {
output(window.navigator.userAgent);
window.onerror = function (params) {
error(params);
}
})();
function hide() {
document.getElementById("result").style.display = "none"
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "none"
document.getElementById("error").innerHTML = "";
}
function error(data) {
document.getElementById("result").style.display = "none"
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "block"
document.getElementById("error").innerHTML = JSON.stringify(data);
// console.log("error:", data);
}
function result(data) {
document.getElementById("error").style.display = "none"
document.getElementById("error").innerHTML = "";
document.getElementById("result").style.display = "block"
document.getElementById("result").innerHTML = JSON.stringify(data);
// console.log("result:", data);
}
function output(key, data) {
document.getElementById("output").innerHTML = key;
if (data) {
document.getElementById("output").innerHTML += "\n\n";
document.getElementById("output").innerHTML += JSON.stringify(data);
document.getElementById("output").innerHTML += "\n\n";
// console.log("output:", data);
}
}
function initSDK(options, callback) {
if (window.IScan) {
IScan.config(options).then(function () {
callback && callback();
}).catch(function (err) {
error(err);
})
} else {
window.addEventListener("IScanReady", function () {
initSDK(callback);
})
}
}
function ready() {
output("call ready");
hide();
var url = "https://vet.iqudoo.com/api?action=api.biz.wechat.JSSDKConfig";
initSDK({ initWechatJssdk: { apiUrl: url } }, function () {
IScan.setStatusListener(function (res) {
output("status", IScan.getStatus());
});
IScan.onScanListener(function (res) {
result(res);
}, "scan", null, 100);
output(window.navigator.userAgent);
result("初始化成功");
});
}
function stopScan() {
output("call stopScan");
hide();
IScan.stopScan();
}
function startScan() {
output("call startScan");
hide();
IScan.startScan();
}
</script>
</body>
</html>

85
dist/index.d.ts vendored Normal file
View File

@@ -0,0 +1,85 @@
/**
* 扫码初始化选项
*/
interface ScanConfigOptions {
/**
* 网页扫码canvas样式
*/
webCanvasStyle?: string,
/**
* 微信JSSDK配置配置后会自动初始化微信JSSDK
*/
initWechatJssdk: {
/**
* 微信JSSDK配置API地址调用接口会带上当前页面url作为参数
*/
apiUrl: string,
/**
* 微信JSSDK配置SDK地址默认为https://res.wx.qq.com/open/js/jweixin-1.6.0.js
*/
sdkUrl?: string,
/**
* 微信JSSDK配置JS-API列表默认追加["scanQRCode"]
*/
jsApiList?: string[]
}
}
/**
* 扫码选项
*/
interface ScanResult {
result: string,
key: string
}
/** IScan */
interface IScan {
/**
* 配置SDK
* @param options 配置选项
*/
config(options?: ScanConfigOptions): Promise<any>;
/**
* 监听扫码状态
* @param callback 监听回调
*/
setStatusListener(callback: (status: "scanning" | "closed") => any): void;
/**
* 监听扫码结果
* @param callback 监听回调
* @param key 监听key
* @param match 监听匹配
* @param level 监听级别
*/
onScanListener(callback: (result: ScanResult) => any, key: string, match: string, level: number): void;
/**
* 取消监听扫码结果
* @param callback 监听回调
*/
offScanListener(callback: (result: ScanResult) => any): void;
/**
* 获取扫码状态
* @returns "scanning" | "closed"
*/
getStatus(): "scanning" | "closed";
/**
* 关闭扫码
*/
stopScan(): void;
/**
* 开启扫码
*/
startScan(): void;
/**
* 清除全部监听
*/
clear(): void;
}
declare var IScan: IScan;
declare interface Window {
IScan: IScan;
}

125
dist/index.html vendored Normal file
View File

@@ -0,0 +1,125 @@
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,user-scalable=no"/><title>Demo</title><script src="https://cdn.jsdelivr.net/npm/vconsole@3.12.0/dist/vconsole.min.js"></script><script>var vconsole = new VConsole();</script><style>.btn {
width: auto;
height: 40px;
line-height: 40px;
margin-bottom: 5px;
margin-left: 5px;
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
text-align: center;
}
.output {
border: 1px solid #eeeeee;
background: #f1f1f1;
padding: 10px;
min-height: 40px;
max-height: 400px;
white-space: pre-wrap;
word-wrap: break-word;
}
.result {
border: 1px solid rgb(28, 79, 180);
background: rgb(71, 123, 228);
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}
.error {
border: 1px solid #6e2020;
background: #804646;
color: #ffffff;
padding: 10px;
min-height: 40px;
max-height: 1000px;
white-space: pre-wrap;
word-wrap: break-word;
display: none;
}</style></head><body><h1 style="text-align: center;">IScan</h1><div style="text-align: center;"><button onclick="ready()" class="btn">ready</button> <button onclick="stopScan()" class="btn">stopScan</button> <button onclick="startScan()" class="btn">startScan</button></div><pre id="output" class="output"></pre><pre id="result" class="result"></pre><pre id="error" class="error"></pre><br><script>(function (params) {
output(window.navigator.userAgent);
window.onerror = function (params) {
error(params);
}
})();
function hide() {
document.getElementById("result").style.display = "none"
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "none"
document.getElementById("error").innerHTML = "";
}
function error(data) {
document.getElementById("result").style.display = "none"
document.getElementById("result").innerHTML = "";
document.getElementById("error").style.display = "block"
document.getElementById("error").innerHTML = JSON.stringify(data);
// console.log("error:", data);
}
function result(data) {
document.getElementById("error").style.display = "none"
document.getElementById("error").innerHTML = "";
document.getElementById("result").style.display = "block"
document.getElementById("result").innerHTML = JSON.stringify(data);
// console.log("result:", data);
}
function output(key, data) {
document.getElementById("output").innerHTML = key;
if (data) {
document.getElementById("output").innerHTML += "\n\n";
document.getElementById("output").innerHTML += JSON.stringify(data);
document.getElementById("output").innerHTML += "\n\n";
// console.log("output:", data);
}
}
function initSDK(options, callback) {
if (window.IScan) {
IScan.config(options).then(function () {
callback && callback();
}).catch(function (err) {
error(err);
})
} else {
window.addEventListener("IScanReady", function () {
initSDK(callback);
})
}
}
function ready() {
output("call ready");
hide();
var url = "https://vet.iqudoo.com/api?action=api.biz.wechat.JSSDKConfig";
initSDK({ initWechatJssdk: { apiUrl: url } }, function () {
IScan.setStatusListener(function (res) {
output("status", IScan.getStatus());
});
IScan.onScanListener(function (res) {
result(res);
}, "scan", null, 100);
output(window.navigator.userAgent);
result("初始化成功");
});
}
function stopScan() {
output("call stopScan");
hide();
IScan.stopScan();
}
function startScan() {
output("call startScan");
hide();
IScan.startScan();
}</script><script src="index.js"></script></body></html>

1
dist/index.js vendored Normal file

File diff suppressed because one or more lines are too long

53
package.json Normal file
View File

@@ -0,0 +1,53 @@
{
"name": "iqudoo-bridge",
"version": "0.0.1",
"description": "oho game jssdk",
"main": "dist/index.js",
"types": "types/index.d.ts",
"typings": "types/index.d.ts",
"scripts": {
"analyzer": "cross-env NODE_ENV=production webpack --mode=production --config webpack.analyzer.js",
"start": "webpack --watch --config webpack.prod.js",
"build": "webpack --config webpack.prod.js",
"test": "echo 'no tests'"
},
"files": [
"dist",
"types"
],
"devDependencies": {
"barcode-detector": "^3.0.8",
"autoprefixer": "^7.2.3",
"axios": "0.21.1",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.0",
"clean-webpack-plugin": "3.0.0",
"concurrently": "^3.5.0",
"cp-webpack-plugin": "^1.0.0",
"html-webpack-plugin": "4.5.2",
"cross-env": "5.2.1",
"css-loader": "^0.28.4",
"es6-promise": "4.2.6",
"exports-loader": "^0.6.4",
"file-loader": "^0.11.2",
"file-saver": "^1.3.3",
"http-server": "0.11.1",
"imports-loader": "^0.7.1",
"postcss-loader": "^2.0.6",
"str-webpack-plugin": "1.0.0",
"style-loader": "^0.18.2",
"uglifyjs-webpack-plugin": "^1.2.2",
"url-loader": "^0.5.9",
"vconsole": "3.3.0",
"quagga": "0.12.1",
"browserify-cipher": "1.0.1",
"webpack": "^4.12.0",
"webpack-bundle-analyzer": "3.6.0",
"webpack-cli": "^3.3.9"
},
"repository": {},
"author": "",
"license": "ISC"
}

7644
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

50
src/_core.js Normal file
View File

@@ -0,0 +1,50 @@
import './polyfill';
import _global from './polyfill/_global';
import { setConfig, getVersion } from './services/config';
import { onScanListener, offScanListener, setStatusListener, getStatus, startScan, stopScan, clear } from './services/provider/scan';
import { initWxJssdk } from './services/wx';
import { printDebug } from './utils/logger';
let _readyPromise = null;
let _calledReady = false;
(function () {
_global.__IScanReady__ && _global.__IScanReady__();
})();
export function isReadyCalled() {
return _calledReady;
}
function config(config) {
if (config) {
setConfig(config);
}
if (_readyPromise) {
return _readyPromise;
}
_readyPromise = Promise.resolve().then(() => {
printDebug('-------------------------------');
printDebug('sdk_version:', getVersion());
printDebug('-------------------------------');
initWxJssdk();
return Promise.resolve().then(() => {
_calledReady = true;
}).catch(err => {
_readyPromise = null;
throw err;
});
})
return _readyPromise;
}
export default Object.assign({}, {
config,
onScanListener,
offScanListener,
setStatusListener,
getStatus,
startScan,
stopScan,
clear,
});

75
src/_export.js Normal file
View File

@@ -0,0 +1,75 @@
import _core, { isReadyCalled } from "./_core";
function _exec(target, func, ...params) {
let instant = target;
let funcs = func.split('.');
while (funcs.length > 1) {
instant = instant[funcs.shift()];
}
if (instant && funcs.length == 1) {
if (instant.hasOwnProperty(funcs[0])) {
return instant[funcs[0]](...params);
}
}
throw `IScan.${func} not defined`;
}
function hook(target, method, action) {
let fns = method.split('.');
fns.forEach((fn, index) => {
if (index == fns.length - 1) {
target[fn] = action;
} else {
if (!target[fn]) {
target[fn] = {};
}
}
target = target[fn];
});
}
function forMethods(obj, parent = "") {
let keys = {}
Object.keys(obj).forEach(key => {
let val = obj[key];
if (typeof val === "object") {
Object.assign(keys, forMethods(val, `${parent}${key}.`));
} else if (typeof val === "function") {
let realKey = `${parent}${key}`;
Object.assign(keys, { [realKey]: realKey });
}
})
return keys;
}
function freezeObj(obj) {
Object.keys(obj).forEach(key => {
let val = obj[key];
if (typeof val === "object") {
freezeObj(val)
}
})
Object.freeze(obj);
}
export function exportSDK(lib, funcs, ...initNames) {
let methods = {};
if (funcs && typeof funcs === 'object') {
methods = funcs;
} else {
methods = forMethods(lib);
}
const library = {};
Object.keys(methods).forEach(method => {
let methodItem = methods[method];
let methodName = methodItem && methodItem.method || method;
hook(library, method, (...params) => {
if (!isReadyCalled() && initNames && initNames.indexOf(method) < 0) {
throw `[IScan]:Can't call the "IScan.${method}" method, because "IScan" not ready, please confirm that "IScan.ready()" has been called. params: ${JSON.stringify(params)}`
}
return _exec(lib, methodName, ...params);
});
});
freezeObj(library);
return library;
}

13
src/index.js Normal file
View File

@@ -0,0 +1,13 @@
import core from "./_core";
import { exportSDK } from './_export';
const IScan = exportSDK(core, null, "config", "setStatusListener", "onScanListener",
"offScanListener", "stopScan", "startScan", "clear");
if (typeof window !== 'undefined') {
window.IScan = IScan
}
module.exports = {
IScan
}

31
src/polyfill/_es6.js Normal file
View File

@@ -0,0 +1,31 @@
import { polyfill } from 'es6-promise';
// Object.assign
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
polyfill();

24
src/polyfill/_global.js Normal file
View File

@@ -0,0 +1,24 @@
let _global = {};
// global
if (typeof GameGlobal !== 'undefined') {
_global = Object.assign({}, GameGlobal);
} else if (typeof window === 'undefined') {
_global = {
setTimeout: (...params) => {
return setTimeout(...params);
},
setInterval: (...params) => {
return setInterval(...params);
},
clearTimeout: (...params) => {
return clearTimeout(...params);
},
clearInterval: (...params) => {
return clearInterval(...params);
}
};
} else {
_global = window;
}
export default _global;

2
src/polyfill/index.js Normal file
View File

@@ -0,0 +1,2 @@
import './_global';
import './_es6';

View File

@@ -0,0 +1,137 @@
import { toAny } from "../../utils/toany";
import { printDebug, printWarn } from "../../utils/logger";
import { createUUID } from "../../utils/uuid";
let _events = {};
let _callbacks = {};
let _bridge = "__bridge_client__";
function _callRuntime(func, ...options) {
let funcs = func.split('.');
let instant = window;
while (funcs.length > 1) {
instant = instant[funcs.shift()];
}
if (instant && funcs.length == 1) {
if (instant.hasOwnProperty(funcs[0])) {
return instant[funcs[0]](...options);
}
}
}
function onCallback(name, callback) {
if (callback && typeof callback === 'function') {
_callbacks[name] = callback;
}
}
function _checkInit() {
let methodName = `${_bridge}_handle_callback`;
if (!window[methodName]) {
window[methodName] = (res) => {
let { method, payload, code, request_id } = toAny(res, {});
let data = toAny(payload, {});
if (request_id) {
_callbacks[request_id] && _callbacks[request_id](code, data);
} else if (_events[method]) {
_events[method].forEach((callback) => {
callback && callback(data);
});
}
}
}
}
export function inRuntime() {
return !!window[_bridge]
}
export function onEvent(method, callback) {
_checkInit();
if (method && callback && typeof callback == 'function') {
let list = _events[method] || [];
if (list.indexOf(callback) == -1) {
list.push(callback);
}
_events[method] = list;
}
}
export function offEvent(method, callback) {
_checkInit();
if (method && callback && typeof callback == 'function') {
let list = _events[method] || [];
let index = list.indexOf(callback);
if (index >= 0) {
list.splice(index, 1);
}
}
}
export function bridgeSync(method, data) {
_checkInit();
let result = toAny(_callRuntime(`${_bridge}.call`, method, toAny(data, "")));
let options = [method];
if (data) {
options.push("params:")
options.push(data)
}
if (result) {
options.push("result:")
options.push(result)
}
printDebug('bridge call >>>', ...options);
return result;
}
export function bridgeAsync(method, data, timeout) {
return new Promise((resolve, reject) => {
if (inRuntime()) {
let called = false;
let timeObj = null;
if (timeout > 0) {
timeObj = setTimeout(() => {
called = true;
timeObj = null;
reject("bridgeAsync timeout");
}, timeout);
}
let request_id = createUUID() + "_" + Date.now();
onCallback(request_id, (code, data) => {
if (called) {
return;
}
if (timeObj) {
clearTimeout(timeObj);
}
if (code == 0) {
resolve(data);
} else {
reject(data);
}
});
bridgeSync(method, Object.assign({ request_id }, data));
} else {
reject(`Can't bridgeAsync, because not in runtime`);
}
}).then(res => {
let options = [method];
if (data) {
options.push("params:")
options.push(data)
}
if (res) {
options.push("resp:")
options.push(res)
}
printDebug('bridge resp >>>', ...options);
return res;
}).catch(err => {
if (data) {
printWarn('bridge err >>>', method, "params:", data, err);
} else {
printWarn('bridge err >>>', method, err);
}
throw err;
})
}

27
src/services/config.js Normal file
View File

@@ -0,0 +1,27 @@
let _defineConfig = {
version: "${lib_version}"
}
let _defConfig = {
}
let _customConfig = {
}
export function getVersion() {
return _defineConfig.version;
}
export function getConfig(key) {
let item = _customConfig[key];
if (!item) {
item = _defConfig[key];
}
return item;
}
export function setConfig(config) {
if (config && typeof config == "object") {
Object.assign(_customConfig, config);
}
}

50
src/services/encrypt.js Normal file
View File

@@ -0,0 +1,50 @@
import ase from "browserify-cipher";
import { hex_md5 } from "../utils/md5";
import { toAny } from "../utils/toany";
import { printDebug } from "../utils/logger";
import { getAppId } from "./env";
let _encryptConfig = {
"transformation": "AES/CBC/PKCS5Padding",
"algorithm": "aes-128-cbc",
"key": "ohogame",
"key_size": 16
}
export function encryptData({ iv, data }) {
try {
let encrypt_data = data;
let app = getAppId();
let options = _encryptConfig;
let realIv = Buffer.from(hex_md5(iv + "_" + app).substring(4, 4 + options.key_size));
let realKey = hex_md5(options.key + "_" + app).substring(4, 4 + options.key_size);
let realData = Buffer.from(encrypt_data);
let decipher = ase.createCipheriv(options.algorithm, realKey, realIv)
decipher.setAutoPadding(true)
let decoded = decipher.update(realData, 'binary', 'base64');
decoded += decipher.final('base64');
return decoded;
} catch (e) {
printDebug("encryptData fail", e);
}
return "";
}
export function decryptData({ iv, data }) {
try {
let encrypt_data = data;
let app = getAppId();
let options = _encryptConfig;
let realIv = Buffer.from(hex_md5(iv + "_" + app).substring(4, 4 + options.key_size));
let realKey = hex_md5(options.key + "_" + app).substring(4, 4 + options.key_size);
let realData = Buffer.from(encrypt_data.split(" ").join("+"), 'base64');
let decipher = ase.createDecipheriv(options.algorithm, realKey, realIv)
decipher.setAutoPadding(true)
let decoded = decipher.update(realData, 'binary', 'utf8');
decoded += decipher.final('utf8');
return toAny(decoded);
} catch (e) {
printDebug("decryptData fail", e);
}
return "";
}

17
src/services/env.js Normal file
View File

@@ -0,0 +1,17 @@
import _global from "../polyfill/_global";
export function execFunc(target, func, ...options) {
if (target) {
let funcs = func.split('.');
let instant = target;
while (funcs.length > 1) {
instant = instant[funcs.shift()];
}
if (instant && funcs.length == 1) {
if (instant.hasOwnProperty(funcs[0])) {
return instant[funcs[0]](...options);
}
}
}
}

View File

@@ -0,0 +1,339 @@
import { inRuntime, bridgeAsync } from "../bridge/appbridge";
import { isSupportWebScan, startScanForWeb, stopScanForWeb, isSupportImageScan, startScanForImage } from "../web";
import { isSupportWxScan, startScanForWx } from "../wx";
import { startScanner, stopScanner } from "../scanner";
import { getConfig } from "../config";
let _scan_status = "closed";
let _scan_status_listener = null;
let _scan_listener_list = [];
let _scan_resolve = null;
let _scan_closing = false;
let _scan_next_start_time = 0;
const SCAN_RESTART_DELAY = 2000;
function __checkScanner() {
if (_scan_listener_list.length > 0) {
startScanner((result) => {
__scannerResult(result);
});
} else {
stopScanner();
}
}
function __match(result, match) {
let reg = null;
if (!!match && typeof match === "string") {
reg = new RegExp(match);
}
if (!!reg) {
return reg.test(result);
}
return true;
}
function __result(result) {
let matched = false;
for (let i = 0; i < _scan_listener_list.length; i++) {
const item = _scan_listener_list[i];
if (item.listener && __match(result, item.match)) {
matched = true;
item.listener({ result, key: item.key });
console.log("__result", { result, key: item.key });
}
}
if (matched) {
_scan_next_start_time = Date.now() + SCAN_RESTART_DELAY;
}
return matched;
}
function __hasMatchedListener(result) {
for (let i = 0; i < _scan_listener_list.length; i++) {
const item = _scan_listener_list[i];
if (item.listener && __match(result, item.match)) {
return true;
}
}
return false;
}
function __scanning() {
if (_scan_status !== "scanning") {
_scan_status = "scanning";
if (_scan_status_listener) {
_scan_status_listener({ status: "scanning" });
}
}
}
function __closed() {
if (_scan_status !== "closed") {
_scan_status = "closed";
if (_scan_status_listener) {
_scan_status_listener({ status: "closed" });
}
}
}
function __finishScan() {
const resolve = _scan_resolve;
_scan_resolve = null;
__closed();
return resolve;
}
function __stopCurrentScan() {
if (inRuntime()) {
return bridgeAsync("stopScan").catch(() => {
// no thing
});
} else if (isSupportWebScan()) {
return stopScanForWeb().catch(() => {
// no thing
});
}
return Promise.resolve();
}
function __scannerResult(result) {
if (!__hasMatchedListener(result)) {
return;
}
if (isScanning()) {
const resolve = __finishScan();
_scan_closing = true;
__stopCurrentScan().then(() => {
setTimeout(() => {
_scan_closing = false;
}, 0);
});
__result(result);
resolve && resolve({
result
});
return;
}
__result(result);
}
function __startBridgeScan() {
return bridgeAsync("startScan", {
closeable: true
}).then(resp => {
if (!isScanning()) {
return resp;
}
if (!resp || !resp.result) {
return resp;
}
if (__result(resp.result)) {
return resp;
}
if (isScanning()) {
return __startBridgeScan();
}
return resp;
}).catch(err => {
if (!isScanning()) {
return err;
}
if (!err || !err.result) {
return err;
}
if (__result(err.result)) {
return err;
}
if (isScanning()) {
return __startBridgeScan();
}
return err;
});
}
function __startWxScan() {
return startScanForWx({
needResult: 1,
scanType: ["qrCode", "barCode"]
}).then(resp => {
if (!isScanning()) {
return resp;
}
if (!resp || !resp.result) {
return resp;
}
if (__result(resp.result)) {
return resp;
}
if (isScanning()) {
return __startWxScan();
}
return resp;
}).catch(err => {
if (!isScanning()) {
return err;
}
if (!err || !err.result) {
return err;
}
if (__result(err.result)) {
return err;
}
if (isScanning()) {
return __startWxScan();
}
return err;
});
}
function __startImageScan() {
return startScanForImage().then(resp => {
if (!isScanning()) {
return resp;
}
if (!resp || !resp.result) {
return resp;
}
if (__result(resp.result)) {
return resp;
}
if (isScanning()) {
return __startImageScan();
}
return resp;
}).catch(err => {
if (!isScanning()) {
return err;
}
if (!err || !err.result) {
return err;
}
if (__result(err.result)) {
return err;
}
if (isScanning()) {
return __startImageScan();
}
return err;
});
}
export function isScanning() {
return _scan_status === "scanning";
}
export function clear() {
for (let i = 0; i < _scan_listener_list.length; i++) {
const item = _scan_listener_list[i];
item.cancel();
}
_scan_listener_list.length = 0;
__checkScanner();
}
export function onScanListener(listener, key, match, level) {
if (typeof listener !== 'function') {
return;
}
let item = null;
for (let i = 0; i < _scan_listener_list.length; i++) {
const _i = _scan_listener_list[i];
if (_i.listener === listener) {
item = _i;
break;
}
}
if (item) {
item.key = key;
item.level = level;
item.match = match;
} else {
item = {
key,
match,
level,
listener,
cancel: () => {
const index = _scan_listener_list.indexOf(item);
if (index !== -1) {
const items = _scan_listener_list.splice(index, 1);
for (let i = 0; i < items.length; i++) {
const item = items[i];
item.listener && item.listener({ cancel: 1 });
}
}
}
};
_scan_listener_list.push(item);
}
// 根据level排序
_scan_listener_list.sort((a, b) => b.level - a.level);
__checkScanner();
return item;
}
export function offScanListener(listener) {
for (let i = 0; i < _scan_listener_list.length; i++) {
const _i = _scan_listener_list[i];
if (_i.listener === listener) {
_i.cancel();
break;
}
}
__checkScanner();
}
export function setStatusListener(listener) {
if (typeof listener !== 'function') {
return;
}
_scan_status_listener = listener;
}
export function getStatus() {
return _scan_status;
}
export function stopScan() {
if (!isScanning()) {
return;
}
__stopCurrentScan().then(() => {
__closed();
});
}
export function startScan() {
if (isScanning() || _scan_closing || Date.now() < _scan_next_start_time) {
return;
}
Promise.resolve().then(() => {
__scanning();
let scannerPromise = new Promise(resolve => {
_scan_resolve = resolve;
});
let scanPromise = Promise.resolve();
if (inRuntime()) {
console.log("startScanForBridge");
scanPromise = __startBridgeScan();
} else if (isSupportWxScan()) {
console.log("startScanForWx");
scanPromise = __startWxScan();
} else if (isSupportWebScan()) {
console.log("startScanForWeb");
scanPromise = startScanForWeb(getConfig("webCanvasStyle"), __result);
} else if (isSupportImageScan()) {
console.log("startScanForImage");
scanPromise = __startImageScan();
} else {
console.log("not support scanner");
}
return Promise.race([scanPromise, scannerPromise]);
}).finally(() => {
_scan_resolve = null;
__closed();
});
}

View File

@@ -0,0 +1,90 @@
let _scannerCallback = null;
let _scannerStatus = "closed";
let _scannerValue = "";
let _scannerTimer = null;
let _scannerLastInputTime = 0;
const SCANNER_INPUT_INTERVAL = 100;
function clearScannerValue() {
_scannerValue = "";
_scannerLastInputTime = 0;
if (_scannerTimer) {
clearTimeout(_scannerTimer);
_scannerTimer = null;
}
}
function delayClearScannerValue() {
if (_scannerTimer) {
clearTimeout(_scannerTimer);
}
_scannerTimer = setTimeout(() => {
clearScannerValue();
}, SCANNER_INPUT_INTERVAL);
}
function normalizeScannerValue(value) {
return value.replace(/[\uFF01-\uFF5E]/g, char => {
return String.fromCharCode(char.charCodeAt(0) - 0xFEE0);
}).replace(/\u3002/g, ".");
}
function stopScannerEvent(event) {
event.preventDefault && event.preventDefault();
event.stopPropagation && event.stopPropagation();
}
function onScannerKeydown(event) {
if (_scannerStatus !== "scanning") {
return;
}
if (event.ctrlKey || event.metaKey || event.altKey) {
return;
}
if (event.key === "Enter") {
const result = normalizeScannerValue(_scannerValue);
if (result) {
stopScannerEvent(event);
}
console.log("onScannerKeydown", result);
clearScannerValue();
if (result && _scannerCallback) {
_scannerCallback(result);
}
return;
}
if (!event.key || event.key.length !== 1) {
return;
}
const now = Date.now();
if (_scannerLastInputTime && now - _scannerLastInputTime > SCANNER_INPUT_INTERVAL) {
clearScannerValue();
}
_scannerLastInputTime = now;
_scannerValue += event.key;
delayClearScannerValue();
}
export function startScanner(callback){
if (!callback || typeof callback !== "function") {
return;
}
_scannerCallback = callback;
if (_scannerStatus === "scanning") {
return;
}
_scannerStatus = "scanning";
clearScannerValue();
window.addEventListener("keydown", onScannerKeydown);
}
export function stopScanner(){
if (_scannerStatus !== "scanning") {
return;
}
_scannerStatus = "closed";
_scannerCallback = null;
clearScannerValue();
window.removeEventListener("keydown", onScannerKeydown);
}

327
src/services/web/index.js Normal file
View File

@@ -0,0 +1,327 @@
import { createUUID } from "../../utils/uuid";
import { getConfig } from "../config";
const scanWeb = {
uuid: null,
finish: true
}
function removeEl(id) {
try {
let el = document.getElementById(id);
document.body.removeChild(el);
} catch (error) {
}
}
function createEl(tagName, id, style, appendChild) {
let el = document.getElementById(id);
if (!el) {
el = document.createElement(tagName);
el.id = id;
el.style = style;
appendChild && document.body.appendChild(el);
}
return el;
}
function canvasDrawLine(context, width, begin, end, color) {
context.beginPath();
context.moveTo(width - begin.x, begin.y);
context.lineTo(width - end.x, end.y);
context.lineWidth = 4;
context.strokeStyle = color;
context.stroke();
}
function getBarcodeFormats(scanType) {
let formats = [];
if (!scanType) {
scanType = ["qrCode", "barCode"];
}
if (scanType.includes('qrCode')) {
formats.push('qr_code');
}
if (scanType.includes('barCode')) {
formats.push(
'ean_13',
'ean_8',
'code_128',
'code_39',
'codabar',
'upc_a',
'upc_e',
'itf',
'aztec',
'data_matrix',
'pdf417'
);
}
return formats;
}
function createBarcodeDetector(scanType) {
return Promise.resolve().then(() => {
if (typeof BarcodeDetector === 'undefined') {
throw new Error("BarcodeDetector is not supported");
}
const formats = getBarcodeFormats(scanType);
if (BarcodeDetector.getSupportedFormats) {
return BarcodeDetector.getSupportedFormats().then(supportedFormats => {
const supported = formats.filter(format => supportedFormats.indexOf(format) !== -1);
if (!supported.length) {
throw new Error("No supported barcode formats");
}
return new BarcodeDetector({ formats: supported });
});
}
return new BarcodeDetector({ formats });
});
}
function drawBarcode(context, width, barcode) {
const cornerPoints = barcode.cornerPoints;
if (cornerPoints && cornerPoints.length) {
for (let i = 0; i < cornerPoints.length; i++) {
canvasDrawLine(context, width, cornerPoints[i], cornerPoints[(i + 1) % cornerPoints.length], "#FF3B58");
}
return;
}
if (barcode.boundingBox) {
const rect = barcode.boundingBox;
const points = [
{ x: rect.x, y: rect.y },
{ x: rect.x + rect.width, y: rect.y },
{ x: rect.x + rect.width, y: rect.y + rect.height },
{ x: rect.x, y: rect.y + rect.height }
];
for (let i = 0; i < points.length; i++) {
canvasDrawLine(context, width, points[i], points[(i + 1) % points.length], "#FF3B58");
}
}
}
export function isSupportWebScan() {
return typeof navigator !== 'undefined'
&& navigator.mediaDevices
&& navigator.mediaDevices.getUserMedia
&& typeof BarcodeDetector !== 'undefined';
}
export function isSupportImageScan() {
return typeof document !== 'undefined'
&& typeof BarcodeDetector !== 'undefined'
&& typeof URL !== 'undefined'
&& URL.createObjectURL;
}
export function stopScanForWeb() {
return Promise.resolve().then(() => {
scanWeb.uuid = null;
})
}
function chooseImageFile() {
return new Promise(resolve => {
const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.style.display = "none";
let finished = false;
let finish = file => {
if (finished) {
return;
}
finished = true;
removeEl("__webscan_image_input__");
resolve(file);
};
input.id = "__webscan_image_input__";
input.onchange = () => {
finish(input.files && input.files[0]);
};
input.oncancel = () => {
finish(null);
};
document.body.appendChild(input);
input.click();
});
}
function detectImageFile(detector, file) {
if (!file) {
return Promise.resolve(null);
}
if (typeof createImageBitmap !== 'undefined') {
return createImageBitmap(file).then(image => {
return detector.detect(image).then(barcodes => {
image.close && image.close();
return barcodes && barcodes[0];
}).catch(err => {
image.close && image.close();
throw err;
});
});
}
return new Promise((resolve, reject) => {
const image = new Image();
const url = URL.createObjectURL(file);
image.onload = () => {
detector.detect(image).then(barcodes => {
URL.revokeObjectURL(url);
resolve(barcodes && barcodes[0]);
}).catch(err => {
URL.revokeObjectURL(url);
reject(err);
});
};
image.onerror = err => {
URL.revokeObjectURL(url);
reject(err);
};
image.src = url;
});
}
export function startScanForImage() {
return createBarcodeDetector(getConfig("scanType")).then(detector => {
return chooseImageFile().then(file => detectImageFile(detector, file));
}).then(code => {
if (code && code.rawValue) {
return {
result: code.rawValue
};
}
return {
success: false,
error: "未识别到二维码或条形码"
};
});
}
export function startScanForWeb(canvasStyle, onResult) {
return new Promise((resolve, reject) => {
try {
scanWeb.uuid = createUUID();
scanWeb.finish = false;
let videoEl = createEl("video",
"__webscan_video__",
"display: none", false);
let canvasDisplay = "";
let canvasBaseStyle = canvasStyle || "position: fixed; width: 300px; height: 240px; top: 0; left: 0; z-index: 9999;";
let canvasEl = createEl("canvas",
"__webscan_canvas__",
canvasBaseStyle + " display: none;", true);
canvasDisplay = canvasEl.style.display;
canvasEl.style.cssText = canvasBaseStyle;
canvasDisplay = canvasEl.style.display;
canvasEl.style.display = "none";
let context = canvasEl.getContext("2d");
let currentUuid = scanWeb.uuid;
videoEl.width = 300;
videoEl.height = 300;
videoEl.uuid = scanWeb.uuid;
createBarcodeDetector(getConfig("scanType")).then(detector => {
return navigator.mediaDevices.getUserMedia({
video: {
facingMode: "environment"
}
}).then(stream => {
return {
detector,
stream
};
});
}).then(function (options) {
const detector = options.detector;
const stream = options.stream;
videoEl.srcObject = stream;
videoEl.setAttribute("playsinline", true); // iOS使用
videoEl.play();
let closeWebScan = getConfig("closeWebScan", () => {
// no thing
});
let displayWebScan = getConfig("displayWebScan", (canvasEl, cancal) => {
// no thing
});
displayWebScan && displayWebScan(canvasEl, () => {
stopScanForWeb();
});
canvasEl.style.display = "none";
let detecting = false;
let displayed = false;
let closed = false;
let close = () => {
if (closed) {
return;
}
closed = true;
try {
stream.getTracks()[0].stop();
} catch (_e) { }
closeWebScan && closeWebScan();
};
let tick = () => {
try {
if (videoEl.readyState === videoEl.HAVE_ENOUGH_DATA && !detecting) {
canvasEl.height = videoEl.videoHeight;
canvasEl.width = videoEl.videoWidth;
context.setTransform(-1, 0, 0, 1, canvasEl.width, 0);
context.drawImage(videoEl, 0, 0, canvasEl.width, canvasEl.height);
context.setTransform(1, 0, 0, 1, 0, 0);
if (!displayed) {
displayed = true;
canvasEl.style.display = canvasDisplay || "";
}
detecting = true;
detector.detect(videoEl).then(barcodes => {
const code = barcodes && barcodes[0];
if (code && code.rawValue && scanWeb.uuid == currentUuid) {
if (onResult && !onResult(code.rawValue)) {
return;
}
drawBarcode(context, canvasEl.width, code);
scanWeb.uuid = null;
scanWeb.finish = true;
close();
resolve({
result: code.rawValue
})
}
}).catch(() => {
}).finally(() => {
detecting = false;
});
}
} catch (e) {
detecting = false;
}
if (scanWeb.uuid == currentUuid) {
requestAnimationFrame(() => {
tick()
});
} else {
if (!scanWeb.finish) {
reject({ cancel: 1 });
scanWeb.finish = true;
}
close();
}
if (scanWeb.finish) {
close();
}
};
requestAnimationFrame(() => {
tick();
});
}).catch(err => {
reject({ error: err });
});
} catch (e) {
reject({ error: e });
}
}).finally(() => {
removeEl("__webscan_video__");
removeEl("__webscan_canvas__");
})
}

162
src/services/wx/index.js Normal file
View File

@@ -0,0 +1,162 @@
import { getConfig } from "../config";
import { request } from "../../utils/request";
import { toAny } from "../../utils/toany";
const WX_JS_SDK_URL = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js";
const WX_SCAN_API = "scanQRCode";
let _wxReadyPromise = null;
let _wxReady = false;
function getWx() {
if (typeof window === "undefined") {
return null;
}
return window.wx;
}
function loadWxScript() {
return new Promise((resolve, reject) => {
const wx = getWx();
if (wx && wx.config && wx.scanQRCode) {
resolve(wx);
return;
}
let initWechatJssdk = toAny(getConfig("initWechatJssdk"), {});
let sdkUrl = toAny(initWechatJssdk.sdkUrl, WX_JS_SDK_URL);
if (!sdkUrl) {
reject(new Error("initWechatJssdk.sdkUrl is required, but not found"));
return;
}
let script = document.getElementById("__wx_jssdk__");
if (script) {
script.addEventListener("load", () => resolve(getWx()));
script.addEventListener("error", reject);
return;
}
script = document.createElement("script");
script.id = "__wx_jssdk__";
script.src = sdkUrl;
script.onload = () => resolve(getWx());
script.onerror = reject;
document.head.appendChild(script);
});
}
function fetchWxConfig() {
let initWechatJssdk = toAny(getConfig("initWechatJssdk"), {});
let apiUrl = toAny(initWechatJssdk.apiUrl, "");
if (!apiUrl) {
return Promise.reject(new Error("initWechatJssdk.apiUrl is required, but not found"));
}
return request({
url: apiUrl,
method: "GET",
data: {
url: window.location.href.split("#")[0]
}
}).then(res => {
let data = toAny(res.data, {});
if (!data) {
return null;
}
if (data.code !== 0) {
throw new Error(data.msg || "wx config fetch failed");
}
if (data.data) {
return data.data;
}
return data;
});
}
export function isWxEnv() {
return typeof navigator !== "undefined"
&& /micromessenger/i.test(navigator.userAgent || "");
}
export function isSupportWxScan() {
const wx = getWx();
return isWxEnv()
&& _wxReady
&& wx
&& wx.scanQRCode;
}
export function initWxJssdk() {
if (!isWxEnv()) {
return Promise.resolve();
}
if (_wxReadyPromise) {
return _wxReadyPromise;
}
_wxReadyPromise = Promise.all([loadWxScript(), fetchWxConfig()]).then(items => {
const wx = items[0];
const config = items[1];
if (!wx || !wx.config) {
throw new Error("wx jssdk is not ready");
}
if (!config) {
throw new Error("wx config is empty");
}
const jsApiList = config.jsApiList || [];
if (jsApiList.indexOf(WX_SCAN_API) === -1) {
jsApiList.push(WX_SCAN_API);
}
return new Promise((resolve, reject) => {
wx.ready(() => {
_wxReady = true;
resolve(wx);
});
wx.error(err => {
_wxReady = false;
_wxReadyPromise = null;
reject(err);
});
wx.config(Object.assign({}, config, {
jsApiList
}));
});
}).catch(err => {
_wxReady = false;
_wxReadyPromise = null;
throw err;
});
return _wxReadyPromise;
}
export function startScanForWx(options) {
return initWxJssdk().then(() => {
return new Promise((resolve, reject) => {
const {
needResult = 1,
scanType = ["qrCode", "barCode"]
} = options || {};
const wx = getWx();
if (!wx || !wx.scanQRCode) {
reject(new Error("wx.scanQRCode is not supported"));
return;
}
wx.scanQRCode({
needResult,
scanType,
success: res => {
resolve({
success: true,
result: res.resultStr,
code: res.resultStr
});
},
cancel: () => {
resolve({
success: false,
error: "用户取消扫码"
});
},
fail: err => {
reject(err);
}
});
});
});
}

86
src/utils/base64.js Normal file
View File

@@ -0,0 +1,86 @@
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function __utf8_encode(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
function __utf8_decode(utftext) {
var string = "", i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
export function encodeBase64(input) {
var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
input = __utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
}
export function decodeBase64(input) {
var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = __utf8_decode(output);
return output;
}

19
src/utils/hook.js Normal file
View File

@@ -0,0 +1,19 @@
export function addHook(target, methodName, hook, upsert = false) {
if (!target) {
return;
}
let method = target[methodName];
if (!method && !upsert) {
return;
}
if (method) {
// bind original method to target
method = method.bind(target);
}
Object.defineProperty(target, methodName, {
value: (...params) => hook(target, methodName, method, ...params),
enumerable: true,
writable: true,
configurable: true,
});
}

12
src/utils/logger.js Normal file
View File

@@ -0,0 +1,12 @@
export function printError(message, ...options) {
console.error("[IScan] ", message, ...options);
}
export function printWarn(message, ...options) {
console.warn("[IScan] ", message, ...options);
}
export function printDebug(message, ...options) {
console.log("[IScan] ", message, ...options);
}

65
src/utils/loop.js Normal file
View File

@@ -0,0 +1,65 @@
import _global from "../polyfill/_global";
const sLoopArr = [];
function requestAnimFrame(...params) {
let requestAnimationFrame =
_global.requestAnimationFrame ||
_global.mozRequestAnimationFrame ||
_global.webkitRequestAnimationFrame ||
_global.msRequestAnimationFrame ||
function (loop) {
return setTimeout(() => {
loop && loop(performance.now())
}, 1000 / 60);
};
return requestAnimationFrame(...params);
}
function runFrameLoop(...params) {
sLoopArr.forEach(function (loop) {
runLoop(loop, ...params);
});
if (sLoopArr.length > 0) {
requestAnimFrame(runFrameLoop);
}
}
function runLoop(obj, ...params) {
if (!obj) {
return;
}
obj.currentFrame++;
if (obj.maxCount > 0 && obj.currentCount >= obj.maxCount) {
obj.stop();
return;
}
if (obj.frameTime > 1 && obj.currentFrame % obj.frameTime != 0) {
return;
}
let state = obj.loopMethod && obj.loopMethod(...params);
if (state === false) {
obj.stop();
return;
}
obj.currentCount++;
}
export function frameLoop(loop, frame = 1, count = 0) {
let loopTask = {
currentCount: 0,
currentFrame: 0,
loopMethod: loop,
frameTime: frame,
maxCount: count,
stop: function () {
let index = sLoopArr.indexOf(this);
if (index >= 0) {
sLoopArr.splice(index, 1);
}
}
}
sLoopArr.push(loopTask);
requestAnimFrame(runFrameLoop);
return loopTask;
}

230
src/utils/md5.js Normal file
View File

@@ -0,0 +1,230 @@
/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len) {
/* append padding */
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);
}
/*
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t) {
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
}
function md5_ff(a, b, c, d, x, s, t) {
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t) {
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t) {
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t) {
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
/*
* Calculate the HMAC-MD5, of a key and some data
*/
function core_hmac_md5(key, data) {
var bkey = str2binl(key);
if (bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for (var i = 0; i < 16; i++) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
return core_md5(opad.concat(hash), 512 + 128);
}
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/*
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str) {
var bin = Array();
var mask = (1 << chrsz) - 1;
for (var i = 0; i < str.length * chrsz; i += chrsz)
bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (i % 32);
return bin;
}
/*
* Convert an array of little-endian words to a string
*/
function binl2str(bin) {
var str = "";
var mask = (1 << chrsz) - 1;
for (var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i >> 5] >>> (i % 32)) & mask);
return str;
}
/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray) {
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for (var i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) +
hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF);
}
return str;
}
/*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray) {
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for (var i = 0; i < binarray.length * 4; i += 3) {
var triplet = (((binarray[i >> 2] >> 8 * (i % 4)) & 0xFF) << 16)
| (((binarray[i + 1 >> 2] >> 8 * ((i + 1) % 4)) & 0xFF) << 8)
| ((binarray[i + 2 >> 2] >> 8 * ((i + 2) % 4)) & 0xFF);
for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
}
}
return str;
}
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
export function hex_md5(s) { return binl2hex(core_md5(str2binl(s), s.length * chrsz)); }
export function b64_md5(s) { return binl2b64(core_md5(str2binl(s), s.length * chrsz)); }
export function str_md5(s) { return binl2str(core_md5(str2binl(s), s.length * chrsz)); }
export function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
export function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
export function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

13
src/utils/object.js Normal file
View File

@@ -0,0 +1,13 @@
/**
* filter out undefined values
* @param options
*/
export function filterUndefinedValues(params) {
if (!params || typeof params !== 'object') {
return {};
}
return Object.keys(params).filter((key) => params[key] !== undefined).reduce((result, key) => {
result[key] = params[key];
return result;
}, {});
}

19
src/utils/onload.js Normal file
View File

@@ -0,0 +1,19 @@
let _ready = null;
export function onload() {
if (!_ready) {
_ready = new Promise((reslove, reject) => {
let _timer;
let _pollingDocument = function () {
if (!!document && document.readyState == 'complete') {
_timer && clearTimeout(_timer);
reslove();
} else {
_timer = setTimeout(_pollingDocument, 1);
}
};
_timer = setTimeout(_pollingDocument, 1);
});
}
return _ready;
}

80
src/utils/query.js Normal file
View File

@@ -0,0 +1,80 @@
import _global from "../polyfill/_global";
export function stringifyQuery(query) {
return Object.keys(query).map(key => {
const value = query[key];
return `${key}=${encodeURIComponent(value)}`;
}).join('&');
}
export function decodeQuery(query) {
return Object.keys(query).reduce((result, key) => {
const value = query[key];
result[key] = decodeURIComponent(value);
return result;
}, {});
}
export function parseQueryParams(path) {
let url = '';
let params = {};
if (!path || typeof path !== 'string') {
return { url, params }
}
let queryString = '';
let indexE = path.indexOf('?');
let indexW = path.indexOf('#');
let indexQ = path.indexOf('=');
if (indexE >= 0) {
url = path.substring(0, indexE);
queryString = path.substring(indexE + 1, indexW > 0 ? indexW : path.length);
} else if (indexQ >= 0) {
url = '';
queryString = path;
} else {
url = path;
queryString = '';
}
queryString.split('&').map(item => {
let index = item.indexOf("=");
if (index > 0) {
let key = item.substring(0, index);
let value = item.substring(index + 1);
params[key] = decodeURIComponent(value);
}
});
return { url, params };
}
export function appendQueryParams(path, query = {}) {
const { url, params } = parseQueryParams(path);
Object.keys(query).forEach(key => {
let value = query[key];
if (value !== undefined && value !== null) {
params[key] = value;
} else {
delete params[key];
}
});
const queryString = Object.keys(params).map(key => {
const value = params[key];
return `${key}=${encodeURIComponent(value)}`;
}).join('&');;
if (!url) {
return queryString;
}
if (!queryString) {
return url;
}
return `${url}?${queryString}`;
}
export function getQueryString(key, def) {
let search = _global.location.search;
var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
var r = search.substr(1).match(reg);
if (r != null) {
return decodeURIComponent(unescape(r[2]));
}
return def;
}

101
src/utils/request.js Normal file
View File

@@ -0,0 +1,101 @@
import { createUUID } from './uuid';
import { hex_md5 } from './md5';
import { appendQueryParams } from './query';
function _xhrRequest(options) {
var url = options['url'] || '';
var method = options['method'] || 'GET';
var headers = options['headers'] || {};
var data = options['data'] || {};
var json = options['json'] || false;
var timeout = options['timeout'] || 6000;
var req = new XMLHttpRequest();
var requestData;
if (method.toUpperCase() == "GET") {
url = appendQueryParams(url, data);
requestData = undefined;
} else if (method.toUpperCase() == "POST") {
if (json) {
requestData = JSON.stringify(data);
} else {
requestData = new FormData();
Object.keys(data).forEach(key => {
let value = data[key];
if (value instanceof File) {
requestData.append(key, value, value.name)
} else {
requestData.append(key, value)
}
})
}
} else {
options.fail && options.fail({
errMsg: 'request:un support ' + method
});
return;
}
req.timeout = timeout;
req.open(method.toUpperCase(), url, true);
if (headers) {
for (var k in headers) {
req.setRequestHeader(k, headers[k]);
}
}
if (json) {
req.setRequestHeader("Content-Type", "application/json");
}
req.onreadystatechange = function () {
if (req.readyState == 4) {
var header = req.getAllResponseHeaders();
var headers = {};
if (header) {
var hs = header.split('\n');
hs.forEach(element => {
if (element) {
var vs = element.split(': ');
if (vs.length > 1) {
headers[vs[0]] = vs[1];
}
}
});
}
if (req.status >= 200 && req.status < 300) {
var res = {
errMsg: 'request:ok'
}
try {
var data = JSON.parse(req.response);
res.data = data;
} catch (error) {
res.data = req.response;
}
res.header = headers;
res.statusCode = req.status;
options.success && options.success(res);
} else {
options.fail && options.fail({
errMsg: 'request:fail',
data: req.response,
header: headers
});
}
}
}
req.send(requestData);
return req;
}
export function request({ url, method, data, headers, json, timeout }) {
let body = Object.assign({ request_id: hex_md5(createUUID() + "_" + Date.now()) }, data)
return new Promise((resolve, reject) => {
_xhrRequest(Object.assign({}, { url, method, data: body, headers, json, timeout }, {
success: resolve,
fail: reject
}));
}).then(res => {
if (res.statusCode && res.statusCode != 200) {
throw res;
}
return res;
});
}

80
src/utils/toany.js Normal file
View File

@@ -0,0 +1,80 @@
function _toNumber_(value) {
try {
return JSON.parse(value);
} catch (ex) {
return parseFloat(value);
}
}
function _toBoolean_(value) {
return !!value && value != 'false' && value != '0';
}
function _toString_(value, def) {
try {
let type = typeof value;
if (type === 'string') {
return value;
} else if (type === 'boolean') {
return value ? 'true' : 'false';
} else if (type === 'number') {
return `${value}`;
} else if (type === 'object') {
return JSON.stringify(value);
}
} catch (ex) {
}
return def;
}
function _toObject_(value, def) {
if (typeof value === 'object') {
return value;
}
try {
return JSON.parse(value);
} catch (ex) {
}
return def;
}
function _inferType(value) {
if (typeof value !== 'string') {
return typeof value;
}
try {
const parsed = JSON.parse(value);
return typeof parsed;
} catch (ex) {
// might be a pure number
const number = parseFloat(value);
if (!isNaN(number) && `${number}` === value) {
return 'number';
}
return 'string';
}
}
export function toAny(value, def) {
if (value === undefined || value === null) {
return def;
}
// try to infer type and return
let type = _inferType(value);
if (def !== undefined && def !== null) {
type = typeof def;
}
switch (type) {
case 'number':
return _toNumber_(value);
case 'boolean':
return _toBoolean_(value);
case 'object':
return _toObject_(value, def);
case 'string':
return _toString_(value, def);
default:
break;
}
return def;
}

6
src/utils/uuid.js Normal file
View File

@@ -0,0 +1,6 @@
export function createUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

85
types/index.d.ts vendored Normal file
View File

@@ -0,0 +1,85 @@
/**
* 扫码初始化选项
*/
interface ScanConfigOptions {
/**
* 网页扫码canvas样式
*/
webCanvasStyle?: string,
/**
* 微信JSSDK配置配置后会自动初始化微信JSSDK
*/
initWechatJssdk: {
/**
* 微信JSSDK配置API地址调用接口会带上当前页面url作为参数
*/
apiUrl: string,
/**
* 微信JSSDK配置SDK地址默认为https://res.wx.qq.com/open/js/jweixin-1.6.0.js
*/
sdkUrl?: string,
/**
* 微信JSSDK配置JS-API列表默认追加["scanQRCode"]
*/
jsApiList?: string[]
}
}
/**
* 扫码选项
*/
interface ScanResult {
result: string,
key: string
}
/** IScan */
interface IScan {
/**
* 配置SDK
* @param options 配置选项
*/
config(options?: ScanConfigOptions): Promise<any>;
/**
* 监听扫码状态
* @param callback 监听回调
*/
setStatusListener(callback: (status: "scanning" | "closed") => any): void;
/**
* 监听扫码结果
* @param callback 监听回调
* @param key 监听key
* @param match 监听匹配
* @param level 监听级别
*/
onScanListener(callback: (result: ScanResult) => any, key: string, match: string, level: number): void;
/**
* 取消监听扫码结果
* @param callback 监听回调
*/
offScanListener(callback: (result: ScanResult) => any): void;
/**
* 获取扫码状态
* @returns "scanning" | "closed"
*/
getStatus(): "scanning" | "closed";
/**
* 关闭扫码
*/
stopScan(): void;
/**
* 开启扫码
*/
startScan(): void;
/**
* 清除全部监听
*/
clear(): void;
}
declare var IScan: IScan;
declare interface Window {
IScan: IScan;
}

13
webpack.analyzer.js Normal file
View File

@@ -0,0 +1,13 @@
const path = require('path');
const baseConfig = require('./webpack.base.js');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const config = Object.assign({}, baseConfig, {
plugins: [
...baseConfig.plugins,
new BundleAnalyzerPlugin()
]
});
config.output.path = path.resolve('./analyzer');
module.exports = config;

38
webpack.base.js Normal file
View File

@@ -0,0 +1,38 @@
const path = require('path');
const rules = require('./webpack.rules.js');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CpWebpackPlugin = require('cp-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const output = path.resolve('./dist');
const entry = {
'index': path.resolve('./src/index.js'),
}
const config = {
entry: entry,
output: {
path: output,
filename: '[name].js',
globalObject: 'this',
libraryTarget: 'var',
},
module: {
rules,
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'demo.html'
}),
new CpWebpackPlugin([
{ from: path.resolve('./types'), to: path.resolve('./dist') }
])
],
devtool: 'cheap-module-souce-map',
mode: 'development',
};
module.exports = config;

50
webpack.plugin.js Normal file
View File

@@ -0,0 +1,50 @@
const StrWebpackPlugin = require('str-webpack-plugin');
const { version } = require('./package.json');
function formatDate(input, fmt) {
let date;
if (input instanceof Date) {
date = input;
} else {
date = new Date(input);
}
var o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'S+': date.getMilliseconds()
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(String(o[k]).length)));
}
}
return fmt;
}
let libVersion = `${version}.${formatDate(new Date(), "MMdd.HHmm")}`
class EnvWebPackage extends StrWebpackPlugin {
constructor({ entry, output, env }) {
super(Object.keys(entry).map((name) => {
return {
src: output,
test: new RegExp(`${name}.js`, 'g'),
replace: {
form: [/\$\{lib_env\}/g, /\$\{lib_version\}/g],
to: [env, libVersion]
}
}
}))
}
}
module.exports = {
EnvWebPackage
}

30
webpack.pre.js Normal file
View File

@@ -0,0 +1,30 @@
const path = require('path');
const baseConfig = require('./webpack.base.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CpWebpackPlugin = require('cp-webpack-plugin');
const { EnvWebPackage } = require('./webpack.plugin.js');
const outputFrom = path.resolve('./dist/index.js');
const outputTo = path.resolve('./dist/lagged_tps.js');
module.exports = Object.assign({}, baseConfig, {
plugins: [
...baseConfig.plugins,
new UglifyJsPlugin({
uglifyOptions: {
output: {
comments: false
}
}
}),
new EnvWebPackage({
entry: baseConfig.entry,
output: baseConfig.output.path,
env: "pre"
}),
new CpWebpackPlugin([
{ from: outputFrom, to: outputTo }
]),
],
mode: 'production',
});

23
webpack.prod.js Normal file
View File

@@ -0,0 +1,23 @@
const baseConfig = require('./webpack.base.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { EnvWebPackage } = require('./webpack.plugin.js');
module.exports = Object.assign({}, baseConfig, {
plugins: [
...baseConfig.plugins,
new UglifyJsPlugin({
uglifyOptions: {
output: {
comments: false
}
}
}),
new EnvWebPackage({
entry: baseConfig.entry,
output: baseConfig.output.path,
env: "prod"
}),
],
devtool: 'cheap-module-souce-map',
mode: 'production',
});

14
webpack.qa.js Normal file
View File

@@ -0,0 +1,14 @@
const baseConfig = require('./webpack.base.js');
const { EnvWebPackage } = require('./webpack.plugin.js');
module.exports = Object.assign({}, baseConfig, {
plugins: [
...baseConfig.plugins,
new EnvWebPackage({
entry: baseConfig.entry,
output: baseConfig.output.path,
env: "qa"
})
],
mode: 'development',
});

42
webpack.rules.js Normal file
View File

@@ -0,0 +1,42 @@
const rules = [{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
['env', {
loose: true,
// debug: true,
}],
],
plugins: [
'transform-class-properties',
],
},
},
}, {
test: /\.(svg|png|jpg|gif)$/,
use: [{
loader: 'url-loader',
options: {
limit: 8192,
},
}],
}, {
test: /\.css$/,
use: [{
loader: 'style-loader',
}, {
loader: 'css-loader',
}, {
loader: 'postcss-loader',
options: {
plugins: () => [
require('autoprefixer')(),
],
},
}],
}];
module.exports = rules;

6999
yarn.lock Normal file

File diff suppressed because it is too large Load Diff