添加常用的js代码

main
youHong.ai 2022-11-24 14:59:07 +08:00
parent aadae46a4a
commit d5f39baa21
1 changed files with 103 additions and 1 deletions

View File

@ -17,3 +17,105 @@
```javascript ```javascript
const userInfo = JSON.parse(localStorage.getItem("theme-account")) const userInfo = JSON.parse(localStorage.getItem("theme-account"))
``` ```
**获取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
```
### 后端