2025年04月18日09:09:00
This commit is contained in:
parent
d2ffa879f8
commit
963ccaac83
1
loaders/injectComponent/index.d.ts
vendored
1
loaders/injectComponent/index.d.ts
vendored
@ -1 +0,0 @@
|
||||
export default function (source: string): string;
|
@ -1,8 +0,0 @@
|
||||
|
||||
'use strict'
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
module.exports = require('./taro-inject-component-loader.cjs.production.min.js')
|
||||
} else {
|
||||
module.exports = require('./taro-inject-component-loader.cjs.development.js')
|
||||
}
|
@ -1,298 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
||||
|
||||
var generate = _interopDefault(require('@babel/generator'));
|
||||
var traverse = _interopDefault(require('@babel/traverse'));
|
||||
var utils = _interopDefault(require('@babel/types'));
|
||||
var parser = require('@babel/parser');
|
||||
var loaderUtils = require('loader-utils');
|
||||
var schemaUtils = require('schema-utils');
|
||||
|
||||
var schema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
importPath: {
|
||||
type: 'string'
|
||||
},
|
||||
isPage: {
|
||||
"instanceof": 'Function'
|
||||
},
|
||||
componentName: {
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
additionalProperties: false
|
||||
};
|
||||
function index (source) {
|
||||
// @ts-ignore
|
||||
var webpackEnv = this;
|
||||
var options = loaderUtils.getOptions(webpackEnv);
|
||||
schemaUtils.validate(schema, options, {
|
||||
name: 'taro-inject-component-loader'
|
||||
});
|
||||
|
||||
var _ref = options || {},
|
||||
_ref$importPath = _ref.importPath,
|
||||
importPath = _ref$importPath === void 0 ? '' : _ref$importPath,
|
||||
_ref$componentName = _ref.componentName,
|
||||
componentName = _ref$componentName === void 0 ? 'WebpackInjected' : _ref$componentName,
|
||||
_ref$isPage = _ref.isPage,
|
||||
isPage = _ref$isPage === void 0 ? defaultJudgePage : _ref$isPage; // 获取原始文件地址
|
||||
|
||||
|
||||
var filePath = webpackEnv.resourcePath;
|
||||
|
||||
if (typeof isPage === 'function' && isPage(filePath)) {
|
||||
// 生成 AST
|
||||
var ast = parser.parse(source, {
|
||||
sourceType: 'module',
|
||||
plugins: ['jsx', 'typescript', 'classProperties']
|
||||
}); // 如果有导入申明,则默认表示已手动导入了组件
|
||||
|
||||
var insert = false; // 保存所有顶层的声明
|
||||
|
||||
var declarations = new Map();
|
||||
traverse(ast, {
|
||||
// 查找是否有导入
|
||||
ImportDeclaration: function ImportDeclaration(path) {
|
||||
if (path.node.source.value === importPath) {
|
||||
insert = true;
|
||||
}
|
||||
},
|
||||
// 收集页面文件里的所有申明
|
||||
// 类组件
|
||||
ClassDeclaration: function ClassDeclaration(path) {
|
||||
// 如果不是顶层的申明,则直接返回
|
||||
if (path.parent.type !== 'Program') return;
|
||||
var type = path.node.type;
|
||||
var name = path.node.id.name;
|
||||
declarations.set(name, type);
|
||||
},
|
||||
// 函数申明
|
||||
FunctionDeclaration: function FunctionDeclaration(path) {
|
||||
var _path$node$id;
|
||||
|
||||
// 如果不是顶层的申明,则直接返回
|
||||
if (path.parent.type !== 'Program') return;
|
||||
var type = path.node.type;
|
||||
var name = (_path$node$id = path.node.id) == null ? void 0 : _path$node$id.name;
|
||||
if (!name) return;
|
||||
declarations.set(name, type);
|
||||
},
|
||||
// 表达式申明
|
||||
VariableDeclaration: function VariableDeclaration(path) {
|
||||
// 如果不是顶层的申明,则直接返回
|
||||
if (path.parent.type !== 'Program') return;
|
||||
path.node.declarations.forEach(function (declaration) {
|
||||
var _declaration$init, _declaration$init3, _declaration$init4;
|
||||
|
||||
// const a = () => {}
|
||||
if (((_declaration$init = declaration.init) == null ? void 0 : _declaration$init.type) === 'ArrowFunctionExpression') {
|
||||
var _declaration$init2, _declaration$id;
|
||||
|
||||
var type = (_declaration$init2 = declaration.init) == null ? void 0 : _declaration$init2.type;
|
||||
var name = (_declaration$id = declaration.id) == null ? void 0 : _declaration$id.name;
|
||||
declarations.set(name, type);
|
||||
} // const a = function(){}
|
||||
|
||||
|
||||
if (((_declaration$init3 = declaration.init) == null ? void 0 : _declaration$init3.type) === 'FunctionExpression') {
|
||||
var _type = declaration.init.type;
|
||||
var _name = declaration.id.name;
|
||||
declarations.set(_name, _type);
|
||||
} // const a = class {}
|
||||
|
||||
|
||||
if (((_declaration$init4 = declaration.init) == null ? void 0 : _declaration$init4.type) === 'ClassExpression') {
|
||||
var _type2 = declaration.init.type;
|
||||
var _name2 = declaration.id.name;
|
||||
declarations.set(_name2, _type2);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (!insert) {
|
||||
// 记录组件插入状态
|
||||
var state = {
|
||||
importedDeclaration: false,
|
||||
importedComponent: false
|
||||
};
|
||||
traverse(ast, {
|
||||
// 添加申明
|
||||
ImportDeclaration: function ImportDeclaration(path) {
|
||||
if (!state.importedDeclaration) {
|
||||
state.importedDeclaration = true;
|
||||
path.insertBefore(utils.importDeclaration([utils.importDefaultSpecifier(utils.identifier('' + componentName))], utils.stringLiteral('' + importPath)));
|
||||
}
|
||||
},
|
||||
// 默认导出的为页面组件
|
||||
ExportDefaultDeclaration: function ExportDefaultDeclaration(path) {
|
||||
// 如果默认导出的是函数
|
||||
if (path.node.declaration.type === 'FunctionDeclaration') {
|
||||
var mainFnBody = path.node.declaration.body.body;
|
||||
var length = mainFnBody.length;
|
||||
var last = mainFnBody[length - 1];
|
||||
insertComponent(last, '' + componentName, state);
|
||||
} // 默认导出箭头函数
|
||||
|
||||
|
||||
if (path.node.declaration.type === 'ArrowFunctionExpression') {
|
||||
// export default () => { return <View></View> }
|
||||
if (path.node.declaration.body.type === 'BlockStatement') {
|
||||
var _mainFnBody = path.node.declaration.body.body;
|
||||
var _length = _mainFnBody.length;
|
||||
var _last = _mainFnBody[_length - 1];
|
||||
insertComponent(_last, '' + componentName, state);
|
||||
} else {
|
||||
// export default () => <View></View>
|
||||
insertComponent(path.node.declaration.body, '' + componentName, state);
|
||||
}
|
||||
} // 默认导出类
|
||||
|
||||
|
||||
if (path.node.declaration.type === 'ClassDeclaration') {
|
||||
traverse(path.node, {
|
||||
ClassMethod: function ClassMethod(path) {
|
||||
if (path.node.key.name === 'render') {
|
||||
var body = path.node.body.body || [];
|
||||
var _last2 = body[body.length - 1];
|
||||
insertComponent(_last2, '' + componentName, state);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, path.scope, path);
|
||||
} // 如果默认导出的是一个申明
|
||||
|
||||
|
||||
if (path.node.declaration.type === "Identifier") {
|
||||
var name = path.node.declaration.name;
|
||||
var componentType = declarations.get(name);
|
||||
traverse(path.parent, {
|
||||
FunctionDeclaration: function FunctionDeclaration(path) {
|
||||
var _path$node$id2, _path$node, _path$node$body;
|
||||
|
||||
if (((_path$node$id2 = path.node.id) == null ? void 0 : _path$node$id2.name) !== name) return;
|
||||
var mainFnBody = (_path$node = path.node) == null ? void 0 : (_path$node$body = _path$node.body) == null ? void 0 : _path$node$body.body;
|
||||
var length = mainFnBody.length;
|
||||
var last = mainFnBody[length - 1];
|
||||
insertComponent(last, '' + componentName, state);
|
||||
},
|
||||
ClassDeclaration: function ClassDeclaration(path) {
|
||||
if (path.node.id.name !== name) return;
|
||||
traverse(path.node, {
|
||||
ClassMethod: function ClassMethod(path) {
|
||||
var _path$node$key;
|
||||
|
||||
if (((_path$node$key = path.node.key) == null ? void 0 : _path$node$key.name) !== 'render') return;
|
||||
var body = path.node.body.body || [];
|
||||
var last = body[body.length - 1];
|
||||
insertComponent(last, '' + componentName, state);
|
||||
}
|
||||
}, path.scope, path);
|
||||
},
|
||||
VariableDeclarator: function VariableDeclarator(path) {
|
||||
if (path.node.id.type !== 'Identifier') return;
|
||||
if (path.node.id.name !== name) return;
|
||||
if (!path.node.init) return;
|
||||
if (path.node.init.type !== componentType) return;
|
||||
|
||||
if (path.node.init.type === 'FunctionExpression') {
|
||||
var _mainFnBody2 = path.node.init.body.body;
|
||||
var _length2 = _mainFnBody2.length;
|
||||
var _last3 = _mainFnBody2[_length2 - 1];
|
||||
insertComponent(_last3, '' + componentName, state);
|
||||
}
|
||||
|
||||
if (path.node.init.type === 'ClassExpression') {
|
||||
traverse(path.node, {
|
||||
ClassMethod: function ClassMethod(path) {
|
||||
if (path.node.key.name !== 'render') return;
|
||||
var body = path.node.body.body || [];
|
||||
var last = body[body.length - 1];
|
||||
insertComponent(last, '' + componentName, state);
|
||||
}
|
||||
}, path.scope, path);
|
||||
}
|
||||
|
||||
if (path.node.init.type === 'ArrowFunctionExpression') {
|
||||
// const A = () => {}
|
||||
// export default A
|
||||
if (path.node.init.body.type == 'BlockStatement') {
|
||||
var _mainFnBody3 = path.node.init.body.body;
|
||||
var _length3 = _mainFnBody3.length;
|
||||
var _last4 = _mainFnBody3[_length3 - 1];
|
||||
insertComponent(_last4, '' + componentName, state);
|
||||
} else {
|
||||
// const A = () => <div></div>
|
||||
// export default A
|
||||
insertComponent(path.node.init.body, '' + componentName, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!state.importedComponent) {
|
||||
webpackEnv.emitWarning("\u9875\u9762: " + filePath + " \u6CE8\u5165\u7EC4\u4EF6\u5931\u8D25\uFF0C\u5EFA\u8BAE\u624B\u52A8\u5F15\u5165\u7EC4\u4EF6\u3002\u7EC4\u4EF6\u6CE8\u5165\u9650\u5236\u8BF7\u67E5\u9605: https://github.com/xdoer/taro-inject-component-loader");
|
||||
}
|
||||
|
||||
if (!state.importedDeclaration) {
|
||||
webpackEnv.emitWarning("\u9875\u9762: " + filePath + " \u6CE8\u5165\u5BFC\u5165\u7533\u660E\u5931\u8D25\uFF0C\u5EFA\u8BAE\u624B\u52A8\u5F15\u5165\u7EC4\u4EF6\u3002\u7EC4\u4EF6\u6CE8\u5165\u9650\u5236\u8BF7\u67E5\u9605: https://github.com/xdoer/taro-inject-component-loader");
|
||||
}
|
||||
|
||||
source = generate(ast).code;
|
||||
}
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
function createElement(name) {
|
||||
var reactIdentifier = utils.identifier('React');
|
||||
var createElementIdentifier = utils.identifier('createElement');
|
||||
var callee = utils.memberExpression(reactIdentifier, createElementIdentifier);
|
||||
return utils.callExpression(callee, [utils.identifier(name)]);
|
||||
}
|
||||
|
||||
function createJSX(name) {
|
||||
return utils.jSXElement(utils.jSXOpeningElement(utils.jsxIdentifier('' + name), [], true), null, [], true);
|
||||
}
|
||||
|
||||
function insertComponent(node, componentName, state) {
|
||||
if ((node == null ? void 0 : node.type) === 'ReturnStatement') {
|
||||
var _node$argument, _node$argument$callee, _node$argument$callee2, _node$argument2;
|
||||
|
||||
// createElement
|
||||
if (((_node$argument = node.argument) == null ? void 0 : (_node$argument$callee = _node$argument.callee) == null ? void 0 : (_node$argument$callee2 = _node$argument$callee.property) == null ? void 0 : _node$argument$callee2.name) === 'createElement' && !state.importedComponent) {
|
||||
state.importedComponent = true;
|
||||
var reactCreateArguments = node.argument.arguments;
|
||||
reactCreateArguments.push(createElement(componentName));
|
||||
} // JSX
|
||||
|
||||
|
||||
if (((_node$argument2 = node.argument) == null ? void 0 : _node$argument2.type) === 'JSXElement' && !state.importedComponent) {
|
||||
state.importedComponent = true;
|
||||
node.argument.children.push(createJSX(componentName));
|
||||
}
|
||||
}
|
||||
|
||||
if (node.type === 'JSXElement' && !state.importedComponent) {
|
||||
state.importedComponent = true;
|
||||
node.children.push(createJSX(componentName));
|
||||
}
|
||||
}
|
||||
|
||||
function defaultJudgePage(filePath) {
|
||||
// 兼容 windows 路径
|
||||
var formatFilePath = filePath.replace(/\\/g, '/');
|
||||
return /(package-.+\/)?pages\/[A-Za-z0-9-]+\/index\.[tj]sx$/.test(formatFilePath);
|
||||
}
|
||||
|
||||
exports.default = index;
|
||||
//# sourceMappingURL=taro-inject-component-loader.cjs.development.js.map
|
File diff suppressed because one or more lines are too long
@ -1,2 +0,0 @@
|
||||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var n=e(require("@babel/generator")),t=e(require("@babel/traverse")),o=e(require("@babel/types")),i=require("@babel/parser"),r=require("loader-utils"),a=require("schema-utils"),d={type:"object",properties:{importPath:{type:"string"},isPage:{instanceof:"Function"},componentName:{type:"string"}},additionalProperties:!1};function l(e){return o.jSXElement(o.jSXOpeningElement(o.jsxIdentifier(""+e),[],!0),null,[],!0)}function p(e,n,t){var i,r,a,d,p,c,s,u;"ReturnStatement"===(null==e?void 0:e.type)&&("createElement"!==(null==(i=e.argument)||null==(r=i.callee)||null==(a=r.property)?void 0:a.name)||t.importedComponent||(t.importedComponent=!0,e.argument.arguments.push((p=n,c=o.identifier("React"),s=o.identifier("createElement"),u=o.memberExpression(c,s),o.callExpression(u,[o.identifier(p)])))),"JSXElement"!==(null==(d=e.argument)?void 0:d.type)||t.importedComponent||(t.importedComponent=!0,e.argument.children.push(l(n)))),"JSXElement"!==e.type||t.importedComponent||(t.importedComponent=!0,e.children.push(l(n)))}function c(e){var n=e.replace(/\\/g,"/");return/(package-.+\/)?pages\/[A-Za-z0-9-]+\/index\.[tj]sx$/.test(n)}exports.default=function(e){var l=r.getOptions(this);a.validate(d,l,{name:"taro-inject-component-loader"});var s=l||{},u=s.importPath,m=void 0===u?"":u,y=s.componentName,f=void 0===y?"WebpackInjected":y,v=s.isPage,b=void 0===v?c:v,g=this.resourcePath;if("function"==typeof b&&b(g)){var h=i.parse(e,{sourceType:"module",plugins:["jsx","typescript","classProperties"]}),x=!1,D=new Map;if(t(h,{ImportDeclaration:function(e){e.node.source.value===m&&(x=!0)},ClassDeclaration:function(e){"Program"===e.parent.type&&D.set(e.node.id.name,e.node.type)},FunctionDeclaration:function(e){var n;if("Program"===e.parent.type){var t=null==(n=e.node.id)?void 0:n.name;t&&D.set(t,e.node.type)}},VariableDeclaration:function(e){"Program"===e.parent.type&&e.node.declarations.forEach((function(e){var n,t,o;if("ArrowFunctionExpression"===(null==(n=e.init)?void 0:n.type)){var i,r,a=null==(i=e.init)?void 0:i.type,d=null==(r=e.id)?void 0:r.name;D.set(d,a)}"FunctionExpression"===(null==(t=e.init)?void 0:t.type)&&D.set(e.id.name,e.init.type),"ClassExpression"===(null==(o=e.init)?void 0:o.type)&&D.set(e.id.name,e.init.type)}))}}),!x){var E={importedDeclaration:!1,importedComponent:!1};t(h,{ImportDeclaration:function(e){E.importedDeclaration||(E.importedDeclaration=!0,e.insertBefore(o.importDeclaration([o.importDefaultSpecifier(o.identifier(""+f))],o.stringLiteral(""+m))))},ExportDefaultDeclaration:function(e){if("FunctionDeclaration"===e.node.declaration.type){var n=e.node.declaration.body.body;p(n[n.length-1],""+f,E)}if("ArrowFunctionExpression"===e.node.declaration.type)if("BlockStatement"===e.node.declaration.body.type){var o=e.node.declaration.body.body;p(o[o.length-1],""+f,E)}else p(e.node.declaration.body,""+f,E);if("ClassDeclaration"===e.node.declaration.type&&t(e.node,{ClassMethod:function(e){if("render"!==e.node.key.name);else{var n=e.node.body.body||[];p(n[n.length-1],""+f,E)}}},e.scope,e),"Identifier"===e.node.declaration.type){var i=e.node.declaration.name,r=D.get(i);t(e.parent,{FunctionDeclaration:function(e){var n,t,o;if((null==(n=e.node.id)?void 0:n.name)===i){var r=null==(t=e.node)||null==(o=t.body)?void 0:o.body;p(r[r.length-1],""+f,E)}},ClassDeclaration:function(e){e.node.id.name===i&&t(e.node,{ClassMethod:function(e){var n;if("render"===(null==(n=e.node.key)?void 0:n.name)){var t=e.node.body.body||[];p(t[t.length-1],""+f,E)}}},e.scope,e)},VariableDeclarator:function(e){if("Identifier"===e.node.id.type&&e.node.id.name===i&&e.node.init&&e.node.init.type===r){if("FunctionExpression"===e.node.init.type){var n=e.node.init.body.body;p(n[n.length-1],""+f,E)}if("ClassExpression"===e.node.init.type&&t(e.node,{ClassMethod:function(e){if("render"===e.node.key.name){var n=e.node.body.body||[];p(n[n.length-1],""+f,E)}}},e.scope,e),"ArrowFunctionExpression"===e.node.init.type)if("BlockStatement"==e.node.init.body.type){var o=e.node.init.body.body;p(o[o.length-1],""+f,E)}else p(e.node.init.body,""+f,E)}}})}}}),E.importedComponent||this.emitWarning("页面: "+g+" 注入组件失败,建议手动引入组件。组件注入限制请查阅: https://github.com/xdoer/taro-inject-component-loader"),E.importedDeclaration||this.emitWarning("页面: "+g+" 注入导入申明失败,建议手动引入组件。组件注入限制请查阅: https://github.com/xdoer/taro-inject-component-loader"),e=n(h).code}}return e};
|
||||
//# sourceMappingURL=taro-inject-component-loader.cjs.production.min.js.map
|
File diff suppressed because one or more lines are too long
@ -1,68 +0,0 @@
|
||||
{
|
||||
"version": "2.1.0",
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"homepage": "https://github.com/xdoer/taro-inject-component-loader",
|
||||
"bugs": {
|
||||
"url": "https://github.com/xdoer/taro-inject-component-loader/issues",
|
||||
"email": "gotoanything@foxmail.com"
|
||||
},
|
||||
"keywords": [
|
||||
"taro",
|
||||
"react",
|
||||
"loader",
|
||||
"webpack",
|
||||
"inject"
|
||||
],
|
||||
"scripts": {
|
||||
"start": "tsdx watch --format cjs",
|
||||
"build": "tsdx build --format cjs",
|
||||
"test": "tsdx test",
|
||||
"lint": "tsdx lint",
|
||||
"prepare": "tsdx build --format cjs",
|
||||
"size": "size-limit",
|
||||
"analyze": "size-limit --why"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "tsdx lint"
|
||||
}
|
||||
},
|
||||
"name": "taro-inject-component-loader",
|
||||
"author": "xdoer",
|
||||
"module": "dist/taro-inject-component-loader.esm.js",
|
||||
"size-limit": [
|
||||
{
|
||||
"path": "dist/taro-inject-component-loader.cjs.production.min.js",
|
||||
"limit": "10 KB"
|
||||
},
|
||||
{
|
||||
"path": "dist/taro-inject-component-loader.esm.js",
|
||||
"limit": "10 KB"
|
||||
}
|
||||
],
|
||||
"peerDependencies": {
|
||||
"webpack": "^4.0.0 || ^5.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"loader-utils": "^2.0.0",
|
||||
"schema-utils": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@size-limit/preset-small-lib": "^4.9.1",
|
||||
"@types/loader-utils": "^2.0.1",
|
||||
"@types/schema-utils": "^2.4.0",
|
||||
"husky": "^4.3.6",
|
||||
"size-limit": "^4.9.1",
|
||||
"tsdx": "^0.14.1",
|
||||
"tslib": "^2.0.3",
|
||||
"typescript": "^4.1.3"
|
||||
}
|
||||
}
|
@ -1,295 +0,0 @@
|
||||
import generate from '@babel/generator'
|
||||
import traverse from '@babel/traverse'
|
||||
import utils from '@babel/types'
|
||||
import { parse } from '@babel/parser'
|
||||
import { getOptions } from 'loader-utils'
|
||||
import { validate } from 'schema-utils'
|
||||
|
||||
const schema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
importPath: {
|
||||
type: 'string',
|
||||
},
|
||||
isPage: {
|
||||
instanceof: 'Function',
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
}
|
||||
|
||||
export default function (source: string) {
|
||||
// @ts-ignore
|
||||
const webpackEnv = this
|
||||
|
||||
const options = getOptions(webpackEnv)
|
||||
|
||||
validate(schema as any, options, { name: 'taro-inject-component-loader' })
|
||||
|
||||
const { importPath = '', componentName = 'WebpackInjected', isPage = defaultJudgePage } = options || {}
|
||||
|
||||
// 获取原始文件地址
|
||||
const filePath = webpackEnv.resourcePath
|
||||
|
||||
if (typeof isPage === 'function' && isPage(filePath)) {
|
||||
// 生成 AST
|
||||
const ast: any = parse(source, {
|
||||
sourceType: 'module',
|
||||
plugins: ['jsx', 'typescript', 'classProperties'],
|
||||
})
|
||||
|
||||
// 如果有导入申明,则默认表示已手动导入了组件
|
||||
let insert = false
|
||||
|
||||
// 保存所有顶层的声明
|
||||
const declarations = new Map()
|
||||
|
||||
traverse(ast, {
|
||||
// 查找是否有导入
|
||||
ImportDeclaration(path) {
|
||||
if (path.node.source.value === importPath) {
|
||||
insert = true
|
||||
}
|
||||
},
|
||||
|
||||
// 收集页面文件里的所有申明
|
||||
// 类组件
|
||||
ClassDeclaration(path) {
|
||||
// 如果不是顶层的申明,则直接返回
|
||||
if (path.parent.type !== 'Program') return
|
||||
|
||||
const type = path.node.type
|
||||
const name = path.node.id.name
|
||||
declarations.set(name, type)
|
||||
},
|
||||
|
||||
// 函数申明
|
||||
FunctionDeclaration(path) {
|
||||
// 如果不是顶层的申明,则直接返回
|
||||
if (path.parent.type !== 'Program') return
|
||||
|
||||
const type = path.node.type
|
||||
const name = path.node.id?.name
|
||||
if (!name) return
|
||||
|
||||
declarations.set(name, type)
|
||||
},
|
||||
|
||||
// 表达式申明
|
||||
VariableDeclaration(path) {
|
||||
// 如果不是顶层的申明,则直接返回
|
||||
if (path.parent.type !== 'Program') return
|
||||
|
||||
path.node.declarations.forEach((declaration: any) => {
|
||||
|
||||
// const a = () => {}
|
||||
if (declaration.init?.type === 'ArrowFunctionExpression') {
|
||||
const type = declaration.init?.type
|
||||
const name = declaration.id?.name
|
||||
declarations.set(name, type)
|
||||
}
|
||||
|
||||
// const a = function(){}
|
||||
if (declaration.init?.type === 'FunctionExpression') {
|
||||
const type = declaration.init.type
|
||||
const name = declaration.id.name
|
||||
declarations.set(name, type)
|
||||
}
|
||||
|
||||
// const a = class {}
|
||||
if (declaration.init?.type === 'ClassExpression') {
|
||||
const type = declaration.init.type
|
||||
const name = declaration.id.name
|
||||
declarations.set(name, type)
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
if (!insert) {
|
||||
// 记录组件插入状态
|
||||
const state = {
|
||||
importedDeclaration: false,
|
||||
importedComponent: false,
|
||||
}
|
||||
|
||||
traverse(ast, {
|
||||
// 添加申明
|
||||
ImportDeclaration(path) {
|
||||
if (!state.importedDeclaration) {
|
||||
state.importedDeclaration = true
|
||||
path.insertBefore(
|
||||
utils.importDeclaration(
|
||||
[
|
||||
utils.importDefaultSpecifier(utils.identifier('' + componentName)),
|
||||
],
|
||||
utils.stringLiteral('' + importPath),
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
|
||||
// 默认导出的为页面组件
|
||||
ExportDefaultDeclaration(path) {
|
||||
|
||||
// 如果默认导出的是函数
|
||||
if (path.node.declaration.type === 'FunctionDeclaration') {
|
||||
const mainFnBody = path.node.declaration.body.body
|
||||
const length = mainFnBody.length
|
||||
const last = mainFnBody[length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
}
|
||||
|
||||
// 默认导出箭头函数
|
||||
if (path.node.declaration.type === 'ArrowFunctionExpression') {
|
||||
// export default () => { return <View></View> }
|
||||
if (path.node.declaration.body.type === 'BlockStatement') {
|
||||
const mainFnBody = path.node.declaration.body.body
|
||||
const length = mainFnBody.length
|
||||
const last = mainFnBody[length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
} else {
|
||||
// export default () => <View></View>
|
||||
insertComponent(path.node.declaration.body, '' + componentName, state)
|
||||
}
|
||||
}
|
||||
|
||||
// 默认导出类
|
||||
if (path.node.declaration.type === 'ClassDeclaration') {
|
||||
traverse(path.node, {
|
||||
ClassMethod(path) {
|
||||
if ((path.node.key as any).name === 'render') {
|
||||
const body = path.node.body.body || []
|
||||
const last = body[body.length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
return
|
||||
}
|
||||
},
|
||||
}, path.scope, path)
|
||||
}
|
||||
|
||||
// 如果默认导出的是一个申明
|
||||
if (path.node.declaration.type === "Identifier") {
|
||||
const name = path.node.declaration.name
|
||||
const componentType = declarations.get(name)
|
||||
|
||||
traverse(path.parent, {
|
||||
FunctionDeclaration(path) {
|
||||
if (path.node.id?.name !== name) return
|
||||
const mainFnBody = path.node?.body?.body
|
||||
const length = mainFnBody.length
|
||||
const last = mainFnBody[length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
},
|
||||
ClassDeclaration(path) {
|
||||
if (path.node.id.name !== name) return
|
||||
traverse(path.node, {
|
||||
ClassMethod(path) {
|
||||
if ((path.node.key as any)?.name !== 'render') return
|
||||
const body = path.node.body.body || []
|
||||
const last = body[body.length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
},
|
||||
}, path.scope, path)
|
||||
},
|
||||
VariableDeclarator(path) {
|
||||
if (path.node.id.type !== 'Identifier') return
|
||||
if (path.node.id.name !== name) return
|
||||
if (!path.node.init) return
|
||||
|
||||
if (path.node.init.type !== componentType) return
|
||||
|
||||
if (path.node.init.type === 'FunctionExpression') {
|
||||
const mainFnBody = path.node.init.body.body
|
||||
const length = mainFnBody.length
|
||||
const last = mainFnBody[length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
}
|
||||
|
||||
if (path.node.init.type === 'ClassExpression') {
|
||||
traverse(path.node, {
|
||||
ClassMethod(path) {
|
||||
if ((path.node.key as any).name !== 'render') return
|
||||
const body = path.node.body.body || []
|
||||
const last = body[body.length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
},
|
||||
}, path.scope, path)
|
||||
}
|
||||
|
||||
if (path.node.init.type === 'ArrowFunctionExpression') {
|
||||
// const A = () => {}
|
||||
// export default A
|
||||
if (path.node.init.body.type == 'BlockStatement') {
|
||||
const mainFnBody = path.node.init.body.body
|
||||
const length = mainFnBody.length
|
||||
const last = mainFnBody[length - 1]
|
||||
insertComponent(last, '' + componentName, state)
|
||||
} else {
|
||||
// const A = () => <div></div>
|
||||
// export default A
|
||||
insertComponent(path.node.init.body, '' + componentName, state)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
if (!state.importedComponent) {
|
||||
webpackEnv.emitWarning(`页面: ${filePath} 注入组件失败,建议手动引入组件。组件注入限制请查阅: https://github.com/xdoer/taro-inject-component-loader`)
|
||||
|
||||
}
|
||||
if (!state.importedDeclaration) {
|
||||
webpackEnv.emitWarning(`页面: ${filePath} 注入导入申明失败,建议手动引入组件。组件注入限制请查阅: https://github.com/xdoer/taro-inject-component-loader`)
|
||||
}
|
||||
|
||||
source = generate(ast).code
|
||||
}
|
||||
}
|
||||
|
||||
return source
|
||||
}
|
||||
|
||||
|
||||
function createElement(name: string) {
|
||||
const reactIdentifier = utils.identifier('React')
|
||||
const createElementIdentifier = utils.identifier('createElement')
|
||||
const callee = utils.memberExpression(reactIdentifier, createElementIdentifier)
|
||||
return utils.callExpression(callee, [utils.identifier(name)])
|
||||
}
|
||||
|
||||
function createJSX(name: string) {
|
||||
return utils.jSXElement(
|
||||
utils.jSXOpeningElement(utils.jsxIdentifier('' + name), [], true),
|
||||
null,
|
||||
[],
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
function insertComponent(node: any, componentName: string, state: any) {
|
||||
if (node?.type === 'ReturnStatement') {
|
||||
// createElement
|
||||
if (node.argument?.callee?.property?.name === 'createElement' && !state.importedComponent) {
|
||||
state.importedComponent = true
|
||||
const reactCreateArguments = node.argument.arguments
|
||||
reactCreateArguments.push(createElement(componentName))
|
||||
}
|
||||
// JSX
|
||||
if (node.argument?.type === 'JSXElement' && !state.importedComponent) {
|
||||
state.importedComponent = true
|
||||
node.argument.children.push(createJSX(componentName))
|
||||
}
|
||||
}
|
||||
if (node.type === 'JSXElement' && !state.importedComponent) {
|
||||
node.children.push(createJSX(componentName))
|
||||
}
|
||||
}
|
||||
|
||||
function defaultJudgePage(filePath: string) {
|
||||
// 兼容 windows 路径
|
||||
const formatFilePath = filePath.replace(/\\/g, '/')
|
||||
return /(package-.+\/)?pages\/[A-Za-z0-9-]+\/index\.[tj]sx$/.test(formatFilePath)
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
{
|
||||
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
|
||||
"include": ["src", "types"],
|
||||
"compilerOptions": {
|
||||
"module": "esnext",
|
||||
"lib": ["dom", "esnext"],
|
||||
"importHelpers": true,
|
||||
// output .d.ts declaration files for consumers
|
||||
"declaration": true,
|
||||
// output .js.map sourcemap files for consumers
|
||||
"sourceMap": true,
|
||||
// match output dir to input dir. e.g. dist/index instead of dist/src/index
|
||||
"rootDir": "./src",
|
||||
// stricter type-checking for stronger correctness. Recommended by TS
|
||||
"strict": true,
|
||||
// linter checks for common issues
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
// use Node's module resolution algorithm, instead of the legacy TS one
|
||||
"moduleResolution": "node",
|
||||
// transpile JSX to React.createElement
|
||||
"jsx": "react",
|
||||
// interop between ESM and CJS modules. Recommended by TS
|
||||
"esModuleInterop": true,
|
||||
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS
|
||||
"skipLibCheck": true,
|
||||
// error out if import and file system have a casing mismatch. Recommended by TS
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
|
||||
"noEmit": true,
|
||||
}
|
||||
}
|
@ -27,8 +27,7 @@
|
||||
"dev:qq": "npm run build:qq -- --watch",
|
||||
"dev:jd": "npm run build:jd -- --watch",
|
||||
"dev:quickapp": "npm run build:quickapp -- --watch",
|
||||
"test": "jest",
|
||||
"update-iconfont": "npx iconfont-taro"
|
||||
"test": "jest"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 3 versions",
|
||||
@ -54,16 +53,12 @@
|
||||
"@tarojs/shared": "3.6.13",
|
||||
"@tarojs/taro": "3.6.13",
|
||||
"babel-plugin-import": "^1.13.8",
|
||||
"bignumber": "^1.1.0",
|
||||
"bignumber.js": "^9.1.2",
|
||||
"cross-env": "^7.0.3",
|
||||
"crypto-js": "^3.3.0",
|
||||
"dayjs": "^1.11.9",
|
||||
"immer": "^10.1.1",
|
||||
"react": "^18.0.0",
|
||||
"react-dom": "^18.0.0",
|
||||
"taro-react-echarts": "^1.2.2",
|
||||
"weapp-qrcode": "^1.0.0",
|
||||
"zustand": "^4.5.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@ -1,4 +1,3 @@
|
||||
import { useGlobalIconFont } from "./components/iconfont/helper";
|
||||
export default defineAppConfig({
|
||||
pages: [
|
||||
"pages/index/index",
|
||||
@ -9,5 +8,4 @@ export default defineAppConfig({
|
||||
navigationBarBackgroundColor: "#fff",
|
||||
navigationBarTextStyle: "black",
|
||||
},
|
||||
usingComponents: Object.assign(useGlobalIconFont()),
|
||||
});
|
||||
|
@ -34,127 +34,6 @@ export default function Index() {
|
||||
}, {
|
||||
left: 0 // 透明度
|
||||
}],
|
||||
colorList: [{
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 1
|
||||
}, {
|
||||
r: 233,
|
||||
g: 30,
|
||||
b: 99,
|
||||
a: 1
|
||||
}, {
|
||||
r: 156,
|
||||
g: 39,
|
||||
b: 176,
|
||||
a: 1
|
||||
}, {
|
||||
r: 103,
|
||||
g: 58,
|
||||
b: 183,
|
||||
a: 1
|
||||
}, {
|
||||
r: 12,
|
||||
g: 27,
|
||||
b: 230,
|
||||
a: 1
|
||||
}, {
|
||||
r: 63,
|
||||
g: 81,
|
||||
b: 181,
|
||||
a: 1
|
||||
}, {
|
||||
r: 33,
|
||||
g: 150,
|
||||
b: 243,
|
||||
a: 1
|
||||
}, {
|
||||
r: 3,
|
||||
g: 169,
|
||||
b: 244,
|
||||
a: 1
|
||||
}, {
|
||||
r: 0,
|
||||
g: 188,
|
||||
b: 212,
|
||||
a: 1
|
||||
}, {
|
||||
r: 0,
|
||||
g: 150,
|
||||
b: 136,
|
||||
a: 1
|
||||
}, {
|
||||
r: 76,
|
||||
g: 175,
|
||||
b: 80,
|
||||
a: 1
|
||||
}, {
|
||||
r: 50,
|
||||
g: 250,
|
||||
b: 3,
|
||||
a: 1
|
||||
}, {
|
||||
r: 139,
|
||||
g: 195,
|
||||
b: 74,
|
||||
a: 1
|
||||
}, {
|
||||
r: 205,
|
||||
g: 220,
|
||||
b: 57,
|
||||
a: 1
|
||||
}, {
|
||||
r: 255,
|
||||
g: 235,
|
||||
b: 59,
|
||||
a: 1
|
||||
}, {
|
||||
r: 255,
|
||||
g: 193,
|
||||
b: 7,
|
||||
a: 1
|
||||
}, {
|
||||
r: 255,
|
||||
g: 152,
|
||||
b: 0,
|
||||
a: 1
|
||||
}, {
|
||||
r: 255,
|
||||
g: 87,
|
||||
b: 34,
|
||||
a: 1
|
||||
}, {
|
||||
r: 121,
|
||||
g: 85,
|
||||
b: 72,
|
||||
a: 1
|
||||
}, {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 1
|
||||
}, {
|
||||
r: 158,
|
||||
g: 158,
|
||||
b: 158,
|
||||
a: 1
|
||||
}, {
|
||||
r: 255,
|
||||
g: 255,
|
||||
b: 255,
|
||||
a: 1
|
||||
}, {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 0.5
|
||||
}, {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 0
|
||||
}]
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
@ -470,7 +349,7 @@ export default function Index() {
|
||||
b: Math.round(rgb.b)
|
||||
};
|
||||
}
|
||||
const { bgcolor, point, rgba, colorList } = data
|
||||
const { bgcolor, point, rgba } = data
|
||||
return (
|
||||
<View className="cp-wrapper">
|
||||
<View className='cp-mask' onClick={close}></View>
|
||||
@ -479,12 +358,16 @@ export default function Index() {
|
||||
<View className="cp-header-button" onClick={close}>取消</View>
|
||||
<View className="cp-header-button cp-button-confirm" onClick={confirm}>确认</View>
|
||||
</View>
|
||||
|
||||
|
||||
|
||||
<View className="cp-color-box" style={'background:' + ('rgb(' + bgcolor.r + ',' + bgcolor.g + ',' + bgcolor.b + ')') + ';'}>
|
||||
<View className="cp-background range-box" data-index="0" onTouchStart={touchstart} onTouchMove={touchmove} >
|
||||
<View className="cp-color-mask"></View>
|
||||
<View className="cp-pointer" style={'top:' + (point[0].top - 8 + 'px') + ';' + ('left:' + (point[0].left - 8 + 'px') + ';')}></View>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<View className="cp-control-box">
|
||||
<View className="cp-control-color">
|
||||
<View className="cp-control-color-content" style={'background:' + ('rgba(' + rgba.r + ',' + rgba.g + ',' + rgba.b + ',' + rgba.a + ')') + ';'}></View>
|
||||
@ -503,18 +386,6 @@ export default function Index() {
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<View className="cp-option">
|
||||
{
|
||||
colorList.map((item, index) => {
|
||||
return <View key={index}>
|
||||
<View className="cp-option-item">
|
||||
<View className="cp-option-item-content" data-color={item} style={'background:' + ('rgba(' + item.r + ',' + item.g + ',' + item.b + ',' + item.a + ')') + ';'} onClick={selectColor}></View>
|
||||
</View>
|
||||
</View>
|
||||
})
|
||||
}
|
||||
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
)
|
||||
|
@ -303,37 +303,3 @@
|
||||
writing-mode: vertical-lr;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.cp-option {
|
||||
margin: 60px 20px 40px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(8, 1fr);
|
||||
gap: 20px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.cp-option-item {
|
||||
width: 50rpx;
|
||||
height: 50rpx;
|
||||
margin: 0 auto;
|
||||
border-radius: 10rpx;
|
||||
background-color: #fff;
|
||||
background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee),
|
||||
linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee);
|
||||
background-size: 36rpx 36rpx;
|
||||
background-position: 0 0, 18rpx 18rpx;
|
||||
border: 1px #eee solid;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.cp-option-item-content {
|
||||
width: 50rpx;
|
||||
height: 50rpx;
|
||||
background: rgba(255, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.cp-option-item:active {
|
||||
transition: all 0.3s;
|
||||
-webkit-transform: scale(1.1);
|
||||
transform: scale(1.1);
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
import { View } from '@tarojs/components'
|
||||
import IconFont from '@/components/iconfont'
|
||||
import './index.less'
|
||||
|
||||
export default function Index({ name, size = 25, style, color, onClick, className }) {
|
||||
return (
|
||||
<View
|
||||
className={`iconfont ${className}`}
|
||||
style={{
|
||||
...style
|
||||
}}
|
||||
onClick={() => onClick && onClick()}
|
||||
>
|
||||
<IconFont name={name} size={size} color={color}></IconFont>
|
||||
</View>
|
||||
)
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
.iconfont {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0 4rpx;
|
||||
display: inline-block;
|
||||
}
|
2
src/components/iconfont/helper.d.ts
vendored
2
src/components/iconfont/helper.d.ts
vendored
@ -1,2 +0,0 @@
|
||||
/* eslint-disable */
|
||||
export declare var useGlobalIconFont: () => { iconfont: string };
|
@ -1,9 +0,0 @@
|
||||
/* eslint-disable */
|
||||
const useGlobalIconFont = () => {
|
||||
return {
|
||||
iconfont: `components/iconfont/${process.env.TARO_ENV}/${process.env.TARO_ENV}`,
|
||||
};
|
||||
};
|
||||
|
||||
// es modules is unavaiable.
|
||||
module.exports.useGlobalIconFont = useGlobalIconFont;
|
13
src/components/iconfont/index.d.ts
vendored
13
src/components/iconfont/index.d.ts
vendored
@ -1,13 +0,0 @@
|
||||
/* eslint-disable */
|
||||
import React, { FunctionComponent } from 'react';
|
||||
|
||||
interface Props {
|
||||
name: 'icon-xiaoshui3' | 'icon-xiaoshui2' | 'icon-xiaoshui1' | 'icon-piandi' | 'icon-piangao' | 'icon-qianshui2' | 'icon-qianshui1' | 'icon-qingxing2' | 'icon-shimian1' | 'icon-qingxing1' | 'icon-shenshui2' | 'icon-shimian2' | 'icon-rushuixiaoshuai' | 'icon-yandong2' | 'icon-yandong1' | 'icon-shenshui1' | 'icon-zongshuimian' | 'icon-shuimianxiaoshuai' | 'icon-qingxingcishu' | 'icon-xitongshezhi1' | 'icon-nvxingzhushou' | 'icon-shiwen' | 'icon-xieyang' | 'icon-tiwen' | 'icon-xieya' | 'icon-xinshuai' | 'icon-shuimian' | 'icon-huodongjilu' | 'icon-ECG' | 'icon-HRV' | 'icon-jibu' | 'icon-beijianhuren' | 'icon-xitongshezhi' | 'icon-yuyanqiehuan' | 'icon-shebeidingwei' | 'icon-wodequanyi' | 'icon-jinjilianxiren' | 'icon-jianhuren' | 'icon-a-huaban24' | 'icon-a-huaban14' | 'icon-a-huaban11' | 'icon-a-huaban25' | 'icon-a-huaban7' | 'icon-a-huaban23' | 'icon-a-huaban15' | 'icon-a-huaban141' | 'icon-a-huaban2' | 'icon-a-huaban12' | 'icon-a-huaban22' | 'icon-a-huaban21' | 'icon-a-huaban26' | 'icon-a-huaban1' | 'icon-a-huaban13' | 'icon-shen' | 'icon-gan' | 'icon-pi' | 'icon-xinzang' | 'icon-duigou' | 'icon-a-ziyuan77' | 'icon-xiangxia' | 'icon-xiangxia1' | 'icon-shangyiye' | 'icon-xiayiye' | 'icon-a-ziyuan15' | 'icon-a-ziyuan21' | 'icon-a-ziyuan42' | 'icon-a-ziyuan29' | 'icon-a-ziyuan31';
|
||||
size?: number;
|
||||
color?: string | string[];
|
||||
style?: React.CSSProperties;
|
||||
}
|
||||
|
||||
declare const IconFont: FunctionComponent<Props>;
|
||||
|
||||
export default IconFont;
|
@ -1,7 +0,0 @@
|
||||
/* eslint-disable */
|
||||
|
||||
const IconFont = () => {
|
||||
return null;
|
||||
};
|
||||
|
||||
export default IconFont;
|
@ -1,16 +0,0 @@
|
||||
/* eslint-disable */
|
||||
|
||||
import React from 'react';
|
||||
import Taro from '@tarojs/taro';
|
||||
|
||||
const IconFont = (props) => {
|
||||
const { name, size, color, style } = props;
|
||||
|
||||
return <iconfont name={name} size={parseFloat(Taro.pxTransform(size))} color={color} style={style} />;
|
||||
};
|
||||
|
||||
IconFont.defaultProps = {
|
||||
size: 18,
|
||||
};
|
||||
|
||||
export default IconFont;
|
@ -1,63 +0,0 @@
|
||||
Component({
|
||||
properties: {
|
||||
// icon-xiaoshui3 | icon-xiaoshui2 | icon-xiaoshui1 | icon-piandi | icon-piangao | icon-qianshui2 | icon-qianshui1 | icon-qingxing2 | icon-shimian1 | icon-qingxing1 | icon-shenshui2 | icon-shimian2 | icon-rushuixiaoshuai | icon-yandong2 | icon-yandong1 | icon-shenshui1 | icon-zongshuimian | icon-shuimianxiaoshuai | icon-qingxingcishu | icon-xitongshezhi1 | icon-nvxingzhushou | icon-shiwen | icon-xieyang | icon-tiwen | icon-xieya | icon-xinshuai | icon-shuimian | icon-huodongjilu | icon-ECG | icon-HRV | icon-jibu | icon-beijianhuren | icon-xitongshezhi | icon-yuyanqiehuan | icon-shebeidingwei | icon-wodequanyi | icon-jinjilianxiren | icon-jianhuren | icon-a-huaban24 | icon-a-huaban14 | icon-a-huaban11 | icon-a-huaban25 | icon-a-huaban7 | icon-a-huaban23 | icon-a-huaban15 | icon-a-huaban141 | icon-a-huaban2 | icon-a-huaban12 | icon-a-huaban22 | icon-a-huaban21 | icon-a-huaban26 | icon-a-huaban1 | icon-a-huaban13 | icon-shen | icon-gan | icon-pi | icon-xinzang | icon-duigou | icon-a-ziyuan77 | icon-xiangxia | icon-xiangxia1 | icon-shangyiye | icon-xiayiye | icon-a-ziyuan15 | icon-a-ziyuan21 | icon-a-ziyuan42 | icon-a-ziyuan29 | icon-a-ziyuan31
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
// string | string[]
|
||||
color: {
|
||||
type: null,
|
||||
observer: function(color) {
|
||||
this.setData({
|
||||
colors: this.fixColor(),
|
||||
isStr: typeof color === 'string',
|
||||
});
|
||||
}
|
||||
},
|
||||
size: {
|
||||
type: Number,
|
||||
value: 18,
|
||||
observer: function(size) {
|
||||
this.setData({
|
||||
svgSize: size / 750 * wx.getSystemInfoSync().windowWidth,
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
colors: '',
|
||||
svgSize: 18 / 750 * wx.getSystemInfoSync().windowWidth,
|
||||
quot: '"',
|
||||
isStr: true,
|
||||
},
|
||||
methods: {
|
||||
fixColor: function() {
|
||||
var color = this.data.color;
|
||||
var hex2rgb = this.hex2rgb;
|
||||
|
||||
if (typeof color === 'string') {
|
||||
return color.indexOf('#') === 0 ? hex2rgb(color) : color;
|
||||
}
|
||||
|
||||
return color.map(function (item) {
|
||||
return item.indexOf('#') === 0 ? hex2rgb(item) : item;
|
||||
});
|
||||
},
|
||||
hex2rgb: function(hex) {
|
||||
var rgb = [];
|
||||
|
||||
hex = hex.substr(1);
|
||||
|
||||
if (hex.length === 3) {
|
||||
hex = hex.replace(/(.)/g, '$1$1');
|
||||
}
|
||||
|
||||
hex.replace(/../g, function(color) {
|
||||
rgb.push(parseInt(color, 0x10));
|
||||
return color;
|
||||
});
|
||||
|
||||
return 'rgb(' + rgb.join(',') + ')';
|
||||
}
|
||||
}
|
||||
});
|
@ -1,4 +0,0 @@
|
||||
{
|
||||
"component": true,
|
||||
"usingComponents": {}
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -1,3 +0,0 @@
|
||||
.icon {
|
||||
background-repeat: no-repeat;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { View } from '@tarojs/components'
|
||||
import './index.less'
|
||||
import { Form, Toast, Cell, Slider, Radio, Switch, Stepper } from "@taroify/core"
|
||||
import { Form, Toast, Cell, Slider, Radio, Switch, Stepper, Button } from "@taroify/core"
|
||||
import { useCallback, useState } from 'react'
|
||||
import { hex, strInsert } from '@/utils/sendOrder'
|
||||
import { debounce } from '@/utils/index'
|
||||
@ -66,8 +66,8 @@ export default function Index() {
|
||||
break;
|
||||
}
|
||||
sendCode(str)
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
<View className="index">
|
||||
<Form className='form'>
|
||||
@ -154,7 +154,6 @@ export default function Index() {
|
||||
<Switch size="24" onChange={(e) => handleChange(e, 'AI')} checked={ruleForm.AI} />
|
||||
</Form.Control>
|
||||
</Form.Item>
|
||||
|
||||
</Cell.Group>
|
||||
</Form>
|
||||
</View >
|
||||
|
@ -1,16 +1,19 @@
|
||||
import { View } from '@tarojs/components'
|
||||
import './index.less'
|
||||
import { Button } from "@taroify/core"
|
||||
import { Scan } from "@taroify/icons"
|
||||
import BLESDK from '../../utils/ble'
|
||||
import Taro, { useDidShow } from '@tarojs/taro'
|
||||
import { useState } from 'react'
|
||||
import { getInitData } from '@/utils/sendOrder'
|
||||
|
||||
export default function Index() {
|
||||
const [deviceInfo, setDeviceInfo] = useState(BLESDK.deviceInfo)
|
||||
const [disabled, setDisabled] = useState(!!BLESDK.deviceInfo.state)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const deviceCallBack = () => {
|
||||
setDeviceInfo({ ...BLESDK.deviceInfo })
|
||||
if (BLESDK.deviceInfo.state) {
|
||||
setDisabled(true)
|
||||
}
|
||||
}
|
||||
const lookSleep = () => {
|
||||
@ -21,7 +24,20 @@ export default function Index() {
|
||||
useDidShow(() => {
|
||||
BLESDK.deviceCallBack(deviceCallBack)
|
||||
setDeviceInfo({ ...BLESDK.deviceInfo })
|
||||
setDisabled(!!BLESDK.deviceInfo.state)
|
||||
})
|
||||
|
||||
const searchBluetooth = () => {
|
||||
BLESDK.startBluetoothDevicesDiscovery()
|
||||
setLoading(true)
|
||||
setDisabled(true)
|
||||
setTimeout(() => {
|
||||
BLESDK.stopBluetoothDevicesDiscovery();
|
||||
setLoading(false)
|
||||
setDisabled(!!BLESDK.deviceInfo.state)
|
||||
}, 6000)
|
||||
}
|
||||
|
||||
return (
|
||||
<View className="index">
|
||||
<View className='device'>
|
||||
@ -38,7 +54,9 @@ export default function Index() {
|
||||
<View className='item-text' style={{ color: deviceInfo.state ? 'green' : 'red' }}>{deviceInfo.name ? deviceInfo.state ? '已连接' : '未连接' : '--'}</View>
|
||||
</View>
|
||||
</View>
|
||||
<Button color="warning" className='btn' onClick={lookSleep}>功能设置</Button>
|
||||
<Button color="info" className='btn' disabled={!!!BLESDK.deviceInfo.state} onClick={getInitData} >获取设备初始化状态</Button>
|
||||
<Button color="info" className='btn' disabled={!!!BLESDK.deviceInfo.state} onClick={lookSleep}>功能设置</Button>
|
||||
<Button color="warning" className='btn connect' disabled={disabled} loading={loading} onClick={searchBluetooth}>连接设备</Button>
|
||||
</View >
|
||||
)
|
||||
}
|
||||
|
@ -17,3 +17,8 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.connect {
|
||||
position: fixed;
|
||||
bottom: 60px;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Taro from "@tarojs/taro";
|
||||
import { handshake, asyncDate } from "./sendOrder";
|
||||
import { handshake1, handshake2 } from "./sendOrder";
|
||||
import useStore from "@/store/index";
|
||||
import { aes128Decrypt } from "@/utils/index";
|
||||
import TASK from "./taskQueue";
|
||||
@ -94,7 +94,6 @@ class Bluetooth {
|
||||
return Taro.openBluetoothAdapter({
|
||||
success: () => {
|
||||
console.log("初始化蓝牙模块");
|
||||
this.startBluetoothDevicesDiscovery()
|
||||
this.isInit = true;
|
||||
},
|
||||
fail: (err) => {
|
||||
@ -128,6 +127,7 @@ class Bluetooth {
|
||||
},
|
||||
});
|
||||
}
|
||||
console.log("未搜索到设备");
|
||||
this.isFindBt && this.clearTimeoutFn("isFindBt");
|
||||
}, 6000);
|
||||
},
|
||||
@ -228,7 +228,7 @@ class Bluetooth {
|
||||
state: true,
|
||||
success: (res) => {
|
||||
console.log("连接成功");
|
||||
|
||||
handshake1()
|
||||
this.callBack(this.deviceInfo);
|
||||
},
|
||||
fail: (err) => this.fail(err),
|
||||
@ -238,31 +238,23 @@ class Bluetooth {
|
||||
onBLECharacteristicValueChange(fn) {
|
||||
Taro.onBLECharacteristicValueChange((data) => {
|
||||
let bytes = new Uint8Array(data.value);
|
||||
let aesBytes = null;
|
||||
let value = String(this.ab2hex(bytes));
|
||||
// 长度在16内的需要解密
|
||||
if (bytes.byteLength == 17 && bytes[0] == 0xfc) {
|
||||
let aesText = this.strInsert(aes128Decrypt(value.substring(2))).split(":");
|
||||
let bf = new ArrayBuffer(aesText.length);
|
||||
let dv = new DataView(bf);
|
||||
aesText.forEach((item, index) => {
|
||||
dv.setUint8(index, `0x${item}`);
|
||||
});
|
||||
aesBytes = new Uint8Array(bf);
|
||||
|
||||
// 第二次握手
|
||||
if (bytes[2] == 0xea && bytes[3] == 0x01) {
|
||||
handshake2()
|
||||
}
|
||||
// 获取电量
|
||||
if (aesBytes && aesBytes[1] == 0xa1 && aesBytes[2] == 0x07 && aesBytes[3] == 0xb3) {
|
||||
useStore.setState((state) => {
|
||||
state.battery = aesBytes[4];
|
||||
});
|
||||
}
|
||||
console.log(`设备回复====>${bytes.byteLength == 17 ? String(this.ab2hex(aesBytes)) : value}`);
|
||||
console.log(`设备回复====>${value}`);
|
||||
TASK.executeTask();
|
||||
fn && fn(bytes, aesBytes, value);
|
||||
fn && fn(bytes, value);
|
||||
});
|
||||
}
|
||||
// 向蓝牙写入数据
|
||||
writeBleValue(value) {
|
||||
if (!this.deviceInfo.state) {
|
||||
console.log("蓝牙未连接")
|
||||
return
|
||||
}
|
||||
console.log("发送指令====>" + String(this.ab2hex(value)).toLocaleUpperCase());
|
||||
return Taro.writeBLECharacteristicValue({
|
||||
deviceId: this.deviceInfo.deviceId,
|
||||
@ -294,8 +286,8 @@ class Bluetooth {
|
||||
}
|
||||
//停止搜寻附近的蓝牙外围设备
|
||||
stopBluetoothDevicesDiscovery() {
|
||||
this.isFindBt && this.clearTimeoutFn("isFindBt");
|
||||
Taro.stopBluetoothDevicesDiscovery();
|
||||
this.isFindBt && this.clearTimeoutFn("isFindBt");
|
||||
console.log("停止搜寻");
|
||||
}
|
||||
// 断开与蓝牙低功耗设备的连接
|
||||
@ -311,7 +303,6 @@ class Bluetooth {
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
// 设备响应回调
|
||||
deviceCallBack(fn) {
|
||||
|
@ -1,4 +1,3 @@
|
||||
import dayjs from "dayjs";
|
||||
import Taro from "@tarojs/taro";
|
||||
import { useEffect, useRef, useState, useCallback } from "react";
|
||||
import CryptoJS from "crypto-js";
|
||||
|
@ -14,9 +14,15 @@ export const hex = (hex) => {
|
||||
return hex < 16 ? '0' + hex.toString(16) : String(hex.toString(16))
|
||||
}
|
||||
|
||||
// 握手
|
||||
export function handshake() {
|
||||
let list = strInsert(aes128Encrypt(`10a1010259414B2D56302E302E310000`))
|
||||
// 第一次握手
|
||||
export function handshake1() {
|
||||
let list = strInsert(`7B12EA01014775696565652d56312e302e30`)
|
||||
TASK.addTask(list)
|
||||
}
|
||||
|
||||
// 第二次握手
|
||||
export function handshake2() {
|
||||
let list = strInsert(`7B0EEA0101436172652079616b6b`)
|
||||
TASK.addTask(list)
|
||||
}
|
||||
|
||||
@ -53,19 +59,39 @@ export function asyncDate() {
|
||||
TASK.addTask(strInsert(aes128Encrypt(str)));
|
||||
}
|
||||
|
||||
// 获取版本号
|
||||
export function getVersions() {
|
||||
TASK.addTask(strInsert(aes128Encrypt(`10a102b1000000000000000000000000`)));
|
||||
}
|
||||
// 重启
|
||||
export function restart() {
|
||||
TASK.addTask(strInsert(aes128Encrypt(`0906800000`)));
|
||||
}
|
||||
// 获取新版睡眠
|
||||
export function getNewSleep() {
|
||||
TASK.addTask(strInsert(aes128Encrypt(`10A31a00ff00000000000000000000`)));
|
||||
}
|
||||
// 新版睡眠-接收回复
|
||||
export function sleepResReply(type, date, num) {
|
||||
TASK.addTask(strInsert(aes128Encrypt(`10A3${hex(type)}${hex(date)}${hex(num)}0000000000000000000000`)));
|
||||
// -----------------获取---------------------
|
||||
export function getInitData() {
|
||||
// 话筒音量
|
||||
TASK.addTask(strInsert(`7B05EA05B100`));
|
||||
|
||||
// 音乐音量
|
||||
TASK.addTask(strInsert(`7B05EA06B100`));
|
||||
|
||||
// 音源设置
|
||||
TASK.addTask(strInsert(`7B05EA07B100`));
|
||||
|
||||
// 蓝牙开关
|
||||
TASK.addTask(strInsert(`7B05EA08B100`));
|
||||
|
||||
// 声卡开关
|
||||
TASK.addTask(strInsert(`7B05EA09B100`));
|
||||
|
||||
// 播放模式
|
||||
TASK.addTask(strInsert(`7B05EA0AB100`));
|
||||
|
||||
// 音乐控制
|
||||
TASK.addTask(strInsert(`7B05EA0BB100`));
|
||||
|
||||
// 超低音强度
|
||||
TASK.addTask(strInsert(`7B05EA0CB100`));
|
||||
|
||||
// 混响模式
|
||||
TASK.addTask(strInsert(`7B05EA0DB100`));
|
||||
|
||||
// 话筒混响
|
||||
TASK.addTask(strInsert(`7B05EA0EB100`));
|
||||
|
||||
// AI
|
||||
TASK.addTask(strInsert(`7B05EA11B100`));
|
||||
}
|
||||
|
||||
|
@ -6,22 +6,28 @@ class taskQueue {
|
||||
overtime = null
|
||||
|
||||
addTask(buffer) {
|
||||
if (!BLESDK.deviceInfo.state) return
|
||||
this.list.push(buffer)
|
||||
if (!this.isExecute) {
|
||||
this.isExecute = true
|
||||
this.executeTask()
|
||||
}
|
||||
|
||||
if (this.isExecute) return
|
||||
this.executeTask()
|
||||
}
|
||||
|
||||
executeTask() {
|
||||
this.isExecute = !!this.list.length
|
||||
if (!this.list.length) return
|
||||
BLESDK.writeBleValue(new Uint8Array(['0x7b', ...this.list[0]]).buffer)
|
||||
if (!BLESDK.deviceInfo.state) {
|
||||
this.list = []
|
||||
this.isExecute = !!this.list.length
|
||||
return
|
||||
}
|
||||
BLESDK.writeBleValue(new Uint8Array([...this.list[0]]).buffer)
|
||||
this.list.shift()
|
||||
|
||||
this.overtime = setTimeout(() => {
|
||||
this.stopOverTime()
|
||||
this.executeTask()
|
||||
}, 1000)
|
||||
}, 800)
|
||||
}
|
||||
stopOverTime() {
|
||||
clearTimeout(this.overtime)
|
||||
|
24
yarn.lock
24
yarn.lock
@ -3798,16 +3798,6 @@ big.js@^5.2.2:
|
||||
resolved "https://registry.npmmirror.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"
|
||||
integrity sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==
|
||||
|
||||
bignumber.js@^9.1.2:
|
||||
version "9.1.2"
|
||||
resolved "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
|
||||
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==
|
||||
|
||||
bignumber@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/bignumber/-/bignumber-1.1.0.tgz#e6ab0a743da5f3ea018e5c17597d121f7868c159"
|
||||
integrity sha512-EGqHCKkEAwVwufcEOCYhZQqdVH+7cNCyPZ9yxisYvSjHFB+d9YcGMvorsFpeN5IJpC+lC6K+FHhu8+S4MgJazw==
|
||||
|
||||
binary-extensions@^2.0.0:
|
||||
version "2.2.0"
|
||||
resolved "https://registry.npmmirror.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
|
||||
@ -4808,11 +4798,6 @@ data-urls@^4.0.0:
|
||||
whatwg-mimetype "^3.0.0"
|
||||
whatwg-url "^12.0.0"
|
||||
|
||||
dayjs@^1.11.9:
|
||||
version "1.11.9"
|
||||
resolved "https://registry.npmmirror.com/dayjs/-/dayjs-1.11.9.tgz#9ca491933fadd0a60a2c19f6c237c03517d71d1a"
|
||||
integrity sha512-QvzAURSbQ0pKdIye2txOzNaHmxtUBXerpY0FJsFXUMKbIZeFm5ht1LS/jFsrncjnmtv8HsG0W2g6c0zUjZWmpA==
|
||||
|
||||
debug@2.6.9:
|
||||
version "2.6.9"
|
||||
resolved "https://registry.npmmirror.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
||||
@ -5973,7 +5958,7 @@ ext-name@^5.0.0:
|
||||
ext-list "^2.0.0"
|
||||
sort-keys-length "^1.0.0"
|
||||
|
||||
extend@^3.0.2, extend@~3.0.2:
|
||||
extend@~3.0.2:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.npmmirror.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"
|
||||
integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==
|
||||
@ -12004,13 +11989,6 @@ wcwidth@^1.0.1:
|
||||
dependencies:
|
||||
defaults "^1.0.3"
|
||||
|
||||
weapp-qrcode@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.npmmirror.com/weapp-qrcode/-/weapp-qrcode-1.0.0.tgz#4f3b4b4e7d37710c513439166734587859aebbfc"
|
||||
integrity sha512-4sa3W0rGDVJ9QaeZpAKlAuUxVyjhDwiUqHyGK/jJMsRMXnhb4yO8qWU/pZruMo+iT5J6CraS67lDMFb1VY+RaA==
|
||||
dependencies:
|
||||
extend "^3.0.2"
|
||||
|
||||
webidl-conversions@^7.0.0:
|
||||
version "7.0.0"
|
||||
resolved "https://registry.npmmirror.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a"
|
||||
|
Loading…
x
Reference in New Issue
Block a user