From d5f39baa211a4e09285080fc4069f05c040d3163 Mon Sep 17 00:00:00 2001 From: "youHong.ai" <774495953@qq.com> Date: Thu, 24 Nov 2022 14:59:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B8=B8=E7=94=A8=E7=9A=84js?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 常用信息.md | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/常用信息.md b/常用信息.md index f44f91d..765c46e 100644 --- a/常用信息.md +++ b/常用信息.md @@ -16,4 +16,106 @@ ```javascript const userInfo = JSON.parse(localStorage.getItem("theme-account")) -``` \ No newline at end of file +``` + +**获取url参数** +> 维护人员: youHong.ai + +```javascript +getQueryString = (name) => { + let reg = new RegExp("(^|&|\/?)" + name + "=([^&]*)(&|$)", "i"); + let searchStr = window.location.href + if (searchStr.startsWith('&')) { + searchStr = searchStr.substr(1) + } + search = searchStr.match(reg) + if (search != null) { + return unescape(search[2]); + } + return null; +} + +``` + +**ajax请求** +> 维护人员: youHong.ai + +```javascript + class ApiUtils { + static request = (url, type = "GET", data, isAsync = true, success = () => { + }, error = () => { + }, complete = () => { + }, contentType = 'application/json', beforeSend = () => { + }) => { + let options = { + url, + type, + dataType: "json", + contentType, + async: isAsync, + data, + beforeSend, + success, + error, + complete, + } + if (contentType == 'application/json') { + options.data = JSON.stringify(data) + } + return $.ajax(options) + } + +} +``` + +**通过js获取react组件,操作组件内部数据** + +```javascript +function FindReact(dom, traverseUp = 0) { + const key = Object.keys(dom).find(key => { + return key.startsWith("__reactFiber$") // react 17+ + || key.startsWith("__reactInternalInstance$"); // react <17 + }); + const domFiber = dom[key]; + if (domFiber == null) return null; + + // react <16 + if (domFiber._currentElement) { + let compFiber = domFiber._currentElement._owner; + for (let i = 0; i < traverseUp; i++) { + compFiber = compFiber._currentElement._owner; + } + return compFiber._instance; + } + + // react 16+ + const GetCompFiber = fiber => { + let parentFiber = fiber.return; + while (typeof parentFiber.type == "string") { + parentFiber = parentFiber.return; + } + return parentFiber; + }; + let compFiber = GetCompFiber(domFiber); + for (let i = 0; i < traverseUp; i++) { + compFiber = GetCompFiber(compFiber); + } + return compFiber.stateNode; +} + +// 用法 +const someElement = document.getElementById("someElement"); +const myComp = FindReact(someElement); +myComp.setState({test1: test2}); +``` + +### 数据库 + +**备份mysql数据库** +> 维护人员: youHong.ai + +```shell +mysqldump -uroot -p'passowrd' --single-transaction -R -E --databases ecology_dev> /tmp/ecology_dev_back.sql +``` + +### 后端