CODE
我受不了我自己了,Please call me later CodeStar!
此页面记录一些,自认为写得很优秀的代码片段
人生中的第一行代码 (2019-9-6)
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
SYJun DataBase 中更新数据的函数
public function updata_row($conn,$table_info,$table_head,$db_append_name,$table_append_name)
{
$head_first = $table_head[0];
foreach ($table_info as $key=>$value)
{
foreach ($value as $k=>$v)
{
$row_first = $table_info[$key][$table_head[0]];
if ($v != $_POST[$key.$k]){
$updata_data = $_POST[$key.$k];
$updata_sql ="UPDATE $table_append_name SET $k='$updata_data' WHERE $head_first = '$row_first'";
if ($conn -> query($updata_sql) != TRUE){
return 'Error'.$conn -> error;
}
}
}
}
return <<<LABEL
<script type="text/javascript">
window.open("database.php?dbname=$db_append_name&tablename=$table_append_name","_self");
</script>
LABEL;
}
CSS加密---动态字体 获取正确的数字映射关系
def get_real_nums():
cipher_nums = {'1010010010': 0, '1001101111': 1, '1001101010': 2,
'1010110010': 3, '1111111111': 4, '1110101001': 5,
'1010101010': 6, '1111111': 7, '1010101011': 8, '1001010100': 9}
# 加载字体文件
online_font = TTFont('cipher.woff')
# 转为xml文件
online_font.saveXML('cipher.xml')
font = parse(r'cipher.xml') # 读取xml文件
xml_list = font.documentElement # 获取xml文档对象,就是拿到DOM树的根
# getElementsByTagName()
# 获取xml文档中的某个父节点下具有相同节点名的节点对象的集合,返回的是list
all_ttg = xml_list.getElementsByTagName('TTGlyph')[1:]
cipher_dict = {}
for TTGlyph in all_ttg:
name = TTGlyph.getAttribute('name')[4:] # 获取节点的属性值
pt = TTGlyph.getElementsByTagName('pt')
num = ''
if (len(pt) < 10):
for i in range(len(pt)):
num += pt[i].getAttribute('on')
else:
for i in range(10):
num += pt[i].getAttribute('on')
num = cipher_nums[num]
cipher_dict[name] = num
return cipher_dict
RBAC权限管理---根据权限动态设置路由
func AdminAuth(ctx *context.Context) {
adminPath := beego.AppConfig.String("adminPath")
userinfo,ok := ctx.Input.Session("userinfo").(models.Manager)
pathname := ctx.Request.URL.String()
// 判断进入后台的用户是否登录
if !(ok&&userinfo.Username != "") { // 若没有登录重定向登录页面
if pathname!= "/"+adminPath+"/login" && pathname!="/"+adminPath+"/login/doLogin" {
ctx.Redirect(302, "/"+adminPath+"/login")
}
}else { // 若登录,判断此用户的权限是否可以访问相应的路由
pathname = strings.Replace(pathname,"/"+adminPath,"",1)
urlObj,_ := url.Parse(pathname)
// 判断管理员是否是超级管理员
if userinfo.IsSuper != 1 && !excludeAuthPath(urlObj.Path) {
// 1:根据角色ID获取当前角色的权限列表,然后把权限ID放在一个map类型的对象里面
roleId := userinfo.RoleId
roleAccess := []models.RoleAccess{}
models.DB.Where("role_id=?",roleId).Find(&roleAccess)
roleAccessMap := make(map[int]int)
for _, val := range roleAccess {
roleAccessMap[val.AccessId] = val.AccessId
}
// 2:获取当前访问的url(row--->23-24),并获取对应的权限id
access := models.Access{}
models.DB.Where("url=?",urlObj.Path).Find(&access)
// 3:判断当前用户访问的路由,是否在用户所拥有的权限列表中
if _,ok := roleAccessMap[access.Id];!ok {
ctx.WriteString("请注意素质,你没有权限访问!")
return
}
}
}
}
func excludeAuthPath(urlPath string) bool {
excludeAuthPathSlice := strings.Split(beego.AppConfig.String("excludeAuthPath"),",")
for _, val := range excludeAuthPathSlice {
if val == urlPath {
return true
}
}
return false
}
Typescript 节流函数
type ThrottleFunction = (fn: Function, delay: number) => Function;
const throttle: ThrottleFunction = (fn, delay) => {
let lastCallTime = 0;
let timerId: number | null = null;
const throttledFn = (...args: any[]) => {
const now = Date.now();
if (now - lastCallTime >= delay) {
fn(...args);
lastCallTime = now;
} else {
if (timerId) clearTimeout(timerId);
timerId = setTimeout(() => {
throttledFn(...args);
lastCallTime = Date.now();
timerId = null;
}, delay - (now - lastCallTime));
}
};
return throttledFn;
};
function expensiveOperation() {
console.log('Expensive operation is executed.');
}
const throttledFn = throttle(expensiveOperation, 1000); // 限制在每秒钟只执行一次
// 模拟频繁调用 expensiveOperation 函数
setInterval(() => {
throttledFn();
}, 100);